]> gcc.gnu.org Git - gcc.git/blob - libstdc++-v3/src/locale.cc
7788a58419d21d08ddb0462c21744645e04bdfad
[gcc.git] / libstdc++-v3 / src / locale.cc
1 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
2 // Free Software Foundation, Inc.
3 //
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)
8 // any later version.
9
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.
14
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,
18 // USA.
19
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.
28
29 #include <clocale>
30 #include <cstring>
31 #include <cctype>
32 #include <cwctype> // For towupper, etc.
33 #include <locale>
34 #include <bits/atomicity.h>
35
36 namespace __gnu_cxx
37 {
38 // Defined in globals.cc.
39 extern std::locale c_locale;
40 extern std::locale::_Impl c_locale_impl;
41 } // namespace __gnu_cxx
42
43 namespace std
44 {
45 using namespace __gnu_cxx;
46
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;
56
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.
60
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;
65
66 // Definitions for static const data members of locale::id
67 _Atomic_word locale::id::_S_highwater; // init'd to 0 by linker
68
69 // Definitions for static const data members of locale::_Impl
70 const locale::id* const
71 locale::_Impl::_S_id_ctype[] =
72 {
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,
78 #endif
79 0
80 };
81
82 const locale::id* const
83 locale::_Impl::_S_id_numeric[] =
84 {
85 &num_get<char>::id,
86 &num_put<char>::id,
87 &numpunct<char>::id,
88 #ifdef _GLIBCPP_USE_WCHAR_T
89 &num_get<wchar_t>::id,
90 &num_put<wchar_t>::id,
91 &numpunct<wchar_t>::id,
92 #endif
93 0
94 };
95
96 const locale::id* const
97 locale::_Impl::_S_id_collate[] =
98 {
99 &std::collate<char>::id,
100 #ifdef _GLIBCPP_USE_WCHAR_T
101 &std::collate<wchar_t>::id,
102 #endif
103 0
104 };
105
106 const locale::id* const
107 locale::_Impl::_S_id_time[] =
108 {
109 &__timepunct<char>::id,
110 &time_get<char>::id,
111 &time_put<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,
116 #endif
117 0
118 };
119
120 const locale::id* const
121 locale::_Impl::_S_id_monetary[] =
122 {
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,
132 #endif
133 0
134 };
135
136 const locale::id* const
137 locale::_Impl::_S_id_messages[] =
138 {
139 &std::messages<char>::id,
140 #ifdef _GLIBCPP_USE_WCHAR_T
141 &std::messages<wchar_t>::id,
142 #endif
143 0
144 };
145
146 const locale::id* const* const
147 locale::_Impl::_S_facet_categories[] =
148 {
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,
156 0
157 };
158
159 locale::locale() throw()
160 {
161 _S_initialize();
162 (_M_impl = _S_global)->_M_add_reference();
163 }
164
165 locale::locale(const locale& __other) throw()
166 { (_M_impl = __other._M_impl)->_M_add_reference(); }
167
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)
172 { }
173
174 locale::locale(const char* __s)
175 {
176 if (__s)
177 {
178 _S_initialize();
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);
183 else
184 {
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)
189 {
190 if (strcmp(__env, "C") == 0 || strcmp(__env, "POSIX") == 0)
191 (_M_impl = _S_classic)->_M_add_reference();
192 else
193 _M_impl = new _Impl(__env, 1);
194 }
195 else
196 {
197 char* __res;
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)
202 __res = strdup("C");
203 else
204 __res = strdup(__env);
205
206 // Scan the categories looking for the first one
207 // different from LANG.
208 size_t __i = 0;
209 if (strcmp(__res, "C") == 0)
210 for (; __i < _S_categories_size; ++__i)
211 {
212 __env = getenv(_S_categories[__i]);
213 if (__env && strcmp(__env, "") != 0
214 && strcmp(__env, "C") != 0
215 && strcmp(__env, "POSIX") != 0)
216 break;
217 }
218 else
219 for (; __i < _S_categories_size; ++__i)
220 {
221 __env = getenv(_S_categories[__i]);
222 if (__env && strcmp(__env, "") != 0
223 && strcmp(__env, __res) != 0)
224 break;
225 }
226
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)
230 {
231 string __str;
232 for (size_t __j = 0; __j < __i; ++__j)
233 {
234 __str += _S_categories[__j];
235 __str += '=';
236 __str += __res;
237 __str += ';';
238 }
239 __str += _S_categories[__i];
240 __str += '=';
241 __str += __env;
242 __str += ';';
243 __i++;
244 for (; __i < _S_categories_size; ++__i)
245 {
246 __env = getenv(_S_categories[__i]);
247 if (!__env || strcmp(__env, "") == 0)
248 {
249 __str += _S_categories[__i];
250 __str += '=';
251 __str += __res;
252 __str += ';';
253 }
254 else if (strcmp(__env, "C") == 0
255 || strcmp(__env, "POSIX") == 0)
256 {
257 __str += _S_categories[__i];
258 __str += "=C;";
259 }
260 else
261 {
262 __str += _S_categories[__i];
263 __str += '=';
264 __str += __env;
265 __str += ';';
266 }
267 }
268 __str.erase(__str.end() - 1);
269 _M_impl = new _Impl(__str.c_str(), 1);
270 }
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();
275 else
276 _M_impl = new _Impl(__res, 1);
277 free(__res);
278 }
279 }
280 }
281 else
282 __throw_runtime_error("locale::locale NULL not valid");
283 }
284
285 locale::locale(const locale& __base, const char* __s, category __cat)
286 {
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.
290 locale __add(__s);
291 _M_coalesce(__base, __add, __cat);
292 }
293
294 locale::locale(const locale& __base, const locale& __add, category __cat)
295 { _M_coalesce(__base, __add, __cat); }
296
297 locale::~locale() throw()
298 { _M_impl->_M_remove_reference(); }
299
300 bool
301 locale::operator==(const locale& __rhs) const throw()
302 {
303 string __name = this->name();
304 return (_M_impl == __rhs._M_impl
305 || (__name != "*" && __name == __rhs.name()));
306 }
307
308 const locale&
309 locale::operator=(const locale& __other) throw()
310 {
311 __other._M_impl->_M_add_reference();
312 _M_impl->_M_remove_reference();
313 _M_impl = __other._M_impl;
314 return *this;
315 }
316
317 locale
318 locale::global(const locale& __other)
319 {
320 // XXX MT
321 _S_initialize();
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());
328
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
333 // destroyed.
334 return locale(__old);
335 }
336
337 string
338 locale::name() const
339 {
340 string __ret;
341 if (_M_impl->_M_check_same_name())
342 __ret = _M_impl->_M_names[0];
343 else
344 {
345 __ret += _S_categories[0];
346 __ret += '=';
347 __ret += _M_impl->_M_names[0];
348 for (size_t __i = 1; __i < _S_categories_size; ++__i)
349 {
350 __ret += ';';
351 __ret += _S_categories[__i];
352 __ret += '=';
353 __ret += _M_impl->_M_names[__i];
354 }
355 }
356 return __ret;
357 }
358
359 const locale&
360 locale::classic()
361 {
362 // Locking protocol: singleton-called-before-threading-starts
363 if (!_S_classic)
364 {
365 try
366 {
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);
372 }
373 catch(...)
374 {
375 // Just call destructor, so that locale_impl_c's memory is
376 // not deallocated via a call to delete.
377 if (_S_classic)
378 _S_classic->~_Impl();
379 _S_classic = _S_global = 0;
380 __throw_exception_again;
381 }
382 }
383 return c_locale;
384 }
385
386 void
387 locale::_M_coalesce(const locale& __base, const locale& __add,
388 category __cat)
389 {
390 __cat = _S_normalize_category(__cat);
391 _M_impl = new _Impl(*__base._M_impl, 1);
392
393 try
394 { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
395 catch (...)
396 {
397 _M_impl->_M_remove_reference();
398 __throw_exception_again;
399 }
400 }
401
402 locale::category
403 locale::_S_normalize_category(category __cat)
404 {
405 int __ret = 0;
406 if (__cat == none || (__cat & all) && !(__cat & ~all))
407 __ret = __cat;
408 else
409 {
410 // NB: May be a C-style "LC_ALL" category; convert.
411 switch (__cat)
412 {
413 case LC_COLLATE:
414 __ret = collate;
415 break;
416 case LC_CTYPE:
417 __ret = ctype;
418 break;
419 case LC_MONETARY:
420 __ret = monetary;
421 break;
422 case LC_NUMERIC:
423 __ret = numeric;
424 break;
425 case LC_TIME:
426 __ret = time;
427 break;
428 #ifdef _GLIBCPP_HAVE_LC_MESSAGES
429 case LC_MESSAGES:
430 __ret = messages;
431 break;
432 #endif
433 case LC_ALL:
434 __ret = all;
435 break;
436 default:
437 __throw_runtime_error("locale::_S_normalize_category "
438 "category not found");
439 }
440 }
441 return __ret;
442 }
443
444 __c_locale
445 locale::facet::_S_c_locale;
446
447 char locale::facet::_S_c_name[2];
448
449 locale::facet::
450 ~facet() { }
451
452 template<>
453 const __numpunct_cache<char>&
454 __use_cache(const locale& __loc)
455 {
456 size_t __i = numpunct<char>::id._M_id();
457 const locale::facet** __caches = __loc._M_impl->_M_caches;
458 if (!__caches[__i])
459 {
460 __numpunct_cache<char>* __tmp = new __numpunct_cache<char>;
461 __tmp->_M_cache(__loc);
462 __loc._M_impl->_M_install_cache(__tmp, __i);
463 }
464 return static_cast<const __numpunct_cache<char>&>(*__caches[__i]);
465 }
466
467 #ifdef _GLIBCPP_USE_WCHAR_T
468 template<>
469 const __numpunct_cache<wchar_t>&
470 __use_cache(const locale& __loc)
471 {
472 size_t __i = numpunct<wchar_t>::id._M_id();
473 const locale::facet** __caches = __loc._M_impl->_M_caches;
474 if (!__caches[__i])
475 {
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);
479 }
480 return static_cast<const __numpunct_cache<wchar_t>&>(*__caches[__i]);
481 }
482 #endif
483
484 // Definitions for static const data members of time_base
485 template<>
486 const char*
487 __timepunct<char>::_S_timezones[14] =
488 {
489 "GMT", "HST", "AKST", "PST", "MST", "CST", "EST", "AST", "NST", "CET",
490 "IST", "EET", "CST", "JST"
491 };
492
493 #ifdef _GLIBCPP_USE_WCHAR_T
494 template<>
495 const wchar_t*
496 __timepunct<wchar_t>::_S_timezones[14] =
497 {
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"
500 };
501 #endif
502
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} };
506
507 const char* __num_base::_S_atoms_in = "0123456789eEabcdfABCDF";
508 const char* __num_base::_S_atoms_out ="-+xX0123456789abcdef0123456789ABCDEF";
509
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".
513 void
514 __num_base::_S_format_float(const ios_base& __io, char* __fptr, char __mod)
515 {
516 ios_base::fmtflags __flags = __io.flags();
517 *__fptr++ = '%';
518 // [22.2.2.2.2] Table 60
519 if (__flags & ios_base::showpos)
520 *__fptr++ = '+';
521 if (__flags & ios_base::showpoint)
522 *__fptr++ = '#';
523
524 // As per DR 231: _always_, not only when
525 // __flags & ios_base::fixed || __prec > 0
526 *__fptr++ = '.';
527 *__fptr++ = '*';
528
529 if (__mod)
530 *__fptr++ = __mod;
531 ios_base::fmtflags __fltfield = __flags & ios_base::floatfield;
532 // [22.2.2.2.2] Table 58
533 if (__fltfield == ios_base::fixed)
534 *__fptr++ = 'f';
535 else if (__fltfield == ios_base::scientific)
536 *__fptr++ = (__flags & ios_base::uppercase) ? 'E' : 'e';
537 else
538 *__fptr++ = (__flags & ios_base::uppercase) ? 'G' : 'g';
539 *__fptr = '\0';
540 }
541 } // namespace std
542
This page took 0.065972 seconds and 4 git commands to generate.