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.
28 #include <bits/std_clocale.h>
29 #include <bits/std_cstring.h>
30 #include <bits/std_cassert.h>
31 #include <bits/std_cctype.h>
32 #include <bits/std_limits.h>
34 #include <bits/std_stdexcept.h>
35 #include <bits/std_locale.h>
36 #include <bits/std_istream.h>
37 #include <bits/std_ostream.h>
38 #include <bits/std_vector.h>
39 #include <bits/std_memory.h> // for auto_ptr
40 #ifdef _GLIBCPP_USE_WCHAR_T
41 # include <bits/std_cwctype.h> // for towupper, etc.
46 // Defined in globals.cc.
47 extern locale::_Impl locale_impl_c;
48 extern locale locale_c;
50 // Definitions for static const data members of locale.
51 const locale::category locale::none;
52 const locale::category locale::ctype;
53 const locale::category locale::numeric;
54 const locale::category locale::collate;
55 const locale::category locale::time;
56 const locale::category locale::monetary;
57 const locale::category locale::messages;
58 const locale::category locale::all;
60 locale::_Impl* locale::_S_classic;
61 locale::_Impl* locale::_S_global;
62 const size_t locale::_S_num_categories;
63 const size_t locale::_S_num_facets;
65 // Definitions for locale::id of standard facets.
66 locale::id ctype<char>::id;
67 locale::id codecvt<char, char, mbstate_t>::id;
69 #ifdef _GLIBCPP_USE_WCHAR_T
70 locale::id ctype<wchar_t>::id;
71 locale::id codecvt<wchar_t, char, mbstate_t>::id;
74 // Definitions for static const data members of locale::id
75 size_t locale::id::_S_highwater; // init'd to 0 by linker
77 // Definitions for static const data members of locale::_Impl
78 const locale::id* const
79 locale::_Impl::_S_id_ctype[] =
81 &std::ctype<char>::id,
82 &codecvt<char, char, mbstate_t>::id,
83 #ifdef _GLIBCPP_USE_WCHAR_T
84 &std::ctype<wchar_t>::id,
85 &codecvt<wchar_t, char, mbstate_t>::id,
90 const locale::id* const
91 locale::_Impl::_S_id_numeric[] =
96 #ifdef _GLIBCPP_USE_WCHAR_T
97 &num_get<wchar_t>::id,
98 &num_put<wchar_t>::id,
99 &numpunct<wchar_t>::id,
104 const locale::id* const
105 locale::_Impl::_S_id_collate[] =
107 &std::collate<char>::id,
108 #ifdef _GLIBCPP_USE_WCHAR_T
109 &std::collate<wchar_t>::id,
114 const locale::id* const
115 locale::_Impl::_S_id_time[] =
117 &__timepunct<char>::id,
120 #ifdef _GLIBCPP_USE_WCHAR_T
121 &__timepunct<wchar_t>::id,
122 &time_get<wchar_t>::id,
123 &time_put<wchar_t>::id,
128 const locale::id* const
129 locale::_Impl::_S_id_monetary[] =
131 &money_get<char>::id,
132 &money_put<char>::id,
133 &moneypunct<char, false>::id,
134 &moneypunct<char, true >::id,
135 #ifdef _GLIBCPP_USE_WCHAR_T
136 &money_get<wchar_t>::id,
137 &money_put<wchar_t>::id,
138 &moneypunct<wchar_t, false>::id,
139 &moneypunct<wchar_t, true >::id,
144 const locale::id* const
145 locale::_Impl::_S_id_messages[] =
147 &std::messages<char>::id,
148 #ifdef _GLIBCPP_USE_WCHAR_T
149 &std::messages<wchar_t>::id,
154 const locale::id* const* const
155 locale::_Impl::_S_facet_categories[] =
157 // Order must match the decl order in class locale.
158 locale::_Impl::_S_id_ctype,
159 locale::_Impl::_S_id_numeric,
160 locale::_Impl::_S_id_collate,
161 locale::_Impl::_S_id_time,
162 locale::_Impl::_S_id_monetary,
163 locale::_Impl::_S_id_messages,
167 locale::~locale() throw()
168 { _M_impl->_M_remove_reference(); }
171 locale::_M_coalesce(const locale& __base, const locale& __add,
174 __cat = _S_normalize_category(__cat);
175 _M_impl = new _Impl(*__base._M_impl, 1);
178 { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
181 _M_impl->_M_remove_reference();
182 __throw_exception_again;
186 locale::locale() throw()
189 (_M_impl = _S_global)->_M_add_reference();
192 locale::locale(const locale& __other) throw()
193 { (_M_impl = __other._M_impl)->_M_add_reference(); }
195 // This is used to initialize global and classic locales, and
196 // assumes that the _Impl objects are constructed correctly.
197 locale::locale(_Impl* __ip) throw() : _M_impl(__ip)
200 locale::locale(const char* __s)
205 if (strcmp(__s, "C") == 0 || strcmp(__s, "POSIX") == 0)
206 (_M_impl = _S_classic)->_M_add_reference();
207 else if (strcmp(__s, "") == 0)
208 _M_impl = new _Impl(setlocale(LC_ALL, __s), 1);
210 _M_impl = new _Impl(__s, 1);
213 __throw_runtime_error("attempt to create locale from NULL name");
216 locale::locale(const locale& __base, const char* __s, category __cat)
218 // NB: There are complicated, yet more efficient ways to do
219 // this. Building up locales on a per-category way is tedious, so
220 // let's do it this way until people complain.
222 _M_coalesce(__base, __add, __cat);
225 locale::locale(const locale& __base, const locale& __add, category __cat)
226 { _M_coalesce(__base, __add, __cat); }
229 locale::operator==(const locale& __rhs) const throw()
231 string __name = this->name();
232 return (_M_impl == __rhs._M_impl
233 || (__name != "*" && __name == __rhs.name()));
237 locale::operator=(const locale& __other) throw()
239 __other._M_impl->_M_add_reference();
240 _M_impl->_M_remove_reference();
241 _M_impl = __other._M_impl;
246 locale::global(const locale& __other)
250 _Impl* __old = _S_global;
251 __other._M_impl->_M_add_reference();
252 _S_global = __other._M_impl;
253 if (_S_global->_M_check_same_name() && _S_global->_M_names[0] != "*")
254 setlocale(LC_ALL, __other.name().c_str());
256 // Reference count sanity check: one reference removed for the
257 // subsition of __other locale, one added by return-by-value. Net
258 // difference: zero. When the returned locale object's destrutor
259 // is called, then the reference count is decremented and possibly
261 return locale(__old);
268 // Need some kind of separator character. This one was pretty much
269 // arbitrarily chosen as to not conflict with glibc locales: the
270 // exact formatting is not set in stone.
271 const char __separator = '|';
273 if (_M_impl->_M_check_same_name())
274 __ret = _M_impl->_M_names[0];
277 for (size_t i = 0; i < _S_num_categories; ++i)
278 __ret += __separator + _M_impl->_M_names[i];
291 // 26 Standard facets, 2 references.
292 // One reference for _M_classic, one for _M_global
293 _S_classic = new (&locale_impl_c) _Impl("C", 2);
294 _S_global = _S_classic;
295 new (&locale_c) locale(_S_classic);
299 // Just call destructor, so that locale_impl_c's memory is
300 // not deallocated via a call to delete.
302 _S_classic->~_Impl();
303 _S_classic = _S_global = 0;
304 __throw_exception_again;
311 locale::_S_normalize_category(category __cat)
314 if (__cat == none || (__cat & all) && !(__cat & ~all))
318 // NB: May be a C-style "LC_ALL" category; convert.
336 #ifdef _GLIBCPP_HAVE_LC_MESSAGES
345 __throw_runtime_error("bad locale category");
352 facet(size_t __refs) throw() : _M_references(__refs)
357 _M_add_reference() throw()
358 { ++_M_references; } // XXX MT
362 _M_remove_reference() throw()
364 if (_M_references-- == 0)
373 // Definitions for static const data members of ctype_base.
374 const ctype_base::mask ctype_base::space;
375 const ctype_base::mask ctype_base::print;
376 const ctype_base::mask ctype_base::cntrl;
377 const ctype_base::mask ctype_base::upper;
378 const ctype_base::mask ctype_base::lower;
379 const ctype_base::mask ctype_base::alpha;
380 const ctype_base::mask ctype_base::digit;
381 const ctype_base::mask ctype_base::punct;
382 const ctype_base::mask ctype_base::xdigit;
383 const ctype_base::mask ctype_base::alnum;
384 const ctype_base::mask ctype_base::graph;
386 // Platform-specific initialization code for ctype tables.
387 #include <bits/ctype_noninline.h>
389 const size_t ctype<char>::table_size;
391 ctype<char>::~ctype()
392 { if (_M_del) delete[] this->table(); }
394 // These are dummy placeholders as these virtual functions are never called.
396 ctype<char>::do_is(mask, char_type) const
400 ctype<char>::do_is(const char_type* __c, const char_type*, mask*) const
404 ctype<char>::do_scan_is(mask, const char_type* __c, const char_type*) const
408 ctype<char>::do_scan_not(mask, const char_type* __c, const char_type*) const
412 ctype<char>::do_widen(char __c) const
416 ctype<char>::do_widen(const char* __lo, const char* __hi, char* __dest) const
418 memcpy(__dest, __lo, __hi - __lo);
423 ctype<char>::do_narrow(char __c, char /*__dfault*/) const
427 ctype<char>::do_narrow(const char* __lo, const char* __hi,
428 char /*__dfault*/, char* __dest) const
430 memcpy(__dest, __lo, __hi - __lo);
435 ctype_byname<char>::ctype_byname(const char* /*__s*/, size_t __refs)
436 : ctype<char>(new mask[table_size], true, __refs)
439 #ifdef _GLIBCPP_USE_WCHAR_T
440 ctype<wchar_t>::__wmask_type
441 ctype<wchar_t>::_M_convert_to_wmask(const mask __m) const
447 __ret = wctype("space");
450 __ret = wctype("print");
453 __ret = wctype("cntrl");
456 __ret = wctype("upper");
459 __ret = wctype("lower");
462 __ret = wctype("alpha");
465 __ret = wctype("digit");
468 __ret = wctype("punct");
471 __ret = wctype("xdigit");
474 __ret = wctype("alnum");
477 __ret = wctype("graph");
485 ctype<wchar_t>::~ctype() { }
487 // NB: These ctype<wchar_t> methods are not configuration-specific,
488 // unlike the ctype<char> bits.
489 ctype<wchar_t>::ctype(size_t __refs)
490 : __ctype_abstract_base<wchar_t>(__refs) { }
492 ctype<wchar_t>::ctype(__c_locale /*__cloc*/, size_t __refs)
493 : __ctype_abstract_base<wchar_t>(__refs) { }
496 ctype<wchar_t>::do_toupper(wchar_t __c) const
497 { return towupper(__c); }
500 ctype<wchar_t>::do_toupper(wchar_t* __lo, const wchar_t* __hi) const
504 *__lo = towupper(*__lo);
511 ctype<wchar_t>::do_tolower(wchar_t __c) const
512 { return towlower(__c); }
515 ctype<wchar_t>::do_tolower(wchar_t* __lo, const wchar_t* __hi) const
519 *__lo = towlower(*__lo);
527 do_is(mask __m, char_type __c) const
528 { return static_cast<bool>(iswctype(__c, _M_convert_to_wmask(__m))); }
532 do_is(const wchar_t* __lo, const wchar_t* __hi, mask* __m) const
534 while (__lo < __hi && !this->is(*__m, *__lo))
541 do_scan_is(mask __m, const wchar_t* __lo, const wchar_t* __hi) const
543 while (__lo < __hi && !this->is(__m, *__lo))
550 do_scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
552 while (__lo < __hi && this->is(__m, *__lo) != 0)
559 do_widen(char __c) const
560 { return btowc(__c); }
564 do_widen(const char* __lo, const char* __hi, wchar_t* __dest) const
567 memset(static_cast<void*>(&__state), 0, sizeof(mbstate_t));
568 mbsrtowcs(__dest, &__lo, __hi - __lo, &__state);
574 do_narrow(wchar_t __wc, char __dfault) const
576 int __c = wctob(__wc);
577 return (__c == EOF ? __dfault : static_cast<char>(__c));
582 do_narrow(const wchar_t* __lo, const wchar_t* __hi, char __dfault,
586 memset(static_cast<void*>(&__state), 0, sizeof(mbstate_t));
587 size_t __len = __hi - __lo;
588 size_t __conv = wcsrtombs(__dest, &__lo, __len, &__state);
595 ctype_byname<wchar_t>::
596 ctype_byname(const char* /*__s*/, size_t __refs)
597 : ctype<wchar_t>(__refs) { }
598 #endif // _GLIBCPP_USE_WCHAR_T
600 // Definitions for static const data members of time_base
603 __timepunct<char>::_S_timezones[14] =
605 "GMT", "HST", "AKST", "PST", "MST", "CST", "EST", "AST", "NST", "CET",
606 "IST", "EET", "CST", "JST"
609 #ifdef _GLIBCPP_USE_WCHAR_T
612 __timepunct<wchar_t>::_S_timezones[14] =
614 L"GMT", L"HST", L"AKST", L"PST", L"MST", L"CST", L"EST", L"AST",
615 L"NST", L"CET", L"IST", L"EET", L"CST", L"JST"
619 // Definitions for static const data members of money_base
620 const money_base::pattern
621 money_base::_S_default_pattern = {{symbol, sign, none, value}};
625 use_facet<ctype<char> >(const locale& __loc)
627 size_t __i = ctype<char>::id._M_index;
628 const locale::_Impl* __tmp = __loc._M_impl;
629 return static_cast<const ctype<char>&>(* (*(__tmp->_M_facets))[__i]);
632 #ifdef _GLIBCPP_USE_WCHAR_T
634 const ctype<wchar_t>&
635 use_facet<ctype<wchar_t> >(const locale& __loc)
637 size_t __i = ctype<wchar_t>::id._M_index;
638 const locale::_Impl* __tmp = __loc._M_impl;
639 return static_cast<const ctype<wchar_t>&>(* (*(__tmp->_M_facets))[__i]);
644 const char __num_base::_S_atoms[] = "0123456789eEabcdfABCDF";
646 const double __num_base::_S_scale_hex = log(10.0)/log(16.0);
648 const double __num_base::_S_scale_oct = log(10.0)/log(8.0);
651 __num_base::_S_format_float(const ios_base& __io, char* __fptr, char __mod,
654 bool __incl_prec = false;
655 ios_base::fmtflags __flags = __io.flags();
657 // [22.2.2.2.2] Table 60
658 if (__flags & ios_base::showpos)
660 if (__flags & ios_base::showpoint)
662 // As per [22.2.2.2.2.11]
663 if (__flags & ios_base::fixed || __prec > 0)
671 ios_base::fmtflags __fltfield = __flags & ios_base::floatfield;
672 // [22.2.2.2.2] Table 58
673 if (__fltfield == ios_base::fixed)
675 else if (__fltfield == ios_base::scientific)
676 *__fptr++ = (__flags & ios_base::uppercase) ? 'E' : 'e';
678 *__fptr++ = (__flags & ios_base::uppercase) ? 'G' : 'g';
684 __num_base::_S_format_int(const ios_base& __io, char* __fptr, char __mod,
687 ios_base::fmtflags __flags = __io.flags();
689 // [22.2.2.2.2] Table 60
690 if (__flags & ios_base::showpos)
692 if (__flags & ios_base::showbase)
696 // For long long types.
700 ios_base::fmtflags __bsefield = __flags & ios_base::basefield;
701 if (__bsefield == ios_base::hex)
702 *__fptr++ = (__flags & ios_base::uppercase) ? 'X' : 'x';
703 else if (__bsefield == ios_base::oct)
711 moneypunct_byname<char, false>::moneypunct_byname(const char* /*__s*/,
713 : moneypunct<char, false>(__refs) { }
716 moneypunct_byname<char, true>::moneypunct_byname(const char* /*__s*/,
718 : moneypunct<char, true>(__refs) { }