00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <clocale>
00030 #include <cstring>
00031 #include <cctype>
00032 #include <cwctype>
00033 #include <locale>
00034 #include <bits/atomicity.h>
00035
00036 namespace __gnu_cxx
00037 {
00038
00039 extern std::locale c_locale;
00040 extern std::locale::_Impl c_locale_impl;
00041 }
00042
00043 namespace std
00044 {
00045 using namespace __gnu_cxx;
00046
00047
00048 const locale::category locale::none;
00049 const locale::category locale::ctype;
00050 const locale::category locale::numeric;
00051 const locale::category locale::collate;
00052 const locale::category locale::time;
00053 const locale::category locale::monetary;
00054 const locale::category locale::messages;
00055 const locale::category locale::all;
00056
00057
00058
00059
00060 locale::_Impl* locale::_S_classic;
00061 locale::_Impl* locale::_S_global;
00062 const size_t locale::_S_categories_size;
00063 _GLIBCPP_ASM_SYMVER(_ZNSt6locale18_S_categories_sizeE, _ZNSt6locale17_S_num_categoriesE, GLIBCPP_3.2)
00064 const size_t locale::_S_extra_categories_size;
00065
00066
00067 _Atomic_word locale::id::_S_highwater;
00068
00069
00070 const locale::id* const
00071 locale::_Impl::_S_id_ctype[] =
00072 {
00073 &std::ctype<char>::id,
00074 &codecvt<char, char, mbstate_t>::id,
00075 #ifdef _GLIBCPP_USE_WCHAR_T
00076 &std::ctype<wchar_t>::id,
00077 &codecvt<wchar_t, char, mbstate_t>::id,
00078 #endif
00079 0
00080 };
00081
00082 const locale::id* const
00083 locale::_Impl::_S_id_numeric[] =
00084 {
00085 &num_get<char>::id,
00086 &num_put<char>::id,
00087 &numpunct<char>::id,
00088 #ifdef _GLIBCPP_USE_WCHAR_T
00089 &num_get<wchar_t>::id,
00090 &num_put<wchar_t>::id,
00091 &numpunct<wchar_t>::id,
00092 #endif
00093 0
00094 };
00095
00096 const locale::id* const
00097 locale::_Impl::_S_id_collate[] =
00098 {
00099 &std::collate<char>::id,
00100 #ifdef _GLIBCPP_USE_WCHAR_T
00101 &std::collate<wchar_t>::id,
00102 #endif
00103 0
00104 };
00105
00106 const locale::id* const
00107 locale::_Impl::_S_id_time[] =
00108 {
00109 &__timepunct<char>::id,
00110 &time_get<char>::id,
00111 &time_put<char>::id,
00112 #ifdef _GLIBCPP_USE_WCHAR_T
00113 &__timepunct<wchar_t>::id,
00114 &time_get<wchar_t>::id,
00115 &time_put<wchar_t>::id,
00116 #endif
00117 0
00118 };
00119
00120 const locale::id* const
00121 locale::_Impl::_S_id_monetary[] =
00122 {
00123 &money_get<char>::id,
00124 &money_put<char>::id,
00125 &moneypunct<char, false>::id,
00126 &moneypunct<char, true >::id,
00127 #ifdef _GLIBCPP_USE_WCHAR_T
00128 &money_get<wchar_t>::id,
00129 &money_put<wchar_t>::id,
00130 &moneypunct<wchar_t, false>::id,
00131 &moneypunct<wchar_t, true >::id,
00132 #endif
00133 0
00134 };
00135
00136 const locale::id* const
00137 locale::_Impl::_S_id_messages[] =
00138 {
00139 &std::messages<char>::id,
00140 #ifdef _GLIBCPP_USE_WCHAR_T
00141 &std::messages<wchar_t>::id,
00142 #endif
00143 0
00144 };
00145
00146 const locale::id* const* const
00147 locale::_Impl::_S_facet_categories[] =
00148 {
00149
00150 locale::_Impl::_S_id_ctype,
00151 locale::_Impl::_S_id_numeric,
00152 locale::_Impl::_S_id_collate,
00153 locale::_Impl::_S_id_time,
00154 locale::_Impl::_S_id_monetary,
00155 locale::_Impl::_S_id_messages,
00156 0
00157 };
00158
00159 locale::locale() throw()
00160 {
00161 _S_initialize();
00162 (_M_impl = _S_global)->_M_add_reference();
00163 }
00164
00165 locale::locale(const locale& __other) throw()
00166 { (_M_impl = __other._M_impl)->_M_add_reference(); }
00167
00168
00169
00170
00171 locale::locale(_Impl* __ip) throw() : _M_impl(__ip)
00172 { }
00173
00174 locale::locale(const char* __s)
00175 {
00176 if (__s)
00177 {
00178 _S_initialize();
00179 if (strcmp(__s, "C") == 0 || strcmp(__s, "POSIX") == 0)
00180 (_M_impl = _S_classic)->_M_add_reference();
00181 else if (strcmp(__s, "") != 0)
00182 _M_impl = new _Impl(__s, 1);
00183 else
00184 {
00185
00186 char* __env = getenv("LC_ALL");
00187
00188 if (__env && strcmp(__env, "") != 0)
00189 {
00190 if (strcmp(__env, "C") == 0 || strcmp(__env, "POSIX") == 0)
00191 (_M_impl = _S_classic)->_M_add_reference();
00192 else
00193 _M_impl = new _Impl(__env, 1);
00194 }
00195 else
00196 {
00197 char* __res;
00198
00199 char* __env = getenv("LANG");
00200 if (!__env || strcmp(__env, "") == 0 || strcmp(__env, "C") == 0
00201 || strcmp(__env, "POSIX") == 0)
00202 __res = strdup("C");
00203 else
00204 __res = strdup(__env);
00205
00206
00207
00208 size_t __i = 0;
00209 if (strcmp(__res, "C") == 0)
00210 for (; __i < _S_categories_size
00211 + _S_extra_categories_size; ++__i)
00212 {
00213 __env = getenv(_S_categories[__i]);
00214 if (__env && strcmp(__env, "") != 0
00215 && strcmp(__env, "C") != 0
00216 && strcmp(__env, "POSIX") != 0)
00217 break;
00218 }
00219 else
00220 for (; __i < _S_categories_size
00221 + _S_extra_categories_size; ++__i)
00222 {
00223 __env = getenv(_S_categories[__i]);
00224 if (__env && strcmp(__env, "") != 0
00225 && strcmp(__env, __res) != 0)
00226 break;
00227 }
00228
00229
00230
00231 if (__i < _S_categories_size + _S_extra_categories_size)
00232 {
00233 string __str;
00234 for (size_t __j = 0; __j < __i; ++__j)
00235 {
00236 __str += _S_categories[__j];
00237 __str += '=';
00238 __str += __res;
00239 __str += ';';
00240 }
00241 __str += _S_categories[__i];
00242 __str += '=';
00243 __str += __env;
00244 __str += ';';
00245 __i++;
00246 for (; __i < _S_categories_size
00247 + _S_extra_categories_size; ++__i)
00248 {
00249 __env = getenv(_S_categories[__i]);
00250 if (!__env || strcmp(__env, "") == 0)
00251 {
00252 __str += _S_categories[__i];
00253 __str += '=';
00254 __str += __res;
00255 __str += ';';
00256 }
00257 else if (strcmp(__env, "C") == 0
00258 || strcmp(__env, "POSIX") == 0)
00259 {
00260 __str += _S_categories[__i];
00261 __str += "=C;";
00262 }
00263 else
00264 {
00265 __str += _S_categories[__i];
00266 __str += '=';
00267 __str += __env;
00268 __str += ';';
00269 }
00270 }
00271 __str.erase(__str.end() - 1);
00272 _M_impl = new _Impl(__str.c_str(), 1);
00273 }
00274
00275
00276 else if (strcmp(__res, "C") == 0)
00277 (_M_impl = _S_classic)->_M_add_reference();
00278 else
00279 _M_impl = new _Impl(__res, 1);
00280 free(__res);
00281 }
00282 }
00283 }
00284 else
00285 __throw_runtime_error("attempt to create locale from NULL name");
00286 }
00287
00288 locale::locale(const locale& __base, const char* __s, category __cat)
00289 {
00290
00291
00292
00293 locale __add(__s);
00294 _M_coalesce(__base, __add, __cat);
00295 }
00296
00297 locale::locale(const locale& __base, const locale& __add, category __cat)
00298 { _M_coalesce(__base, __add, __cat); }
00299
00300 locale::~locale() throw()
00301 { _M_impl->_M_remove_reference(); }
00302
00303 bool
00304 locale::operator==(const locale& __rhs) const throw()
00305 {
00306 string __name = this->name();
00307 return (_M_impl == __rhs._M_impl
00308 || (__name != "*" && __name == __rhs.name()));
00309 }
00310
00311 const locale&
00312 locale::operator=(const locale& __other) throw()
00313 {
00314 __other._M_impl->_M_add_reference();
00315 _M_impl->_M_remove_reference();
00316 _M_impl = __other._M_impl;
00317 return *this;
00318 }
00319
00320 locale
00321 locale::global(const locale& __other)
00322 {
00323
00324 _S_initialize();
00325 _Impl* __old = _S_global;
00326 __other._M_impl->_M_add_reference();
00327 _S_global = __other._M_impl;
00328 if (_S_global->_M_check_same_name()
00329 && (strcmp(_S_global->_M_names[0], "*") != 0))
00330 setlocale(LC_ALL, __other.name().c_str());
00331
00332
00333
00334
00335
00336
00337 return locale(__old);
00338 }
00339
00340 string
00341 locale::name() const
00342 {
00343 string __ret;
00344 if (_M_impl->_M_check_same_name())
00345 __ret = _M_impl->_M_names[0];
00346 else
00347 {
00348 __ret += _S_categories[0];
00349 __ret += '=';
00350 __ret += _M_impl->_M_names[0];
00351 for (size_t __i = 1;
00352 __i < _S_categories_size + _S_extra_categories_size;
00353 ++__i)
00354 {
00355 __ret += ';';
00356 __ret += _S_categories[__i];
00357 __ret += '=';
00358 __ret += _M_impl->_M_names[__i];
00359 }
00360 }
00361 return __ret;
00362 }
00363
00364 const locale&
00365 locale::classic()
00366 {
00367
00368 if (!_S_classic)
00369 {
00370 try
00371 {
00372
00373
00374 _S_classic = new (&c_locale_impl) _Impl(0, 2, true);
00375 _S_global = _S_classic;
00376 new (&c_locale) locale(_S_classic);
00377 }
00378 catch(...)
00379 {
00380
00381
00382 if (_S_classic)
00383 _S_classic->~_Impl();
00384 _S_classic = _S_global = 0;
00385 __throw_exception_again;
00386 }
00387 }
00388 return c_locale;
00389 }
00390
00391 void
00392 locale::_M_coalesce(const locale& __base, const locale& __add,
00393 category __cat)
00394 {
00395 __cat = _S_normalize_category(__cat);
00396 _M_impl = new _Impl(*__base._M_impl, 1);
00397
00398 try
00399 { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
00400 catch (...)
00401 {
00402 _M_impl->_M_remove_reference();
00403 __throw_exception_again;
00404 }
00405 }
00406
00407 locale::category
00408 locale::_S_normalize_category(category __cat)
00409 {
00410 int __ret = 0;
00411 if (__cat == none || (__cat & all) && !(__cat & ~all))
00412 __ret = __cat;
00413 else
00414 {
00415
00416 switch (__cat)
00417 {
00418 case LC_COLLATE:
00419 __ret = collate;
00420 break;
00421 case LC_CTYPE:
00422 __ret = ctype;
00423 break;
00424 case LC_MONETARY:
00425 __ret = monetary;
00426 break;
00427 case LC_NUMERIC:
00428 __ret = numeric;
00429 break;
00430 case LC_TIME:
00431 __ret = time;
00432 break;
00433 #ifdef _GLIBCPP_HAVE_LC_MESSAGES
00434 case LC_MESSAGES:
00435 __ret = messages;
00436 break;
00437 #endif
00438 case LC_ALL:
00439 __ret = all;
00440 break;
00441 default:
00442 __throw_runtime_error("bad locale category");
00443 }
00444 }
00445 return __ret;
00446 }
00447
00448 __c_locale
00449 locale::facet::_S_c_locale;
00450
00451 char locale::facet::_S_c_name[2];
00452
00453 locale::facet::
00454 ~facet() { }
00455
00456 locale::facet::
00457 facet(size_t __refs) throw() : _M_references(__refs ? 1 : 0)
00458 { }
00459
00460 void
00461 locale::facet::
00462 _M_add_reference() throw()
00463 { __atomic_add(&_M_references, 1); }
00464
00465 void
00466 locale::facet::
00467 _M_remove_reference() throw()
00468 {
00469 if (__exchange_and_add(&_M_references, -1) == 1)
00470 {
00471 try
00472 { delete this; }
00473 catch (...)
00474 { }
00475 }
00476 }
00477
00478 locale::id::id()
00479 { }
00480
00481
00482 template<>
00483 const char*
00484 __timepunct<char>::_S_timezones[14] =
00485 {
00486 "GMT", "HST", "AKST", "PST", "MST", "CST", "EST", "AST", "NST", "CET",
00487 "IST", "EET", "CST", "JST"
00488 };
00489
00490 #ifdef _GLIBCPP_USE_WCHAR_T
00491 template<>
00492 const wchar_t*
00493 __timepunct<wchar_t>::_S_timezones[14] =
00494 {
00495 L"GMT", L"HST", L"AKST", L"PST", L"MST", L"CST", L"EST", L"AST",
00496 L"NST", L"CET", L"IST", L"EET", L"CST", L"JST"
00497 };
00498 #endif
00499
00500
00501 const money_base::pattern
00502 money_base::_S_default_pattern = { {symbol, sign, none, value} };
00503
00504 const char* __num_base::_S_atoms_in = "0123456789eEabcdfABCDF";
00505 const char* __num_base::_S_atoms_out ="-+xX0123456789abcdef0123456789ABCDEF";
00506
00507
00508
00509
00510 void
00511 __num_base::_S_format_float(const ios_base& __io, char* __fptr,
00512 char __mod, streamsize)
00513 {
00514 ios_base::fmtflags __flags = __io.flags();
00515 *__fptr++ = '%';
00516
00517 if (__flags & ios_base::showpos)
00518 *__fptr++ = '+';
00519 if (__flags & ios_base::showpoint)
00520 *__fptr++ = '#';
00521
00522
00523
00524 *__fptr++ = '.';
00525 *__fptr++ = '*';
00526
00527 if (__mod)
00528 *__fptr++ = __mod;
00529 ios_base::fmtflags __fltfield = __flags & ios_base::floatfield;
00530
00531 if (__fltfield == ios_base::fixed)
00532 *__fptr++ = 'f';
00533 else if (__fltfield == ios_base::scientific)
00534 *__fptr++ = (__flags & ios_base::uppercase) ? 'E' : 'e';
00535 else
00536 *__fptr++ = (__flags & ios_base::uppercase) ? 'G' : 'g';
00537 *__fptr = '\0';
00538 }
00539
00540 void
00541 __num_base::_S_format_int(const ios_base& __io, char* __fptr, char __mod,
00542 char __modl)
00543 {
00544 ios_base::fmtflags __flags = __io.flags();
00545 *__fptr++ = '%';
00546
00547 if (__flags & ios_base::showpos)
00548 *__fptr++ = '+';
00549 if (__flags & ios_base::showbase)
00550 *__fptr++ = '#';
00551 *__fptr++ = 'l';
00552
00553
00554 if (__modl)
00555 *__fptr++ = __modl;
00556
00557 ios_base::fmtflags __bsefield = __flags & ios_base::basefield;
00558 if (__bsefield == ios_base::hex)
00559 *__fptr++ = (__flags & ios_base::uppercase) ? 'X' : 'x';
00560 else if (__bsefield == ios_base::oct)
00561 *__fptr++ = 'o';
00562 else
00563 *__fptr++ = __mod;
00564 *__fptr = '\0';
00565 }
00566 }
00567