1 // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
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 2, or (at your option)
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.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING. If not, write to the Free
16 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // As a special exception, you may use this file as part of a free software
20 // library without restriction. Specifically, if other files instantiate
21 // templates or use macros or inline functions from this file, or you compile
22 // this file and link it with other files to produce an executable, this
23 // file does not by itself cause the resulting executable to be covered by
24 // the GNU General Public License. This exception does not however
25 // invalidate any other reasons why the executable file might be covered by
26 // the GNU General Public License.
39 #include <memory> // for auto_ptr
40 #ifdef _GLIBCPP_USE_WCHAR_T
41 # include <cwctype> // for towupper, etc.
44 #include <bits/atomicity.h>
48 // Defined in globals.cc.
49 extern locale::_Impl locale_impl_c;
50 extern locale locale_c;
52 // Definitions for static const data members of locale.
53 const locale::category locale::none;
54 const locale::category locale::ctype;
55 const locale::category locale::numeric;
56 const locale::category locale::collate;
57 const locale::category locale::time;
58 const locale::category locale::monetary;
59 const locale::category locale::messages;
60 const locale::category locale::all;
62 locale::_Impl* locale::_S_classic;
63 locale::_Impl* locale::_S_global;
64 const size_t locale::_S_num_categories;
65 const size_t locale::_S_num_facets;
67 // Definitions for locale::id of standard facets.
68 locale::id ctype<char>::id;
69 locale::id codecvt<char, char, mbstate_t>::id;
71 #ifdef _GLIBCPP_USE_WCHAR_T
72 locale::id ctype<wchar_t>::id;
73 locale::id codecvt<wchar_t, char, mbstate_t>::id;
76 // Definitions for static const data members of locale::id
77 _Atomic_word locale::id::_S_highwater; // init'd to 0 by linker
79 // Definitions for static const data members of locale::_Impl
80 const locale::id* const
81 locale::_Impl::_S_id_ctype[] =
83 &std::ctype<char>::id,
84 &codecvt<char, char, mbstate_t>::id,
85 #ifdef _GLIBCPP_USE_WCHAR_T
86 &std::ctype<wchar_t>::id,
87 &codecvt<wchar_t, char, mbstate_t>::id,
92 const locale::id* const
93 locale::_Impl::_S_id_numeric[] =
98 #ifdef _GLIBCPP_USE_WCHAR_T
99 &num_get<wchar_t>::id,
100 &num_put<wchar_t>::id,
101 &numpunct<wchar_t>::id,
106 const locale::id* const
107 locale::_Impl::_S_id_collate[] =
109 &std::collate<char>::id,
110 #ifdef _GLIBCPP_USE_WCHAR_T
111 &std::collate<wchar_t>::id,
116 const locale::id* const
117 locale::_Impl::_S_id_time[] =
119 &__timepunct<char>::id,
122 #ifdef _GLIBCPP_USE_WCHAR_T
123 &__timepunct<wchar_t>::id,
124 &time_get<wchar_t>::id,
125 &time_put<wchar_t>::id,
130 const locale::id* const
131 locale::_Impl::_S_id_monetary[] =
133 &money_get<char>::id,
134 &money_put<char>::id,
135 &moneypunct<char, false>::id,
136 &moneypunct<char, true >::id,
137 #ifdef _GLIBCPP_USE_WCHAR_T
138 &money_get<wchar_t>::id,
139 &money_put<wchar_t>::id,
140 &moneypunct<wchar_t, false>::id,
141 &moneypunct<wchar_t, true >::id,
146 const locale::id* const
147 locale::_Impl::_S_id_messages[] =
149 &std::messages<char>::id,
150 #ifdef _GLIBCPP_USE_WCHAR_T
151 &std::messages<wchar_t>::id,
156 const locale::id* const* const
157 locale::_Impl::_S_facet_categories[] =
159 // Order must match the decl order in class locale.
160 locale::_Impl::_S_id_ctype,
161 locale::_Impl::_S_id_numeric,
162 locale::_Impl::_S_id_collate,
163 locale::_Impl::_S_id_time,
164 locale::_Impl::_S_id_monetary,
165 locale::_Impl::_S_id_messages,
169 locale::~locale() throw()
170 { _M_impl->_M_remove_reference(); }
173 locale::_M_coalesce(const locale& __base, const locale& __add,
176 __cat = _S_normalize_category(__cat);
177 _M_impl = new _Impl(*__base._M_impl, 1);
180 { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
183 _M_impl->_M_remove_reference();
184 __throw_exception_again;
188 locale::locale() throw()
191 (_M_impl = _S_global)->_M_add_reference();
194 locale::locale(const locale& __other) throw()
195 { (_M_impl = __other._M_impl)->_M_add_reference(); }
197 // This is used to initialize global and classic locales, and
198 // assumes that the _Impl objects are constructed correctly.
199 locale::locale(_Impl* __ip) throw() : _M_impl(__ip)
202 locale::locale(const char* __s)
207 if (strcmp(__s, "C") == 0 || strcmp(__s, "POSIX") == 0)
208 (_M_impl = _S_classic)->_M_add_reference();
209 else if (strcmp(__s, "") == 0)
210 _M_impl = new _Impl(setlocale(LC_ALL, __s), 1);
212 _M_impl = new _Impl(__s, 1);
215 __throw_runtime_error("attempt to create locale from NULL name");
218 locale::locale(const locale& __base, const char* __s, category __cat)
220 // NB: There are complicated, yet more efficient ways to do
221 // this. Building up locales on a per-category way is tedious, so
222 // let's do it this way until people complain.
224 _M_coalesce(__base, __add, __cat);
227 locale::locale(const locale& __base, const locale& __add, category __cat)
228 { _M_coalesce(__base, __add, __cat); }
231 locale::operator==(const locale& __rhs) const throw()
233 string __name = this->name();
234 return (_M_impl == __rhs._M_impl
235 || (__name != "*" && __name == __rhs.name()));
239 locale::operator=(const locale& __other) throw()
241 __other._M_impl->_M_add_reference();
242 _M_impl->_M_remove_reference();
243 _M_impl = __other._M_impl;
248 locale::global(const locale& __other)
252 _Impl* __old = _S_global;
253 __other._M_impl->_M_add_reference();
254 _S_global = __other._M_impl;
255 if (_S_global->_M_check_same_name() && _S_global->_M_names[0] != "*")
256 setlocale(LC_ALL, __other.name().c_str());
258 // Reference count sanity check: one reference removed for the
259 // subsition of __other locale, one added by return-by-value. Net
260 // difference: zero. When the returned locale object's destrutor
261 // is called, then the reference count is decremented and possibly
263 return locale(__old);
270 // Need some kind of separator character. This one was pretty much
271 // arbitrarily chosen as to not conflict with glibc locales: the
272 // exact formatting is not set in stone.
273 const char __separator = '|';
275 if (_M_impl->_M_check_same_name())
276 __ret = _M_impl->_M_names[0];
279 for (size_t i = 0; i < _S_num_categories; ++i)
280 __ret += __separator + _M_impl->_M_names[i];
288 static _STL_mutex_lock __lock __STL_MUTEX_INITIALIZER;
289 _STL_auto_lock __auto(__lock);
295 // 26 Standard facets, 2 references.
296 // One reference for _M_classic, one for _M_global
297 _S_classic = new (&locale_impl_c) _Impl("C", 2);
298 _S_global = _S_classic;
299 new (&locale_c) locale(_S_classic);
303 // Just call destructor, so that locale_impl_c's memory is
304 // not deallocated via a call to delete.
306 _S_classic->~_Impl();
307 _S_classic = _S_global = 0;
308 __throw_exception_again;
315 locale::_S_normalize_category(category __cat)
318 if (__cat == none || (__cat & all) && !(__cat & ~all))
322 // NB: May be a C-style "LC_ALL" category; convert.
340 #ifdef _GLIBCPP_HAVE_LC_MESSAGES
349 __throw_runtime_error("bad locale category");
356 locale::facet::_S_c_locale;
362 facet(size_t __refs) throw() : _M_references(__refs)
365 _S_create_c_locale(_S_c_locale, "C");
370 _M_add_reference() throw()
371 { __atomic_add(&_M_references, 1); }
375 _M_remove_reference() throw()
377 if (__exchange_and_add(&_M_references, -1) == 0)
388 // Definitions for static const data members of ctype_base.
389 const ctype_base::mask ctype_base::space;
390 const ctype_base::mask ctype_base::print;
391 const ctype_base::mask ctype_base::cntrl;
392 const ctype_base::mask ctype_base::upper;
393 const ctype_base::mask ctype_base::lower;
394 const ctype_base::mask ctype_base::alpha;
395 const ctype_base::mask ctype_base::digit;
396 const ctype_base::mask ctype_base::punct;
397 const ctype_base::mask ctype_base::xdigit;
398 const ctype_base::mask ctype_base::alnum;
399 const ctype_base::mask ctype_base::graph;
401 // Platform-specific initialization code for ctype tables.
402 #include <bits/ctype_noninline.h>
404 const size_t ctype<char>::table_size;
406 ctype<char>::~ctype()
408 if (_M_c_locale_ctype)
409 _S_destroy_c_locale(_M_c_locale_ctype);
411 delete[] this->table();
414 // These are dummy placeholders as these virtual functions are never called.
416 ctype<char>::do_is(mask, char_type) const
420 ctype<char>::do_is(const char_type* __c, const char_type*, mask*) const
424 ctype<char>::do_scan_is(mask, const char_type* __c, const char_type*) const
428 ctype<char>::do_scan_not(mask, const char_type* __c, const char_type*) const
432 ctype<char>::do_widen(char __c) const
436 ctype<char>::do_widen(const char* __lo, const char* __hi, char* __dest) const
438 memcpy(__dest, __lo, __hi - __lo);
443 ctype<char>::do_narrow(char __c, char /*__dfault*/) const
447 ctype<char>::do_narrow(const char* __lo, const char* __hi,
448 char /*__dfault*/, char* __dest) const
450 memcpy(__dest, __lo, __hi - __lo);
454 #ifdef _GLIBCPP_USE_WCHAR_T
455 ctype<wchar_t>::ctype(size_t __refs)
456 : __ctype_abstract_base<wchar_t>(__refs)
457 { _M_c_locale_ctype = _S_clone_c_locale(_S_c_locale); }
459 ctype<wchar_t>::ctype(__c_locale __cloc, size_t __refs)
460 : __ctype_abstract_base<wchar_t>(__refs)
461 { _M_c_locale_ctype = _S_clone_c_locale(__cloc); }
463 ctype<wchar_t>::~ctype()
464 { _S_destroy_c_locale(_M_c_locale_ctype); }
467 ctype_byname<wchar_t>::ctype_byname(const char* __s, size_t __refs)
468 : ctype<wchar_t>(__refs)
470 _S_destroy_c_locale(_M_c_locale_ctype);
471 _S_create_c_locale(_M_c_locale_ctype, __s);
475 // Definitions for static const data members of time_base
478 __timepunct<char>::_S_timezones[14] =
480 "GMT", "HST", "AKST", "PST", "MST", "CST", "EST", "AST", "NST", "CET",
481 "IST", "EET", "CST", "JST"
484 #ifdef _GLIBCPP_USE_WCHAR_T
487 __timepunct<wchar_t>::_S_timezones[14] =
489 L"GMT", L"HST", L"AKST", L"PST", L"MST", L"CST", L"EST", L"AST",
490 L"NST", L"CET", L"IST", L"EET", L"CST", L"JST"
494 // Definitions for static const data members of money_base
495 const money_base::pattern
496 money_base::_S_default_pattern = { {symbol, sign, none, value} };
500 use_facet<ctype<char> >(const locale& __loc)
502 size_t __i = ctype<char>::id._M_index;
503 const locale::_Impl* __tmp = __loc._M_impl;
504 return static_cast<const ctype<char>&>(* (*(__tmp->_M_facets))[__i]);
507 #ifdef _GLIBCPP_USE_WCHAR_T
509 const ctype<wchar_t>&
510 use_facet<ctype<wchar_t> >(const locale& __loc)
512 size_t __i = ctype<wchar_t>::id._M_index;
513 const locale::_Impl* __tmp = __loc._M_impl;
514 return static_cast<const ctype<wchar_t>&>(* (*(__tmp->_M_facets))[__i]);
519 const char __num_base::_S_atoms[] = "0123456789eEabcdfABCDF";
521 const double __num_base::_S_scale_hex = log(10.0)/log(16.0);
523 const double __num_base::_S_scale_oct = log(10.0)/log(8.0);
526 __num_base::_S_format_float(const ios_base& __io, char* __fptr, char __mod,
529 bool __incl_prec = false;
530 ios_base::fmtflags __flags = __io.flags();
532 // [22.2.2.2.2] Table 60
533 if (__flags & ios_base::showpos)
535 if (__flags & ios_base::showpoint)
537 // As per [22.2.2.2.2.11]
538 if (__flags & ios_base::fixed || __prec > 0)
546 ios_base::fmtflags __fltfield = __flags & ios_base::floatfield;
547 // [22.2.2.2.2] Table 58
548 if (__fltfield == ios_base::fixed)
550 else if (__fltfield == ios_base::scientific)
551 *__fptr++ = (__flags & ios_base::uppercase) ? 'E' : 'e';
553 *__fptr++ = (__flags & ios_base::uppercase) ? 'G' : 'g';
559 __num_base::_S_format_int(const ios_base& __io, char* __fptr, char __mod,
562 ios_base::fmtflags __flags = __io.flags();
564 // [22.2.2.2.2] Table 60
565 if (__flags & ios_base::showpos)
567 if (__flags & ios_base::showbase)
571 // For long long types.
575 ios_base::fmtflags __bsefield = __flags & ios_base::basefield;
576 if (__bsefield == ios_base::hex)
577 *__fptr++ = (__flags & ios_base::uppercase) ? 'X' : 'x';
578 else if (__bsefield == ios_base::oct)
586 moneypunct_byname<char, false>::moneypunct_byname(const char*,
588 : moneypunct<char, false>(__refs) { }
591 moneypunct_byname<char, true>::moneypunct_byname(const char*,
593 : moneypunct<char, true>(__refs) { }