# locale.cc

    00001 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
    00002 // Free Software Foundation, Inc.
    00003 //
    00004 // This file is part of the GNU ISO C++ Library.  This library is free
    00005 // software; you can redistribute it and/or modify it under the
    00006 // terms of the GNU General Public License as published by the
    00007 // Free Software Foundation; either version 2, or (at your option)
    00008 // any later version.
    00009
    00010 // This library is distributed in the hope that it will be useful,
    00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00013 // GNU General Public License for more details.
    00014
    00015 // You should have received a copy of the GNU General Public License along
    00016 // with this library; see the file COPYING.  If not, write to the Free
    00017 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    00018 // USA.
    00019
    00020 // As a special exception, you may use this file as part of a free software
    00021 // library without restriction.  Specifically, if other files instantiate
    00022 // templates or use macros or inline functions from this file, or you compile
    00023 // this file and link it with other files to produce an executable, this
    00024 // file does not by itself cause the resulting executable to be covered by
    00025 // the GNU General Public License.  This exception does not however
    00026 // invalidate any other reasons why the executable file might be covered by
    00027 // the GNU General Public License.
    00028
    00029 #include <clocale>
    00030 #include <cstring>
    00031 #include <cassert>
    00032 #include <cctype>
    00033 #include <cwctype>     // For towupper, etc.
    00034 #include <locale>
    00035 #include <bits/atomicity.h>
    00036
    00037 namespace __gnu_cxx
    00038 {
    00039   // Defined in globals.cc.
    00040   extern std::locale        c_locale;
    00041   extern std::locale::_Impl     c_locale_impl;
    00042 } // namespace __gnu_cxx
    00043
    00044 namespace std
    00045 {
    00046   using namespace __gnu_cxx;
    00047
    00048   // Definitions for static const data members of locale.
    00049   const locale::category    locale::none;
    00050   const locale::category    locale::ctype;
    00051   const locale::category    locale::numeric;
    00052   const locale::category    locale::collate;
    00053   const locale::category    locale::time;
    00054   const locale::category    locale::monetary;
    00055   const locale::category    locale::messages;
    00056   const locale::category    locale::all;
    00057
    00058   // In the future, GLIBCXX_ABI > 5 should remove all uses of
    00059   // _GLIBCPP_ASM_SYMVER in this file, and remove exports of any
    00060   // static data members of locale.
    00061   locale::_Impl*        locale::_S_classic;
    00062   locale::_Impl*        locale::_S_global;
    00063   const size_t          locale::_S_categories_size;
    00064   _GLIBCPP_ASM_SYMVER(_ZNSt6locale18_S_categories_sizeE, _ZNSt6locale17_S_num_categoriesE, GLIBCPP_3.2)
    00065   const size_t          locale::_S_extra_categories_size;
    00066
    00067   // Definitions for static const data members of locale::id
    00068   _Atomic_word locale::id::_S_highwater;  // init'd to 0 by linker
    00069
    00070   // Definitions for static const data members of locale::_Impl
    00071   const locale::id* const
    00072   locale::_Impl::_S_id_ctype[] =
    00073   {
    00074     &std::ctype<char>::id,
    00075     &codecvt<char, char, mbstate_t>::id,
    00076 #ifdef _GLIBCPP_USE_WCHAR_T
    00077     &std::ctype<wchar_t>::id,
    00078     &codecvt<wchar_t, char, mbstate_t>::id,
    00079 #endif
    00080     0
    00081   };
    00082
    00083   const locale::id* const
    00084   locale::_Impl::_S_id_numeric[] =
    00085   {
    00086     &num_get<char>::id,
    00087     &num_put<char>::id,
    00088     &numpunct<char>::id,
    00089 #ifdef _GLIBCPP_USE_WCHAR_T
    00090     &num_get<wchar_t>::id,
    00091     &num_put<wchar_t>::id,
    00092     &numpunct<wchar_t>::id,
    00093 #endif
    00094     0
    00095   };
    00096
    00097   const locale::id* const
    00098   locale::_Impl::_S_id_collate[] =
    00099   {
    00100     &std::collate<char>::id,
    00101 #ifdef _GLIBCPP_USE_WCHAR_T
    00102     &std::collate<wchar_t>::id,
    00103 #endif
    00104     0
    00105   };
    00106
    00107   const locale::id* const
    00108   locale::_Impl::_S_id_time[] =
    00109   {
    00110     &__timepunct<char>::id,
    00111     &time_get<char>::id,
    00112     &time_put<char>::id,
    00113 #ifdef _GLIBCPP_USE_WCHAR_T
    00114     &__timepunct<wchar_t>::id,
    00115     &time_get<wchar_t>::id,
    00116     &time_put<wchar_t>::id,
    00117 #endif
    00118     0
    00119   };
    00120
    00121   const locale::id* const
    00122   locale::_Impl::_S_id_monetary[] =
    00123   {
    00124     &money_get<char>::id,
    00125     &money_put<char>::id,
    00126     &moneypunct<char, false>::id,
    00127     &moneypunct<char, true >::id,
    00128 #ifdef _GLIBCPP_USE_WCHAR_T
    00129     &money_get<wchar_t>::id,
    00130     &money_put<wchar_t>::id,
    00131     &moneypunct<wchar_t, false>::id,
    00132     &moneypunct<wchar_t, true >::id,
    00133 #endif
    00134     0
    00135   };
    00136
    00137   const locale::id* const
    00138   locale::_Impl::_S_id_messages[] =
    00139   {
    00140     &std::messages<char>::id,
    00141 #ifdef _GLIBCPP_USE_WCHAR_T
    00142     &std::messages<wchar_t>::id,
    00143 #endif
    00144     0
    00145   };
    00146
    00147   const locale::id* const* const
    00148   locale::_Impl::_S_facet_categories[] =
    00149   {
    00150     // Order must match the decl order in class locale.
    00151     locale::_Impl::_S_id_ctype,
    00152     locale::_Impl::_S_id_numeric,
    00153     locale::_Impl::_S_id_collate,
    00154     locale::_Impl::_S_id_time,
    00155     locale::_Impl::_S_id_monetary,
    00156     locale::_Impl::_S_id_messages,
    00157     0
    00158   };
    00159
    00160   locale::locale() throw()
    00161   {
    00162     _S_initialize();
    00163     (_M_impl = _S_global)->_M_add_reference();
    00164   }
    00165
    00166   locale::locale(const locale& __other) throw()
    00167   { (_M_impl = __other._M_impl)->_M_add_reference(); }
    00168
    00169   // This is used to initialize global and classic locales, and
    00170   // assumes that the _Impl objects are constructed correctly.
    00171   // The lack of a reference increment is intentional.
    00172   locale::locale(_Impl* __ip) throw() : _M_impl(__ip)
    00173   { }
    00174
    00175   locale::locale(const char* __s)
    00176   {
    00177     if (__s)
    00178       {
    00179     _S_initialize();
    00180     if (strcmp(__s, "C") == 0 || strcmp(__s, "POSIX") == 0)
    00181       (_M_impl = _S_classic)->_M_add_reference();
    00182     else if (strcmp(__s, "") != 0)
    00183       _M_impl = new _Impl(__s, 1);
    00184     else
    00185       {
    00186         // Get it from the environment.
    00187         char* __env = getenv("LC_ALL");
    00188         // If LC_ALL is set we are done.
    00189         if (__env && strcmp(__env, "") != 0)
    00190           {
    00191         if (strcmp(__env, "C") == 0 || strcmp(__env, "POSIX") == 0)
    00192           (_M_impl = _S_classic)->_M_add_reference();
    00193         else
    00194           _M_impl = new _Impl(__env, 1);
    00195           }
    00196         else
    00197           {
    00198         char* __res;
    00199         // LANG may set a default different from "C".
    00200         char* __env = getenv("LANG");
    00201         if (!__env || strcmp(__env, "") == 0 || strcmp(__env, "C") == 0
    00202             || strcmp(__env, "POSIX") == 0)
    00203           __res = strdup("C");
    00204         else
    00205           __res = strdup(__env);
    00206
    00207         // Scan the categories looking for the first one
    00208         // different from LANG.
    00209         size_t __i = 0;
    00210         if (strcmp(__res, "C") == 0)
    00211           for (__i = 0;
    00212                __i < _S_categories_size + _S_extra_categories_size;
    00213                ++__i)
    00214             {
    00215               __env = getenv(_S_categories[__i]);
    00216               if (__env && strcmp(__env, "") != 0
    00217               && strcmp(__env, "C") != 0
    00218               && strcmp(__env, "POSIX") != 0)
    00219             break;
    00220             }
    00221         else
    00222           for (__i = 0;
    00223                __i < _S_categories_size + _S_extra_categories_size;
    00224                ++__i)
    00225             {
    00226               __env = getenv(_S_categories[__i]);
    00227               if (__env && strcmp(__env, "") != 0
    00228               && strcmp(__env, __res) != 0)
    00229             break;
    00230             }
    00231
    00232         // If one is found, build the complete string of
    00233         // the form LC_CTYPE=xxx;LC_NUMERIC=yyy; and so on...
    00234         if (__i < _S_categories_size + _S_extra_categories_size)
    00235           {
    00236             string __str;
    00237             for (size_t __j = 0; __j < __i; ++__j)
    00238               {
    00239             __str += _S_categories[__j];
    00240             __str += "=";
    00241             __str += __res;
    00242             __str += ";";
    00243               }
    00244             __str += _S_categories[__i];
    00245             __str += "=";
    00246             __str += __env;
    00247             __str += ";";
    00248             __i++;
    00249             for (; __i < _S_categories_size
    00250                + _S_extra_categories_size; ++__i)
    00251               {
    00252             __env = getenv(_S_categories[__i]);
    00253             if (!__env || strcmp(__env, "") == 0)
    00254               {
    00255                 __str += _S_categories[__i];
    00256                 __str += '=';
    00257                 __str += __res;
    00258                 __str += ';';
    00259               }
    00260             else if (strcmp(__env, "C") == 0
    00261                  || strcmp(__env, "POSIX") == 0)
    00262               {
    00263                 __str += _S_categories[__i];
    00264                 __str += "=C;";
    00265               }
    00266             else
    00267               {
    00268                 __str += _S_categories[__i];
    00269                 __str += "=";
    00270                 __str += __env;
    00271                 __str += ";";
    00272               }
    00273               }
    00274             __str.erase(__str.end() - 1);
    00275             _M_impl = new _Impl(__str.c_str(), 1);
    00276           }
    00277         // ... otherwise either an additional instance of
    00278         // the "C" locale or LANG.
    00279         else if (strcmp(__res, "C") == 0)
    00280           (_M_impl = _S_classic)->_M_add_reference();
    00281         else
    00282           _M_impl = new _Impl(__res, 1);
    00283         free(__res);
    00284           }
    00285       }
    00286       }
    00287     else
    00288       __throw_runtime_error("attempt to create locale from NULL name");
    00289   }
    00290
    00291   locale::locale(const locale& __base, const char* __s, category __cat)
    00292   {
    00293     // NB: There are complicated, yet more efficient ways to do
    00294     // this. Building up locales on a per-category way is tedious, so
    00295     // let's do it this way until people complain.
    00296     locale __add(__s);
    00297     _M_coalesce(__base, __add, __cat);
    00298   }
    00299
    00300   locale::locale(const locale& __base, const locale& __add, category __cat)
    00301   { _M_coalesce(__base, __add, __cat); }
    00302
    00303   locale::~locale() throw()
    00304   { _M_impl->_M_remove_reference(); }
    00305
    00306   bool
    00307   locale::operator==(const locale& __rhs) const throw()
    00308   {
    00309     string __name = this->name();
    00310     return (_M_impl == __rhs._M_impl
    00311         || (__name != "*" && __name == __rhs.name()));
    00312   }
    00313
    00314   const locale&
    00315   locale::operator=(const locale& __other) throw()
    00316   {
    00317     __other._M_impl->_M_add_reference();
    00318     _M_impl->_M_remove_reference();
    00319     _M_impl = __other._M_impl;
    00320     return *this;
    00321   }
    00322
    00323   locale
    00324   locale::global(const locale& __other)
    00325   {
    00326     // XXX MT
    00327     _S_initialize();
    00328     _Impl* __old = _S_global;
    00329     __other._M_impl->_M_add_reference();
    00330     _S_global = __other._M_impl;
    00331     if (_S_global->_M_check_same_name()
    00332     && (strcmp(_S_global->_M_names[0], "*") != 0))
    00333       setlocale(LC_ALL, __other.name().c_str());
    00334
    00335     // Reference count sanity check: one reference removed for the
    00336     // subsition of __other locale, one added by return-by-value. Net
    00337     // difference: zero. When the returned locale object's destrutor
    00338     // is called, then the reference count is decremented and possibly
    00339     // destroyed.
    00340     return locale(__old);
    00341   }
    00342
    00343   string
    00344   locale::name() const
    00345   {
    00346     string __ret;
    00347     if (_M_impl->_M_check_same_name())
    00348       __ret = _M_impl->_M_names[0];
    00349     else
    00350       {
    00351     __ret += _S_categories[0];
    00352     __ret += "=";
    00353     __ret += _M_impl->_M_names[0];
    00354     for (size_t __i = 1;
    00355          __i < _S_categories_size + _S_extra_categories_size;
    00356          ++__i)
    00357       {
    00358         __ret += ";";
    00359         __ret += _S_categories[__i];
    00360         __ret += "=";
    00361         __ret += _M_impl->_M_names[__i];
    00362       }
    00363       }
    00364     return __ret;
    00365   }
    00366
    00367   const locale&
    00368   locale::classic()
    00369   {
    00370     // Locking protocol: singleton-called-before-threading-starts
    00371     if (!_S_classic)
    00372       {
    00373     try
    00374       {
    00375         // 26 Standard facets, 2 references.
    00376         // One reference for _S_classic, one for _S_global
    00377         _S_classic = new (&c_locale_impl) _Impl(0, 2, true);
    00378         _S_global = _S_classic;
    00379         new (&c_locale) locale(_S_classic);
    00380       }
    00381     catch(...)
    00382       {
    00383         // Just call destructor, so that locale_impl_c's memory is
    00384         // not deallocated via a call to delete.
    00385         if (_S_classic)
    00386           _S_classic->~_Impl();
    00387         _S_classic = _S_global = 0;
    00388         __throw_exception_again;
    00389       }
    00390       }
    00391     return c_locale;
    00392   }
    00393
    00394   void
    00395   locale::_M_coalesce(const locale& __base, const locale& __add,
    00396               category __cat)
    00397   {
    00398     __cat = _S_normalize_category(__cat);
    00399     _M_impl = new _Impl(*__base._M_impl, 1);
    00400
    00401     try
    00402       { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
    00403     catch (...)
    00404       {
    00405     _M_impl->_M_remove_reference();
    00406     __throw_exception_again;
    00407       }
    00408   }
    00409
    00410   locale::category
    00411   locale::_S_normalize_category(category __cat)
    00412   {
    00413     int __ret = 0;
    00414     if (__cat == none || (__cat & all) && !(__cat & ~all))
    00415       __ret = __cat;
    00416     else
    00417       {
    00418     // NB: May be a C-style "LC_ALL" category; convert.
    00419     switch (__cat)
    00420       {
    00421       case LC_COLLATE:
    00422         __ret = collate;
    00423         break;
    00424       case LC_CTYPE:
    00425         __ret = ctype;
    00426         break;
    00427       case LC_MONETARY:
    00428         __ret = monetary;
    00429         break;
    00430       case LC_NUMERIC:
    00431         __ret = numeric;
    00432         break;
    00433       case LC_TIME:
    00434         __ret = time;
    00435         break;
    00436 #ifdef _GLIBCPP_HAVE_LC_MESSAGES
    00437       case LC_MESSAGES:
    00438         __ret = messages;
    00439         break;
    00440 #endif
    00441       case LC_ALL:
    00442         __ret = all;
    00443         break;
    00444       default:
    00445         __throw_runtime_error("bad locale category");
    00446       }
    00447       }
    00448     return __ret;
    00449   }
    00450
    00451   __c_locale
    00452   locale::facet::_S_c_locale;
    00453
    00454   locale::facet::
    00455   ~facet() { }
    00456
    00457   locale::facet::
    00458   facet(size_t __refs) throw() : _M_references(__refs ? 1 : 0)
    00459   { }
    00460
    00461   void
    00462   locale::facet::
    00463   _M_add_reference() throw()
    00464   { __atomic_add(&_M_references, 1); }
    00465
    00466   void
    00467   locale::facet::
    00468   _M_remove_reference() throw()
    00469   {
    00470     if (__exchange_and_add(&_M_references, -1) == 1)
    00471       {
    00472         try
    00473       { delete this; }
    00474     catch (...)
    00475       { }
    00476       }
    00477   }
    00478
    00479   locale::id::id()
    00480   { }
    00481
    00482   // Definitions for static const data members of time_base
    00483   template<>
    00484     const char*
    00485     __timepunct<char>::_S_timezones[14] =
    00486     {
    00487       "GMT", "HST", "AKST", "PST", "MST", "CST", "EST", "AST", "NST", "CET",
    00488       "IST", "EET", "CST", "JST"
    00489     };
    00490
    00491 #ifdef _GLIBCPP_USE_WCHAR_T
    00492   template<>
    00493     const wchar_t*
    00494     __timepunct<wchar_t>::_S_timezones[14] =
    00495     {
    00496       L"GMT", L"HST", L"AKST", L"PST", L"MST", L"CST", L"EST", L"AST",
    00497       L"NST", L"CET", L"IST", L"EET", L"CST", L"JST"
    00498     };
    00499 #endif
    00500
    00501   // Definitions for static const data members of money_base
    00502   const money_base::pattern
    00503   money_base::_S_default_pattern =  { {symbol, sign, none, value} };
    00504
    00505   const char __num_base::_S_atoms[] = "0123456789eEabcdfABCDF";
    00506
    00507   bool
    00508   __num_base::_S_format_float(const ios_base& __io, char* __fptr, char __mod,
    00509                   streamsize __prec)
    00510   {
    00511     bool __incl_prec = false;
    00512     ios_base::fmtflags __flags = __io.flags();
    00513     *__fptr++ = '%';
    00514     // [22.2.2.2.2] Table 60
    00515     if (__flags & ios_base::showpos)
    00516       *__fptr++ = '+';
    00517     if (__flags & ios_base::showpoint)
    00518       *__fptr++ = '#';
    00519     // As per [22.2.2.2.2.11]
    00520     if (__flags & ios_base::fixed || __prec > 0)
    00521       {
    00522     *__fptr++ = '.';
    00523     *__fptr++ = '*';
    00524     __incl_prec = true;
    00525       }
    00526     if (__mod)
    00527       *__fptr++ = __mod;
    00528     ios_base::fmtflags __fltfield = __flags & ios_base::floatfield;
    00529     // [22.2.2.2.2] Table 58
    00530     if (__fltfield == ios_base::fixed)
    00531       *__fptr++ = 'f';
    00532     else if (__fltfield == ios_base::scientific)
    00533       *__fptr++ = (__flags & ios_base::uppercase) ? 'E' : 'e';
    00534     else
    00535       *__fptr++ = (__flags & ios_base::uppercase) ? 'G' : 'g';
    00536     *__fptr = '\0';
    00537     return __incl_prec;
    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     // [22.2.2.2.2] Table 60
    00547     if (__flags & ios_base::showpos)
    00548       *__fptr++ = '+';
    00549     if (__flags & ios_base::showbase)
    00550       *__fptr++ = '#';
    00551     *__fptr++ = 'l';
    00552
    00553     // For long long types.
    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 } // namespace std
    00567