OSDN Git Service

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