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.
39 #ifdef _GLIBCPP_USE_WCHAR_T
40 # include <cwctype> // for towupper, etc.
43 #include <bits/atomicity.h>
47 // Defined in globals.cc.
48 extern locale::_Impl locale_impl_c;
49 extern locale locale_c;
51 // Definitions for static const data members of locale.
52 const locale::category locale::none;
53 const locale::category locale::ctype;
54 const locale::category locale::numeric;
55 const locale::category locale::collate;
56 const locale::category locale::time;
57 const locale::category locale::monetary;
58 const locale::category locale::messages;
59 const locale::category locale::all;
61 locale::_Impl* locale::_S_classic;
62 locale::_Impl* locale::_S_global;
63 const size_t locale::_S_num_categories;
64 const size_t locale::_S_num_facets;
66 // Definitions for locale::id of standard facets that are specialized.
67 locale::id ctype<char>::id;
68 locale::id codecvt<char, char, mbstate_t>::id;
70 #ifdef _GLIBCPP_USE_WCHAR_T
71 locale::id ctype<wchar_t>::id;
72 locale::id codecvt<wchar_t, char, mbstate_t>::id;
75 // Definitions for static const data members of locale::id
76 _Atomic_word locale::id::_S_highwater; // init'd to 0 by linker
78 // Definitions for static const data members of locale::_Impl
79 const locale::id* const
80 locale::_Impl::_S_id_ctype[] =
82 &std::ctype<char>::id,
83 &codecvt<char, char, mbstate_t>::id,
84 #ifdef _GLIBCPP_USE_WCHAR_T
85 &std::ctype<wchar_t>::id,
86 &codecvt<wchar_t, char, mbstate_t>::id,
91 const locale::id* const
92 locale::_Impl::_S_id_numeric[] =
97 #ifdef _GLIBCPP_USE_WCHAR_T
98 &num_get<wchar_t>::id,
99 &num_put<wchar_t>::id,
100 &numpunct<wchar_t>::id,
105 const locale::id* const
106 locale::_Impl::_S_id_collate[] =
108 &std::collate<char>::id,
109 #ifdef _GLIBCPP_USE_WCHAR_T
110 &std::collate<wchar_t>::id,
115 const locale::id* const
116 locale::_Impl::_S_id_time[] =
118 &__timepunct<char>::id,
121 #ifdef _GLIBCPP_USE_WCHAR_T
122 &__timepunct<wchar_t>::id,
123 &time_get<wchar_t>::id,
124 &time_put<wchar_t>::id,
129 const locale::id* const
130 locale::_Impl::_S_id_monetary[] =
132 &money_get<char>::id,
133 &money_put<char>::id,
134 &moneypunct<char, false>::id,
135 &moneypunct<char, true >::id,
136 #ifdef _GLIBCPP_USE_WCHAR_T
137 &money_get<wchar_t>::id,
138 &money_put<wchar_t>::id,
139 &moneypunct<wchar_t, false>::id,
140 &moneypunct<wchar_t, true >::id,
145 const locale::id* const
146 locale::_Impl::_S_id_messages[] =
148 &std::messages<char>::id,
149 #ifdef _GLIBCPP_USE_WCHAR_T
150 &std::messages<wchar_t>::id,
155 const locale::id* const* const
156 locale::_Impl::_S_facet_categories[] =
158 // Order must match the decl order in class locale.
159 locale::_Impl::_S_id_ctype,
160 locale::_Impl::_S_id_numeric,
161 locale::_Impl::_S_id_collate,
162 locale::_Impl::_S_id_time,
163 locale::_Impl::_S_id_monetary,
164 locale::_Impl::_S_id_messages,
168 locale::~locale() throw()
169 { _M_impl->_M_remove_reference(); }
172 locale::_M_coalesce(const locale& __base, const locale& __add,
175 __cat = _S_normalize_category(__cat);
176 _M_impl = new _Impl(*__base._M_impl, 1);
179 { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
182 _M_impl->_M_remove_reference();
183 __throw_exception_again;
187 locale::locale() throw()
190 (_M_impl = _S_global)->_M_add_reference();
193 locale::locale(const locale& __other) throw()
194 { (_M_impl = __other._M_impl)->_M_add_reference(); }
196 // This is used to initialize global and classic locales, and
197 // assumes that the _Impl objects are constructed correctly.
198 locale::locale(_Impl* __ip) throw() : _M_impl(__ip)
201 locale::locale(const char* __s)
206 if (strcmp(__s, "C") == 0 || strcmp(__s, "POSIX") == 0)
207 (_M_impl = _S_classic)->_M_add_reference();
208 else if (strcmp(__s, "") == 0)
209 _M_impl = new _Impl(setlocale(LC_ALL, __s), 1);
211 _M_impl = new _Impl(__s, 1);
214 __throw_runtime_error("attempt to create locale from NULL name");
217 locale::locale(const locale& __base, const char* __s, category __cat)
219 // NB: There are complicated, yet more efficient ways to do
220 // this. Building up locales on a per-category way is tedious, so
221 // let's do it this way until people complain.
223 _M_coalesce(__base, __add, __cat);
226 locale::locale(const locale& __base, const locale& __add, category __cat)
227 { _M_coalesce(__base, __add, __cat); }
230 locale::operator==(const locale& __rhs) const throw()
232 string __name = this->name();
233 return (_M_impl == __rhs._M_impl
234 || (__name != "*" && __name == __rhs.name()));
238 locale::operator=(const locale& __other) throw()
240 __other._M_impl->_M_add_reference();
241 _M_impl->_M_remove_reference();
242 _M_impl = __other._M_impl;
247 locale::global(const locale& __other)
251 _Impl* __old = _S_global;
252 __other._M_impl->_M_add_reference();
253 _S_global = __other._M_impl;
254 if (_S_global->_M_check_same_name() && _S_global->_M_names[0] != "*")
255 setlocale(LC_ALL, __other.name().c_str());
257 // Reference count sanity check: one reference removed for the
258 // subsition of __other locale, one added by return-by-value. Net
259 // difference: zero. When the returned locale object's destrutor
260 // is called, then the reference count is decremented and possibly
262 return locale(__old);
269 // Need some kind of separator character. This one was pretty much
270 // arbitrarily chosen as to not conflict with glibc locales: the
271 // exact formatting is not set in stone.
272 const char __separator = '|';
274 if (_M_impl->_M_check_same_name())
275 __ret = _M_impl->_M_names[0];
278 for (size_t i = 0; i < _S_num_categories; ++i)
279 __ret += __separator + _M_impl->_M_names[i];
287 static _STL_mutex_lock __lock __STL_MUTEX_INITIALIZER;
288 _STL_auto_lock __auto(__lock);
294 // 26 Standard facets, 2 references.
295 // One reference for _M_classic, one for _M_global
296 _S_classic = new (&locale_impl_c) _Impl("C", 2);
297 _S_global = _S_classic;
298 new (&locale_c) locale(_S_classic);
302 // Just call destructor, so that locale_impl_c's memory is
303 // not deallocated via a call to delete.
305 _S_classic->~_Impl();
306 _S_classic = _S_global = 0;
307 __throw_exception_again;
314 locale::_S_normalize_category(category __cat)
317 if (__cat == none || (__cat & all) && !(__cat & ~all))
321 // NB: May be a C-style "LC_ALL" category; convert.
339 #ifdef _GLIBCPP_HAVE_LC_MESSAGES
348 __throw_runtime_error("bad locale category");
355 locale::facet::_S_c_locale;
361 facet(size_t __refs) throw() : _M_references(__refs)
364 _S_create_c_locale(_S_c_locale, "C");
369 _M_add_reference() throw()
370 { __atomic_add(&_M_references, 1); }
374 _M_remove_reference() throw()
376 if (__exchange_and_add(&_M_references, -1) == 0)
387 // Definitions for static const data members of ctype_base.
388 const ctype_base::mask ctype_base::space;
389 const ctype_base::mask ctype_base::print;
390 const ctype_base::mask ctype_base::cntrl;
391 const ctype_base::mask ctype_base::upper;
392 const ctype_base::mask ctype_base::lower;
393 const ctype_base::mask ctype_base::alpha;
394 const ctype_base::mask ctype_base::digit;
395 const ctype_base::mask ctype_base::punct;
396 const ctype_base::mask ctype_base::xdigit;
397 const ctype_base::mask ctype_base::alnum;
398 const ctype_base::mask ctype_base::graph;
400 // Platform-specific initialization code for ctype tables.
401 #include <bits/ctype_noninline.h>
403 const size_t ctype<char>::table_size;
405 ctype<char>::~ctype()
407 if (_M_c_locale_ctype)
408 _S_destroy_c_locale(_M_c_locale_ctype);
410 delete[] this->table();
413 // These are dummy placeholders as these virtual functions are never called.
415 ctype<char>::do_is(mask, char_type) const
419 ctype<char>::do_is(const char_type* __c, const char_type*, mask*) const
423 ctype<char>::do_scan_is(mask, const char_type* __c, const char_type*) const
427 ctype<char>::do_scan_not(mask, const char_type* __c, const char_type*) const
431 ctype<char>::do_widen(char __c) const
435 ctype<char>::do_widen(const char* __lo, const char* __hi, char* __dest) const
437 memcpy(__dest, __lo, __hi - __lo);
442 ctype<char>::do_narrow(char __c, char /*__dfault*/) const
446 ctype<char>::do_narrow(const char* __lo, const char* __hi,
447 char /*__dfault*/, char* __dest) const
449 memcpy(__dest, __lo, __hi - __lo);
453 #ifdef _GLIBCPP_USE_WCHAR_T
454 ctype<wchar_t>::ctype(size_t __refs)
455 : __ctype_abstract_base<wchar_t>(__refs)
456 { _M_c_locale_ctype = _S_clone_c_locale(_S_c_locale); }
458 ctype<wchar_t>::ctype(__c_locale __cloc, size_t __refs)
459 : __ctype_abstract_base<wchar_t>(__refs)
460 { _M_c_locale_ctype = _S_clone_c_locale(__cloc); }
462 ctype<wchar_t>::~ctype()
463 { _S_destroy_c_locale(_M_c_locale_ctype); }
466 ctype_byname<wchar_t>::ctype_byname(const char* __s, size_t __refs)
467 : ctype<wchar_t>(__refs)
469 _S_destroy_c_locale(_M_c_locale_ctype);
470 _S_create_c_locale(_M_c_locale_ctype, __s);
474 // Definitions for static const data members of time_base
477 __timepunct<char>::_S_timezones[14] =
479 "GMT", "HST", "AKST", "PST", "MST", "CST", "EST", "AST", "NST", "CET",
480 "IST", "EET", "CST", "JST"
483 #ifdef _GLIBCPP_USE_WCHAR_T
486 __timepunct<wchar_t>::_S_timezones[14] =
488 L"GMT", L"HST", L"AKST", L"PST", L"MST", L"CST", L"EST", L"AST",
489 L"NST", L"CET", L"IST", L"EET", L"CST", L"JST"
493 // Definitions for static const data members of money_base
494 const money_base::pattern
495 money_base::_S_default_pattern = { {symbol, sign, none, value} };
499 use_facet<ctype<char> >(const locale& __loc)
501 size_t __i = ctype<char>::id._M_index;
502 const locale::_Impl* __tmp = __loc._M_impl;
503 return static_cast<const ctype<char>&>(*(__tmp->_M_facets[__i]));
506 #ifdef _GLIBCPP_USE_WCHAR_T
508 const ctype<wchar_t>&
509 use_facet<ctype<wchar_t> >(const locale& __loc)
511 size_t __i = ctype<wchar_t>::id._M_index;
512 const locale::_Impl* __tmp = __loc._M_impl;
513 return static_cast<const ctype<wchar_t>&>(*(__tmp->_M_facets[__i]));
517 const char __num_base::_S_atoms[] = "0123456789eEabcdfABCDF";
520 __num_base::_S_format_float(const ios_base& __io, char* __fptr, char __mod,
523 bool __incl_prec = false;
524 ios_base::fmtflags __flags = __io.flags();
526 // [22.2.2.2.2] Table 60
527 if (__flags & ios_base::showpos)
529 if (__flags & ios_base::showpoint)
531 // As per [22.2.2.2.2.11]
532 if (__flags & ios_base::fixed || __prec > 0)
540 ios_base::fmtflags __fltfield = __flags & ios_base::floatfield;
541 // [22.2.2.2.2] Table 58
542 if (__fltfield == ios_base::fixed)
544 else if (__fltfield == ios_base::scientific)
545 *__fptr++ = (__flags & ios_base::uppercase) ? 'E' : 'e';
547 *__fptr++ = (__flags & ios_base::uppercase) ? 'G' : 'g';
553 __num_base::_S_format_int(const ios_base& __io, char* __fptr, char __mod,
556 ios_base::fmtflags __flags = __io.flags();
558 // [22.2.2.2.2] Table 60
559 if (__flags & ios_base::showpos)
561 if (__flags & ios_base::showbase)
565 // For long long types.
569 ios_base::fmtflags __bsefield = __flags & ios_base::basefield;
570 if (__bsefield == ios_base::hex)
571 *__fptr++ = (__flags & ios_base::uppercase) ? 'X' : 'x';
572 else if (__bsefield == ios_base::oct)