8246f4a5f475a448f86ae09d0695f61c1df3dd2e
1 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
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.
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
;
66 const size_t locale::_S_extra_categories_size
;
68 // Definitions for static const data members of locale::id
69 _Atomic_word
locale::id::_S_highwater
; // init'd to 0 by linker
71 // Definitions for static const data members of locale::_Impl
72 const locale::id
* const
73 locale::_Impl::_S_id_ctype
[] =
75 &std::ctype
<char>::id
,
76 &codecvt
<char, char, mbstate_t>::id
,
77 #ifdef _GLIBCPP_USE_WCHAR_T
78 &std::ctype
<wchar_t>::id
,
79 &codecvt
<wchar_t, char, mbstate_t>::id
,
84 const locale::id
* const
85 locale::_Impl::_S_id_numeric
[] =
90 #ifdef _GLIBCPP_USE_WCHAR_T
91 &num_get
<wchar_t>::id
,
92 &num_put
<wchar_t>::id
,
93 &numpunct
<wchar_t>::id
,
98 const locale::id
* const
99 locale::_Impl::_S_id_collate
[] =
101 &std::collate
<char>::id
,
102 #ifdef _GLIBCPP_USE_WCHAR_T
103 &std::collate
<wchar_t>::id
,
108 const locale::id
* const
109 locale::_Impl::_S_id_time
[] =
111 &__timepunct
<char>::id
,
114 #ifdef _GLIBCPP_USE_WCHAR_T
115 &__timepunct
<wchar_t>::id
,
116 &time_get
<wchar_t>::id
,
117 &time_put
<wchar_t>::id
,
122 const locale::id
* const
123 locale::_Impl::_S_id_monetary
[] =
125 &money_get
<char>::id
,
126 &money_put
<char>::id
,
127 &moneypunct
<char, false>::id
,
128 &moneypunct
<char, true >::id
,
129 #ifdef _GLIBCPP_USE_WCHAR_T
130 &money_get
<wchar_t>::id
,
131 &money_put
<wchar_t>::id
,
132 &moneypunct
<wchar_t, false>::id
,
133 &moneypunct
<wchar_t, true >::id
,
138 const locale::id
* const
139 locale::_Impl::_S_id_messages
[] =
141 &std::messages
<char>::id
,
142 #ifdef _GLIBCPP_USE_WCHAR_T
143 &std::messages
<wchar_t>::id
,
148 const locale::id
* const* const
149 locale::_Impl::_S_facet_categories
[] =
151 // Order must match the decl order in class locale.
152 locale::_Impl::_S_id_ctype
,
153 locale::_Impl::_S_id_numeric
,
154 locale::_Impl::_S_id_collate
,
155 locale::_Impl::_S_id_time
,
156 locale::_Impl::_S_id_monetary
,
157 locale::_Impl::_S_id_messages
,
161 locale::locale() throw()
164 (_M_impl
= _S_global
)->_M_add_reference();
167 locale::locale(const locale
& __other
) throw()
168 { (_M_impl
= __other
._M_impl
)->_M_add_reference(); }
170 // This is used to initialize global and classic locales, and
171 // assumes that the _Impl objects are constructed correctly.
172 // The lack of a reference increment is intentional.
173 locale::locale(_Impl
* __ip
) throw() : _M_impl(__ip
)
176 locale::locale(const char* __s
)
181 if (strcmp(__s
, "C") == 0 || strcmp(__s
, "POSIX") == 0)
182 (_M_impl
= _S_classic
)->_M_add_reference();
183 else if (strcmp(__s
, "") != 0)
184 _M_impl
= new _Impl(__s
, 1);
187 // Get it from the environment.
188 char* __env
= getenv("LC_ALL");
189 // If LC_ALL is set we are done.
190 if (__env
&& strcmp(__env
, "") != 0)
192 if (strcmp(__env
, "C") == 0 || strcmp(__env
, "POSIX") == 0)
193 (_M_impl
= _S_classic
)->_M_add_reference();
195 _M_impl
= new _Impl(__env
, 1);
200 // LANG may set a default different from "C".
201 char* __env
= getenv("LANG");
202 if (!__env
|| strcmp(__env
, "") == 0 || strcmp(__env
, "C") == 0
203 || strcmp(__env
, "POSIX") == 0)
206 __res
= strdup(__env
);
208 // Scan the categories looking for the first one
209 // different from LANG.
211 if (strcmp(__res
, "C") == 0)
212 for (; __i
< _S_categories_size
213 + _S_extra_categories_size
; ++__i
)
215 __env
= getenv(_S_categories
[__i
]);
216 if (__env
&& strcmp(__env
, "") != 0
217 && strcmp(__env
, "C") != 0
218 && strcmp(__env
, "POSIX") != 0)
222 for (; __i
< _S_categories_size
223 + _S_extra_categories_size
; ++__i
)
225 __env
= getenv(_S_categories
[__i
]);
226 if (__env
&& strcmp(__env
, "") != 0
227 && strcmp(__env
, __res
) != 0)
231 // If one is found, build the complete string of
232 // the form LC_CTYPE=xxx;LC_NUMERIC=yyy; and so on...
233 if (__i
< _S_categories_size
+ _S_extra_categories_size
)
236 for (size_t __j
= 0; __j
< __i
; ++__j
)
238 __str
+= _S_categories
[__j
];
243 __str
+= _S_categories
[__i
];
248 for (; __i
< _S_categories_size
249 + _S_extra_categories_size
; ++__i
)
251 __env
= getenv(_S_categories
[__i
]);
252 if (!__env
|| strcmp(__env
, "") == 0)
254 __str
+= _S_categories
[__i
];
259 else if (strcmp(__env
, "C") == 0
260 || strcmp(__env
, "POSIX") == 0)
262 __str
+= _S_categories
[__i
];
267 __str
+= _S_categories
[__i
];
273 __str
.erase(__str
.end() - 1);
274 _M_impl
= new _Impl(__str
.c_str(), 1);
276 // ... otherwise either an additional instance of
277 // the "C" locale or LANG.
278 else if (strcmp(__res
, "C") == 0)
279 (_M_impl
= _S_classic
)->_M_add_reference();
281 _M_impl
= new _Impl(__res
, 1);
287 __throw_runtime_error("attempt to create locale from NULL name");
290 locale::locale(const locale
& __base
, const char* __s
, category __cat
)
292 // NB: There are complicated, yet more efficient ways to do
293 // this. Building up locales on a per-category way is tedious, so
294 // let's do it this way until people complain.
296 _M_coalesce(__base
, __add
, __cat
);
299 locale::locale(const locale
& __base
, const locale
& __add
, category __cat
)
300 { _M_coalesce(__base
, __add
, __cat
); }
302 locale::~locale() throw()
303 { _M_impl
->_M_remove_reference(); }
306 locale::operator==(const locale
& __rhs
) const throw()
308 string __name
= this->name();
309 return (_M_impl
== __rhs
._M_impl
310 || (__name
!= "*" && __name
== __rhs
.name()));
314 locale::operator=(const locale
& __other
) throw()
316 __other
._M_impl
->_M_add_reference();
317 _M_impl
->_M_remove_reference();
318 _M_impl
= __other
._M_impl
;
323 locale::global(const locale
& __other
)
327 _Impl
* __old
= _S_global
;
328 __other
._M_impl
->_M_add_reference();
329 _S_global
= __other
._M_impl
;
330 if (_S_global
->_M_check_same_name()
331 && (strcmp(_S_global
->_M_names
[0], "*") != 0))
332 setlocale(LC_ALL
, __other
.name().c_str());
334 // Reference count sanity check: one reference removed for the
335 // subsition of __other locale, one added by return-by-value. Net
336 // difference: zero. When the returned locale object's destrutor
337 // is called, then the reference count is decremented and possibly
339 return locale(__old
);
346 if (_M_impl
->_M_check_same_name())
347 __ret
= _M_impl
->_M_names
[0];
350 __ret
+= _S_categories
[0];
352 __ret
+= _M_impl
->_M_names
[0];
354 __i
< _S_categories_size
+ _S_extra_categories_size
;
358 __ret
+= _S_categories
[__i
];
360 __ret
+= _M_impl
->_M_names
[__i
];
369 // Locking protocol: singleton-called-before-threading-starts
374 // 26 Standard facets, 2 references.
375 // One reference for _S_classic, one for _S_global
376 _S_classic
= new (&c_locale_impl
) _Impl(0, 2, true);
377 _S_global
= _S_classic
;
378 new (&c_locale
) locale(_S_classic
);
382 // Just call destructor, so that locale_impl_c's memory is
383 // not deallocated via a call to delete.
385 _S_classic
->~_Impl();
386 _S_classic
= _S_global
= 0;
387 __throw_exception_again
;
394 locale::_M_coalesce(const locale
& __base
, const locale
& __add
,
397 __cat
= _S_normalize_category(__cat
);
398 _M_impl
= new _Impl(*__base
._M_impl
, 1);
401 { _M_impl
->_M_replace_categories(__add
._M_impl
, __cat
); }
404 _M_impl
->_M_remove_reference();
405 __throw_exception_again
;
410 locale::_S_normalize_category(category __cat
)
413 if (__cat
== none
|| (__cat
& all
) && !(__cat
& ~all
))
417 // NB: May be a C-style "LC_ALL" category; convert.
435 #ifdef _GLIBCPP_HAVE_LC_MESSAGES
444 __throw_runtime_error("bad locale category");
451 locale::facet::_S_c_locale
;
453 char locale::facet::_S_c_name
[2];
459 facet(size_t __refs
) throw() : _M_references(__refs
? 1 : 0)
464 _M_add_reference() throw()
465 { __atomic_add(&_M_references
, 1); }
469 _M_remove_reference() throw()
471 if (__exchange_and_add(&_M_references
, -1) == 1)
483 // Definitions for static const data members of time_base
486 __timepunct
<char>::_S_timezones
[14] =
488 "GMT", "HST", "AKST", "PST", "MST", "CST", "EST", "AST", "NST", "CET",
489 "IST", "EET", "CST", "JST"
492 #ifdef _GLIBCPP_USE_WCHAR_T
495 __timepunct
<wchar_t>::_S_timezones
[14] =
497 L
"GMT", L
"HST", L
"AKST", L
"PST", L
"MST", L
"CST", L
"EST", L
"AST",
498 L
"NST", L
"CET", L
"IST", L
"EET", L
"CST", L
"JST"
502 // Definitions for static const data members of money_base
503 const money_base::pattern
504 money_base::_S_default_pattern
= { {symbol
, sign
, none
, value
} };
506 const char __num_base::_S_atoms
[] = "0123456789eEabcdfABCDF";
509 __num_base::_S_format_float(const ios_base
& __io
, char* __fptr
, char __mod
,
512 bool __incl_prec
= false;
513 ios_base::fmtflags __flags
= __io
.flags();
515 // [22.2.2.2.2] Table 60
516 if (__flags
& ios_base::showpos
)
518 if (__flags
& ios_base::showpoint
)
520 // As per [22.2.2.2.2.11]
521 if (__flags
& ios_base::fixed
|| __prec
> 0)
529 ios_base::fmtflags __fltfield
= __flags
& ios_base::floatfield
;
530 // [22.2.2.2.2] Table 58
531 if (__fltfield
== ios_base::fixed
)
533 else if (__fltfield
== ios_base::scientific
)
534 *__fptr
++ = (__flags
& ios_base::uppercase
) ? 'E' : 'e';
536 *__fptr
++ = (__flags
& ios_base::uppercase
) ? 'G' : 'g';
542 __num_base::_S_format_int(const ios_base
& __io
, char* __fptr
, char __mod
,
545 ios_base::fmtflags __flags
= __io
.flags();
547 // [22.2.2.2.2] Table 60
548 if (__flags
& ios_base::showpos
)
550 if (__flags
& ios_base::showbase
)
554 // For long long types.
558 ios_base::fmtflags __bsefield
= __flags
& ios_base::basefield
;
559 if (__bsefield
== ios_base::hex
)
560 *__fptr
++ = (__flags
& ios_base::uppercase
) ? 'X' : 'x';
561 else if (__bsefield
== ios_base::oct
)