1 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
2 // Free Software Foundation, Inc.
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)
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,
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.
33 #include <cwctype> // For towupper, etc.
35 #include <bits/atomicity.h>
39 // Defined in globals.cc.
40 extern std::locale c_locale;
41 extern std::locale::_Impl c_locale_impl;
42 } // namespace __gnu_cxx
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 // _GLIBCPP_ASM_SYMVER in this file, and remove exports of any
60 // static data members of locale.
61 locale::_Impl* locale::_S_classic;
62 locale::_Impl* locale::_S_global;
63 const size_t locale::_S_categories_size;
64 _GLIBCPP_ASM_SYMVER(_ZNSt6locale18_S_categories_sizeE, _ZNSt6locale17_S_num_categoriesE, GLIBCPP_3.2)
65 const size_t locale::_S_extra_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 _GLIBCPP_USE_WCHAR_T
77 &std::ctype<wchar_t>::id,
78 &codecvt<wchar_t, char, mbstate_t>::id,
83 const locale::id* const
84 locale::_Impl::_S_id_numeric[] =
89 #ifdef _GLIBCPP_USE_WCHAR_T
90 &num_get<wchar_t>::id,
91 &num_put<wchar_t>::id,
92 &numpunct<wchar_t>::id,
97 const locale::id* const
98 locale::_Impl::_S_id_collate[] =
100 &std::collate<char>::id,
101 #ifdef _GLIBCPP_USE_WCHAR_T
102 &std::collate<wchar_t>::id,
107 const locale::id* const
108 locale::_Impl::_S_id_time[] =
110 &__timepunct<char>::id,
113 #ifdef _GLIBCPP_USE_WCHAR_T
114 &__timepunct<wchar_t>::id,
115 &time_get<wchar_t>::id,
116 &time_put<wchar_t>::id,
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 _GLIBCPP_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,
137 const locale::id* const
138 locale::_Impl::_S_id_messages[] =
140 &std::messages<char>::id,
141 #ifdef _GLIBCPP_USE_WCHAR_T
142 &std::messages<wchar_t>::id,
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()
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)
180 if (strcmp(__s, "C") == 0 || strcmp(__s, "POSIX") == 0)
181 (_M_impl = _S_classic)->_M_add_reference();
182 else if (strcmp(__s, "") != 0)
183 _M_impl = new _Impl(__s, 1);
186 // Get it from the environment.
187 char* __env = getenv("LC_ALL");
188 // If LC_ALL is set we are done.
189 if (__env && strcmp(__env, "") != 0)
191 if (strcmp(__env, "C") == 0 || strcmp(__env, "POSIX") == 0)
192 (_M_impl = _S_classic)->_M_add_reference();
194 _M_impl = new _Impl(__env, 1);
199 // LANG may set a default different from "C".
200 char* __env = getenv("LANG");
201 if (!__env || strcmp(__env, "") == 0 || strcmp(__env, "C") == 0
202 || strcmp(__env, "POSIX") == 0)
205 __res = strdup(__env);
207 // Scan the categories looking for the first one
208 // different from LANG.
210 if (strcmp(__res, "C") == 0)
212 __i < _S_categories_size + _S_extra_categories_size;
215 __env = getenv(_S_categories[__i]);
216 if (__env && strcmp(__env, "") != 0
217 && strcmp(__env, "C") != 0
218 && strcmp(__env, "POSIX") != 0)
223 __i < _S_categories_size + _S_extra_categories_size;
226 __env = getenv(_S_categories[__i]);
227 if (__env && strcmp(__env, "") != 0
228 && strcmp(__env, __res) != 0)
232 // If one is found, build the complete string of
233 // the form LC_CTYPE=xxx;LC_NUMERIC=yyy; and so on...
234 if (__i < _S_categories_size + _S_extra_categories_size)
237 for (size_t __j = 0; __j < __i; ++__j)
239 __str += _S_categories[__j];
244 __str += _S_categories[__i];
249 for (; __i < _S_categories_size
250 + _S_extra_categories_size; ++__i)
252 __env = getenv(_S_categories[__i]);
253 if (!__env || strcmp(__env, "") == 0)
255 __str += _S_categories[__i];
260 else if (strcmp(__env, "C") == 0
261 || strcmp(__env, "POSIX") == 0)
263 __str += _S_categories[__i];
268 __str += _S_categories[__i];
274 __str.erase(__str.end() - 1);
275 _M_impl = new _Impl(__str.c_str(), 1);
277 // ... otherwise either an additional instance of
278 // the "C" locale or LANG.
279 else if (strcmp(__res, "C") == 0)
280 (_M_impl = _S_classic)->_M_add_reference();
282 _M_impl = new _Impl(__res, 1);
288 __throw_runtime_error("attempt to create locale from NULL name");
291 locale::locale(const locale& __base, const char* __s, category __cat)
293 // NB: There are complicated, yet more efficient ways to do
294 // this. Building up locales on a per-category way is tedious, so
295 // let's do it this way until people complain.
297 _M_coalesce(__base, __add, __cat);
300 locale::locale(const locale& __base, const locale& __add, category __cat)
301 { _M_coalesce(__base, __add, __cat); }
303 locale::~locale() throw()
304 { _M_impl->_M_remove_reference(); }
307 locale::operator==(const locale& __rhs) const throw()
309 string __name = this->name();
310 return (_M_impl == __rhs._M_impl
311 || (__name != "*" && __name == __rhs.name()));
315 locale::operator=(const locale& __other) throw()
317 __other._M_impl->_M_add_reference();
318 _M_impl->_M_remove_reference();
319 _M_impl = __other._M_impl;
324 locale::global(const locale& __other)
328 _Impl* __old = _S_global;
329 __other._M_impl->_M_add_reference();
330 _S_global = __other._M_impl;
331 if (_S_global->_M_check_same_name()
332 && (strcmp(_S_global->_M_names[0], "*") != 0))
333 setlocale(LC_ALL, __other.name().c_str());
335 // Reference count sanity check: one reference removed for the
336 // subsition of __other locale, one added by return-by-value. Net
337 // difference: zero. When the returned locale object's destrutor
338 // is called, then the reference count is decremented and possibly
340 return locale(__old);
347 if (_M_impl->_M_check_same_name())
348 __ret = _M_impl->_M_names[0];
351 __ret += _S_categories[0];
353 __ret += _M_impl->_M_names[0];
355 __i < _S_categories_size + _S_extra_categories_size;
359 __ret += _S_categories[__i];
361 __ret += _M_impl->_M_names[__i];
370 static _STL_mutex_lock __lock __STL_MUTEX_INITIALIZER;
371 _STL_auto_lock __auto(__lock);
377 // 26 Standard facets, 2 references.
378 // One reference for _S_classic, one for _S_global
379 _S_classic = new (&c_locale_impl) _Impl(0, 2, true);
380 _S_global = _S_classic;
381 new (&c_locale) locale(_S_classic);
385 // Just call destructor, so that locale_impl_c's memory is
386 // not deallocated via a call to delete.
388 _S_classic->~_Impl();
389 _S_classic = _S_global = 0;
390 __throw_exception_again;
397 locale::_M_coalesce(const locale& __base, const locale& __add,
400 __cat = _S_normalize_category(__cat);
401 _M_impl = new _Impl(*__base._M_impl, 1);
404 { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
407 _M_impl->_M_remove_reference();
408 __throw_exception_again;
413 locale::_S_normalize_category(category __cat)
416 if (__cat == none || (__cat & all) && !(__cat & ~all))
420 // NB: May be a C-style "LC_ALL" category; convert.
438 #ifdef _GLIBCPP_HAVE_LC_MESSAGES
447 __throw_runtime_error("bad locale category");
454 locale::facet::_S_c_locale;
460 facet(size_t __refs) throw() : _M_references(__refs ? 1 : 0)
465 _M_add_reference() throw()
466 { __atomic_add(&_M_references, 1); }
470 _M_remove_reference() throw()
472 if (__exchange_and_add(&_M_references, -1) == 1)
484 // Definitions for static const data members of time_base
487 __timepunct<char>::_S_timezones[14] =
489 "GMT", "HST", "AKST", "PST", "MST", "CST", "EST", "AST", "NST", "CET",
490 "IST", "EET", "CST", "JST"
493 #ifdef _GLIBCPP_USE_WCHAR_T
496 __timepunct<wchar_t>::_S_timezones[14] =
498 L"GMT", L"HST", L"AKST", L"PST", L"MST", L"CST", L"EST", L"AST",
499 L"NST", L"CET", L"IST", L"EET", L"CST", L"JST"
503 // Definitions for static const data members of money_base
504 const money_base::pattern
505 money_base::_S_default_pattern = { {symbol, sign, none, value} };
507 const char __num_base::_S_atoms[] = "0123456789eEabcdfABCDF";
510 __num_base::_S_format_float(const ios_base& __io, char* __fptr, char __mod,
513 bool __incl_prec = false;
514 ios_base::fmtflags __flags = __io.flags();
516 // [22.2.2.2.2] Table 60
517 if (__flags & ios_base::showpos)
519 if (__flags & ios_base::showpoint)
521 // As per [22.2.2.2.2.11]
522 if (__flags & ios_base::fixed || __prec > 0)
530 ios_base::fmtflags __fltfield = __flags & ios_base::floatfield;
531 // [22.2.2.2.2] Table 58
532 if (__fltfield == ios_base::fixed)
534 else if (__fltfield == ios_base::scientific)
535 *__fptr++ = (__flags & ios_base::uppercase) ? 'E' : 'e';
537 *__fptr++ = (__flags & ios_base::uppercase) ? 'G' : 'g';
543 __num_base::_S_format_int(const ios_base& __io, char* __fptr, char __mod,
546 ios_base::fmtflags __flags = __io.flags();
548 // [22.2.2.2.2] Table 60
549 if (__flags & ios_base::showpos)
551 if (__flags & ios_base::showbase)
555 // For long long types.
559 ios_base::fmtflags __bsefield = __flags & ios_base::basefield;
560 if (__bsefield == ios_base::hex)
561 *__fptr++ = (__flags & ios_base::uppercase) ? 'X' : 'x';
562 else if (__bsefield == ios_base::oct)