PR c++/65046
[official-gcc.git] / libstdc++-v3 / config / locale / gnu / messages_members.cc
blob2e6122d2ea520fca9f44659b2b34d9c6f1fa1772
1 // std::messages implementation details, GNU version -*- C++ -*-
3 // Copyright (C) 2001-2015 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
26 // ISO C++ 14882: 22.2.7.1.2 messages virtual functions
29 // Written by Benjamin Kosnik <bkoz@redhat.com>
31 #include <locale>
32 #include <bits/c++locale_internal.h>
34 #include <limits>
35 #include <algorithm>
36 #include <vector>
37 #include <cstdlib> // std::free
38 #include <string.h> // ::strdup
40 #include <backward/auto_ptr.h>
41 #include <ext/concurrence.h>
43 namespace
45 using namespace std;
47 typedef messages_base::catalog catalog;
49 struct Catalog_info
51 Catalog_info(catalog __id, const string& __domain, locale __loc)
52 : _M_id(__id), _M_domain(__domain), _M_locale(__loc)
53 { }
55 catalog _M_id;
56 string _M_domain;
57 locale _M_locale;
60 class Catalogs
62 public:
63 Catalogs() : _M_catalog_counter(0) { }
65 ~Catalogs()
67 for (vector<Catalog_info*>::iterator __it = _M_infos.begin();
68 __it != _M_infos.end(); ++__it)
69 delete *__it;
72 catalog
73 _M_add(const string& __domain, locale __l)
75 __gnu_cxx::__scoped_lock lock(_M_mutex);
77 // The counter is not likely to roll unless catalogs keep on being
78 // opened/closed which is consider as an application mistake for the
79 // moment.
80 if (_M_catalog_counter == numeric_limits<catalog>::max())
81 return -1;
83 std::auto_ptr<Catalog_info> info(new Catalog_info(_M_catalog_counter++,
84 __domain, __l));
85 _M_infos.push_back(info.get());
86 return info.release()->_M_id;
89 void
90 _M_erase(catalog __c)
92 __gnu_cxx::__scoped_lock lock(_M_mutex);
94 vector<Catalog_info*>::iterator __res =
95 lower_bound(_M_infos.begin(), _M_infos.end(), __c, _Comp());
96 if (__res == _M_infos.end() || (*__res)->_M_id != __c)
97 return;
99 delete *__res;
100 _M_infos.erase(__res);
102 // Just in case closed catalog was the last open.
103 if (__c == _M_catalog_counter - 1)
104 --_M_catalog_counter;
107 const Catalog_info*
108 _M_get(catalog __c) const
110 __gnu_cxx::__scoped_lock lock(_M_mutex);
112 vector<Catalog_info*>::const_iterator __res =
113 lower_bound(_M_infos.begin(), _M_infos.end(), __c, _Comp());
115 if (__res != _M_infos.end() && (*__res)->_M_id == __c)
116 return *__res;
118 return 0;
121 private:
122 struct _Comp
124 bool operator()(catalog __cat, const Catalog_info* __info) const
125 { return __cat < __info->_M_id; }
127 bool operator()(const Catalog_info* __info, catalog __cat) const
128 { return __info->_M_id < __cat; }
131 mutable __gnu_cxx::__mutex _M_mutex;
132 catalog _M_catalog_counter;
133 std::vector<Catalog_info*> _M_infos;
136 Catalogs&
137 get_catalogs()
139 static Catalogs __catalogs;
140 return __catalogs;
143 const char*
144 get_glibc_msg(__c_locale __locale_messages __attribute__((unused)),
145 const char* __name_messages __attribute__((unused)),
146 const char* __domainname,
147 const char* __dfault)
149 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
150 std::__c_locale __old = __uselocale(__locale_messages);
151 const char* __msg = dgettext(__domainname, __dfault);
152 __uselocale(__old);
153 return __msg;
154 #else
155 if (char* __sav = strdup(setlocale(LC_ALL, 0)))
157 setlocale(LC_ALL, __name_messages);
158 const char* __msg = dgettext(__domainname, __dfault);
159 setlocale(LC_ALL, __sav);
160 free(__sav);
161 return __msg;
163 return __dfault;
164 #endif
168 namespace std _GLIBCXX_VISIBILITY(default)
170 _GLIBCXX_BEGIN_NAMESPACE_VERSION
172 // Specializations.
173 template<>
174 typename messages<char>::catalog
175 messages<char>::do_open(const basic_string<char>& __s,
176 const locale& __l) const
178 typedef codecvt<char, char, mbstate_t> __codecvt_t;
179 const __codecvt_t& __codecvt = use_facet<__codecvt_t>(__l);
181 bind_textdomain_codeset(__s.c_str(),
182 __nl_langinfo_l(CODESET, __codecvt._M_c_locale_codecvt));
183 return get_catalogs()._M_add(__s, __l);
186 template<>
187 void
188 messages<char>::do_close(catalog __c) const
189 { get_catalogs()._M_erase(__c); }
191 template<>
192 string
193 messages<char>::do_get(catalog __c, int, int,
194 const string& __dfault) const
196 if (__c < 0 || __dfault.empty())
197 return __dfault;
199 const Catalog_info* __cat_info = get_catalogs()._M_get(__c);
201 if (!__cat_info)
202 return __dfault;
204 return get_glibc_msg(_M_c_locale_messages, _M_name_messages,
205 __cat_info->_M_domain.c_str(),
206 __dfault.c_str());
209 #ifdef _GLIBCXX_USE_WCHAR_T
210 template<>
211 typename messages<wchar_t>::catalog
212 messages<wchar_t>::do_open(const basic_string<char>& __s,
213 const locale& __l) const
215 typedef codecvt<wchar_t, char, mbstate_t> __codecvt_t;
216 const __codecvt_t& __codecvt = use_facet<__codecvt_t>(__l);
218 bind_textdomain_codeset(__s.c_str(),
219 __nl_langinfo_l(CODESET, __codecvt._M_c_locale_codecvt));
221 return get_catalogs()._M_add(__s, __l);
224 template<>
225 void
226 messages<wchar_t>::do_close(catalog __c) const
227 { get_catalogs()._M_erase(__c); }
229 template<>
230 wstring
231 messages<wchar_t>::do_get(catalog __c, int, int,
232 const wstring& __wdfault) const
234 if (__c < 0 || __wdfault.empty())
235 return __wdfault;
237 const Catalog_info* __cat_info = get_catalogs()._M_get(__c);
239 if (!__cat_info)
240 return __wdfault;
242 typedef codecvt<wchar_t, char, mbstate_t> __codecvt_t;
243 const __codecvt_t& __conv =
244 use_facet<__codecvt_t>(__cat_info->_M_locale);
246 const char* __translation;
247 mbstate_t __state;
248 __builtin_memset(&__state, 0, sizeof(mbstate_t));
250 const wchar_t* __wdfault_next;
251 size_t __mb_size = __wdfault.size() * __conv.max_length();;
252 char* __dfault =
253 static_cast<char*>(__builtin_alloca(sizeof(char) * (__mb_size + 1)));
254 char* __dfault_next;
255 __conv.out(__state,
256 __wdfault.data(), __wdfault.data() + __wdfault.size(),
257 __wdfault_next,
258 __dfault, __dfault + __mb_size, __dfault_next);
260 // Make sure string passed to dgettext is \0 terminated.
261 *__dfault_next = '\0';
262 __translation = get_glibc_msg(_M_c_locale_messages, _M_name_messages,
263 __cat_info->_M_domain.c_str(), __dfault);
265 // If we end up getting default value back we can simply return original
266 // default value.
267 if (__translation == __dfault)
268 return __wdfault;
271 __builtin_memset(&__state, 0, sizeof(mbstate_t));
272 size_t __size = __builtin_strlen(__translation);
273 const char* __translation_next;
274 wchar_t* __wtranslation =
275 static_cast<wchar_t*>(__builtin_alloca(sizeof(wchar_t) * (__size + 1)));
276 wchar_t* __wtranslation_next;
277 __conv.in(__state, __translation, __translation + __size,
278 __translation_next,
279 __wtranslation, __wtranslation + __size,
280 __wtranslation_next);
281 return wstring(__wtranslation, __wtranslation_next);
283 #endif
285 _GLIBCXX_END_NAMESPACE_VERSION
286 } // namespace