locale.cc

00001 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
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 <cctype>
00032 #include <cwctype>     // For towupper, etc.
00033 #include <locale>
00034 #include <bits/atomicity.h>
00035 
00036 namespace __gnu_cxx
00037 {
00038   // Defined in globals.cc.
00039   extern std::locale        c_locale;
00040   extern std::locale::_Impl     c_locale_impl;
00041 } // namespace __gnu_cxx
00042 
00043 namespace std 
00044 {
00045   using namespace __gnu_cxx;
00046 
00047   // Definitions for static const data members of locale.
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   // In the future, GLIBCXX_ABI > 5 should remove all uses of
00058   // _GLIBCPP_ASM_SYMVER in this file, and remove exports of any
00059   // static data members of locale.
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   // Definitions for static const data members of locale::id
00067   _Atomic_word locale::id::_S_highwater;  // init'd to 0 by linker
00068 
00069   // Definitions for static const data members of locale::_Impl
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     // Order must match the decl order in class locale.
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   // This is used to initialize global and classic locales, and
00169   // assumes that the _Impl objects are constructed correctly.
00170   // The lack of a reference increment is intentional.
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         // Get it from the environment.
00186         char* __env = getenv("LC_ALL");
00187         // If LC_ALL is set we are done.
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         // LANG may set a default different from "C".
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         // Scan the categories looking for the first one
00207         // different from LANG.
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         // If one is found, build the complete string of
00230         // the form LC_CTYPE=xxx;LC_NUMERIC=yyy; and so on...
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         // ... otherwise either an additional instance of
00275         // the "C" locale or LANG.
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     // NB: There are complicated, yet more efficient ways to do
00291     // this. Building up locales on a per-category way is tedious, so
00292     // let's do it this way until people complain.
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     // XXX MT
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     // Reference count sanity check: one reference removed for the
00333     // subsition of __other locale, one added by return-by-value. Net
00334     // difference: zero. When the returned locale object's destrutor
00335     // is called, then the reference count is decremented and possibly
00336     // destroyed.
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     // Locking protocol: singleton-called-before-threading-starts
00368     if (!_S_classic)
00369       {
00370     try 
00371       {
00372         // 26 Standard facets, 2 references.
00373         // One reference for _S_classic, one for _S_global
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         // Just call destructor, so that locale_impl_c's memory is
00381         // not deallocated via a call to delete.
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     // NB: May be a C-style "LC_ALL" category; convert.
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   // Definitions for static const data members of time_base
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   // Definitions for static const data members of money_base
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   // _GLIBCPP_RESOLVE_LIB_DEFECTS
00508   // According to the resolution of DR 231, about 22.2.2.2.2, p11,
00509   // "str.precision() is specified in the conversion specification".
00510   void
00511   __num_base::_S_format_float(const ios_base& __io, char* __fptr,
00512                   char __mod, streamsize/* unused post DR 231 */)
00513   {
00514     ios_base::fmtflags __flags = __io.flags();
00515     *__fptr++ = '%';
00516     // [22.2.2.2.2] Table 60
00517     if (__flags & ios_base::showpos)
00518       *__fptr++ = '+';
00519     if (__flags & ios_base::showpoint)
00520       *__fptr++ = '#';
00521 
00522     // As per DR 231: _always_, not only when 
00523     // __flags & ios_base::fixed || __prec > 0
00524     *__fptr++ = '.';
00525     *__fptr++ = '*';
00526 
00527     if (__mod)
00528       *__fptr++ = __mod;
00529     ios_base::fmtflags __fltfield = __flags & ios_base::floatfield;
00530     // [22.2.2.2.2] Table 58
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     // [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 

Generated on Sat Jan 10 15:26:32 2004 for libstdc++-v3 Source by doxygen 1.3.4