xref: /openbsd/gnu/gcc/libstdc++-v3/src/localename.cc (revision 404b540a)
1*404b540aSrobert // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
2*404b540aSrobert // Free Software Foundation, Inc.
3*404b540aSrobert //
4*404b540aSrobert // This file is part of the GNU ISO C++ Library.  This library is free
5*404b540aSrobert // software; you can redistribute it and/or modify it under the
6*404b540aSrobert // terms of the GNU General Public License as published by the
7*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
8*404b540aSrobert // any later version.
9*404b540aSrobert 
10*404b540aSrobert // This library is distributed in the hope that it will be useful,
11*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
12*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*404b540aSrobert // GNU General Public License for more details.
14*404b540aSrobert 
15*404b540aSrobert // You should have received a copy of the GNU General Public License along
16*404b540aSrobert // with this library; see the file COPYING.  If not, write to the Free
17*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18*404b540aSrobert // USA.
19*404b540aSrobert 
20*404b540aSrobert // As a special exception, you may use this file as part of a free software
21*404b540aSrobert // library without restriction.  Specifically, if other files instantiate
22*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
23*404b540aSrobert // this file and link it with other files to produce an executable, this
24*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
25*404b540aSrobert // the GNU General Public License.  This exception does not however
26*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
27*404b540aSrobert // the GNU General Public License.
28*404b540aSrobert 
29*404b540aSrobert #include <clocale>
30*404b540aSrobert #include <cstring>
31*404b540aSrobert #include <locale>
32*404b540aSrobert 
33*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(std)
34*404b540aSrobert 
35*404b540aSrobert   using namespace __gnu_cxx;
36*404b540aSrobert 
locale(const char * __s)37*404b540aSrobert   locale::locale(const char* __s) : _M_impl(0)
38*404b540aSrobert   {
39*404b540aSrobert     if (__s)
40*404b540aSrobert       {
41*404b540aSrobert 	_S_initialize();
42*404b540aSrobert 	if (std::strcmp(__s, "C") == 0 || std::strcmp(__s, "POSIX") == 0)
43*404b540aSrobert 	  (_M_impl = _S_classic)->_M_add_reference();
44*404b540aSrobert 	else if (std::strcmp(__s, "") != 0)
45*404b540aSrobert 	  _M_impl = new _Impl(__s, 1);
46*404b540aSrobert 	else
47*404b540aSrobert 	  {
48*404b540aSrobert 	    // Get it from the environment.
49*404b540aSrobert 	    char* __env = std::getenv("LC_ALL");
50*404b540aSrobert 	    // If LC_ALL is set we are done.
51*404b540aSrobert 	    if (__env && std::strcmp(__env, "") != 0)
52*404b540aSrobert 	      {
53*404b540aSrobert 		if (std::strcmp(__env, "C") == 0
54*404b540aSrobert 		    || std::strcmp(__env, "POSIX") == 0)
55*404b540aSrobert 		  (_M_impl = _S_classic)->_M_add_reference();
56*404b540aSrobert 		else
57*404b540aSrobert 		  _M_impl = new _Impl(__env, 1);
58*404b540aSrobert 	      }
59*404b540aSrobert 	    else
60*404b540aSrobert 	      {
61*404b540aSrobert 		// LANG may set a default different from "C".
62*404b540aSrobert 		string __lang;
63*404b540aSrobert 		__env = std::getenv("LANG");
64*404b540aSrobert 		if (!__env || std::strcmp(__env, "") == 0
65*404b540aSrobert 		    || std::strcmp(__env, "C") == 0
66*404b540aSrobert 		    || std::strcmp(__env, "POSIX") == 0)
67*404b540aSrobert 		  __lang = "C";
68*404b540aSrobert 		else
69*404b540aSrobert 		  __lang = __env;
70*404b540aSrobert 
71*404b540aSrobert 		// Scan the categories looking for the first one
72*404b540aSrobert 		// different from LANG.
73*404b540aSrobert 		size_t __i = 0;
74*404b540aSrobert 		if (__lang == "C")
75*404b540aSrobert 		  for (; __i < _S_categories_size; ++__i)
76*404b540aSrobert 		    {
77*404b540aSrobert 		      __env = std::getenv(_S_categories[__i]);
78*404b540aSrobert 		      if (__env && std::strcmp(__env, "") != 0
79*404b540aSrobert 			  && std::strcmp(__env, "C") != 0
80*404b540aSrobert 			  && std::strcmp(__env, "POSIX") != 0)
81*404b540aSrobert 			break;
82*404b540aSrobert 		    }
83*404b540aSrobert 		else
84*404b540aSrobert 		  for (; __i < _S_categories_size; ++__i)
85*404b540aSrobert 		    {
86*404b540aSrobert 		      __env = std::getenv(_S_categories[__i]);
87*404b540aSrobert 		      if (__env && std::strcmp(__env, "") != 0
88*404b540aSrobert 			  && __lang != __env)
89*404b540aSrobert 			break;
90*404b540aSrobert 		    }
91*404b540aSrobert 
92*404b540aSrobert 		// If one is found, build the complete string of
93*404b540aSrobert 		// the form LC_CTYPE=xxx;LC_NUMERIC=yyy; and so on...
94*404b540aSrobert 		if (__i < _S_categories_size)
95*404b540aSrobert 		  {
96*404b540aSrobert 		    string __str;
97*404b540aSrobert 		    __str.reserve(128);
98*404b540aSrobert 		    for (size_t __j = 0; __j < __i; ++__j)
99*404b540aSrobert 		      {
100*404b540aSrobert 			__str += _S_categories[__j];
101*404b540aSrobert 			__str += '=';
102*404b540aSrobert 			__str += __lang;
103*404b540aSrobert 			__str += ';';
104*404b540aSrobert 		      }
105*404b540aSrobert 		    __str += _S_categories[__i];
106*404b540aSrobert 		    __str += '=';
107*404b540aSrobert 		    __str += __env;
108*404b540aSrobert 		    __str += ';';
109*404b540aSrobert 		    ++__i;
110*404b540aSrobert 		    for (; __i < _S_categories_size; ++__i)
111*404b540aSrobert 		      {
112*404b540aSrobert 			__env = std::getenv(_S_categories[__i]);
113*404b540aSrobert 			__str += _S_categories[__i];
114*404b540aSrobert 			if (!__env || std::strcmp(__env, "") == 0)
115*404b540aSrobert 			  {
116*404b540aSrobert 			    __str += '=';
117*404b540aSrobert 			    __str += __lang;
118*404b540aSrobert 			    __str += ';';
119*404b540aSrobert 			  }
120*404b540aSrobert 			else if (std::strcmp(__env, "C") == 0
121*404b540aSrobert 				 || std::strcmp(__env, "POSIX") == 0)
122*404b540aSrobert 			  __str += "=C;";
123*404b540aSrobert 			else
124*404b540aSrobert 			  {
125*404b540aSrobert 			    __str += '=';
126*404b540aSrobert 			    __str += __env;
127*404b540aSrobert 			    __str += ';';
128*404b540aSrobert 			  }
129*404b540aSrobert 		      }
130*404b540aSrobert 		    __str.erase(__str.end() - 1);
131*404b540aSrobert 		    _M_impl = new _Impl(__str.c_str(), 1);
132*404b540aSrobert 		  }
133*404b540aSrobert 		// ... otherwise either an additional instance of
134*404b540aSrobert 		// the "C" locale or LANG.
135*404b540aSrobert 		else if (__lang == "C")
136*404b540aSrobert 		  (_M_impl = _S_classic)->_M_add_reference();
137*404b540aSrobert 		else
138*404b540aSrobert 		  _M_impl = new _Impl(__lang.c_str(), 1);
139*404b540aSrobert 	      }
140*404b540aSrobert 	  }
141*404b540aSrobert       }
142*404b540aSrobert     else
143*404b540aSrobert       __throw_runtime_error(__N("locale::locale NULL not valid"));
144*404b540aSrobert   }
145*404b540aSrobert 
locale(const locale & __base,const char * __s,category __cat)146*404b540aSrobert   locale::locale(const locale& __base, const char* __s, category __cat)
147*404b540aSrobert   : _M_impl(0)
148*404b540aSrobert   {
149*404b540aSrobert     // NB: There are complicated, yet more efficient ways to do
150*404b540aSrobert     // this. Building up locales on a per-category way is tedious, so
151*404b540aSrobert     // let's do it this way until people complain.
152*404b540aSrobert     locale __add(__s);
153*404b540aSrobert     _M_coalesce(__base, __add, __cat);
154*404b540aSrobert   }
155*404b540aSrobert 
locale(const locale & __base,const locale & __add,category __cat)156*404b540aSrobert   locale::locale(const locale& __base, const locale& __add, category __cat)
157*404b540aSrobert   : _M_impl(0)
158*404b540aSrobert   { _M_coalesce(__base, __add, __cat); }
159*404b540aSrobert 
160*404b540aSrobert   void
_M_coalesce(const locale & __base,const locale & __add,category __cat)161*404b540aSrobert   locale::_M_coalesce(const locale& __base, const locale& __add,
162*404b540aSrobert 		      category __cat)
163*404b540aSrobert   {
164*404b540aSrobert     __cat = _S_normalize_category(__cat);
165*404b540aSrobert     _M_impl = new _Impl(*__base._M_impl, 1);
166*404b540aSrobert 
167*404b540aSrobert     try
168*404b540aSrobert       { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
169*404b540aSrobert     catch (...)
170*404b540aSrobert       {
171*404b540aSrobert 	_M_impl->_M_remove_reference();
172*404b540aSrobert 	__throw_exception_again;
173*404b540aSrobert       }
174*404b540aSrobert   }
175*404b540aSrobert 
176*404b540aSrobert   // Construct named _Impl.
177*404b540aSrobert   locale::_Impl::
_Impl(const char * __s,size_t __refs)178*404b540aSrobert   _Impl(const char* __s, size_t __refs)
179*404b540aSrobert   : _M_refcount(__refs), _M_facets(0), _M_facets_size(_GLIBCXX_NUM_FACETS),
180*404b540aSrobert     _M_caches(0), _M_names(0)
181*404b540aSrobert   {
182*404b540aSrobert     // Initialize the underlying locale model, which also checks to
183*404b540aSrobert     // see if the given name is valid.
184*404b540aSrobert     __c_locale __cloc;
185*404b540aSrobert     locale::facet::_S_create_c_locale(__cloc, __s);
186*404b540aSrobert 
187*404b540aSrobert     try
188*404b540aSrobert       {
189*404b540aSrobert 	_M_facets = new const facet*[_M_facets_size];
190*404b540aSrobert 	for (size_t __i = 0; __i < _M_facets_size; ++__i)
191*404b540aSrobert 	  _M_facets[__i] = 0;
192*404b540aSrobert 	_M_caches = new const facet*[_M_facets_size];
193*404b540aSrobert 	for (size_t __j = 0; __j < _M_facets_size; ++__j)
194*404b540aSrobert 	  _M_caches[__j] = 0;
195*404b540aSrobert 	_M_names = new char*[_S_categories_size];
196*404b540aSrobert 	for (size_t __k = 0; __k < _S_categories_size; ++__k)
197*404b540aSrobert 	  _M_names[__k] = 0;
198*404b540aSrobert 
199*404b540aSrobert 	// Name the categories.
200*404b540aSrobert 	const size_t __len = std::strlen(__s);
201*404b540aSrobert 	if (!std::memchr(__s, ';', __len))
202*404b540aSrobert 	  {
203*404b540aSrobert 	    _M_names[0] = new char[__len + 1];
204*404b540aSrobert 	    std::memcpy(_M_names[0], __s, __len + 1);
205*404b540aSrobert 	  }
206*404b540aSrobert 	else
207*404b540aSrobert 	  {
208*404b540aSrobert 	    const char* __end = __s;
209*404b540aSrobert 	    for (size_t __i = 0; __i < _S_categories_size; ++__i)
210*404b540aSrobert 	      {
211*404b540aSrobert 		const char* __beg = std::strchr(__end + 1, '=') + 1;
212*404b540aSrobert 		__end = std::strchr(__beg, ';');
213*404b540aSrobert 		if (!__end)
214*404b540aSrobert 		  __end = __s + __len;
215*404b540aSrobert 		_M_names[__i] = new char[__end - __beg + 1];
216*404b540aSrobert 		std::memcpy(_M_names[__i], __beg, __end - __beg);
217*404b540aSrobert 		_M_names[__i][__end - __beg] = '\0';
218*404b540aSrobert 	      }
219*404b540aSrobert 	  }
220*404b540aSrobert 
221*404b540aSrobert 	// Construct all standard facets and add them to _M_facets.
222*404b540aSrobert 	_M_init_facet(new std::ctype<char>(__cloc, 0, false));
223*404b540aSrobert 	_M_init_facet(new codecvt<char, char, mbstate_t>(__cloc));
224*404b540aSrobert 	_M_init_facet(new numpunct<char>(__cloc));
225*404b540aSrobert 	_M_init_facet(new num_get<char>);
226*404b540aSrobert 	_M_init_facet(new num_put<char>);
227*404b540aSrobert 	_M_init_facet(new std::collate<char>(__cloc));
228*404b540aSrobert 	_M_init_facet(new moneypunct<char, false>(__cloc, __s));
229*404b540aSrobert 	_M_init_facet(new moneypunct<char, true>(__cloc, __s));
230*404b540aSrobert 	_M_init_facet(new money_get<char>);
231*404b540aSrobert 	_M_init_facet(new money_put<char>);
232*404b540aSrobert 	_M_init_facet(new __timepunct<char>(__cloc, __s));
233*404b540aSrobert 	_M_init_facet(new time_get<char>);
234*404b540aSrobert 	_M_init_facet(new time_put<char>);
235*404b540aSrobert 	_M_init_facet(new std::messages<char>(__cloc, __s));
236*404b540aSrobert 
237*404b540aSrobert #ifdef  _GLIBCXX_USE_WCHAR_T
238*404b540aSrobert 	_M_init_facet(new std::ctype<wchar_t>(__cloc));
239*404b540aSrobert 	_M_init_facet(new codecvt<wchar_t, char, mbstate_t>(__cloc));
240*404b540aSrobert 	_M_init_facet(new numpunct<wchar_t>(__cloc));
241*404b540aSrobert 	_M_init_facet(new num_get<wchar_t>);
242*404b540aSrobert 	_M_init_facet(new num_put<wchar_t>);
243*404b540aSrobert 	_M_init_facet(new std::collate<wchar_t>(__cloc));
244*404b540aSrobert 	_M_init_facet(new moneypunct<wchar_t, false>(__cloc, __s));
245*404b540aSrobert 	_M_init_facet(new moneypunct<wchar_t, true>(__cloc, __s));
246*404b540aSrobert 	_M_init_facet(new money_get<wchar_t>);
247*404b540aSrobert 	_M_init_facet(new money_put<wchar_t>);
248*404b540aSrobert 	_M_init_facet(new __timepunct<wchar_t>(__cloc, __s));
249*404b540aSrobert 	_M_init_facet(new time_get<wchar_t>);
250*404b540aSrobert 	_M_init_facet(new time_put<wchar_t>);
251*404b540aSrobert 	_M_init_facet(new std::messages<wchar_t>(__cloc, __s));
252*404b540aSrobert #endif
253*404b540aSrobert 	locale::facet::_S_destroy_c_locale(__cloc);
254*404b540aSrobert       }
255*404b540aSrobert     catch(...)
256*404b540aSrobert       {
257*404b540aSrobert 	locale::facet::_S_destroy_c_locale(__cloc);
258*404b540aSrobert 	this->~_Impl();
259*404b540aSrobert 	__throw_exception_again;
260*404b540aSrobert       }
261*404b540aSrobert   }
262*404b540aSrobert 
263*404b540aSrobert   void
264*404b540aSrobert   locale::_Impl::
_M_replace_categories(const _Impl * __imp,category __cat)265*404b540aSrobert   _M_replace_categories(const _Impl* __imp, category __cat)
266*404b540aSrobert   {
267*404b540aSrobert     category __mask = 1;
268*404b540aSrobert     const bool __have_names = _M_names[0] && __imp->_M_names[0];
269*404b540aSrobert     for (size_t __ix = 0; __ix < _S_categories_size; ++__ix, __mask <<= 1)
270*404b540aSrobert       {
271*404b540aSrobert 	if (__mask & __cat)
272*404b540aSrobert 	  {
273*404b540aSrobert 	    // Need to replace entry in _M_facets with other locale's info.
274*404b540aSrobert 	    _M_replace_category(__imp, _S_facet_categories[__ix]);
275*404b540aSrobert 	    // If both have names, go ahead and mangle.
276*404b540aSrobert 	    if (__have_names)
277*404b540aSrobert 	      {
278*404b540aSrobert 		if (!_M_names[1])
279*404b540aSrobert 		  {
280*404b540aSrobert 		    // A full set of _M_names must be prepared, all identical
281*404b540aSrobert 		    // to _M_names[0] to begin with. Then, below, a few will
282*404b540aSrobert 		    // be replaced by the corresponding __imp->_M_names. I.e.,
283*404b540aSrobert 		    // not a "simple" locale anymore (see locale::operator==).
284*404b540aSrobert 		    const size_t __len = std::strlen(_M_names[0]) + 1;
285*404b540aSrobert 		    for (size_t __i = 1; __i < _S_categories_size; ++__i)
286*404b540aSrobert 		      {
287*404b540aSrobert 			_M_names[__i] = new char[__len];
288*404b540aSrobert 			std::memcpy(_M_names[__i], _M_names[0], __len);
289*404b540aSrobert 		      }
290*404b540aSrobert 		  }
291*404b540aSrobert 
292*404b540aSrobert 		// FIXME: Hack for libstdc++/29217: the numerical encodings
293*404b540aSrobert 		// of the time and collate categories are swapped vs the
294*404b540aSrobert 		// order of the names in locale::_S_categories.  We'd like to
295*404b540aSrobert 		// adjust the former (the latter is dictated by compatibility
296*404b540aSrobert 		// with glibc) but we can't for binary compatibility.
297*404b540aSrobert 		size_t __ix_name = __ix;
298*404b540aSrobert 		if (__ix == 2 || __ix == 3)
299*404b540aSrobert 		  __ix_name = 5 - __ix;
300*404b540aSrobert 
301*404b540aSrobert 		char* __src = __imp->_M_names[__ix_name] ?
302*404b540aSrobert 		              __imp->_M_names[__ix_name] : __imp->_M_names[0];
303*404b540aSrobert 		const size_t __len = std::strlen(__src) + 1;
304*404b540aSrobert 		char* __new = new char[__len];
305*404b540aSrobert 		std::memcpy(__new, __src, __len);
306*404b540aSrobert 		delete [] _M_names[__ix_name];
307*404b540aSrobert 		_M_names[__ix_name] = __new;
308*404b540aSrobert 	      }
309*404b540aSrobert 	  }
310*404b540aSrobert       }
311*404b540aSrobert   }
312*404b540aSrobert 
313*404b540aSrobert _GLIBCXX_END_NAMESPACE
314