OSDN Git Service

2005-12-18 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / src / localename.cc
index 8fa9118..e1a1114 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
+// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
 // Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
@@ -14,7 +14,7 @@
 
 // You should have received a copy of the GNU General Public License along
 // with this library; see the file COPYING.  If not, write to the Free
-// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 // USA.
 
 // As a special exception, you may use this file as part of a free software
 #include <cstring>
 #include <locale>
 
-namespace std
-{
-  // Defined in globals.cc.
-  extern std::ctype<char>                      ctype_c;
-  extern std::collate<char>                    collate_c;
-  extern numpunct<char>                        numpunct_c;
-  extern num_get<char>                                 num_get_c;
-  extern num_put<char>                                 num_put_c;
-  extern codecvt<char, char, mbstate_t>                codecvt_c;
-  extern moneypunct<char, false>               moneypunct_fc;
-  extern moneypunct<char, true>                moneypunct_tc;
-  extern money_get<char>                       money_get_c;
-  extern money_put<char>                       money_put_c;
-  extern __timepunct<char>                     timepunct_c;
-  extern time_get<char>                        time_get_c;
-  extern time_put<char>                        time_put_c;
-  extern std::messages<char>                   messages_c;
-#ifdef  _GLIBCPP_USE_WCHAR_T
-  extern std::ctype<wchar_t>                   ctype_w;
-  extern std::collate<wchar_t>                         collate_w;
-  extern numpunct<wchar_t>                     numpunct_w;
-  extern num_get<wchar_t>                      num_get_w;
-  extern num_put<wchar_t>                      num_put_w;
-  extern codecvt<wchar_t, char, mbstate_t>     codecvt_w;
-  extern moneypunct<wchar_t, false>            moneypunct_fw;
-  extern moneypunct<wchar_t, true>             moneypunct_tw;
-  extern money_get<wchar_t>                    money_get_w;
-  extern money_put<wchar_t>                    money_put_w;
-  extern __timepunct<wchar_t>                  timepunct_w;
-  extern time_get<wchar_t>                     time_get_w;
-  extern time_put<wchar_t>                     time_put_w;
-  extern std::messages<wchar_t>                messages_w;
-#endif
+_GLIBCXX_BEGIN_NAMESPACE(std)
 
-  locale::_Impl::
-  ~_Impl() throw()
+  using namespace __gnu_cxx;
+
+  locale::locale(const char* __s) : _M_impl(0)
   {
-    for (size_t __i = 0; __i < _M_facets_size; ++__i)
-      if (_M_facets[__i])
-       _M_facets[__i]->_M_remove_reference();
-    delete [] _M_facets;
+    if (__s)
+      {
+       _S_initialize(); 
+       if (std::strcmp(__s, "C") == 0 || std::strcmp(__s, "POSIX") == 0)
+         (_M_impl = _S_classic)->_M_add_reference();
+       else if (std::strcmp(__s, "") != 0)
+         _M_impl = new _Impl(__s, 1);
+       else
+         {
+           // Get it from the environment.
+           char* __env = std::getenv("LC_ALL");
+           // If LC_ALL is set we are done.
+           if (__env && std::strcmp(__env, "") != 0)
+             {
+               if (std::strcmp(__env, "C") == 0 
+                   || std::strcmp(__env, "POSIX") == 0)
+                 (_M_impl = _S_classic)->_M_add_reference();
+               else
+                 _M_impl = new _Impl(__env, 1);
+             }
+           else
+             {
+               // LANG may set a default different from "C".
+               string __lang;
+               __env = std::getenv("LANG");
+               if (!__env || std::strcmp(__env, "") == 0 
+                   || std::strcmp(__env, "C") == 0 
+                   || std::strcmp(__env, "POSIX") == 0)
+                 __lang = "C";
+               else 
+                 __lang = __env;
+               
+               // Scan the categories looking for the first one
+               // different from LANG.
+               size_t __i = 0;
+               if (__lang == "C")
+                 for (; __i < _S_categories_size; ++__i)
+                   {
+                     __env = std::getenv(_S_categories[__i]);
+                     if (__env && std::strcmp(__env, "") != 0 
+                         && std::strcmp(__env, "C") != 0 
+                         && std::strcmp(__env, "POSIX") != 0)
+                       break;
+                   }
+               else
+                 for (; __i < _S_categories_size; ++__i)
+                   {
+                     __env = std::getenv(_S_categories[__i]);
+                     if (__env && std::strcmp(__env, "") != 0
+                         && __lang != __env)
+                       break;
+                   }
+       
+               // If one is found, build the complete string of
+               // the form LC_CTYPE=xxx;LC_NUMERIC=yyy; and so on...
+               if (__i < _S_categories_size)
+                 {
+                   string __str;
+                   __str.reserve(128);
+                   for (size_t __j = 0; __j < __i; ++__j)
+                     {
+                       __str += _S_categories[__j];
+                       __str += '=';
+                       __str += __lang;
+                       __str += ';';
+                     }
+                   __str += _S_categories[__i];
+                   __str += '=';
+                   __str += __env;
+                   __str += ';';
+                   ++__i;
+                   for (; __i < _S_categories_size; ++__i)
+                     {
+                       __env = std::getenv(_S_categories[__i]);
+                       __str += _S_categories[__i];
+                       if (!__env || std::strcmp(__env, "") == 0)
+                         {
+                           __str += '=';
+                           __str += __lang;
+                           __str += ';';
+                         }
+                       else if (std::strcmp(__env, "C") == 0
+                                || std::strcmp(__env, "POSIX") == 0)
+                         __str += "=C;";
+                       else
+                         {
+                           __str += '=';
+                           __str += __env;
+                           __str += ';';
+                         }
+                     }
+                   __str.erase(__str.end() - 1);
+                   _M_impl = new _Impl(__str.c_str(), 1);
+                 }
+               // ... otherwise either an additional instance of
+               // the "C" locale or LANG.
+               else if (__lang == "C")
+                 (_M_impl = _S_classic)->_M_add_reference();
+               else
+                 _M_impl = new _Impl(__lang.c_str(), 1);
+             }
+         }
+      }
+    else
+      __throw_runtime_error(__N("locale::locale NULL not valid"));
   }
 
-  // Clone existing _Impl object.
-  locale::_Impl::
-  _Impl(const _Impl& __imp, size_t __refs)
-  : _M_references(__refs), _M_facets_size(__imp._M_facets_size) // XXX
+  locale::locale(const locale& __base, const char* __s, category __cat)
+  : _M_impl(0)
+  { 
+    // NB: There are complicated, yet more efficient ways to do
+    // this. Building up locales on a per-category way is tedious, so
+    // let's do it this way until people complain.
+    locale __add(__s);
+    _M_coalesce(__base, __add, __cat);
+  }
+
+  locale::locale(const locale& __base, const locale& __add, category __cat)
+  : _M_impl(0)
+  { _M_coalesce(__base, __add, __cat); }
+
+  void
+  locale::_M_coalesce(const locale& __base, const locale& __add, 
+                     category __cat)
   {
-    try
+    __cat = _S_normalize_category(__cat);  
+    _M_impl = new _Impl(*__base._M_impl, 1);  
+
+    try 
+      { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
+    catch (...) 
       { 
-       _M_facets = new facet*[_M_facets_size]; 
-       for (size_t __i = 0; __i < _M_facets_size; ++__i)
-         _M_facets[__i] = 0;
-      }
-    catch(...) 
-      {
-       delete [] _M_facets;
+       _M_impl->_M_remove_reference(); 
        __throw_exception_again;
       }
-    for (size_t __i = 0; __i < _M_facets_size; ++__i)
-      {
-       _M_facets[__i] = __imp._M_facets[__i];
-       if (_M_facets[__i])
-         _M_facets[__i]->_M_add_reference();
-      }
-    for (size_t __i = 0; __i < _S_num_categories; ++__i)
-      _M_names[__i] = __imp._M_names[__i];
   }
 
   // Construct named _Impl.
   locale::_Impl::
-  _Impl(const char* __s, size_t __refs) 
-  : _M_references(__refs), _M_facets_size(_GLIBCPP_NUM_FACETS) // XXX
+  _Impl(const char* __s, size_t __refs)
+  : _M_refcount(__refs), _M_facets(0), _M_facets_size(_GLIBCXX_NUM_FACETS),
+    _M_caches(0), _M_names(0)
   {
-    // Initialize the underlying locale model, which also checks
-    // to see if the given name is valid.
+    // Initialize the underlying locale model, which also checks to
+    // see if the given name is valid.
     __c_locale __cloc;
     locale::facet::_S_create_c_locale(__cloc, __s);
 
     try
-      { 
-       _M_facets = new facet*[_M_facets_size]; 
+      {
+       _M_facets = new const facet*[_M_facets_size];
        for (size_t __i = 0; __i < _M_facets_size; ++__i)
          _M_facets[__i] = 0;
-      }
-    catch(...) 
-      {
-       delete [] _M_facets;
-       __throw_exception_again;
-      }
+       _M_caches = new const facet*[_M_facets_size];
+       for (size_t __j = 0; __j < _M_facets_size; ++__j)
+         _M_caches[__j] = 0;
+       _M_names = new char*[_S_categories_size];
+       for (size_t __k = 0; __k < _S_categories_size; ++__k)
+         _M_names[__k] = 0;
 
-    // Name all the categories.
-    for (size_t i = 0; i < _S_num_categories; ++i)
-      _M_names[i] = __s;
-
-    // Construct all standard facets and add them to _M_facets.
-    _M_init_facet(new std::ctype<char>(__cloc));
-    _M_init_facet(new codecvt<char, char, mbstate_t>);
-    _M_init_facet(new numpunct<char>(__cloc));
-    _M_init_facet(new num_get<char>);
-    _M_init_facet(new num_put<char>);
-    _M_init_facet(new std::collate<char>(__cloc));
-    _M_init_facet(new moneypunct<char, false>(__cloc, __s));
-    _M_init_facet(new moneypunct<char, true>(__cloc, __s));
-    _M_init_facet(new money_get<char>);
-    _M_init_facet(new money_put<char>);
-    _M_init_facet(new __timepunct<char>(__cloc, __s));
-    _M_init_facet(new time_get<char>);
-    _M_init_facet(new time_put<char>);
-    _M_init_facet(new std::messages<char>(__cloc, __s));
+       // Name the categories.
+       const size_t __len = std::strlen(__s);
+       if (!std::memchr(__s, ';', __len))
+         {
+           _M_names[0] = new char[__len + 1];
+           std::memcpy(_M_names[0], __s, __len + 1);       
+         }
+       else
+         {
+           const char* __end = __s;
+           for (size_t __i = 0; __i < _S_categories_size; ++__i)
+             {
+               const char* __beg = std::strchr(__end + 1, '=') + 1;
+               __end = std::strchr(__beg, ';');
+               if (!__end)
+                 __end = __s + __len;
+               _M_names[__i] = new char[__end - __beg + 1];
+               std::memcpy(_M_names[__i], __beg, __end - __beg);
+               _M_names[__i][__end - __beg] = '\0';
+             }
+         }
+       // Construct all standard facets and add them to _M_facets.
+       _M_init_facet(new std::ctype<char>(__cloc, 0, false));
+       _M_init_facet(new codecvt<char, char, mbstate_t>(__cloc));
+       _M_init_facet(new numpunct<char>(__cloc));
+       _M_init_facet(new num_get<char>);
+       _M_init_facet(new num_put<char>);
+       _M_init_facet(new std::collate<char>(__cloc));
+       _M_init_facet(new moneypunct<char, false>(__cloc, __s));
+       _M_init_facet(new moneypunct<char, true>(__cloc, __s));
+       _M_init_facet(new money_get<char>);
+       _M_init_facet(new money_put<char>);
+       _M_init_facet(new __timepunct<char>(__cloc, __s));
+       _M_init_facet(new time_get<char>);
+       _M_init_facet(new time_put<char>);
+       _M_init_facet(new std::messages<char>(__cloc, __s));
        
-#ifdef  _GLIBCPP_USE_WCHAR_T
-    _M_init_facet(new std::ctype<wchar_t>(__cloc));
-    _M_init_facet(new codecvt<wchar_t, char, mbstate_t>);
-    _M_init_facet(new numpunct<wchar_t>(__cloc));
-    _M_init_facet(new num_get<wchar_t>);
-    _M_init_facet(new num_put<wchar_t>);
-    _M_init_facet(new std::collate<wchar_t>(__cloc));
-    _M_init_facet(new moneypunct<wchar_t, false>(__cloc, __s));
-    _M_init_facet(new moneypunct<wchar_t, true>(__cloc, __s));
-    _M_init_facet(new money_get<wchar_t>);
-    _M_init_facet(new money_put<wchar_t>);
-    _M_init_facet(new __timepunct<wchar_t>(__cloc, __s));
-    _M_init_facet(new time_get<wchar_t>);
-    _M_init_facet(new time_put<wchar_t>);
-    _M_init_facet(new std::messages<wchar_t>(__cloc, __s));
+#ifdef  _GLIBCXX_USE_WCHAR_T
+       _M_init_facet(new std::ctype<wchar_t>(__cloc));
+       _M_init_facet(new codecvt<wchar_t, char, mbstate_t>(__cloc));
+       _M_init_facet(new numpunct<wchar_t>(__cloc));
+       _M_init_facet(new num_get<wchar_t>);
+       _M_init_facet(new num_put<wchar_t>);
+       _M_init_facet(new std::collate<wchar_t>(__cloc));
+       _M_init_facet(new moneypunct<wchar_t, false>(__cloc, __s));
+       _M_init_facet(new moneypunct<wchar_t, true>(__cloc, __s));
+       _M_init_facet(new money_get<wchar_t>);
+       _M_init_facet(new money_put<wchar_t>);
+       _M_init_facet(new __timepunct<wchar_t>(__cloc, __s));
+       _M_init_facet(new time_get<wchar_t>);
+       _M_init_facet(new time_put<wchar_t>);
+       _M_init_facet(new std::messages<wchar_t>(__cloc, __s));
 #endif   
-    locale::facet::_S_destroy_c_locale(__cloc);
+       locale::facet::_S_destroy_c_locale(__cloc);
+      }
+    catch(...)
+      {
+       locale::facet::_S_destroy_c_locale(__cloc);
+       this->~_Impl();
+       __throw_exception_again;
+      }        
   }
 
-  // Construct "C" _Impl.
-  locale::_Impl::
-  _Impl(facet** __f, size_t __refs, bool) 
-  : _M_references(__refs), _M_facets(__f), _M_facets_size(_GLIBCPP_NUM_FACETS)
-  {
-    // Name all the categories.
-    for (size_t i = 0; i < _S_num_categories; ++i)
-      _M_names[i] = "C";
-
-    // This is needed as presently the C++ version of "C" locales
-    // != data in the underlying locale model for __timepunct,
-    // numpunct, and moneypunct. Also, the "C" locales must be
-    // constructed in a way such that they are pre-allocated.
-    _M_init_facet(new (&ctype_c) std::ctype<char>);
-    _M_init_facet(new (&codecvt_c) codecvt<char, char, mbstate_t>);
-    _M_init_facet(new (&numpunct_c) numpunct<char>);
-    _M_init_facet(new (&num_get_c) num_get<char>);
-    _M_init_facet(new (&num_put_c) num_put<char>);
-    _M_init_facet(new (&collate_c) std::collate<char>);
-    _M_init_facet(new (&moneypunct_fc) moneypunct<char, false>);
-    _M_init_facet(new (&moneypunct_tc) moneypunct<char, true>);
-    _M_init_facet(new (&money_get_c) money_get<char>);
-    _M_init_facet(new (&money_put_c) money_put<char>);
-    _M_init_facet(new (&timepunct_c) __timepunct<char>);
-    _M_init_facet(new (&time_get_c) time_get<char>);
-    _M_init_facet(new (&time_put_c) time_put<char>);
-    _M_init_facet(new (&messages_c) std::messages<char>);      
-#ifdef  _GLIBCPP_USE_WCHAR_T
-    _M_init_facet(new (&ctype_w) std::ctype<wchar_t>);
-    _M_init_facet(new (&codecvt_w) codecvt<wchar_t, char, mbstate_t>);
-    _M_init_facet(new (&numpunct_w) numpunct<wchar_t>);
-    _M_init_facet(new (&num_get_w) num_get<wchar_t>);
-    _M_init_facet(new (&num_put_w) num_put<wchar_t>);
-    _M_init_facet(new (&collate_w) std::collate<wchar_t>);
-    _M_init_facet(new (&moneypunct_fw) moneypunct<wchar_t, false>);
-    _M_init_facet(new (&moneypunct_tw) moneypunct<wchar_t, true>);
-    _M_init_facet(new (&money_get_w) money_get<wchar_t>);
-    _M_init_facet(new (&money_put_w) money_put<wchar_t>);
-    _M_init_facet(new (&timepunct_w) __timepunct<wchar_t>);
-    _M_init_facet(new (&time_get_w) time_get<wchar_t>);
-    _M_init_facet(new (&time_put_w) time_put<wchar_t>);
-    _M_init_facet(new (&messages_w) std::messages<wchar_t>);
-#endif   
-  }
-  
   void
   locale::_Impl::
   _M_replace_categories(const _Impl* __imp, category __cat)
   {
-    category __mask;
-    for (unsigned int __ix = 0; __ix < _S_num_categories; ++__ix)
+    category __mask = 1;
+    const bool __have_names = _M_names[0] && __imp->_M_names[0];
+    for (size_t __ix = 0; __ix < _S_categories_size; ++__ix, __mask <<= 1)
       {
-       __mask = 1 << __ix;
        if (__mask & __cat)
          {
            // Need to replace entry in _M_facets with other locale's info.
            _M_replace_category(__imp, _S_facet_categories[__ix]);
            // If both have names, go ahead and mangle.
-           if (strcmp(_M_names[__ix], "*") != 0 
-               && strcmp(__imp->_M_names[__ix], "*") != 0)
-             _M_names[__ix] = __imp->_M_names[__ix];
+           if (__have_names)
+             {
+               if (!_M_names[1])
+                 {
+                   // A full set of _M_names must be prepared, all identical
+                   // to _M_names[0] to begin with. Then, below, a few will
+                   // be replaced by the corresponding __imp->_M_names. I.e.,
+                   // not a "simple" locale anymore (see locale::operator==).
+                   const size_t __len = std::strlen(_M_names[0]) + 1;
+                   for (size_t __i = 1; __i < _S_categories_size; ++__i)
+                     {
+                       _M_names[__i] = new char[__len];
+                       std::memcpy(_M_names[__i], _M_names[0], __len);
+                     }
+                 }
+               char* __src = __imp->_M_names[__ix] ? __imp->_M_names[__ix]
+                                                   : __imp->_M_names[0];
+               const size_t __len = std::strlen(__src) + 1;
+               char* __new = new char[__len];
+               std::memcpy(__new, __src, __len);
+               delete [] _M_names[__ix];
+               _M_names[__ix] = __new;
+             }
          }
       }
   }
 
-  void
-  locale::_Impl::
-  _M_replace_category(const _Impl* __imp, const locale::id* const* __idpp)
-  {
-    for (; *__idpp; ++__idpp)
-      _M_replace_facet(__imp, *__idpp);
-  }
-  
-  void
-  locale::_Impl::
-  _M_replace_facet(const _Impl* __imp, const locale::id* __idp)
-  {
-    size_t __index = __idp->_M_id();
-    if ((__index > (__imp->_M_facets_size - 1)) || !__imp->_M_facets[__index])
-      __throw_runtime_error("no locale facet");
-    _M_install_facet(__idp, __imp->_M_facets[__index]); 
-  }
-
-  void
-  locale::_Impl::
-  _M_install_facet(const locale::id* __idp, facet* __fp)
-  {
-    if (__fp)
-      {
-       size_t __index = __idp->_M_id();
-       if (__index > _M_facets_size - 1)
-         {
-           facet** __old = _M_facets;
-           facet** __new;
-           const size_t __new_size = __index + 4;
-           __new = new facet*[__new_size]; 
-           for (size_t __i = 0; __i < _M_facets_size; ++__i)
-             __new[__i] = _M_facets[__i];
-           for (size_t __i2 = _M_facets_size; __i2 < __new_size; ++__i2)
-             __new[__i2] = 0;
-
-           _M_facets_size = __new_size;
-           _M_facets = __new;
-           delete [] __old;
-         }
-
-       facet*& __fpr = _M_facets[__index];
-       if (__fpr)
-         {
-           // Replacing an existing facet. Order matters.
-           __fp->_M_add_reference();
-           __fpr->_M_remove_reference();
-           __fpr = __fp;
-         }
-       else
-         {
-           // Installing a newly created facet into an empty
-           // _M_facets container, say a newly-constructed,
-           // swanky-fresh _Impl.
-           _M_facets[__index] = __fp;
-         }
-      }
-  }
-} // namespace std
+_GLIBCXX_END_NAMESPACE