xref: /netbsd-src/external/gpl3/gcc/dist/libstdc++-v3/src/c++98/localename.cc (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1 // Copyright (C) 1997-2022 Free Software Foundation, Inc.
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
6 // Free Software Foundation; either version 3, or (at your option)
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 
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.
17 
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/>.
22 
23 #if __cplusplus != 201103L
24 # error This file must be compiled as C++11
25 #endif
26 
27 #define _GLIBCXX_USE_CXX11_ABI 1
28 #include <clocale>
29 #include <cstring>
30 #include <cstdlib>
31 #include <locale>
32 
33 namespace std _GLIBCXX_VISIBILITY(default)
34 {
35 _GLIBCXX_BEGIN_NAMESPACE_VERSION
36 
37   using namespace __gnu_cxx;
38 
locale(const char * __s)39   locale::locale(const char* __s) : _M_impl(0)
40   {
41     if (__s)
42       {
43 	_S_initialize();
44 	if (std::strcmp(__s, "C") == 0 || std::strcmp(__s, "POSIX") == 0)
45 	  (_M_impl = _S_classic)->_M_add_reference();
46 	else if (std::strcmp(__s, "") != 0)
47 	  _M_impl = new _Impl(__s, 1);
48 	else
49 	  {
50 	    // Get it from the environment.
51 	    char* __env = std::getenv("LC_ALL");
52 	    // If LC_ALL is set we are done.
53 	    if (__env && std::strcmp(__env, "") != 0)
54 	      {
55 		if (std::strcmp(__env, "C") == 0
56 		    || std::strcmp(__env, "POSIX") == 0)
57 		  (_M_impl = _S_classic)->_M_add_reference();
58 		else
59 		  _M_impl = new _Impl(__env, 1);
60 	      }
61 	    else
62 	      {
63 		// LANG may set a default different from "C".
64 		string __lang;
65 		__env = std::getenv("LANG");
66 		if (!__env || std::strcmp(__env, "") == 0
67 		    || std::strcmp(__env, "C") == 0
68 		    || std::strcmp(__env, "POSIX") == 0)
69 		  __lang = "C";
70 		else
71 		  __lang = __env;
72 
73 		// Scan the categories looking for the first one
74 		// different from LANG.
75 		size_t __i = 0;
76 		if (__lang == "C")
77 		  for (; __i < _S_categories_size; ++__i)
78 		    {
79 		      __env = std::getenv(_S_categories[__i]);
80 		      if (__env && std::strcmp(__env, "") != 0
81 			  && std::strcmp(__env, "C") != 0
82 			  && std::strcmp(__env, "POSIX") != 0)
83 			break;
84 		    }
85 		else
86 		  for (; __i < _S_categories_size; ++__i)
87 		    {
88 		      __env = std::getenv(_S_categories[__i]);
89 		      if (__env && std::strcmp(__env, "") != 0
90 			  && __lang != __env)
91 			break;
92 		    }
93 
94 		// If one is found, build the complete string of
95 		// the form LC_CTYPE=xxx;LC_NUMERIC=yyy; and so on...
96 		if (__i < _S_categories_size)
97 		  {
98 		    string __str;
99 		    __str.reserve(128);
100 		    for (size_t __j = 0; __j < __i; ++__j)
101 		      {
102 			__str += _S_categories[__j];
103 			__str += '=';
104 			__str += __lang;
105 			__str += ';';
106 		      }
107 		    __str += _S_categories[__i];
108 		    __str += '=';
109 		    __str += __env;
110 		    __str += ';';
111 		    ++__i;
112 		    for (; __i < _S_categories_size; ++__i)
113 		      {
114 			__env = std::getenv(_S_categories[__i]);
115 			__str += _S_categories[__i];
116 			if (!__env || std::strcmp(__env, "") == 0)
117 			  {
118 			    __str += '=';
119 			    __str += __lang;
120 			    __str += ';';
121 			  }
122 			else if (std::strcmp(__env, "C") == 0
123 				 || std::strcmp(__env, "POSIX") == 0)
124 			  __str += "=C;";
125 			else
126 			  {
127 			    __str += '=';
128 			    __str += __env;
129 			    __str += ';';
130 			  }
131 		      }
132 		    __str.erase(__str.end() - 1);
133 		    _M_impl = new _Impl(__str.c_str(), 1);
134 		  }
135 		// ... otherwise either an additional instance of
136 		// the "C" locale or LANG.
137 		else if (__lang == "C")
138 		  (_M_impl = _S_classic)->_M_add_reference();
139 		else
140 		  _M_impl = new _Impl(__lang.c_str(), 1);
141 	      }
142 	  }
143       }
144     else
145       __throw_runtime_error(__N("locale::locale null not valid"));
146   }
147 
locale(const locale & __base,const char * __s,category __cat)148   locale::locale(const locale& __base, const char* __s, category __cat)
149   : _M_impl(0)
150   {
151     // NB: There are complicated, yet more efficient ways to do
152     // this. Building up locales on a per-category way is tedious, so
153     // let's do it this way until people complain.
154     locale __add(__s);
155     _M_coalesce(__base, __add, __cat);
156   }
157 
locale(const locale & __base,const locale & __add,category __cat)158   locale::locale(const locale& __base, const locale& __add, category __cat)
159   : _M_impl(0)
160   { _M_coalesce(__base, __add, __cat); }
161 
162   void
_M_coalesce(const locale & __base,const locale & __add,category __cat)163   locale::_M_coalesce(const locale& __base, const locale& __add,
164 		      category __cat)
165   {
166     __cat = _S_normalize_category(__cat);
167     _M_impl = new _Impl(*__base._M_impl, 1);
168 
169     __try
170       { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
171     __catch(...)
172       {
173 	_M_impl->_M_remove_reference();
174 	__throw_exception_again;
175       }
176   }
177 
178 const int num_facets = (
179     _GLIBCXX_NUM_FACETS + _GLIBCXX_NUM_CXX11_FACETS
180 #ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
181     + _GLIBCXX_NUM_LBDL_ALT128_FACETS
182 #endif
183     )
184 #ifdef _GLIBCXX_USE_WCHAR_T
185   * 2
186 #endif
187   + _GLIBCXX_NUM_UNICODE_FACETS;
188 
189   // Construct named _Impl.
190   locale::_Impl::
_Impl(const char * __s,size_t __refs)191   _Impl(const char* __s, size_t __refs)
192   : _M_refcount(__refs), _M_facets(0), _M_facets_size(num_facets),
193     _M_caches(0), _M_names(0)
194   {
195     // Initialize the underlying locale model, which also checks to
196     // see if the given name is valid.
197     __c_locale __cloc;
198     locale::facet::_S_create_c_locale(__cloc, __s);
199     __c_locale __clocm = __cloc;
200 
201     __try
202       {
203 	_M_facets = new const facet*[_M_facets_size]();
204 	_M_caches = new const facet*[_M_facets_size]();
205 	_M_names = new char*[_S_categories_size]();
206 
207 	// Name the categories.
208 	const char* __smon = __s;
209 	const size_t __len = std::strlen(__s);
210 	if (!std::memchr(__s, ';', __len))
211 	  {
212 	    _M_names[0] = new char[__len + 1];
213 	    std::memcpy(_M_names[0], __s, __len + 1);
214 	  }
215 	else
216 	  {
217 	    const char* __end = __s;
218 	    bool __found_ctype = false;
219 	    bool __found_monetary = false;
220 	    size_t __ci = 0, __mi = 0;
221 	    for (size_t __i = 0; __i < _S_categories_size; ++__i)
222 	      {
223 		const char* __beg = std::strchr(__end + 1, '=') + 1;
224 		__end = std::strchr(__beg, ';');
225 		if (!__end)
226 		  __end = __s + __len;
227 		_M_names[__i] = new char[__end - __beg + 1];
228 		std::memcpy(_M_names[__i], __beg, __end - __beg);
229 		_M_names[__i][__end - __beg] = '\0';
230 		if (!__found_ctype
231 		    && *(__beg - 2) == 'E' && *(__beg - 3) == 'P')
232 		  {
233 		    __found_ctype = true;
234 		    __ci = __i;
235 		  }
236 		else if (!__found_monetary && *(__beg - 2) == 'Y')
237 		  {
238 		    __found_monetary = true;
239 		    __mi = __i;
240 		  }
241 	      }
242 
243 	    if (std::strcmp(_M_names[__ci], _M_names[__mi]))
244 	      {
245 		__smon = _M_names[__mi];
246 		__clocm = locale::facet::_S_lc_ctype_c_locale(__cloc,
247 							      __smon);
248 	      }
249 	  }
250 
251 	// Construct all standard facets and add them to _M_facets.
252 	_M_init_facet(new std::ctype<char>(__cloc, 0, false));
253 	_M_init_facet(new codecvt<char, char, mbstate_t>(__cloc));
254 	_M_init_facet(new numpunct<char>(__cloc));
255 	_M_init_facet(new num_get<char>);
256 	_M_init_facet(new num_put<char>);
257 	_M_init_facet(new std::collate<char>(__cloc));
258 	_M_init_facet(new moneypunct<char, false>(__cloc, 0));
259 	_M_init_facet(new moneypunct<char, true>(__cloc, 0));
260 	_M_init_facet(new money_get<char>);
261 	_M_init_facet(new money_put<char>);
262 	_M_init_facet(new __timepunct<char>(__cloc, __s));
263 	_M_init_facet(new time_get<char>);
264 	_M_init_facet(new time_put<char>);
265 	_M_init_facet(new std::messages<char>(__cloc, __s));
266 
267 #ifdef  _GLIBCXX_USE_WCHAR_T
268 	_M_init_facet(new std::ctype<wchar_t>(__cloc));
269 	_M_init_facet(new codecvt<wchar_t, char, mbstate_t>(__cloc));
270 	_M_init_facet(new numpunct<wchar_t>(__cloc));
271 	_M_init_facet(new num_get<wchar_t>);
272 	_M_init_facet(new num_put<wchar_t>);
273 	_M_init_facet(new std::collate<wchar_t>(__cloc));
274 	_M_init_facet(new moneypunct<wchar_t, false>(__clocm, __smon));
275 	_M_init_facet(new moneypunct<wchar_t, true>(__clocm, __smon));
276 	_M_init_facet(new money_get<wchar_t>);
277 	_M_init_facet(new money_put<wchar_t>);
278 	_M_init_facet(new __timepunct<wchar_t>(__cloc, __s));
279 	_M_init_facet(new time_get<wchar_t>);
280 	_M_init_facet(new time_put<wchar_t>);
281 	_M_init_facet(new std::messages<wchar_t>(__cloc, __s));
282 #endif
283 
284 #if _GLIBCXX_NUM_UNICODE_FACETS != 0
285         _M_init_facet(new codecvt<char16_t, char, mbstate_t>);
286         _M_init_facet(new codecvt<char32_t, char, mbstate_t>);
287 
288 #ifdef _GLIBCXX_USE_CHAR8_T
289         _M_init_facet(new codecvt<char16_t, char8_t, mbstate_t>);
290         _M_init_facet(new codecvt<char32_t, char8_t, mbstate_t>);
291 #endif
292 
293 #endif
294 
295 #if _GLIBCXX_USE_DUAL_ABI
296         _M_init_extra(&__cloc, &__clocm, __s, __smon);
297 #endif
298 
299 #ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
300 	_M_init_extra_ldbl128(false);
301 #endif
302 
303 	locale::facet::_S_destroy_c_locale(__cloc);
304 	if (__clocm != __cloc)
305 	  locale::facet::_S_destroy_c_locale(__clocm);
306       }
307     __catch(...)
308       {
309 	locale::facet::_S_destroy_c_locale(__cloc);
310 	if (__clocm != __cloc)
311 	  locale::facet::_S_destroy_c_locale(__clocm);
312 	this->~_Impl();
313 	__throw_exception_again;
314       }
315   }
316 
317   void
318   locale::_Impl::
_M_replace_categories(const _Impl * __imp,category __cat)319   _M_replace_categories(const _Impl* __imp, category __cat)
320   {
321     category __mask = 1;
322     if (!_M_names[0] || !__imp->_M_names[0])
323       {
324 	if (_M_names[0])
325 	  {
326 	    delete [] _M_names[0];
327 	    _M_names[0] = 0;   // Unnamed.
328 	  }
329 
330 	for (size_t __ix = 0; __ix < _S_categories_size; ++__ix, __mask <<= 1)
331 	  {
332 	    if (__mask & __cat)
333 	      // Need to replace entry in _M_facets with other locale's info.
334 	      _M_replace_category(__imp, _S_facet_categories[__ix]);
335 	  }
336       }
337     else
338       {
339 	if (!_M_names[1])
340 	  {
341 	    // A full set of _M_names must be prepared, all identical
342 	    // to _M_names[0] to begin with. Then, below, a few will
343 	    // be replaced by the corresponding __imp->_M_names. I.e.,
344 	    // not a "simple" locale anymore (see locale::operator==).
345 	    const size_t __len = std::strlen(_M_names[0]) + 1;
346 	    for (size_t __i = 1; __i < _S_categories_size; ++__i)
347 	      {
348 		_M_names[__i] = new char[__len];
349 		std::memcpy(_M_names[__i], _M_names[0], __len);
350 	      }
351 	  }
352 
353 	for (size_t __ix = 0; __ix < _S_categories_size; ++__ix, __mask <<= 1)
354 	  {
355 	    if (__mask & __cat)
356 	      {
357 		// Need to replace entry in _M_facets with other locale's info.
358 		_M_replace_category(__imp, _S_facet_categories[__ix]);
359 
360 		// FIXME: Hack for libstdc++/29217: the numerical encodings
361 		// of the time and collate categories are swapped vs the
362 		// order of the names in locale::_S_categories.  We'd like to
363 		// adjust the former (the latter is dictated by compatibility
364 		// with glibc) but we can't for binary compatibility.
365 		size_t __ix_name = __ix;
366 		if (__ix == 2 || __ix == 3)
367 		  __ix_name = 5 - __ix;
368 
369 		char* __src = __imp->_M_names[__ix_name] ?
370 		              __imp->_M_names[__ix_name] : __imp->_M_names[0];
371 		const size_t __len = std::strlen(__src) + 1;
372 		char* __new = new char[__len];
373 		std::memcpy(__new, __src, __len);
374 		delete [] _M_names[__ix_name];
375 		_M_names[__ix_name] = __new;
376 	      }
377 	  }
378       }
379   }
380 
381 _GLIBCXX_END_NAMESPACE_VERSION
382 } // namespace
383