cp:
[official-gcc.git] / libstdc++-v3 / src / locale.cc
blobd1de4db6c1a5c87aa356c36b3d84ae48e3511e34
1 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
2 // Free Software Foundation, Inc.
3 //
4 // This file is part of the GNU ISO C++ Library. This library is free
5 // software; you can redistribute it and/or modify it under the
6 // terms of the GNU General Public License as published by the
7 // Free Software Foundation; either version 2, or (at your option)
8 // any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this library; see the file COPYING. If not, write to the Free
17 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 // USA.
20 // As a special exception, you may use this file as part of a free software
21 // library without restriction. Specifically, if other files instantiate
22 // templates or use macros or inline functions from this file, or you compile
23 // this file and link it with other files to produce an executable, this
24 // file does not by itself cause the resulting executable to be covered by
25 // the GNU General Public License. This exception does not however
26 // invalidate any other reasons why the executable file might be covered by
27 // the GNU General Public License.
29 #include <clocale>
30 #include <cstring>
31 #include <cstdlib> // For getenv, free.
32 #include <cctype>
33 #include <cwctype> // For towupper, etc.
34 #include <locale>
35 #include <bits/atomicity.h>
37 namespace __gnu_cxx
39 // Defined in globals.cc.
40 extern std::locale c_locale;
41 extern std::locale::_Impl c_locale_impl;
42 } // namespace __gnu_cxx
44 namespace std
46 using namespace __gnu_cxx;
48 // Definitions for static const data members of locale.
49 const locale::category locale::none;
50 const locale::category locale::ctype;
51 const locale::category locale::numeric;
52 const locale::category locale::collate;
53 const locale::category locale::time;
54 const locale::category locale::monetary;
55 const locale::category locale::messages;
56 const locale::category locale::all;
58 // In the future, GLIBCXX_ABI > 5 should remove all uses of
59 // _GLIBCXX_ASM_SYMVER in this file, and remove exports of any
60 // static data members of locale.
62 // These are no longer exported.
63 locale::_Impl* locale::_S_classic;
64 locale::_Impl* locale::_S_global;
65 const size_t locale::_S_categories_size;
67 // Definitions for static const data members of locale::id
68 _Atomic_word locale::id::_S_highwater; // init'd to 0 by linker
70 // Definitions for static const data members of locale::_Impl
71 const locale::id* const
72 locale::_Impl::_S_id_ctype[] =
74 &std::ctype<char>::id,
75 &codecvt<char, char, mbstate_t>::id,
76 #ifdef _GLIBCXX_USE_WCHAR_T
77 &std::ctype<wchar_t>::id,
78 &codecvt<wchar_t, char, mbstate_t>::id,
79 #endif
83 const locale::id* const
84 locale::_Impl::_S_id_numeric[] =
86 &num_get<char>::id,
87 &num_put<char>::id,
88 &numpunct<char>::id,
89 #ifdef _GLIBCXX_USE_WCHAR_T
90 &num_get<wchar_t>::id,
91 &num_put<wchar_t>::id,
92 &numpunct<wchar_t>::id,
93 #endif
97 const locale::id* const
98 locale::_Impl::_S_id_collate[] =
100 &std::collate<char>::id,
101 #ifdef _GLIBCXX_USE_WCHAR_T
102 &std::collate<wchar_t>::id,
103 #endif
107 const locale::id* const
108 locale::_Impl::_S_id_time[] =
110 &__timepunct<char>::id,
111 &time_get<char>::id,
112 &time_put<char>::id,
113 #ifdef _GLIBCXX_USE_WCHAR_T
114 &__timepunct<wchar_t>::id,
115 &time_get<wchar_t>::id,
116 &time_put<wchar_t>::id,
117 #endif
121 const locale::id* const
122 locale::_Impl::_S_id_monetary[] =
124 &money_get<char>::id,
125 &money_put<char>::id,
126 &moneypunct<char, false>::id,
127 &moneypunct<char, true >::id,
128 #ifdef _GLIBCXX_USE_WCHAR_T
129 &money_get<wchar_t>::id,
130 &money_put<wchar_t>::id,
131 &moneypunct<wchar_t, false>::id,
132 &moneypunct<wchar_t, true >::id,
133 #endif
137 const locale::id* const
138 locale::_Impl::_S_id_messages[] =
140 &std::messages<char>::id,
141 #ifdef _GLIBCXX_USE_WCHAR_T
142 &std::messages<wchar_t>::id,
143 #endif
147 const locale::id* const* const
148 locale::_Impl::_S_facet_categories[] =
150 // Order must match the decl order in class locale.
151 locale::_Impl::_S_id_ctype,
152 locale::_Impl::_S_id_numeric,
153 locale::_Impl::_S_id_collate,
154 locale::_Impl::_S_id_time,
155 locale::_Impl::_S_id_monetary,
156 locale::_Impl::_S_id_messages,
160 locale::locale() throw()
162 _S_initialize();
163 (_M_impl = _S_global)->_M_add_reference();
166 locale::locale(const locale& __other) throw()
167 { (_M_impl = __other._M_impl)->_M_add_reference(); }
169 // This is used to initialize global and classic locales, and
170 // assumes that the _Impl objects are constructed correctly.
171 // The lack of a reference increment is intentional.
172 locale::locale(_Impl* __ip) throw() : _M_impl(__ip)
175 locale::locale(const char* __s)
177 if (__s)
179 _S_initialize();
180 if (std::strcmp(__s, "C") == 0 || std::strcmp(__s, "POSIX") == 0)
181 (_M_impl = _S_classic)->_M_add_reference();
182 else if (std::strcmp(__s, "") != 0)
183 _M_impl = new _Impl(__s, 1);
184 else
186 // Get it from the environment.
187 char* __env = std::getenv("LC_ALL");
188 // If LC_ALL is set we are done.
189 if (__env && std::strcmp(__env, "") != 0)
191 if (std::strcmp(__env, "C") == 0
192 || std::strcmp(__env, "POSIX") == 0)
193 (_M_impl = _S_classic)->_M_add_reference();
194 else
195 _M_impl = new _Impl(__env, 1);
197 else
199 char* __res;
200 // LANG may set a default different from "C".
201 char* __env = std::getenv("LANG");
202 if (!__env || std::strcmp(__env, "") == 0
203 || std::strcmp(__env, "C") == 0
204 || std::strcmp(__env, "POSIX") == 0)
205 __res = strdup("C");
206 else
207 __res = strdup(__env);
209 // Scan the categories looking for the first one
210 // different from LANG.
211 size_t __i = 0;
212 if (std::strcmp(__res, "C") == 0)
213 for (; __i < _S_categories_size; ++__i)
215 __env = std::getenv(_S_categories[__i]);
216 if (__env && std::strcmp(__env, "") != 0
217 && std::strcmp(__env, "C") != 0
218 && std::strcmp(__env, "POSIX") != 0)
219 break;
221 else
222 for (; __i < _S_categories_size; ++__i)
224 __env = std::getenv(_S_categories[__i]);
225 if (__env && std::strcmp(__env, "") != 0
226 && std::strcmp(__env, __res) != 0)
227 break;
230 // If one is found, build the complete string of
231 // the form LC_CTYPE=xxx;LC_NUMERIC=yyy; and so on...
232 if (__i < _S_categories_size)
234 string __str;
235 for (size_t __j = 0; __j < __i; ++__j)
237 __str += _S_categories[__j];
238 __str += '=';
239 __str += __res;
240 __str += ';';
242 __str += _S_categories[__i];
243 __str += '=';
244 __str += __env;
245 __str += ';';
246 __i++;
247 for (; __i < _S_categories_size; ++__i)
249 __env = std::getenv(_S_categories[__i]);
250 if (!__env || std::strcmp(__env, "") == 0)
252 __str += _S_categories[__i];
253 __str += '=';
254 __str += __res;
255 __str += ';';
257 else if (std::strcmp(__env, "C") == 0
258 || std::strcmp(__env, "POSIX") == 0)
260 __str += _S_categories[__i];
261 __str += "=C;";
263 else
265 __str += _S_categories[__i];
266 __str += '=';
267 __str += __env;
268 __str += ';';
271 __str.erase(__str.end() - 1);
272 _M_impl = new _Impl(__str.c_str(), 1);
274 // ... otherwise either an additional instance of
275 // the "C" locale or LANG.
276 else if (std::strcmp(__res, "C") == 0)
277 (_M_impl = _S_classic)->_M_add_reference();
278 else
279 _M_impl = new _Impl(__res, 1);
280 std::free(__res);
284 else
285 __throw_runtime_error("locale::locale NULL not valid");
288 locale::locale(const locale& __base, const char* __s, category __cat)
290 // NB: There are complicated, yet more efficient ways to do
291 // this. Building up locales on a per-category way is tedious, so
292 // let's do it this way until people complain.
293 locale __add(__s);
294 _M_coalesce(__base, __add, __cat);
297 locale::locale(const locale& __base, const locale& __add, category __cat)
298 { _M_coalesce(__base, __add, __cat); }
300 locale::~locale() throw()
301 { _M_impl->_M_remove_reference(); }
303 bool
304 locale::operator==(const locale& __rhs) const throw()
306 string __name = this->name();
307 return (_M_impl == __rhs._M_impl
308 || (__name != "*" && __name == __rhs.name()));
311 const locale&
312 locale::operator=(const locale& __other) throw()
314 __other._M_impl->_M_add_reference();
315 _M_impl->_M_remove_reference();
316 _M_impl = __other._M_impl;
317 return *this;
320 locale
321 locale::global(const locale& __other)
323 // XXX MT
324 _S_initialize();
325 _Impl* __old = _S_global;
326 __other._M_impl->_M_add_reference();
327 _S_global = __other._M_impl;
328 if (_S_global->_M_check_same_name()
329 && (std::strcmp(_S_global->_M_names[0], "*") != 0))
330 setlocale(LC_ALL, __other.name().c_str());
332 // Reference count sanity check: one reference removed for the
333 // subsition of __other locale, one added by return-by-value. Net
334 // difference: zero. When the returned locale object's destrutor
335 // is called, then the reference count is decremented and possibly
336 // destroyed.
337 return locale(__old);
340 string
341 locale::name() const
343 string __ret;
344 if (_M_impl->_M_check_same_name())
345 __ret = _M_impl->_M_names[0];
346 else
348 __ret += _S_categories[0];
349 __ret += '=';
350 __ret += _M_impl->_M_names[0];
351 for (size_t __i = 1; __i < _S_categories_size; ++__i)
353 __ret += ';';
354 __ret += _S_categories[__i];
355 __ret += '=';
356 __ret += _M_impl->_M_names[__i];
359 return __ret;
362 const locale&
363 locale::classic()
365 // Locking protocol: singleton-called-before-threading-starts
366 if (!_S_classic)
368 try
370 // 26 Standard facets, 2 references.
371 // One reference for _S_classic, one for _S_global
372 _S_classic = new (&c_locale_impl) _Impl(0, 2, true);
373 _S_global = _S_classic;
374 new (&c_locale) locale(_S_classic);
376 catch(...)
378 // Just call destructor, so that locale_impl_c's memory is
379 // not deallocated via a call to delete.
380 if (_S_classic)
381 _S_classic->~_Impl();
382 _S_classic = _S_global = 0;
383 __throw_exception_again;
386 return c_locale;
389 void
390 locale::_M_coalesce(const locale& __base, const locale& __add,
391 category __cat)
393 __cat = _S_normalize_category(__cat);
394 _M_impl = new _Impl(*__base._M_impl, 1);
396 try
397 { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
398 catch (...)
400 _M_impl->_M_remove_reference();
401 __throw_exception_again;
405 locale::category
406 locale::_S_normalize_category(category __cat)
408 int __ret = 0;
409 if (__cat == none || (__cat & all) && !(__cat & ~all))
410 __ret = __cat;
411 else
413 // NB: May be a C-style "LC_ALL" category; convert.
414 switch (__cat)
416 case LC_COLLATE:
417 __ret = collate;
418 break;
419 case LC_CTYPE:
420 __ret = ctype;
421 break;
422 case LC_MONETARY:
423 __ret = monetary;
424 break;
425 case LC_NUMERIC:
426 __ret = numeric;
427 break;
428 case LC_TIME:
429 __ret = time;
430 break;
431 #ifdef _GLIBCXX_HAVE_LC_MESSAGES
432 case LC_MESSAGES:
433 __ret = messages;
434 break;
435 #endif
436 case LC_ALL:
437 __ret = all;
438 break;
439 default:
440 __throw_runtime_error("locale::_S_normalize_category "
441 "category not found");
444 return __ret;
447 __c_locale
448 locale::facet::_S_c_locale;
450 char locale::facet::_S_c_name[2];
452 locale::facet::
453 ~facet() { }
455 // Definitions for static const data members of time_base.
456 template<>
457 const char*
458 __timepunct_cache<char>::_S_timezones[14] =
460 "GMT", "HST", "AKST", "PST", "MST", "CST", "EST", "AST", "NST", "CET",
461 "IST", "EET", "CST", "JST"
464 #ifdef _GLIBCXX_USE_WCHAR_T
465 template<>
466 const wchar_t*
467 __timepunct_cache<wchar_t>::_S_timezones[14] =
469 L"GMT", L"HST", L"AKST", L"PST", L"MST", L"CST", L"EST", L"AST",
470 L"NST", L"CET", L"IST", L"EET", L"CST", L"JST"
472 #endif
474 // Definitions for static const data members of money_base.
475 const money_base::pattern
476 money_base::_S_default_pattern = { {symbol, sign, none, value} };
478 const char* __num_base::_S_atoms_in = "-+xX0123456789eEabcdfABCDF";
479 const char* __num_base::_S_atoms_out ="-+xX0123456789abcdef0123456789ABCDEF";
481 // _GLIBCXX_RESOLVE_LIB_DEFECTS
482 // According to the resolution of DR 231, about 22.2.2.2.2, p11,
483 // "str.precision() is specified in the conversion specification".
484 void
485 __num_base::_S_format_float(const ios_base& __io, char* __fptr, char __mod)
487 ios_base::fmtflags __flags = __io.flags();
488 *__fptr++ = '%';
489 // [22.2.2.2.2] Table 60
490 if (__flags & ios_base::showpos)
491 *__fptr++ = '+';
492 if (__flags & ios_base::showpoint)
493 *__fptr++ = '#';
495 // As per DR 231: _always_, not only when
496 // __flags & ios_base::fixed || __prec > 0
497 *__fptr++ = '.';
498 *__fptr++ = '*';
500 if (__mod)
501 *__fptr++ = __mod;
502 ios_base::fmtflags __fltfield = __flags & ios_base::floatfield;
503 // [22.2.2.2.2] Table 58
504 if (__fltfield == ios_base::fixed)
505 *__fptr++ = 'f';
506 else if (__fltfield == ios_base::scientific)
507 *__fptr++ = (__flags & ios_base::uppercase) ? 'E' : 'e';
508 else
509 *__fptr++ = (__flags & ios_base::uppercase) ? 'G' : 'g';
510 *__fptr = '\0';
512 } // namespace std