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 <limits>
00035 #include <exception>
00036 #include <locale>
00037 #include <istream>
00038 #include <ostream>
00039 #include <bits/atomicity.h>
00040 
00041 namespace std 
00042 {
00043   // Defined in globals.cc.
00044   extern locale         c_locale;
00045   extern locale::_Impl      c_locale_impl;
00046   extern locale::facet**    facet_vec;
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   locale::_Impl*        locale::_S_classic;
00059   locale::_Impl*        locale::_S_global; 
00060   const size_t          locale::_S_num_categories;
00061 
00062   // Definitions for locale::id of standard facets that are specialized.
00063   locale::id ctype<char>::id;
00064   locale::id codecvt<char, char, mbstate_t>::id;
00065 
00066 #ifdef _GLIBCPP_USE_WCHAR_T  
00067   locale::id ctype<wchar_t>::id;
00068   locale::id codecvt<wchar_t, char, mbstate_t>::id;
00069 #endif
00070 
00071   // Definitions for static const data members of locale::id
00072   _Atomic_word locale::id::_S_highwater;  // init'd to 0 by linker
00073 
00074   // Definitions for static const data members of locale::_Impl
00075   const locale::id* const
00076   locale::_Impl::_S_id_ctype[] =
00077   {
00078     &std::ctype<char>::id, 
00079     &codecvt<char, char, mbstate_t>::id,
00080 #ifdef _GLIBCPP_USE_WCHAR_T
00081     &std::ctype<wchar_t>::id,
00082     &codecvt<wchar_t, char, mbstate_t>::id,
00083 #endif
00084     0
00085   };
00086 
00087   const locale::id* const
00088   locale::_Impl::_S_id_numeric[] =
00089   {
00090     &num_get<char>::id,  
00091     &num_put<char>::id,  
00092     &numpunct<char>::id, 
00093 #ifdef _GLIBCPP_USE_WCHAR_T
00094     &num_get<wchar_t>::id,
00095     &num_put<wchar_t>::id,
00096     &numpunct<wchar_t>::id,
00097 #endif
00098     0
00099   };
00100   
00101   const locale::id* const
00102   locale::_Impl::_S_id_collate[] =
00103   {
00104     &std::collate<char>::id,
00105 #ifdef _GLIBCPP_USE_WCHAR_T
00106     &std::collate<wchar_t>::id,
00107 #endif
00108     0
00109   };
00110 
00111   const locale::id* const
00112   locale::_Impl::_S_id_time[] =
00113   {
00114     &__timepunct<char>::id, 
00115     &time_get<char>::id, 
00116     &time_put<char>::id, 
00117 #ifdef _GLIBCPP_USE_WCHAR_T
00118     &__timepunct<wchar_t>::id, 
00119     &time_get<wchar_t>::id,
00120     &time_put<wchar_t>::id,
00121 #endif
00122     0
00123   };
00124   
00125   const locale::id* const
00126   locale::_Impl::_S_id_monetary[] =
00127   {
00128     &money_get<char>::id,        
00129     &money_put<char>::id,        
00130     &moneypunct<char, false>::id, 
00131     &moneypunct<char, true >::id, 
00132 #ifdef _GLIBCPP_USE_WCHAR_T
00133     &money_get<wchar_t>::id,
00134     &money_put<wchar_t>::id,
00135     &moneypunct<wchar_t, false>::id,
00136     &moneypunct<wchar_t, true >::id,
00137 #endif
00138     0
00139   };
00140 
00141   const locale::id* const
00142   locale::_Impl::_S_id_messages[] =
00143   {
00144     &std::messages<char>::id, 
00145 #ifdef _GLIBCPP_USE_WCHAR_T
00146     &std::messages<wchar_t>::id,
00147 #endif
00148     0
00149   };
00150   
00151   const locale::id* const* const
00152   locale::_Impl::_S_facet_categories[] =
00153   {
00154     // Order must match the decl order in class locale.
00155     locale::_Impl::_S_id_ctype,
00156     locale::_Impl::_S_id_numeric,
00157     locale::_Impl::_S_id_collate,
00158     locale::_Impl::_S_id_monetary,
00159     locale::_Impl::_S_id_time,
00160     locale::_Impl::_S_id_messages,
00161     0
00162   };
00163 
00164   locale::~locale() throw()
00165   { _M_impl->_M_remove_reference(); }
00166 
00167   void
00168   locale::_M_coalesce(const locale& __base, const locale& __add, 
00169               category __cat)
00170   {
00171     __cat = _S_normalize_category(__cat);  
00172     _M_impl = new _Impl(*__base._M_impl, 1);  
00173 
00174     try 
00175       { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
00176     catch (...) 
00177       { 
00178     _M_impl->_M_remove_reference(); 
00179     __throw_exception_again;
00180       }
00181   }
00182 
00183   locale::locale() throw()
00184   { 
00185     _S_initialize(); 
00186     (_M_impl = _S_global)->_M_add_reference(); 
00187   }
00188 
00189   locale::locale(const locale& __other) throw()
00190   { (_M_impl = __other._M_impl)->_M_add_reference(); }
00191 
00192   // This is used to initialize global and classic locales, and
00193   // assumes that the _Impl objects are constructed correctly.
00194   locale::locale(_Impl* __ip) throw() : _M_impl(__ip)
00195   { }
00196 
00197   locale::locale(const char* __s)
00198   {
00199     if (__s)
00200       {
00201     _S_initialize(); 
00202     if (strcmp(__s, "C") == 0 || strcmp(__s, "POSIX") == 0)
00203       (_M_impl = _S_classic)->_M_add_reference();
00204     else if (strcmp(__s, "") == 0)
00205       _M_impl = new _Impl(setlocale(LC_ALL, __s), 1);
00206     else
00207       _M_impl = new _Impl(__s, 1);
00208       }
00209     else
00210       __throw_runtime_error("attempt to create locale from NULL name");
00211   }
00212 
00213   locale::locale(const locale& __base, const char* __s, category __cat)
00214   { 
00215     // NB: There are complicated, yet more efficient ways to do
00216     // this. Building up locales on a per-category way is tedious, so
00217     // let's do it this way until people complain.
00218     locale __add(__s);
00219     _M_coalesce(__base, __add, __cat);
00220   }
00221 
00222   locale::locale(const locale& __base, const locale& __add, category __cat)
00223   { _M_coalesce(__base, __add, __cat); }
00224 
00225   bool
00226   locale::operator==(const locale& __rhs) const throw()
00227   {
00228     string __name = this->name();
00229     return (_M_impl == __rhs._M_impl 
00230         || (__name != "*" && __name == __rhs.name()));
00231   }
00232 
00233   const locale&
00234   locale::operator=(const locale& __other) throw()
00235   {
00236     __other._M_impl->_M_add_reference();
00237     _M_impl->_M_remove_reference();
00238     _M_impl = __other._M_impl;
00239     return *this;
00240   }
00241 
00242   locale
00243   locale::global(const locale& __other)
00244   {
00245     // XXX MT
00246     _S_initialize();
00247     _Impl* __old = _S_global;
00248     __other._M_impl->_M_add_reference();
00249     _S_global = __other._M_impl; 
00250     if (_S_global->_M_check_same_name() 
00251     && (strcmp(_S_global->_M_names[0], "*") != 0))
00252       setlocale(LC_ALL, __other.name().c_str());
00253 
00254     // Reference count sanity check: one reference removed for the
00255     // subsition of __other locale, one added by return-by-value. Net
00256     // difference: zero. When the returned locale object's destrutor
00257     // is called, then the reference count is decremented and possibly
00258     // destroyed.
00259     return locale(__old);
00260   }
00261 
00262   string
00263   locale::name() const
00264   {
00265     // Need some kind of separator character. This one was pretty much
00266     // arbitrarily chosen as to not conflict with glibc locales: the
00267     // exact formatting is not set in stone.
00268     const char __separator = '|';
00269 
00270     string __ret;
00271     if (_M_impl->_M_check_same_name())
00272       __ret = _M_impl->_M_names[0];
00273     else
00274       {
00275     for (size_t i = 0; i < _S_num_categories; ++i)
00276       {
00277         __ret += __separator;
00278         __ret += _M_impl->_M_names[i];
00279       }
00280       }
00281     return __ret;
00282   }
00283 
00284   const locale&
00285   locale::classic()
00286   {
00287     static _STL_mutex_lock __lock __STL_MUTEX_INITIALIZER;
00288     _STL_auto_lock __auto(__lock);
00289 
00290     if (!_S_classic)
00291       {
00292     try 
00293       {
00294         // 26 Standard facets, 2 references.
00295         // One reference for _M_classic, one for _M_global
00296         facet** f = new(&facet_vec) facet*[_GLIBCPP_NUM_FACETS];
00297         for (size_t __i = 0; __i < _GLIBCPP_NUM_FACETS; ++__i)
00298           f[__i] = 0;
00299 
00300         _S_classic = new (&c_locale_impl) _Impl(f, 2, true);
00301         _S_global = _S_classic;         
00302         new (&c_locale) locale(_S_classic);
00303       }
00304     catch(...) 
00305       {
00306         // Just call destructor, so that locale_impl_c's memory is
00307         // not deallocated via a call to delete.
00308         if (_S_classic)
00309           _S_classic->~_Impl();
00310         _S_classic = _S_global = 0;
00311         __throw_exception_again;
00312       }
00313       }
00314     return c_locale;
00315   }
00316 
00317   locale::category
00318   locale::_S_normalize_category(category __cat) 
00319   {
00320     int __ret = 0;
00321     if (__cat == none || (__cat & all) && !(__cat & ~all))
00322       __ret = __cat;
00323     else
00324       {
00325     // NB: May be a C-style "LC_ALL" category; convert.
00326     switch (__cat)
00327       {
00328       case LC_COLLATE:  
00329         __ret = collate; 
00330         break;
00331       case LC_CTYPE:    
00332         __ret = ctype;
00333         break;
00334       case LC_MONETARY: 
00335         __ret = monetary;
00336         break;
00337       case LC_NUMERIC:  
00338         __ret = numeric;
00339         break;
00340       case LC_TIME:     
00341         __ret = time; 
00342         break;
00343 #ifdef _GLIBCPP_HAVE_LC_MESSAGES
00344       case LC_MESSAGES: 
00345         __ret = messages;
00346         break;
00347 #endif  
00348       case LC_ALL:      
00349         __ret = all;
00350         break;
00351       default:
00352         __throw_runtime_error("bad locale category");
00353       }
00354       }
00355     return __ret;
00356   }
00357 
00358   __c_locale
00359   locale::facet::_S_c_locale;
00360   
00361   locale::facet::
00362   ~facet() { }
00363 
00364   locale::facet::
00365   facet(size_t __refs) throw() : _M_references(__refs) 
00366   { 
00367     if (!_S_c_locale)
00368       _S_create_c_locale(_S_c_locale, "C");
00369   }
00370 
00371   void  
00372   locale::facet::
00373   _M_add_reference() throw()
00374   { __atomic_add(&_M_references, 1); }
00375 
00376   void  
00377   locale::facet::
00378   _M_remove_reference() throw()
00379   {
00380     if (__exchange_and_add(&_M_references, -1) == 0)
00381       {
00382         try 
00383       { delete this; }  
00384     catch (...) 
00385       { }
00386       }
00387   }
00388   
00389   locale::id::id() 
00390   { }
00391 
00392   // Definitions for static const data members of ctype_base.
00393   const ctype_base::mask ctype_base::space;
00394   const ctype_base::mask ctype_base::print;
00395   const ctype_base::mask ctype_base::cntrl;
00396   const ctype_base::mask ctype_base::upper;
00397   const ctype_base::mask ctype_base::lower;
00398   const ctype_base::mask ctype_base::alpha;
00399   const ctype_base::mask ctype_base::digit;
00400   const ctype_base::mask ctype_base::punct;
00401   const ctype_base::mask ctype_base::xdigit;
00402   const ctype_base::mask ctype_base::alnum;
00403   const ctype_base::mask ctype_base::graph;
00404 
00405   // Platform-specific initialization code for ctype tables.
00406   #include <bits/ctype_noninline.h>
00407 
00408   const size_t ctype<char>::table_size;
00409 
00410   ctype<char>::~ctype()
00411   { 
00412     if (_M_c_locale_ctype != _S_c_locale)
00413       _S_destroy_c_locale(_M_c_locale_ctype);
00414     if (_M_del) 
00415       delete[] this->table(); 
00416   }
00417 
00418   // These are dummy placeholders as these virtual functions are never called.
00419   bool 
00420   ctype<char>::do_is(mask, char_type) const 
00421   { return false; }
00422   
00423   const char*
00424   ctype<char>::do_is(const char_type* __c, const char_type*, mask*) const 
00425   { return __c; }
00426   
00427   const char*
00428   ctype<char>::do_scan_is(mask, const char_type* __c, const char_type*) const 
00429   { return __c; }
00430 
00431   const char* 
00432   ctype<char>::do_scan_not(mask, const char_type* __c, const char_type*) const
00433   { return __c; }
00434 
00435   char
00436   ctype<char>::do_widen(char __c) const
00437   { return __c; }
00438   
00439   const char* 
00440   ctype<char>::do_widen(const char* __lo, const char* __hi, char* __dest) const
00441   {
00442     memcpy(__dest, __lo, __hi - __lo);
00443     return __hi;
00444   }
00445   
00446   char
00447   ctype<char>::do_narrow(char __c, char /*__dfault*/) const
00448   { return __c; }
00449   
00450   const char* 
00451   ctype<char>::do_narrow(const char* __lo, const char* __hi, 
00452              char /*__dfault*/, char* __dest) const
00453   {
00454     memcpy(__dest, __lo, __hi - __lo);
00455     return __hi;
00456   }
00457 
00458 #ifdef _GLIBCPP_USE_WCHAR_T
00459   ctype<wchar_t>::ctype(size_t __refs) 
00460   : __ctype_abstract_base<wchar_t>(__refs)
00461   { _M_c_locale_ctype = _S_c_locale; }
00462 
00463   ctype<wchar_t>::ctype(__c_locale __cloc, size_t __refs) 
00464   : __ctype_abstract_base<wchar_t>(__refs) 
00465   { _M_c_locale_ctype = _S_clone_c_locale(__cloc); }
00466 
00467   ctype<wchar_t>::~ctype() 
00468   { 
00469     if (_M_c_locale_ctype != _S_c_locale)
00470       _S_destroy_c_locale(_M_c_locale_ctype); 
00471   }
00472 
00473   template<>
00474     ctype_byname<wchar_t>::ctype_byname(const char* __s, size_t __refs)
00475     : ctype<wchar_t>(__refs) 
00476     {   
00477       if (_M_c_locale_ctype != _S_c_locale)
00478     _S_destroy_c_locale(_M_c_locale_ctype);
00479       _S_create_c_locale(_M_c_locale_ctype, __s); 
00480     }
00481 #endif
00482 
00483   // Definitions for static const data members of time_base
00484   template<> 
00485     const char*
00486     __timepunct<char>::_S_timezones[14] =
00487     { 
00488       "GMT", "HST", "AKST", "PST", "MST", "CST", "EST", "AST", "NST", "CET", 
00489       "IST", "EET", "CST", "JST"  
00490     };
00491  
00492 #ifdef _GLIBCPP_USE_WCHAR_T
00493   template<> 
00494     const wchar_t*
00495     __timepunct<wchar_t>::_S_timezones[14] =
00496     { 
00497       L"GMT", L"HST", L"AKST", L"PST", L"MST", L"CST", L"EST", L"AST", 
00498       L"NST", L"CET", L"IST", L"EET", L"CST", L"JST"  
00499     };
00500 #endif
00501 
00502   // Definitions for static const data members of money_base
00503   const money_base::pattern 
00504   money_base::_S_default_pattern =  { {symbol, sign, none, value} };
00505 
00506   template<>
00507     const ctype<char>&
00508     use_facet<ctype<char> >(const locale& __loc)
00509     {
00510       size_t __i = ctype<char>::id._M_id();
00511       const locale::_Impl* __tmp = __loc._M_impl;
00512       return static_cast<const ctype<char>&>(*(__tmp->_M_facets[__i]));
00513     }
00514 
00515 #ifdef _GLIBCPP_USE_WCHAR_T
00516   template<>
00517     const ctype<wchar_t>&
00518     use_facet<ctype<wchar_t> >(const locale& __loc)
00519     {
00520       size_t __i = ctype<wchar_t>::id._M_id();
00521       const locale::_Impl* __tmp = __loc._M_impl;
00522       return static_cast<const ctype<wchar_t>&>(*(__tmp->_M_facets[__i]));
00523     }
00524 #endif
00525 
00526   const char __num_base::_S_atoms[] = "0123456789eEabcdfABCDF";
00527 
00528   bool
00529   __num_base::_S_format_float(const ios_base& __io, char* __fptr, char __mod,
00530                   streamsize __prec)
00531   {
00532     bool __incl_prec = false;
00533     ios_base::fmtflags __flags = __io.flags();
00534     *__fptr++ = '%';
00535     // [22.2.2.2.2] Table 60
00536     if (__flags & ios_base::showpos)
00537       *__fptr++ = '+';
00538     if (__flags & ios_base::showpoint)
00539       *__fptr++ = '#';
00540     // As per [22.2.2.2.2.11]
00541     if (__flags & ios_base::fixed || __prec > 0)
00542       {
00543     *__fptr++ = '.';
00544     *__fptr++ = '*';
00545     __incl_prec = true;
00546       }
00547     if (__mod)
00548       *__fptr++ = __mod;
00549     ios_base::fmtflags __fltfield = __flags & ios_base::floatfield;
00550     // [22.2.2.2.2] Table 58
00551     if (__fltfield == ios_base::fixed)
00552       *__fptr++ = 'f';
00553     else if (__fltfield == ios_base::scientific)
00554       *__fptr++ = (__flags & ios_base::uppercase) ? 'E' : 'e';
00555     else
00556       *__fptr++ = (__flags & ios_base::uppercase) ? 'G' : 'g';
00557     *__fptr = '\0';
00558     return __incl_prec;
00559   }
00560   
00561   void
00562   __num_base::_S_format_int(const ios_base& __io, char* __fptr, char __mod, 
00563                 char __modl)
00564   {
00565     ios_base::fmtflags __flags = __io.flags();
00566     *__fptr++ = '%';
00567     // [22.2.2.2.2] Table 60
00568     if (__flags & ios_base::showpos)
00569       *__fptr++ = '+';
00570     if (__flags & ios_base::showbase)
00571       *__fptr++ = '#';
00572     *__fptr++ = 'l';
00573 
00574     // For long long types.
00575     if (__modl)
00576       *__fptr++ = __modl;
00577 
00578     ios_base::fmtflags __bsefield = __flags & ios_base::basefield;
00579     if (__bsefield == ios_base::hex)
00580       *__fptr++ = (__flags & ios_base::uppercase) ? 'X' : 'x';
00581     else if (__bsefield == ios_base::oct)
00582       *__fptr++ = 'o';
00583     else
00584       *__fptr++ = __mod;
00585     *__fptr = '\0';
00586   }
00587 } // namespace std
00588 

Generated on Wed May 1 19:19:32 2002 for libstdc++-v3 Source by doxygen1.2.15