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 string __res;
00198
00199 char* __env = getenv("LANG");
00200 if (!__env || strcmp(__env, "") == 0 || strcmp(__env, "C") == 0
00201 || strcmp(__env, "POSIX") == 0)
00202 __res = "C";
00203 else
00204 __res = __env;
00205
00206
00207
00208 size_t __i = 0;
00209 if (__res == "C")
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 && __res != __env)
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 (__res == "C")
00277 (_M_impl = _S_classic)->_M_add_reference();
00278 else
00279 _M_impl = new _Impl(__res.c_str(), 1);
00280 }
00281 }
00282 }
00283 else
00284 __throw_runtime_error("attempt to create locale from NULL name");
00285 }
00286
00287 locale::locale(const locale& __base, const char* __s, category __cat)
00288 {
00289
00290
00291
00292 locale __add(__s);
00293 _M_coalesce(__base, __add, __cat);
00294 }
00295
00296 locale::locale(const locale& __base, const locale& __add, category __cat)
00297 { _M_coalesce(__base, __add, __cat); }
00298
00299 locale::~locale() throw()
00300 { _M_impl->_M_remove_reference(); }
00301
00302 bool
00303 locale::operator==(const locale& __rhs) const throw()
00304 {
00305 bool __ret = true;
00306 if (_M_impl == __rhs._M_impl)
00307 ;
00308 else if (!std::strcmp(_M_impl->_M_names[0], "*"))
00309 __ret = false;
00310 else
00311 for (size_t __i = 0; __ret && __i < _S_categories_size; ++__i)
00312 __ret = !std::strcmp(_M_impl->_M_names[__i],
00313 __rhs._M_impl->_M_names[__i]);
00314 return __ret;
00315 }
00316
00317 const locale&
00318 locale::operator=(const locale& __other) throw()
00319 {
00320 __other._M_impl->_M_add_reference();
00321 _M_impl->_M_remove_reference();
00322 _M_impl = __other._M_impl;
00323 return *this;
00324 }
00325
00326 locale
00327 locale::global(const locale& __other)
00328 {
00329
00330 _S_initialize();
00331 _Impl* __old = _S_global;
00332 __other._M_impl->_M_add_reference();
00333 _S_global = __other._M_impl;
00334 const string __other_name = __other.name();
00335 if (__other_name != "*")
00336 setlocale(LC_ALL, __other_name.c_str());
00337
00338
00339
00340
00341
00342
00343 return locale(__old);
00344 }
00345
00346 string
00347 locale::name() const
00348 {
00349 string __ret;
00350 if (_M_impl->_M_check_same_name())
00351 __ret = _M_impl->_M_names[0];
00352 else
00353 {
00354 __ret += _S_categories[0];
00355 __ret += '=';
00356 __ret += _M_impl->_M_names[0];
00357 for (size_t __i = 1;
00358 __i < _S_categories_size + _S_extra_categories_size;
00359 ++__i)
00360 {
00361 __ret += ';';
00362 __ret += _S_categories[__i];
00363 __ret += '=';
00364 __ret += _M_impl->_M_names[__i];
00365 }
00366 }
00367 return __ret;
00368 }
00369
00370 const locale&
00371 locale::classic()
00372 {
00373
00374 if (!_S_classic)
00375 {
00376 try
00377 {
00378
00379
00380 _S_classic = new (&c_locale_impl) _Impl(0, 2, true);
00381 _S_global = _S_classic;
00382 new (&c_locale) locale(_S_classic);
00383 }
00384 catch(...)
00385 {
00386
00387
00388 if (_S_classic)
00389 _S_classic->~_Impl();
00390 _S_classic = _S_global = 0;
00391 __throw_exception_again;
00392 }
00393 }
00394 return c_locale;
00395 }
00396
00397 void
00398 locale::_M_coalesce(const locale& __base, const locale& __add,
00399 category __cat)
00400 {
00401 __cat = _S_normalize_category(__cat);
00402 _M_impl = new _Impl(*__base._M_impl, 1);
00403
00404 try
00405 { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
00406 catch (...)
00407 {
00408 _M_impl->_M_remove_reference();
00409 __throw_exception_again;
00410 }
00411 }
00412
00413 locale::category
00414 locale::_S_normalize_category(category __cat)
00415 {
00416 int __ret = 0;
00417 if (__cat == none || (__cat & all) && !(__cat & ~all))
00418 __ret = __cat;
00419 else
00420 {
00421
00422 switch (__cat)
00423 {
00424 case LC_COLLATE:
00425 __ret = collate;
00426 break;
00427 case LC_CTYPE:
00428 __ret = ctype;
00429 break;
00430 case LC_MONETARY:
00431 __ret = monetary;
00432 break;
00433 case LC_NUMERIC:
00434 __ret = numeric;
00435 break;
00436 case LC_TIME:
00437 __ret = time;
00438 break;
00439 #ifdef _GLIBCPP_HAVE_LC_MESSAGES
00440 case LC_MESSAGES:
00441 __ret = messages;
00442 break;
00443 #endif
00444 case LC_ALL:
00445 __ret = all;
00446 break;
00447 default:
00448 __throw_runtime_error("bad locale category");
00449 }
00450 }
00451 return __ret;
00452 }
00453
00454 __c_locale
00455 locale::facet::_S_c_locale;
00456
00457 char locale::facet::_S_c_name[2];
00458
00459 locale::facet::
00460 ~facet() { }
00461
00462 locale::facet::
00463 facet(size_t __refs) throw() : _M_references(__refs ? 1 : 0)
00464 { }
00465
00466 void
00467 locale::facet::
00468 _M_add_reference() throw()
00469 { __atomic_add(&_M_references, 1); }
00470
00471 void
00472 locale::facet::
00473 _M_remove_reference() throw()
00474 {
00475 if (__exchange_and_add(&_M_references, -1) == 1)
00476 {
00477 try
00478 { delete this; }
00479 catch (...)
00480 { }
00481 }
00482 }
00483
00484 locale::id::id()
00485 { }
00486
00487
00488 template<>
00489 const char*
00490 __timepunct<char>::_S_timezones[14] =
00491 {
00492 "GMT", "HST", "AKST", "PST", "MST", "CST", "EST", "AST", "NST", "CET",
00493 "IST", "EET", "CST", "JST"
00494 };
00495
00496 #ifdef _GLIBCPP_USE_WCHAR_T
00497 template<>
00498 const wchar_t*
00499 __timepunct<wchar_t>::_S_timezones[14] =
00500 {
00501 L"GMT", L"HST", L"AKST", L"PST", L"MST", L"CST", L"EST", L"AST",
00502 L"NST", L"CET", L"IST", L"EET", L"CST", L"JST"
00503 };
00504 #endif
00505
00506
00507 const money_base::pattern
00508 money_base::_S_default_pattern = { {symbol, sign, none, value} };
00509
00510 const char* __num_base::_S_atoms_in = "0123456789eEabcdfABCDF";
00511 const char* __num_base::_S_atoms_out ="-+xX0123456789abcdef0123456789ABCDEF";
00512
00513
00514
00515
00516 void
00517 __num_base::_S_format_float(const ios_base& __io, char* __fptr,
00518 char __mod, streamsize)
00519 {
00520 ios_base::fmtflags __flags = __io.flags();
00521 *__fptr++ = '%';
00522
00523 if (__flags & ios_base::showpos)
00524 *__fptr++ = '+';
00525 if (__flags & ios_base::showpoint)
00526 *__fptr++ = '#';
00527
00528
00529
00530 *__fptr++ = '.';
00531 *__fptr++ = '*';
00532
00533 if (__mod)
00534 *__fptr++ = __mod;
00535 ios_base::fmtflags __fltfield = __flags & ios_base::floatfield;
00536
00537 if (__fltfield == ios_base::fixed)
00538 *__fptr++ = 'f';
00539 else if (__fltfield == ios_base::scientific)
00540 *__fptr++ = (__flags & ios_base::uppercase) ? 'E' : 'e';
00541 else
00542 *__fptr++ = (__flags & ios_base::uppercase) ? 'G' : 'g';
00543 *__fptr = '\0';
00544 }
00545
00546 void
00547 __num_base::_S_format_int(const ios_base& __io, char* __fptr, char __mod,
00548 char __modl)
00549 {
00550 ios_base::fmtflags __flags = __io.flags();
00551 *__fptr++ = '%';
00552
00553 if (__flags & ios_base::showpos)
00554 *__fptr++ = '+';
00555 if (__flags & ios_base::showbase)
00556 *__fptr++ = '#';
00557 *__fptr++ = 'l';
00558
00559
00560 if (__modl)
00561 *__fptr++ = __modl;
00562
00563 ios_base::fmtflags __bsefield = __flags & ios_base::basefield;
00564 if (__bsefield == ios_base::hex)
00565 *__fptr++ = (__flags & ios_base::uppercase) ? 'X' : 'x';
00566 else if (__bsefield == ios_base::oct)
00567 *__fptr++ = 'o';
00568 else
00569 *__fptr++ = __mod;
00570 *__fptr = '\0';
00571 }
00572 }
00573