OSDN Git Service

2003-01-23 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 <cassert>
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   // _GLIBCPP_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   const size_t                  locale::_S_extra_categories_size;
67
68   // Definitions for static const data members of locale::id
69   _Atomic_word locale::id::_S_highwater;  // init'd to 0 by linker
70
71   // Definitions for static const data members of locale::_Impl
72   const locale::id* const
73   locale::_Impl::_S_id_ctype[] =
74   {
75     &std::ctype<char>::id, 
76     &codecvt<char, char, mbstate_t>::id,
77 #ifdef _GLIBCPP_USE_WCHAR_T
78     &std::ctype<wchar_t>::id,
79     &codecvt<wchar_t, char, mbstate_t>::id,
80 #endif
81     0
82   };
83
84   const locale::id* const
85   locale::_Impl::_S_id_numeric[] =
86   {
87     &num_get<char>::id,  
88     &num_put<char>::id,  
89     &numpunct<char>::id, 
90 #ifdef _GLIBCPP_USE_WCHAR_T
91     &num_get<wchar_t>::id,
92     &num_put<wchar_t>::id,
93     &numpunct<wchar_t>::id,
94 #endif
95     0
96   };
97   
98   const locale::id* const
99   locale::_Impl::_S_id_collate[] =
100   {
101     &std::collate<char>::id,
102 #ifdef _GLIBCPP_USE_WCHAR_T
103     &std::collate<wchar_t>::id,
104 #endif
105     0
106   };
107
108   const locale::id* const
109   locale::_Impl::_S_id_time[] =
110   {
111     &__timepunct<char>::id, 
112     &time_get<char>::id, 
113     &time_put<char>::id, 
114 #ifdef _GLIBCPP_USE_WCHAR_T
115     &__timepunct<wchar_t>::id, 
116     &time_get<wchar_t>::id,
117     &time_put<wchar_t>::id,
118 #endif
119     0
120   };
121   
122   const locale::id* const
123   locale::_Impl::_S_id_monetary[] =
124   {
125     &money_get<char>::id,        
126     &money_put<char>::id,        
127     &moneypunct<char, false>::id, 
128     &moneypunct<char, true >::id, 
129 #ifdef _GLIBCPP_USE_WCHAR_T
130     &money_get<wchar_t>::id,
131     &money_put<wchar_t>::id,
132     &moneypunct<wchar_t, false>::id,
133     &moneypunct<wchar_t, true >::id,
134 #endif
135     0
136   };
137
138   const locale::id* const
139   locale::_Impl::_S_id_messages[] =
140   {
141     &std::messages<char>::id, 
142 #ifdef _GLIBCPP_USE_WCHAR_T
143     &std::messages<wchar_t>::id,
144 #endif
145     0
146   };
147   
148   const locale::id* const* const
149   locale::_Impl::_S_facet_categories[] =
150   {
151     // Order must match the decl order in class locale.
152     locale::_Impl::_S_id_ctype,
153     locale::_Impl::_S_id_numeric,
154     locale::_Impl::_S_id_collate,
155     locale::_Impl::_S_id_time,
156     locale::_Impl::_S_id_monetary,
157     locale::_Impl::_S_id_messages,
158     0
159   };
160
161   locale::locale() throw()
162   { 
163     _S_initialize(); 
164     (_M_impl = _S_global)->_M_add_reference(); 
165   }
166
167   locale::locale(const locale& __other) throw()
168   { (_M_impl = __other._M_impl)->_M_add_reference(); }
169
170   // This is used to initialize global and classic locales, and
171   // assumes that the _Impl objects are constructed correctly.
172   // The lack of a reference increment is intentional.
173   locale::locale(_Impl* __ip) throw() : _M_impl(__ip)
174   { }
175
176   locale::locale(const char* __s)
177   {
178     if (__s)
179       {
180         _S_initialize(); 
181         if (strcmp(__s, "C") == 0 || strcmp(__s, "POSIX") == 0)
182           (_M_impl = _S_classic)->_M_add_reference();
183         else if (strcmp(__s, "") != 0)
184           _M_impl = new _Impl(__s, 1);
185         else
186           {
187             // Get it from the environment.
188             char* __env = getenv("LC_ALL");
189             // If LC_ALL is set we are done.
190             if (__env && strcmp(__env, "") != 0)
191               {
192                 if (strcmp(__env, "C") == 0 || strcmp(__env, "POSIX") == 0)
193                   (_M_impl = _S_classic)->_M_add_reference();
194                 else
195                   _M_impl = new _Impl(__env, 1);
196               }
197             else
198               {
199                 char* __res;
200                 // LANG may set a default different from "C".
201                 char* __env = getenv("LANG");
202                 if (!__env || strcmp(__env, "") == 0 || strcmp(__env, "C") == 0
203                     || strcmp(__env, "POSIX") == 0)
204                   __res = strdup("C");
205                 else 
206                   __res = strdup(__env);
207                 
208                 // Scan the categories looking for the first one
209                 // different from LANG.
210                 size_t __i = 0;
211                 if (strcmp(__res, "C") == 0)
212                   for (; __i < _S_categories_size
213                          + _S_extra_categories_size; ++__i)
214                     {
215                       __env = getenv(_S_categories[__i]);
216                       if (__env && strcmp(__env, "") != 0 
217                           && strcmp(__env, "C") != 0 
218                           && strcmp(__env, "POSIX") != 0) 
219                         break;
220                     }
221                 else
222                   for (; __i < _S_categories_size
223                          + _S_extra_categories_size; ++__i)
224                     {
225                       __env = getenv(_S_categories[__i]);
226                       if (__env && strcmp(__env, "") != 0 
227                           && strcmp(__env, __res) != 0) 
228                         break;
229                     }
230         
231                 // If one is found, build the complete string of
232                 // the form LC_CTYPE=xxx;LC_NUMERIC=yyy; and so on...
233                 if (__i < _S_categories_size + _S_extra_categories_size)
234                   {
235                     string __str;
236                     for (size_t __j = 0; __j < __i; ++__j)
237                       {
238                         __str += _S_categories[__j];
239                         __str += '=';
240                         __str += __res;
241                         __str += ';';
242                       }
243                     __str += _S_categories[__i];
244                     __str += '=';
245                     __str += __env;
246                     __str += ';';
247                     __i++;
248                     for (; __i < _S_categories_size
249                            + _S_extra_categories_size; ++__i)
250                       {
251                         __env = getenv(_S_categories[__i]);
252                         if (!__env || strcmp(__env, "") == 0)
253                           {
254                             __str += _S_categories[__i];
255                             __str += '=';
256                             __str += __res;
257                             __str += ';';
258                           }
259                         else if (strcmp(__env, "C") == 0
260                                  || strcmp(__env, "POSIX") == 0)
261                           {
262                             __str += _S_categories[__i];
263                             __str += "=C;";
264                           }
265                         else
266                           {
267                             __str += _S_categories[__i];
268                             __str += '=';
269                             __str += __env;
270                             __str += ';';
271                           }
272                       }
273                     __str.erase(__str.end() - 1);
274                     _M_impl = new _Impl(__str.c_str(), 1);
275                   }
276                 // ... otherwise either an additional instance of
277                 // the "C" locale or LANG.
278                 else if (strcmp(__res, "C") == 0)
279                   (_M_impl = _S_classic)->_M_add_reference();
280                 else
281                   _M_impl = new _Impl(__res, 1);
282                 free(__res);
283               }
284           }
285       }
286     else
287       __throw_runtime_error("attempt to create locale from NULL name");
288   }
289
290   locale::locale(const locale& __base, const char* __s, category __cat)
291   { 
292     // NB: There are complicated, yet more efficient ways to do
293     // this. Building up locales on a per-category way is tedious, so
294     // let's do it this way until people complain.
295     locale __add(__s);
296     _M_coalesce(__base, __add, __cat);
297   }
298
299   locale::locale(const locale& __base, const locale& __add, category __cat)
300   { _M_coalesce(__base, __add, __cat); }
301
302   locale::~locale() throw()
303   { _M_impl->_M_remove_reference(); }
304
305   bool
306   locale::operator==(const locale& __rhs) const throw()
307   {
308     string __name = this->name();
309     return (_M_impl == __rhs._M_impl 
310             || (__name != "*" && __name == __rhs.name()));
311   }
312
313   const locale&
314   locale::operator=(const locale& __other) throw()
315   {
316     __other._M_impl->_M_add_reference();
317     _M_impl->_M_remove_reference();
318     _M_impl = __other._M_impl;
319     return *this;
320   }
321
322   locale
323   locale::global(const locale& __other)
324   {
325     // XXX MT
326     _S_initialize();
327     _Impl* __old = _S_global;
328     __other._M_impl->_M_add_reference();
329     _S_global = __other._M_impl; 
330     if (_S_global->_M_check_same_name() 
331         && (strcmp(_S_global->_M_names[0], "*") != 0))
332       setlocale(LC_ALL, __other.name().c_str());
333
334     // Reference count sanity check: one reference removed for the
335     // subsition of __other locale, one added by return-by-value. Net
336     // difference: zero. When the returned locale object's destrutor
337     // is called, then the reference count is decremented and possibly
338     // destroyed.
339     return locale(__old);
340   }
341
342   string
343   locale::name() const
344   {
345     string __ret;
346     if (_M_impl->_M_check_same_name())
347       __ret = _M_impl->_M_names[0];
348     else
349       {
350         __ret += _S_categories[0];
351         __ret += '=';
352         __ret += _M_impl->_M_names[0]; 
353         for (size_t __i = 1; 
354              __i < _S_categories_size + _S_extra_categories_size; 
355              ++__i)
356           {
357             __ret += ';';
358             __ret += _S_categories[__i];
359             __ret += '=';
360             __ret += _M_impl->_M_names[__i];
361           }
362       }
363     return __ret;
364   }
365
366   const locale&
367   locale::classic()
368   {
369     // Locking protocol: singleton-called-before-threading-starts
370     if (!_S_classic)
371       {
372         try 
373           {
374             // 26 Standard facets, 2 references.
375             // One reference for _S_classic, one for _S_global
376             _S_classic = new (&c_locale_impl) _Impl(0, 2, true);
377             _S_global = _S_classic;         
378             new (&c_locale) locale(_S_classic);
379           }
380         catch(...) 
381           {
382             // Just call destructor, so that locale_impl_c's memory is
383             // not deallocated via a call to delete.
384             if (_S_classic)
385               _S_classic->~_Impl();
386             _S_classic = _S_global = 0;
387             __throw_exception_again;
388           }
389       }
390     return c_locale;
391   }
392
393   void
394   locale::_M_coalesce(const locale& __base, const locale& __add, 
395                       category __cat)
396   {
397     __cat = _S_normalize_category(__cat);  
398     _M_impl = new _Impl(*__base._M_impl, 1);  
399
400     try 
401       { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
402     catch (...) 
403       { 
404         _M_impl->_M_remove_reference(); 
405         __throw_exception_again;
406       }
407   }
408
409   locale::category
410   locale::_S_normalize_category(category __cat) 
411   {
412     int __ret = 0;
413     if (__cat == none || (__cat & all) && !(__cat & ~all))
414       __ret = __cat;
415     else
416       {
417         // NB: May be a C-style "LC_ALL" category; convert.
418         switch (__cat)
419           {
420           case LC_COLLATE:  
421             __ret = collate; 
422             break;
423           case LC_CTYPE:    
424             __ret = ctype;
425             break;
426           case LC_MONETARY: 
427             __ret = monetary;
428             break;
429           case LC_NUMERIC:  
430             __ret = numeric;
431             break;
432           case LC_TIME:     
433             __ret = time; 
434             break;
435 #ifdef _GLIBCPP_HAVE_LC_MESSAGES
436           case LC_MESSAGES: 
437             __ret = messages;
438             break;
439 #endif  
440           case LC_ALL:      
441             __ret = all;
442             break;
443           default:
444             __throw_runtime_error("bad locale category");
445           }
446       }
447     return __ret;
448   }
449
450   __c_locale
451   locale::facet::_S_c_locale;
452   
453   char locale::facet::_S_c_name[2];
454
455   locale::facet::
456   ~facet() { }
457
458   locale::facet::
459   facet(size_t __refs) throw() : _M_references(__refs ? 1 : 0) 
460   { }
461
462   void  
463   locale::facet::
464   _M_add_reference() throw()
465   { __atomic_add(&_M_references, 1); }
466
467   void  
468   locale::facet::
469   _M_remove_reference() throw()
470   {
471     if (__exchange_and_add(&_M_references, -1) == 1)
472       {
473         try 
474           { delete this; }  
475         catch (...) 
476           { }
477       }
478   }
479   
480   locale::id::id() 
481   { }
482
483   // Definitions for static const data members of time_base
484   template<> 
485     const char*
486     __timepunct<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 _GLIBCPP_USE_WCHAR_T
493   template<> 
494     const wchar_t*
495     __timepunct<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[] = "0123456789eEabcdfABCDF";
507
508   bool
509   __num_base::_S_format_float(const ios_base& __io, char* __fptr, char __mod,
510                               streamsize __prec)
511   {
512     bool __incl_prec = false;
513     ios_base::fmtflags __flags = __io.flags();
514     *__fptr++ = '%';
515     // [22.2.2.2.2] Table 60
516     if (__flags & ios_base::showpos)
517       *__fptr++ = '+';
518     if (__flags & ios_base::showpoint)
519       *__fptr++ = '#';
520     // As per [22.2.2.2.2.11]
521     if (__flags & ios_base::fixed || __prec > 0)
522       {
523         *__fptr++ = '.';
524         *__fptr++ = '*';
525         __incl_prec = true;
526       }
527     if (__mod)
528       *__fptr++ = __mod;
529     ios_base::fmtflags __fltfield = __flags & ios_base::floatfield;
530     // [22.2.2.2.2] Table 58
531     if (__fltfield == ios_base::fixed)
532       *__fptr++ = 'f';
533     else if (__fltfield == ios_base::scientific)
534       *__fptr++ = (__flags & ios_base::uppercase) ? 'E' : 'e';
535     else
536       *__fptr++ = (__flags & ios_base::uppercase) ? 'G' : 'g';
537     *__fptr = '\0';
538     return __incl_prec;
539   }
540   
541   void
542   __num_base::_S_format_int(const ios_base& __io, char* __fptr, char __mod, 
543                             char __modl)
544   {
545     ios_base::fmtflags __flags = __io.flags();
546     *__fptr++ = '%';
547     // [22.2.2.2.2] Table 60
548     if (__flags & ios_base::showpos)
549       *__fptr++ = '+';
550     if (__flags & ios_base::showbase)
551       *__fptr++ = '#';
552     *__fptr++ = 'l';
553
554     // For long long types.
555     if (__modl)
556       *__fptr++ = __modl;
557
558     ios_base::fmtflags __bsefield = __flags & ios_base::basefield;
559     if (__bsefield == ios_base::hex)
560       *__fptr++ = (__flags & ios_base::uppercase) ? 'X' : 'x';
561     else if (__bsefield == ios_base::oct)
562       *__fptr++ = 'o';
563     else
564       *__fptr++ = __mod;
565     *__fptr = '\0';
566   }
567 } // namespace std
568