]>
Commit | Line | Data |
---|---|---|
8d9254fc | 1 | // Copyright (C) 1997-2020 Free Software Foundation, Inc. |
b2dad0e3 BK |
2 | // |
3 | // This file is part of the GNU ISO C++ Library. This library is free | |
4 | // software; you can redistribute it and/or modify it under the | |
5 | // terms of the GNU General Public License as published by the | |
748086b7 | 6 | // Free Software Foundation; either version 3, or (at your option) |
b2dad0e3 BK |
7 | // any later version. |
8 | ||
9 | // This library is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | ||
748086b7 JJ |
14 | // Under Section 7 of GPL version 3, you are granted additional |
15 | // permissions described in the GCC Runtime Library Exception, version | |
16 | // 3.1, as published by the Free Software Foundation. | |
b2dad0e3 | 17 | |
748086b7 JJ |
18 | // You should have received a copy of the GNU General Public License and |
19 | // a copy of the GCC Runtime Library Exception along with this program; | |
20 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
21 | // <http://www.gnu.org/licenses/>. | |
b2dad0e3 | 22 | |
34a2b755 | 23 | #define _GLIBCXX_USE_CXX11_ABI 1 |
54c1bf78 BK |
24 | #include <clocale> |
25 | #include <cstring> | |
4ba851b5 | 26 | #include <cstdlib> |
54c1bf78 | 27 | #include <locale> |
b2dad0e3 | 28 | |
12ffa228 BK |
29 | namespace std _GLIBCXX_VISIBILITY(default) |
30 | { | |
31 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
3cbc7af0 | 32 | |
a7817e1d | 33 | using namespace __gnu_cxx; |
d3a193e3 | 34 | |
26c691a8 | 35 | locale::locale(const char* __s) : _M_impl(0) |
e4cc8659 | 36 | { |
c755e77d | 37 | if (__s) |
91675f9d | 38 | { |
f92ab29f | 39 | _S_initialize(); |
c755e77d BK |
40 | if (std::strcmp(__s, "C") == 0 || std::strcmp(__s, "POSIX") == 0) |
41 | (_M_impl = _S_classic)->_M_add_reference(); | |
42 | else if (std::strcmp(__s, "") != 0) | |
43 | _M_impl = new _Impl(__s, 1); | |
44 | else | |
91675f9d | 45 | { |
c755e77d BK |
46 | // Get it from the environment. |
47 | char* __env = std::getenv("LC_ALL"); | |
48 | // If LC_ALL is set we are done. | |
49 | if (__env && std::strcmp(__env, "") != 0) | |
50 | { | |
f92ab29f | 51 | if (std::strcmp(__env, "C") == 0 |
c755e77d BK |
52 | || std::strcmp(__env, "POSIX") == 0) |
53 | (_M_impl = _S_classic)->_M_add_reference(); | |
54 | else | |
55 | _M_impl = new _Impl(__env, 1); | |
56 | } | |
57 | else | |
58 | { | |
c755e77d | 59 | // LANG may set a default different from "C". |
17c08c32 PC |
60 | string __lang; |
61 | __env = std::getenv("LANG"); | |
f92ab29f CG |
62 | if (!__env || std::strcmp(__env, "") == 0 |
63 | || std::strcmp(__env, "C") == 0 | |
c755e77d | 64 | || std::strcmp(__env, "POSIX") == 0) |
17c08c32 | 65 | __lang = "C"; |
f92ab29f | 66 | else |
17c08c32 | 67 | __lang = __env; |
f92ab29f | 68 | |
c755e77d BK |
69 | // Scan the categories looking for the first one |
70 | // different from LANG. | |
71 | size_t __i = 0; | |
17c08c32 | 72 | if (__lang == "C") |
c755e77d BK |
73 | for (; __i < _S_categories_size; ++__i) |
74 | { | |
75 | __env = std::getenv(_S_categories[__i]); | |
f92ab29f CG |
76 | if (__env && std::strcmp(__env, "") != 0 |
77 | && std::strcmp(__env, "C") != 0 | |
c755e77d BK |
78 | && std::strcmp(__env, "POSIX") != 0) |
79 | break; | |
80 | } | |
81 | else | |
82 | for (; __i < _S_categories_size; ++__i) | |
83 | { | |
84 | __env = std::getenv(_S_categories[__i]); | |
85 | if (__env && std::strcmp(__env, "") != 0 | |
17c08c32 | 86 | && __lang != __env) |
c755e77d BK |
87 | break; |
88 | } | |
f92ab29f | 89 | |
c755e77d BK |
90 | // If one is found, build the complete string of |
91 | // the form LC_CTYPE=xxx;LC_NUMERIC=yyy; and so on... | |
92 | if (__i < _S_categories_size) | |
93 | { | |
94 | string __str; | |
4df9c41d | 95 | __str.reserve(128); |
c755e77d BK |
96 | for (size_t __j = 0; __j < __i; ++__j) |
97 | { | |
98 | __str += _S_categories[__j]; | |
99 | __str += '='; | |
17c08c32 | 100 | __str += __lang; |
c755e77d BK |
101 | __str += ';'; |
102 | } | |
103 | __str += _S_categories[__i]; | |
104 | __str += '='; | |
105 | __str += __env; | |
106 | __str += ';'; | |
17c08c32 | 107 | ++__i; |
c755e77d BK |
108 | for (; __i < _S_categories_size; ++__i) |
109 | { | |
110 | __env = std::getenv(_S_categories[__i]); | |
17c08c32 | 111 | __str += _S_categories[__i]; |
c755e77d BK |
112 | if (!__env || std::strcmp(__env, "") == 0) |
113 | { | |
c755e77d | 114 | __str += '='; |
17c08c32 | 115 | __str += __lang; |
c755e77d BK |
116 | __str += ';'; |
117 | } | |
118 | else if (std::strcmp(__env, "C") == 0 | |
119 | || std::strcmp(__env, "POSIX") == 0) | |
17c08c32 | 120 | __str += "=C;"; |
c755e77d BK |
121 | else |
122 | { | |
c755e77d BK |
123 | __str += '='; |
124 | __str += __env; | |
125 | __str += ';'; | |
126 | } | |
127 | } | |
128 | __str.erase(__str.end() - 1); | |
129 | _M_impl = new _Impl(__str.c_str(), 1); | |
130 | } | |
131 | // ... otherwise either an additional instance of | |
132 | // the "C" locale or LANG. | |
17c08c32 | 133 | else if (__lang == "C") |
c755e77d BK |
134 | (_M_impl = _S_classic)->_M_add_reference(); |
135 | else | |
17c08c32 | 136 | _M_impl = new _Impl(__lang.c_str(), 1); |
c755e77d | 137 | } |
91675f9d | 138 | } |
73c4dcc6 | 139 | } |
c755e77d | 140 | else |
8fc81078 | 141 | __throw_runtime_error(__N("locale::locale null not valid")); |
c755e77d BK |
142 | } |
143 | ||
144 | locale::locale(const locale& __base, const char* __s, category __cat) | |
26c691a8 | 145 | : _M_impl(0) |
f92ab29f | 146 | { |
c755e77d BK |
147 | // NB: There are complicated, yet more efficient ways to do |
148 | // this. Building up locales on a per-category way is tedious, so | |
149 | // let's do it this way until people complain. | |
150 | locale __add(__s); | |
151 | _M_coalesce(__base, __add, __cat); | |
152 | } | |
153 | ||
154 | locale::locale(const locale& __base, const locale& __add, category __cat) | |
26c691a8 | 155 | : _M_impl(0) |
3ecaae52 | 156 | { _M_coalesce(__base, __add, __cat); } |
c755e77d BK |
157 | |
158 | void | |
f92ab29f | 159 | locale::_M_coalesce(const locale& __base, const locale& __add, |
c755e77d BK |
160 | category __cat) |
161 | { | |
f92ab29f CG |
162 | __cat = _S_normalize_category(__cat); |
163 | _M_impl = new _Impl(*__base._M_impl, 1); | |
c755e77d | 164 | |
f92ab29f | 165 | __try |
c755e77d | 166 | { _M_impl->_M_replace_categories(__add._M_impl, __cat); } |
f92ab29f CG |
167 | __catch(...) |
168 | { | |
169 | _M_impl->_M_remove_reference(); | |
73c4dcc6 BK |
170 | __throw_exception_again; |
171 | } | |
e4cc8659 BK |
172 | } |
173 | ||
bb93f35d | 174 | const int num_facets = _GLIBCXX_NUM_FACETS + _GLIBCXX_NUM_UNICODE_FACETS |
34a2b755 JW |
175 | + (_GLIBCXX_USE_DUAL_ABI ? _GLIBCXX_NUM_CXX11_FACETS : 0); |
176 | ||
d3a193e3 | 177 | // Construct named _Impl. |
22b9554c | 178 | locale::_Impl:: |
17c08c32 | 179 | _Impl(const char* __s, size_t __refs) |
34a2b755 | 180 | : _M_refcount(__refs), _M_facets(0), _M_facets_size(num_facets), |
17c08c32 | 181 | _M_caches(0), _M_names(0) |
7dc08a20 | 182 | { |
215f9e28 BK |
183 | // Initialize the underlying locale model, which also checks to |
184 | // see if the given name is valid. | |
def9790d | 185 | __c_locale __cloc; |
d3a193e3 | 186 | locale::facet::_S_create_c_locale(__cloc, __s); |
c8036448 | 187 | __c_locale __clocm = __cloc; |
def9790d | 188 | |
bc2631e0 | 189 | __try |
215f9e28 | 190 | { |
38dbb2a5 JW |
191 | _M_facets = new const facet*[_M_facets_size](); |
192 | _M_caches = new const facet*[_M_facets_size](); | |
193 | _M_names = new char*[_S_categories_size](); | |
215f9e28 | 194 | |
4df9c41d | 195 | // Name the categories. |
c8036448 | 196 | const char* __smon = __s; |
91675f9d | 197 | const size_t __len = std::strlen(__s); |
17c08c32 | 198 | if (!std::memchr(__s, ';', __len)) |
aa53f832 | 199 | { |
4df9c41d | 200 | _M_names[0] = new char[__len + 1]; |
c8036448 | 201 | std::memcpy(_M_names[0], __s, __len + 1); |
aa53f832 | 202 | } |
91675f9d | 203 | else |
aa53f832 | 204 | { |
2d91a076 | 205 | const char* __end = __s; |
c8036448 PC |
206 | bool __found_ctype = false; |
207 | bool __found_monetary = false; | |
208 | size_t __ci = 0, __mi = 0; | |
91675f9d PC |
209 | for (size_t __i = 0; __i < _S_categories_size; ++__i) |
210 | { | |
2d91a076 PC |
211 | const char* __beg = std::strchr(__end + 1, '=') + 1; |
212 | __end = std::strchr(__beg, ';'); | |
91675f9d PC |
213 | if (!__end) |
214 | __end = __s + __len; | |
4df9c41d PC |
215 | _M_names[__i] = new char[__end - __beg + 1]; |
216 | std::memcpy(_M_names[__i], __beg, __end - __beg); | |
217 | _M_names[__i][__end - __beg] = '\0'; | |
c8036448 PC |
218 | if (!__found_ctype |
219 | && *(__beg - 2) == 'E' && *(__beg - 3) == 'P') | |
220 | { | |
221 | __found_ctype = true; | |
222 | __ci = __i; | |
223 | } | |
224 | else if (!__found_monetary && *(__beg - 2) == 'Y') | |
225 | { | |
226 | __found_monetary = true; | |
227 | __mi = __i; | |
228 | } | |
229 | } | |
230 | ||
231 | if (std::strcmp(_M_names[__ci], _M_names[__mi])) | |
232 | { | |
233 | __smon = _M_names[__mi]; | |
234 | __clocm = locale::facet::_S_lc_ctype_c_locale(__cloc, | |
235 | __smon); | |
91675f9d | 236 | } |
aa53f832 | 237 | } |
f92ab29f | 238 | |
70863cbd PC |
239 | // Construct all standard facets and add them to _M_facets. |
240 | _M_init_facet(new std::ctype<char>(__cloc, 0, false)); | |
241 | _M_init_facet(new codecvt<char, char, mbstate_t>(__cloc)); | |
242 | _M_init_facet(new numpunct<char>(__cloc)); | |
243 | _M_init_facet(new num_get<char>); | |
244 | _M_init_facet(new num_put<char>); | |
245 | _M_init_facet(new std::collate<char>(__cloc)); | |
c8036448 PC |
246 | _M_init_facet(new moneypunct<char, false>(__cloc, 0)); |
247 | _M_init_facet(new moneypunct<char, true>(__cloc, 0)); | |
70863cbd PC |
248 | _M_init_facet(new money_get<char>); |
249 | _M_init_facet(new money_put<char>); | |
250 | _M_init_facet(new __timepunct<char>(__cloc, __s)); | |
251 | _M_init_facet(new time_get<char>); | |
252 | _M_init_facet(new time_put<char>); | |
253 | _M_init_facet(new std::messages<char>(__cloc, __s)); | |
f92ab29f | 254 | |
70863cbd PC |
255 | #ifdef _GLIBCXX_USE_WCHAR_T |
256 | _M_init_facet(new std::ctype<wchar_t>(__cloc)); | |
257 | _M_init_facet(new codecvt<wchar_t, char, mbstate_t>(__cloc)); | |
258 | _M_init_facet(new numpunct<wchar_t>(__cloc)); | |
259 | _M_init_facet(new num_get<wchar_t>); | |
260 | _M_init_facet(new num_put<wchar_t>); | |
261 | _M_init_facet(new std::collate<wchar_t>(__cloc)); | |
c8036448 PC |
262 | _M_init_facet(new moneypunct<wchar_t, false>(__clocm, __smon)); |
263 | _M_init_facet(new moneypunct<wchar_t, true>(__clocm, __smon)); | |
70863cbd PC |
264 | _M_init_facet(new money_get<wchar_t>); |
265 | _M_init_facet(new money_put<wchar_t>); | |
266 | _M_init_facet(new __timepunct<wchar_t>(__cloc, __s)); | |
267 | _M_init_facet(new time_get<wchar_t>); | |
268 | _M_init_facet(new time_put<wchar_t>); | |
269 | _M_init_facet(new std::messages<wchar_t>(__cloc, __s)); | |
bb93f35d JW |
270 | #endif |
271 | ||
612c9c70 | 272 | #if _GLIBCXX_NUM_UNICODE_FACETS != 0 |
bb93f35d JW |
273 | _M_init_facet(new codecvt<char16_t, char, mbstate_t>); |
274 | _M_init_facet(new codecvt<char32_t, char, mbstate_t>); | |
c124af93 TH |
275 | |
276 | #ifdef _GLIBCXX_USE_CHAR8_T | |
277 | _M_init_facet(new codecvt<char16_t, char8_t, mbstate_t>); | |
278 | _M_init_facet(new codecvt<char32_t, char8_t, mbstate_t>); | |
279 | #endif | |
280 | ||
bb93f35d | 281 | #endif |
34a2b755 JW |
282 | |
283 | #if _GLIBCXX_USE_DUAL_ABI | |
284 | _M_init_extra(&__cloc, &__clocm, __s, __smon); | |
285 | #endif | |
286 | ||
70863cbd | 287 | locale::facet::_S_destroy_c_locale(__cloc); |
c8036448 PC |
288 | if (__clocm != __cloc) |
289 | locale::facet::_S_destroy_c_locale(__clocm); | |
aa53f832 | 290 | } |
bc2631e0 | 291 | __catch(...) |
91675f9d | 292 | { |
70863cbd | 293 | locale::facet::_S_destroy_c_locale(__cloc); |
c8036448 PC |
294 | if (__clocm != __cloc) |
295 | locale::facet::_S_destroy_c_locale(__clocm); | |
91675f9d PC |
296 | this->~_Impl(); |
297 | __throw_exception_again; | |
f92ab29f | 298 | } |
b2dad0e3 | 299 | } |
d3a193e3 | 300 | |
e4cc8659 BK |
301 | void |
302 | locale::_Impl:: | |
d358ecd0 | 303 | _M_replace_categories(const _Impl* __imp, category __cat) |
e4cc8659 | 304 | { |
4df9c41d | 305 | category __mask = 1; |
3ecaae52 PC |
306 | if (!_M_names[0] || !__imp->_M_names[0]) |
307 | { | |
308 | if (_M_names[0]) | |
309 | { | |
310 | delete [] _M_names[0]; | |
311 | _M_names[0] = 0; // Unnamed. | |
312 | } | |
313 | ||
314 | for (size_t __ix = 0; __ix < _S_categories_size; ++__ix, __mask <<= 1) | |
315 | { | |
316 | if (__mask & __cat) | |
317 | // Need to replace entry in _M_facets with other locale's info. | |
318 | _M_replace_category(__imp, _S_facet_categories[__ix]); | |
319 | } | |
320 | } | |
321 | else | |
e4cc8659 | 322 | { |
3ecaae52 | 323 | if (!_M_names[1]) |
e4cc8659 | 324 | { |
3ecaae52 PC |
325 | // A full set of _M_names must be prepared, all identical |
326 | // to _M_names[0] to begin with. Then, below, a few will | |
327 | // be replaced by the corresponding __imp->_M_names. I.e., | |
328 | // not a "simple" locale anymore (see locale::operator==). | |
329 | const size_t __len = std::strlen(_M_names[0]) + 1; | |
330 | for (size_t __i = 1; __i < _S_categories_size; ++__i) | |
aa53f832 | 331 | { |
3ecaae52 PC |
332 | _M_names[__i] = new char[__len]; |
333 | std::memcpy(_M_names[__i], _M_names[0], __len); | |
334 | } | |
335 | } | |
336 | ||
337 | for (size_t __ix = 0; __ix < _S_categories_size; ++__ix, __mask <<= 1) | |
338 | { | |
339 | if (__mask & __cat) | |
340 | { | |
341 | // Need to replace entry in _M_facets with other locale's info. | |
342 | _M_replace_category(__imp, _S_facet_categories[__ix]); | |
6ae72522 PC |
343 | |
344 | // FIXME: Hack for libstdc++/29217: the numerical encodings | |
345 | // of the time and collate categories are swapped vs the | |
346 | // order of the names in locale::_S_categories. We'd like to | |
347 | // adjust the former (the latter is dictated by compatibility | |
348 | // with glibc) but we can't for binary compatibility. | |
349 | size_t __ix_name = __ix; | |
350 | if (__ix == 2 || __ix == 3) | |
351 | __ix_name = 5 - __ix; | |
352 | ||
353 | char* __src = __imp->_M_names[__ix_name] ? | |
354 | __imp->_M_names[__ix_name] : __imp->_M_names[0]; | |
4df9c41d PC |
355 | const size_t __len = std::strlen(__src) + 1; |
356 | char* __new = new char[__len]; | |
357 | std::memcpy(__new, __src, __len); | |
6ae72522 PC |
358 | delete [] _M_names[__ix_name]; |
359 | _M_names[__ix_name] = __new; | |
aa53f832 | 360 | } |
e4cc8659 BK |
361 | } |
362 | } | |
363 | } | |
3cbc7af0 | 364 | |
12ffa228 BK |
365 | _GLIBCXX_END_NAMESPACE_VERSION |
366 | } // namespace |