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         string __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 = "C";
00203         else 
00204           __res = __env;
00205         
00206         // Scan the categories looking for the first one
00207         // different from LANG.
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         // 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 (__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     // NB: There are complicated, yet more efficient ways to do
00290     // this. Building up locales on a per-category way is tedious, so
00291     // let's do it this way until people complain.
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     string __name = this->name();
00306     return (_M_impl == __rhs._M_impl 
00307         || (__name != "*" && __name == __rhs.name()));
00308   }
00309 
00310   const locale&
00311   locale::operator=(const locale& __other) throw()
00312   {
00313     __other._M_impl->_M_add_reference();
00314     _M_impl->_M_remove_reference();
00315     _M_impl = __other._M_impl;
00316     return *this;
00317   }
00318 
00319   locale
00320   locale::global(const locale& __other)
00321   {
00322     // XXX MT
00323     _S_initialize();
00324     _Impl* __old = _S_global;
00325     __other._M_impl->_M_add_reference();
00326     _S_global = __other._M_impl; 
00327     if (_S_global->_M_check_same_name() 
00328     && (strcmp(_S_global->_M_names[0], "*") != 0))
00329       setlocale(LC_ALL, __other.name().c_str());
00330 
00331     // Reference count sanity check: one reference removed for the
00332     // subsition of __other locale, one added by return-by-value. Net
00333     // difference: zero. When the returned locale object's destrutor
00334     // is called, then the reference count is decremented and possibly
00335     // destroyed.
00336     return locale(__old);
00337   }
00338 
00339   string
00340   locale::name() const
00341   {
00342     string __ret;
00343     if (_M_impl->_M_check_same_name())
00344       __ret = _M_impl->_M_names[0];
00345     else
00346       {
00347     __ret += _S_categories[0];
00348     __ret += '=';
00349     __ret += _M_impl->_M_names[0]; 
00350     for (size_t __i = 1; 
00351          __i < _S_categories_size + _S_extra_categories_size; 
00352          ++__i)
00353       {
00354         __ret += ';';
00355         __ret += _S_categories[__i];
00356         __ret += '=';
00357         __ret += _M_impl->_M_names[__i];
00358       }
00359       }
00360     return __ret;
00361   }
00362 
00363   const locale&
00364   locale::classic()
00365   {
00366     // Locking protocol: singleton-called-before-threading-starts
00367     if (!_S_classic)
00368       {
00369     try 
00370       {
00371         // 26 Standard facets, 2 references.
00372         // One reference for _S_classic, one for _S_global
00373         _S_classic = new (&c_locale_impl) _Impl(0, 2, true);
00374         _S_global = _S_classic;         
00375         new (&c_locale) locale(_S_classic);
00376       }
00377     catch(...) 
00378       {
00379         // Just call destructor, so that locale_impl_c's memory is
00380         // not deallocated via a call to delete.
00381         if (_S_classic)
00382           _S_classic->~_Impl();
00383         _S_classic = _S_global = 0;
00384         __throw_exception_again;
00385       }
00386       }
00387     return c_locale;
00388   }
00389 
00390   void
00391   locale::_M_coalesce(const locale& __base, const locale& __add, 
00392               category __cat)
00393   {
00394     __cat = _S_normalize_category(__cat);  
00395     _M_impl = new _Impl(*__base._M_impl, 1);  
00396 
00397     try 
00398       { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
00399     catch (...) 
00400       { 
00401     _M_impl->_M_remove_reference(); 
00402     __throw_exception_again;
00403       }
00404   }
00405 
00406   locale::category
00407   locale::_S_normalize_category(category __cat) 
00408   {
00409     int __ret = 0;
00410     if (__cat == none || (__cat & all) && !(__cat & ~all))
00411       __ret = __cat;
00412     else
00413       {
00414     // NB: May be a C-style "LC_ALL" category; convert.
00415     switch (__cat)
00416       {
00417       case LC_COLLATE:  
00418         __ret = collate; 
00419         break;
00420       case LC_CTYPE:    
00421         __ret = ctype;
00422         break;
00423       case LC_MONETARY: 
00424         __ret = monetary;
00425         break;
00426       case LC_NUMERIC:  
00427         __ret = numeric;
00428         break;
00429       case LC_TIME:     
00430         __ret = time; 
00431         break;
00432 #ifdef _GLIBCPP_HAVE_LC_MESSAGES
00433       case LC_MESSAGES: 
00434         __ret = messages;
00435         break;
00436 #endif  
00437       case LC_ALL:      
00438         __ret = all;
00439         break;
00440       default:
00441         __throw_runtime_error("bad locale category");
00442       }
00443       }
00444     return __ret;
00445   }
00446 
00447   __c_locale
00448   locale::facet::_S_c_locale;
00449   
00450   char locale::facet::_S_c_name[2];
00451 
00452   locale::facet::
00453   ~facet() { }
00454 
00455   locale::facet::
00456   facet(size_t __refs) throw() : _M_references(__refs ? 1 : 0) 
00457   { }
00458 
00459   void  
00460   locale::facet::
00461   _M_add_reference() throw()
00462   { __atomic_add(&_M_references, 1); }
00463 
00464   void  
00465   locale::facet::
00466   _M_remove_reference() throw()
00467   {
00468     if (__exchange_and_add(&_M_references, -1) == 1)
00469       {
00470         try 
00471       { delete this; }  
00472     catch (...) 
00473       { }
00474       }
00475   }
00476   
00477   locale::id::id() 
00478   { }
00479 
00480   // Definitions for static const data members of time_base
00481   template<> 
00482     const char*
00483     __timepunct<char>::_S_timezones[14] =
00484     { 
00485       "GMT", "HST", "AKST", "PST", "MST", "CST", "EST", "AST", "NST", "CET", 
00486       "IST", "EET", "CST", "JST"  
00487     };
00488  
00489 #ifdef _GLIBCPP_USE_WCHAR_T
00490   template<> 
00491     const wchar_t*
00492     __timepunct<wchar_t>::_S_timezones[14] =
00493     { 
00494       L"GMT", L"HST", L"AKST", L"PST", L"MST", L"CST", L"EST", L"AST", 
00495       L"NST", L"CET", L"IST", L"EET", L"CST", L"JST"  
00496     };
00497 #endif
00498 
00499   // Definitions for static const data members of money_base
00500   const money_base::pattern 
00501   money_base::_S_default_pattern =  { {symbol, sign, none, value} };
00502 
00503   const char* __num_base::_S_atoms_in = "0123456789eEabcdfABCDF";
00504   const char* __num_base::_S_atoms_out ="-+xX0123456789abcdef0123456789ABCDEF";
00505 
00506   // _GLIBCPP_RESOLVE_LIB_DEFECTS
00507   // According to the resolution of DR 231, about 22.2.2.2.2, p11,
00508   // "str.precision() is specified in the conversion specification".
00509   void
00510   __num_base::_S_format_float(const ios_base& __io, char* __fptr,
00511                   char __mod, streamsize/* unused post DR 231 */)
00512   {
00513     ios_base::fmtflags __flags = __io.flags();
00514     *__fptr++ = '%';
00515     // [22.2.2.2.2] Table 60
00516     if (__flags & ios_base::showpos)
00517       *__fptr++ = '+';
00518     if (__flags & ios_base::showpoint)
00519       *__fptr++ = '#';
00520 
00521     // As per DR 231: _always_, not only when 
00522     // __flags & ios_base::fixed || __prec > 0
00523     *__fptr++ = '.';
00524     *__fptr++ = '*';
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   }
00538   
00539   void
00540   __num_base::_S_format_int(const ios_base& __io, char* __fptr, char __mod, 
00541                 char __modl)
00542   {
00543     ios_base::fmtflags __flags = __io.flags();
00544     *__fptr++ = '%';
00545     // [22.2.2.2.2] Table 60
00546     if (__flags & ios_base::showpos)
00547       *__fptr++ = '+';
00548     if (__flags & ios_base::showbase)
00549       *__fptr++ = '#';
00550     *__fptr++ = 'l';
00551 
00552     // For long long types.
00553     if (__modl)
00554       *__fptr++ = __modl;
00555 
00556     ios_base::fmtflags __bsefield = __flags & ios_base::basefield;
00557     if (__bsefield == ios_base::hex)
00558       *__fptr++ = (__flags & ios_base::uppercase) ? 'X' : 'x';
00559     else if (__bsefield == ios_base::oct)
00560       *__fptr++ = 'o';
00561     else
00562       *__fptr++ = __mod;
00563     *__fptr = '\0';
00564   }
00565 } // namespace std
00566 

Generated on Thu Feb 10 23:22:56 2005 for libstdc++-v3 Source by  doxygen 1.4.0