OSDN Git Service

2003-10-02 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / src / locale.cc
1 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
2 // Free Software Foundation, Inc.
3 //
4 // This file is part of the GNU ISO C++ Library.  This library is free
5 // software; you can redistribute it and/or modify it under the
6 // terms of the GNU General Public License as published by the
7 // Free Software Foundation; either version 2, or (at your option)
8 // any later version.
9
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14
15 // You should have received a copy of the GNU General Public License along
16 // with this library; see the file COPYING.  If not, write to the Free
17 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 // USA.
19
20 // As a special exception, you may use this file as part of a free software
21 // library without restriction.  Specifically, if other files instantiate
22 // templates or use macros or inline functions from this file, or you compile
23 // this file and link it with other files to produce an executable, this
24 // file does not by itself cause the resulting executable to be covered by
25 // the GNU General Public License.  This exception does not however
26 // invalidate any other reasons why the executable file might be covered by
27 // the GNU General Public License.
28
29 #include <clocale>
30 #include <cstring>
31 #include <cstdlib>     // For getenv, free.
32 #include <cctype>
33 #include <cwctype>     // For towupper, etc.
34 #include <locale>
35 #include <bits/atomicity.h>
36
37 namespace __gnu_cxx
38 {
39   // Defined in globals.cc.
40   extern std::locale            c_locale;
41   extern std::locale::_Impl     c_locale_impl;
42 } // namespace __gnu_cxx
43
44 namespace std 
45 {
46   using namespace __gnu_cxx;
47
48   // Definitions for static const data members of locale.
49   const locale::category        locale::none;
50   const locale::category        locale::ctype;
51   const locale::category        locale::numeric;
52   const locale::category        locale::collate;
53   const locale::category        locale::time;
54   const locale::category        locale::monetary;
55   const locale::category        locale::messages;
56   const locale::category        locale::all;
57
58   // In the future, GLIBCXX_ABI > 5 should remove all uses of
59   // _GLIBCXX_ASM_SYMVER in this file, and remove exports of any
60   // static data members of locale.
61
62   // These are no longer exported.
63   locale::_Impl*                locale::_S_classic;
64   locale::_Impl*                locale::_S_global; 
65   const size_t                  locale::_S_categories_size;
66
67 #ifdef __GTHREADS
68   __gthread_once_t              locale::_S_once = __GTHREAD_ONCE_INIT;
69 #endif
70
71   // Definitions for static const data members of locale::id
72   _Atomic_word locale::id::_S_highwater;  // init'd to 0 by linker
73
74   // Definitions for static const data members of locale::_Impl
75   const locale::id* const
76   locale::_Impl::_S_id_ctype[] =
77   {
78     &std::ctype<char>::id, 
79     &codecvt<char, char, mbstate_t>::id,
80 #ifdef _GLIBCXX_USE_WCHAR_T
81     &std::ctype<wchar_t>::id,
82     &codecvt<wchar_t, char, mbstate_t>::id,
83 #endif
84     0
85   };
86
87   const locale::id* const
88   locale::_Impl::_S_id_numeric[] =
89   {
90     &num_get<char>::id,  
91     &num_put<char>::id,  
92     &numpunct<char>::id, 
93 #ifdef _GLIBCXX_USE_WCHAR_T
94     &num_get<wchar_t>::id,
95     &num_put<wchar_t>::id,
96     &numpunct<wchar_t>::id,
97 #endif
98     0
99   };
100   
101   const locale::id* const
102   locale::_Impl::_S_id_collate[] =
103   {
104     &std::collate<char>::id,
105 #ifdef _GLIBCXX_USE_WCHAR_T
106     &std::collate<wchar_t>::id,
107 #endif
108     0
109   };
110
111   const locale::id* const
112   locale::_Impl::_S_id_time[] =
113   {
114     &__timepunct<char>::id, 
115     &time_get<char>::id, 
116     &time_put<char>::id, 
117 #ifdef _GLIBCXX_USE_WCHAR_T
118     &__timepunct<wchar_t>::id, 
119     &time_get<wchar_t>::id,
120     &time_put<wchar_t>::id,
121 #endif
122     0
123   };
124   
125   const locale::id* const
126   locale::_Impl::_S_id_monetary[] =
127   {
128     &money_get<char>::id,        
129     &money_put<char>::id,        
130     &moneypunct<char, false>::id, 
131     &moneypunct<char, true >::id, 
132 #ifdef _GLIBCXX_USE_WCHAR_T
133     &money_get<wchar_t>::id,
134     &money_put<wchar_t>::id,
135     &moneypunct<wchar_t, false>::id,
136     &moneypunct<wchar_t, true >::id,
137 #endif
138     0
139   };
140
141   const locale::id* const
142   locale::_Impl::_S_id_messages[] =
143   {
144     &std::messages<char>::id, 
145 #ifdef _GLIBCXX_USE_WCHAR_T
146     &std::messages<wchar_t>::id,
147 #endif
148     0
149   };
150   
151   const locale::id* const* const
152   locale::_Impl::_S_facet_categories[] =
153   {
154     // Order must match the decl order in class locale.
155     locale::_Impl::_S_id_ctype,
156     locale::_Impl::_S_id_numeric,
157     locale::_Impl::_S_id_collate,
158     locale::_Impl::_S_id_time,
159     locale::_Impl::_S_id_monetary,
160     locale::_Impl::_S_id_messages,
161     0
162   };
163
164   locale::locale() throw()
165   { 
166     _S_initialize(); 
167     (_M_impl = _S_global)->_M_add_reference(); 
168   }
169
170   locale::locale(const locale& __other) throw()
171   { (_M_impl = __other._M_impl)->_M_add_reference(); }
172
173   // This is used to initialize global and classic locales, and
174   // assumes that the _Impl objects are constructed correctly.
175   // The lack of a reference increment is intentional.
176   locale::locale(_Impl* __ip) throw() : _M_impl(__ip)
177   { }
178
179   locale::locale(const char* __s)
180   {
181     if (__s)
182       {
183         _S_initialize(); 
184         if (std::strcmp(__s, "C") == 0 || std::strcmp(__s, "POSIX") == 0)
185           (_M_impl = _S_classic)->_M_add_reference();
186         else if (std::strcmp(__s, "") != 0)
187           _M_impl = new _Impl(__s, 1);
188         else
189           {
190             // Get it from the environment.
191             char* __env = std::getenv("LC_ALL");
192             // If LC_ALL is set we are done.
193             if (__env && std::strcmp(__env, "") != 0)
194               {
195                 if (std::strcmp(__env, "C") == 0 
196                     || std::strcmp(__env, "POSIX") == 0)
197                   (_M_impl = _S_classic)->_M_add_reference();
198                 else
199                   _M_impl = new _Impl(__env, 1);
200               }
201             else
202               {
203                 char* __res;
204                 // LANG may set a default different from "C".
205                 char* __env = std::getenv("LANG");
206                 if (!__env || std::strcmp(__env, "") == 0 
207                     || std::strcmp(__env, "C") == 0 
208                     || std::strcmp(__env, "POSIX") == 0)
209                   __res = strdup("C");
210                 else 
211                   __res = strdup(__env);
212                 
213                 // Scan the categories looking for the first one
214                 // different from LANG.
215                 size_t __i = 0;
216                 if (std::strcmp(__res, "C") == 0)
217                   for (; __i < _S_categories_size; ++__i)
218                     {
219                       __env = std::getenv(_S_categories[__i]);
220                       if (__env && std::strcmp(__env, "") != 0 
221                           && std::strcmp(__env, "C") != 0 
222                           && std::strcmp(__env, "POSIX") != 0) 
223                         break;
224                     }
225                 else
226                   for (; __i < _S_categories_size; ++__i)
227                     {
228                       __env = std::getenv(_S_categories[__i]);
229                       if (__env && std::strcmp(__env, "") != 0 
230                           && std::strcmp(__env, __res) != 0) 
231                         break;
232                     }
233         
234                 // If one is found, build the complete string of
235                 // the form LC_CTYPE=xxx;LC_NUMERIC=yyy; and so on...
236                 if (__i < _S_categories_size)
237                   {
238                     string __str;
239                     for (size_t __j = 0; __j < __i; ++__j)
240                       {
241                         __str += _S_categories[__j];
242                         __str += '=';
243                         __str += __res;
244                         __str += ';';
245                       }
246                     __str += _S_categories[__i];
247                     __str += '=';
248                     __str += __env;
249                     __str += ';';
250                     __i++;
251                     for (; __i < _S_categories_size; ++__i)
252                       {
253                         __env = std::getenv(_S_categories[__i]);
254                         if (!__env || std::strcmp(__env, "") == 0)
255                           {
256                             __str += _S_categories[__i];
257                             __str += '=';
258                             __str += __res;
259                             __str += ';';
260                           }
261                         else if (std::strcmp(__env, "C") == 0
262                                  || std::strcmp(__env, "POSIX") == 0)
263                           {
264                             __str += _S_categories[__i];
265                             __str += "=C;";
266                           }
267                         else
268                           {
269                             __str += _S_categories[__i];
270                             __str += '=';
271                             __str += __env;
272                             __str += ';';
273                           }
274                       }
275                     __str.erase(__str.end() - 1);
276                     _M_impl = new _Impl(__str.c_str(), 1);
277                   }
278                 // ... otherwise either an additional instance of
279                 // the "C" locale or LANG.
280                 else if (std::strcmp(__res, "C") == 0)
281                   (_M_impl = _S_classic)->_M_add_reference();
282                 else
283                   _M_impl = new _Impl(__res, 1);
284                 std::free(__res);
285               }
286           }
287       }
288     else
289       __throw_runtime_error("locale::locale NULL not valid");
290   }
291
292   locale::locale(const locale& __base, const char* __s, category __cat)
293   { 
294     // NB: There are complicated, yet more efficient ways to do
295     // this. Building up locales on a per-category way is tedious, so
296     // let's do it this way until people complain.
297     locale __add(__s);
298     _M_coalesce(__base, __add, __cat);
299   }
300
301   locale::locale(const locale& __base, const locale& __add, category __cat)
302   { _M_coalesce(__base, __add, __cat); }
303
304   locale::~locale() throw()
305   { _M_impl->_M_remove_reference(); }
306
307   bool
308   locale::operator==(const locale& __rhs) const throw()
309   {
310     string __name = this->name();
311     return (_M_impl == __rhs._M_impl 
312             || (__name != "*" && __name == __rhs.name()));
313   }
314
315   const locale&
316   locale::operator=(const locale& __other) throw()
317   {
318     __other._M_impl->_M_add_reference();
319     _M_impl->_M_remove_reference();
320     _M_impl = __other._M_impl;
321     return *this;
322   }
323
324   locale
325   locale::global(const locale& __other)
326   {
327     _S_initialize();
328
329     // XXX MT
330     _Impl* __old = _S_global;
331     __other._M_impl->_M_add_reference();
332     _S_global = __other._M_impl; 
333     if (_S_global->_M_check_same_name() 
334         && (std::strcmp(_S_global->_M_names[0], "*") != 0))
335       setlocale(LC_ALL, __other.name().c_str());
336
337     // Reference count sanity check: one reference removed for the
338     // subsition of __other locale, one added by return-by-value. Net
339     // difference: zero. When the returned locale object's destrutor
340     // is called, then the reference count is decremented and possibly
341     // destroyed.
342     return locale(__old);
343   }
344
345   string
346   locale::name() const
347   {
348     string __ret;
349     if (_M_impl->_M_check_same_name())
350       __ret = _M_impl->_M_names[0];
351     else
352       {
353         __ret += _S_categories[0];
354         __ret += '=';
355         __ret += _M_impl->_M_names[0]; 
356         for (size_t __i = 1; __i < _S_categories_size; ++__i)
357           {
358             __ret += ';';
359             __ret += _S_categories[__i];
360             __ret += '=';
361             __ret += _M_impl->_M_names[__i];
362           }
363       }
364     return __ret;
365   }
366
367   const locale&
368   locale::classic()
369   {
370     _S_initialize();
371     return c_locale;
372   }
373
374   void
375   locale::_S_initialize_once()
376   {
377     // 2 references.
378     // One reference for _S_classic, one for _S_global
379     _S_classic = new (&c_locale_impl) _Impl(2);
380     _S_global = _S_classic;         
381     new (&c_locale) locale(_S_classic);
382   }
383
384   void  
385   locale::_S_initialize()
386   {
387 #ifdef __GTHREADS
388     __gthread_once(&_S_once, _S_initialize_once);
389 #else
390     if (!_S_classic)
391       _S_initialize_once();
392 #endif
393   }
394
395   void
396   locale::_M_coalesce(const locale& __base, const locale& __add, 
397                       category __cat)
398   {
399     __cat = _S_normalize_category(__cat);  
400     _M_impl = new _Impl(*__base._M_impl, 1);  
401
402     try 
403       { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
404     catch (...) 
405       { 
406         _M_impl->_M_remove_reference(); 
407         __throw_exception_again;
408       }
409   }
410
411   locale::category
412   locale::_S_normalize_category(category __cat) 
413   {
414     int __ret = 0;
415     if (__cat == none || (__cat & all) && !(__cat & ~all))
416       __ret = __cat;
417     else
418       {
419         // NB: May be a C-style "LC_ALL" category; convert.
420         switch (__cat)
421           {
422           case LC_COLLATE:  
423             __ret = collate; 
424             break;
425           case LC_CTYPE:    
426             __ret = ctype;
427             break;
428           case LC_MONETARY: 
429             __ret = monetary;
430             break;
431           case LC_NUMERIC:  
432             __ret = numeric;
433             break;
434           case LC_TIME:     
435             __ret = time; 
436             break;
437 #ifdef _GLIBCXX_HAVE_LC_MESSAGES
438           case LC_MESSAGES: 
439             __ret = messages;
440             break;
441 #endif  
442           case LC_ALL:      
443             __ret = all;
444             break;
445           default:
446             __throw_runtime_error("locale::_S_normalize_category "
447                                   "category not found");
448           }
449       }
450     return __ret;
451   }
452
453   __c_locale locale::facet::_S_c_locale;
454
455   const char locale::facet::_S_c_name[2] = "C";
456
457 #ifdef __GTHREADS
458   __gthread_once_t locale::facet::_S_once = __GTHREAD_ONCE_INIT;
459 #endif
460
461   locale::facet::
462   ~facet() { }
463
464   void
465   locale::facet::_S_initialize_once()
466   {
467     // Initialize the underlying locale model.
468     _S_create_c_locale(_S_c_locale, _S_c_name);
469   }
470
471   __c_locale
472   locale::facet::_S_get_c_locale()
473   {
474 #ifdef __GHTREADS
475     __gthread_once(&_S_once, _S_initialize_once);
476 #else
477     if (!_S_c_locale)
478       _S_initialize_once();
479 #endif
480     return _S_c_locale;
481   }
482
483   // Definitions for static const data members of time_base.
484   template<> 
485     const char*
486     __timepunct_cache<char>::_S_timezones[14] =
487     { 
488       "GMT", "HST", "AKST", "PST", "MST", "CST", "EST", "AST", "NST", "CET", 
489       "IST", "EET", "CST", "JST"  
490     };
491  
492 #ifdef _GLIBCXX_USE_WCHAR_T
493   template<> 
494     const wchar_t*
495     __timepunct_cache<wchar_t>::_S_timezones[14] =
496     { 
497       L"GMT", L"HST", L"AKST", L"PST", L"MST", L"CST", L"EST", L"AST", 
498       L"NST", L"CET", L"IST", L"EET", L"CST", L"JST"  
499     };
500 #endif
501
502   // Definitions for static const data members of money_base.
503   const money_base::pattern 
504   money_base::_S_default_pattern =  { {symbol, sign, none, value} };
505
506   const char* __num_base::_S_atoms_in = "-+xX0123456789eEabcdfABCDF";
507   const char* __num_base::_S_atoms_out ="-+xX0123456789abcdef0123456789ABCDEF";
508
509   // _GLIBCXX_RESOLVE_LIB_DEFECTS
510   // According to the resolution of DR 231, about 22.2.2.2.2, p11,
511   // "str.precision() is specified in the conversion specification".
512   void
513   __num_base::_S_format_float(const ios_base& __io, char* __fptr, char __mod)
514   {
515     ios_base::fmtflags __flags = __io.flags();
516     *__fptr++ = '%';
517     // [22.2.2.2.2] Table 60
518     if (__flags & ios_base::showpos)
519       *__fptr++ = '+';
520     if (__flags & ios_base::showpoint)
521       *__fptr++ = '#';
522
523     // As per DR 231: _always_, not only when 
524     // __flags & ios_base::fixed || __prec > 0
525     *__fptr++ = '.';
526     *__fptr++ = '*';
527
528     if (__mod)
529       *__fptr++ = __mod;
530     ios_base::fmtflags __fltfield = __flags & ios_base::floatfield;
531     // [22.2.2.2.2] Table 58
532     if (__fltfield == ios_base::fixed)
533       *__fptr++ = 'f';
534     else if (__fltfield == ios_base::scientific)
535       *__fptr++ = (__flags & ios_base::uppercase) ? 'E' : 'e';
536     else
537       *__fptr++ = (__flags & ios_base::uppercase) ? 'G' : 'g';
538     *__fptr = '\0';
539   }
540 } // namespace std
541