]>
gcc.gnu.org Git - gcc.git/blob - libstdc++-v3/src/locale.cc
7788a58419d21d08ddb0462c21744645e04bdfad
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.
32 #include <cwctype> // For towupper, etc.
34 #include <bits/atomicity.h>
38 // Defined in globals.cc.
39 extern std::locale c_locale
;
40 extern std::locale::_Impl c_locale_impl
;
41 } // namespace __gnu_cxx
45 using namespace __gnu_cxx
;
47 // Definitions for static const data members of locale.
48 const locale::category
locale::none
;
49 const locale::category
locale::ctype
;
50 const locale::category
locale::numeric
;
51 const locale::category
locale::collate
;
52 const locale::category
locale::time
;
53 const locale::category
locale::monetary
;
54 const locale::category
locale::messages
;
55 const locale::category
locale::all
;
57 // In the future, GLIBCXX_ABI > 5 should remove all uses of
58 // _GLIBCPP_ASM_SYMVER in this file, and remove exports of any
59 // static data members of locale.
61 // These are no longer exported.
62 locale::_Impl
* locale::_S_classic
;
63 locale::_Impl
* locale::_S_global
;
64 const size_t locale::_S_categories_size
;
66 // Definitions for static const data members of locale::id
67 _Atomic_word
locale::id::_S_highwater
; // init'd to 0 by linker
69 // Definitions for static const data members of locale::_Impl
70 const locale::id
* const
71 locale::_Impl::_S_id_ctype
[] =
73 &std::ctype
<char>::id
,
74 &codecvt
<char, char, mbstate_t>::id
,
75 #ifdef _GLIBCPP_USE_WCHAR_T
76 &std::ctype
<wchar_t>::id
,
77 &codecvt
<wchar_t, char, mbstate_t>::id
,
82 const locale::id
* const
83 locale::_Impl::_S_id_numeric
[] =
88 #ifdef _GLIBCPP_USE_WCHAR_T
89 &num_get
<wchar_t>::id
,
90 &num_put
<wchar_t>::id
,
91 &numpunct
<wchar_t>::id
,
96 const locale::id
* const
97 locale::_Impl::_S_id_collate
[] =
99 &std::collate
<char>::id
,
100 #ifdef _GLIBCPP_USE_WCHAR_T
101 &std::collate
<wchar_t>::id
,
106 const locale::id
* const
107 locale::_Impl::_S_id_time
[] =
109 &__timepunct
<char>::id
,
112 #ifdef _GLIBCPP_USE_WCHAR_T
113 &__timepunct
<wchar_t>::id
,
114 &time_get
<wchar_t>::id
,
115 &time_put
<wchar_t>::id
,
120 const locale::id
* const
121 locale::_Impl::_S_id_monetary
[] =
123 &money_get
<char>::id
,
124 &money_put
<char>::id
,
125 &moneypunct
<char, false>::id
,
126 &moneypunct
<char, true >::id
,
127 #ifdef _GLIBCPP_USE_WCHAR_T
128 &money_get
<wchar_t>::id
,
129 &money_put
<wchar_t>::id
,
130 &moneypunct
<wchar_t, false>::id
,
131 &moneypunct
<wchar_t, true >::id
,
136 const locale::id
* const
137 locale::_Impl::_S_id_messages
[] =
139 &std::messages
<char>::id
,
140 #ifdef _GLIBCPP_USE_WCHAR_T
141 &std::messages
<wchar_t>::id
,
146 const locale::id
* const* const
147 locale::_Impl::_S_facet_categories
[] =
149 // Order must match the decl order in class locale.
150 locale::_Impl::_S_id_ctype
,
151 locale::_Impl::_S_id_numeric
,
152 locale::_Impl::_S_id_collate
,
153 locale::_Impl::_S_id_time
,
154 locale::_Impl::_S_id_monetary
,
155 locale::_Impl::_S_id_messages
,
159 locale::locale() throw()
162 (_M_impl
= _S_global
)->_M_add_reference();
165 locale::locale(const locale
& __other
) throw()
166 { (_M_impl
= __other
._M_impl
)->_M_add_reference(); }
168 // This is used to initialize global and classic locales, and
169 // assumes that the _Impl objects are constructed correctly.
170 // The lack of a reference increment is intentional.
171 locale::locale(_Impl
* __ip
) throw() : _M_impl(__ip
)
174 locale::locale(const char* __s
)
179 if (strcmp(__s
, "C") == 0 || strcmp(__s
, "POSIX") == 0)
180 (_M_impl
= _S_classic
)->_M_add_reference();
181 else if (strcmp(__s
, "") != 0)
182 _M_impl
= new _Impl(__s
, 1);
185 // Get it from the environment.
186 char* __env
= getenv("LC_ALL");
187 // If LC_ALL is set we are done.
188 if (__env
&& strcmp(__env
, "") != 0)
190 if (strcmp(__env
, "C") == 0 || strcmp(__env
, "POSIX") == 0)
191 (_M_impl
= _S_classic
)->_M_add_reference();
193 _M_impl
= new _Impl(__env
, 1);
198 // LANG may set a default different from "C".
199 char* __env
= getenv("LANG");
200 if (!__env
|| strcmp(__env
, "") == 0 || strcmp(__env
, "C") == 0
201 || strcmp(__env
, "POSIX") == 0)
204 __res
= strdup(__env
);
206 // Scan the categories looking for the first one
207 // different from LANG.
209 if (strcmp(__res
, "C") == 0)
210 for (; __i
< _S_categories_size
; ++__i
)
212 __env
= getenv(_S_categories
[__i
]);
213 if (__env
&& strcmp(__env
, "") != 0
214 && strcmp(__env
, "C") != 0
215 && strcmp(__env
, "POSIX") != 0)
219 for (; __i
< _S_categories_size
; ++__i
)
221 __env
= getenv(_S_categories
[__i
]);
222 if (__env
&& strcmp(__env
, "") != 0
223 && strcmp(__env
, __res
) != 0)
227 // If one is found, build the complete string of
228 // the form LC_CTYPE=xxx;LC_NUMERIC=yyy; and so on...
229 if (__i
< _S_categories_size
)
232 for (size_t __j
= 0; __j
< __i
; ++__j
)
234 __str
+= _S_categories
[__j
];
239 __str
+= _S_categories
[__i
];
244 for (; __i
< _S_categories_size
; ++__i
)
246 __env
= getenv(_S_categories
[__i
]);
247 if (!__env
|| strcmp(__env
, "") == 0)
249 __str
+= _S_categories
[__i
];
254 else if (strcmp(__env
, "C") == 0
255 || strcmp(__env
, "POSIX") == 0)
257 __str
+= _S_categories
[__i
];
262 __str
+= _S_categories
[__i
];
268 __str
.erase(__str
.end() - 1);
269 _M_impl
= new _Impl(__str
.c_str(), 1);
271 // ... otherwise either an additional instance of
272 // the "C" locale or LANG.
273 else if (strcmp(__res
, "C") == 0)
274 (_M_impl
= _S_classic
)->_M_add_reference();
276 _M_impl
= new _Impl(__res
, 1);
282 __throw_runtime_error("locale::locale NULL not valid");
285 locale::locale(const locale
& __base
, const char* __s
, category __cat
)
287 // NB: There are complicated, yet more efficient ways to do
288 // this. Building up locales on a per-category way is tedious, so
289 // let's do it this way until people complain.
291 _M_coalesce(__base
, __add
, __cat
);
294 locale::locale(const locale
& __base
, const locale
& __add
, category __cat
)
295 { _M_coalesce(__base
, __add
, __cat
); }
297 locale::~locale() throw()
298 { _M_impl
->_M_remove_reference(); }
301 locale::operator==(const locale
& __rhs
) const throw()
303 string __name
= this->name();
304 return (_M_impl
== __rhs
._M_impl
305 || (__name
!= "*" && __name
== __rhs
.name()));
309 locale::operator=(const locale
& __other
) throw()
311 __other
._M_impl
->_M_add_reference();
312 _M_impl
->_M_remove_reference();
313 _M_impl
= __other
._M_impl
;
318 locale::global(const locale
& __other
)
322 _Impl
* __old
= _S_global
;
323 __other
._M_impl
->_M_add_reference();
324 _S_global
= __other
._M_impl
;
325 if (_S_global
->_M_check_same_name()
326 && (strcmp(_S_global
->_M_names
[0], "*") != 0))
327 setlocale(LC_ALL
, __other
.name().c_str());
329 // Reference count sanity check: one reference removed for the
330 // subsition of __other locale, one added by return-by-value. Net
331 // difference: zero. When the returned locale object's destrutor
332 // is called, then the reference count is decremented and possibly
334 return locale(__old
);
341 if (_M_impl
->_M_check_same_name())
342 __ret
= _M_impl
->_M_names
[0];
345 __ret
+= _S_categories
[0];
347 __ret
+= _M_impl
->_M_names
[0];
348 for (size_t __i
= 1; __i
< _S_categories_size
; ++__i
)
351 __ret
+= _S_categories
[__i
];
353 __ret
+= _M_impl
->_M_names
[__i
];
362 // Locking protocol: singleton-called-before-threading-starts
367 // 26 Standard facets, 2 references.
368 // One reference for _S_classic, one for _S_global
369 _S_classic
= new (&c_locale_impl
) _Impl(0, 2, true);
370 _S_global
= _S_classic
;
371 new (&c_locale
) locale(_S_classic
);
375 // Just call destructor, so that locale_impl_c's memory is
376 // not deallocated via a call to delete.
378 _S_classic
->~_Impl();
379 _S_classic
= _S_global
= 0;
380 __throw_exception_again
;
387 locale::_M_coalesce(const locale
& __base
, const locale
& __add
,
390 __cat
= _S_normalize_category(__cat
);
391 _M_impl
= new _Impl(*__base
._M_impl
, 1);
394 { _M_impl
->_M_replace_categories(__add
._M_impl
, __cat
); }
397 _M_impl
->_M_remove_reference();
398 __throw_exception_again
;
403 locale::_S_normalize_category(category __cat
)
406 if (__cat
== none
|| (__cat
& all
) && !(__cat
& ~all
))
410 // NB: May be a C-style "LC_ALL" category; convert.
428 #ifdef _GLIBCPP_HAVE_LC_MESSAGES
437 __throw_runtime_error("locale::_S_normalize_category "
438 "category not found");
445 locale::facet::_S_c_locale
;
447 char locale::facet::_S_c_name
[2];
453 const __numpunct_cache
<char>&
454 __use_cache(const locale
& __loc
)
456 size_t __i
= numpunct
<char>::id
._M_id();
457 const locale::facet
** __caches
= __loc
._M_impl
->_M_caches
;
460 __numpunct_cache
<char>* __tmp
= new __numpunct_cache
<char>;
461 __tmp
->_M_cache(__loc
);
462 __loc
._M_impl
->_M_install_cache(__tmp
, __i
);
464 return static_cast<const __numpunct_cache
<char>&>(*__caches
[__i
]);
467 #ifdef _GLIBCPP_USE_WCHAR_T
469 const __numpunct_cache
<wchar_t>&
470 __use_cache(const locale
& __loc
)
472 size_t __i
= numpunct
<wchar_t>::id
._M_id();
473 const locale::facet
** __caches
= __loc
._M_impl
->_M_caches
;
476 __numpunct_cache
<wchar_t>* __tmp
= new __numpunct_cache
<wchar_t>;
477 __tmp
->_M_cache(__loc
);
478 __loc
._M_impl
->_M_install_cache(__tmp
, __i
);
480 return static_cast<const __numpunct_cache
<wchar_t>&>(*__caches
[__i
]);
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_in
= "0123456789eEabcdfABCDF";
508 const char* __num_base::_S_atoms_out
="-+xX0123456789abcdef0123456789ABCDEF";
510 // _GLIBCPP_RESOLVE_LIB_DEFECTS
511 // According to the resolution of DR 231, about 22.2.2.2.2, p11,
512 // "str.precision() is specified in the conversion specification".
514 __num_base::_S_format_float(const ios_base
& __io
, char* __fptr
, char __mod
)
516 ios_base::fmtflags __flags
= __io
.flags();
518 // [22.2.2.2.2] Table 60
519 if (__flags
& ios_base::showpos
)
521 if (__flags
& ios_base::showpoint
)
524 // As per DR 231: _always_, not only when
525 // __flags & ios_base::fixed || __prec > 0
531 ios_base::fmtflags __fltfield
= __flags
& ios_base::floatfield
;
532 // [22.2.2.2.2] Table 58
533 if (__fltfield
== ios_base::fixed
)
535 else if (__fltfield
== ios_base::scientific
)
536 *__fptr
++ = (__flags
& ios_base::uppercase
) ? 'E' : 'e';
538 *__fptr
++ = (__flags
& ios_base::uppercase
) ? 'G' : 'g';
This page took 0.065972 seconds and 4 git commands to generate.