OSDN Git Service

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