OSDN Git Service

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