OSDN Git Service

2002-04-15 Benjamin Kosnik <bkoz@redhat.com>
[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 <limits>
35 #include <exception>
36 #include <locale>
37 #include <istream>
38 #include <ostream>
39 #include <bits/atomicity.h>
40
41 namespace std 
42 {
43   // Defined in globals.cc.
44   extern locale                 c_locale;
45   extern locale::_Impl          c_locale_impl;
46   extern locale::facet**        facet_vec;
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   locale::_Impl*                locale::_S_classic;
59   locale::_Impl*                locale::_S_global; 
60   const size_t                  locale::_S_num_categories;
61
62   // Definitions for locale::id of standard facets that are specialized.
63   locale::id ctype<char>::id;
64   locale::id codecvt<char, char, mbstate_t>::id;
65
66 #ifdef _GLIBCPP_USE_WCHAR_T  
67   locale::id ctype<wchar_t>::id;
68   locale::id codecvt<wchar_t, char, mbstate_t>::id;
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 _GLIBCPP_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 _GLIBCPP_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 _GLIBCPP_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 _GLIBCPP_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 _GLIBCPP_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 _GLIBCPP_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_monetary,
159     locale::_Impl::_S_id_time,
160     locale::_Impl::_S_id_messages,
161     0
162   };
163
164   locale::~locale() throw()
165   { _M_impl->_M_remove_reference(); }
166
167   void
168   locale::_M_coalesce(const locale& __base, const locale& __add, 
169                       category __cat)
170   {
171     __cat = _S_normalize_category(__cat);  
172     _M_impl = new _Impl(*__base._M_impl, 1);  
173
174     try 
175       { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
176     catch (...) 
177       { 
178         _M_impl->_M_remove_reference(); 
179         __throw_exception_again;
180       }
181   }
182
183   locale::locale() throw()
184   { 
185     _S_initialize(); 
186     (_M_impl = _S_global)->_M_add_reference(); 
187   }
188
189   locale::locale(const locale& __other) throw()
190   { (_M_impl = __other._M_impl)->_M_add_reference(); }
191
192   // This is used to initialize global and classic locales, and
193   // assumes that the _Impl objects are constructed correctly.
194   locale::locale(_Impl* __ip) throw() : _M_impl(__ip)
195   { }
196
197   locale::locale(const char* __s)
198   {
199     if (__s)
200       {
201         _S_initialize(); 
202         if (strcmp(__s, "C") == 0 || strcmp(__s, "POSIX") == 0)
203           (_M_impl = _S_classic)->_M_add_reference();
204         else if (strcmp(__s, "") == 0)
205           _M_impl = new _Impl(setlocale(LC_ALL, __s), 1);
206         else
207           _M_impl = new _Impl(__s, 1);
208       }
209     else
210       __throw_runtime_error("attempt to create locale from NULL name");
211   }
212
213   locale::locale(const locale& __base, const char* __s, category __cat)
214   { 
215     // NB: There are complicated, yet more efficient ways to do
216     // this. Building up locales on a per-category way is tedious, so
217     // let's do it this way until people complain.
218     locale __add(__s);
219     _M_coalesce(__base, __add, __cat);
220   }
221
222   locale::locale(const locale& __base, const locale& __add, category __cat)
223   { _M_coalesce(__base, __add, __cat); }
224
225   bool
226   locale::operator==(const locale& __rhs) const throw()
227   {
228     string __name = this->name();
229     return (_M_impl == __rhs._M_impl || (__name != "*" && __name == __rhs.name()));
230   }
231
232   const locale&
233   locale::operator=(const locale& __other) throw()
234   {
235     __other._M_impl->_M_add_reference();
236     _M_impl->_M_remove_reference();
237     _M_impl = __other._M_impl;
238     return *this;
239   }
240
241   locale
242   locale::global(const locale& __other)
243   {
244     // XXX MT
245     _S_initialize();
246     _Impl* __old = _S_global;
247     __other._M_impl->_M_add_reference();
248     _S_global = __other._M_impl; 
249     if (_S_global->_M_check_same_name() 
250         && (strcmp(_S_global->_M_names[0], "*") != 0))
251       setlocale(LC_ALL, __other.name().c_str());
252
253     // Reference count sanity check: one reference removed for the
254     // subsition of __other locale, one added by return-by-value. Net
255     // difference: zero. When the returned locale object's destrutor
256     // is called, then the reference count is decremented and possibly
257     // destroyed.
258     return locale(__old);
259   }
260
261   string
262   locale::name() const
263   {
264     // Need some kind of separator character. This one was pretty much
265     // arbitrarily chosen as to not conflict with glibc locales: the
266     // exact formatting is not set in stone.
267     const char __separator = '|';
268
269     string __ret;
270     if (_M_impl->_M_check_same_name())
271       __ret = _M_impl->_M_names[0];
272     else
273       {
274         for (size_t i = 0; i < _S_num_categories; ++i)
275           {
276             __ret += __separator;
277             __ret += _M_impl->_M_names[i];
278           }
279       }
280     return __ret;
281   }
282
283   const locale&
284   locale::classic()
285   {
286     static _STL_mutex_lock __lock __STL_MUTEX_INITIALIZER;
287     _STL_auto_lock __auto(__lock);
288
289     if (!_S_classic)
290       {
291         try 
292           {
293             // 26 Standard facets, 2 references.
294             // One reference for _M_classic, one for _M_global
295             facet** f = new(&facet_vec) facet*[_GLIBCPP_NUM_FACETS];
296             for (size_t __i = 0; __i < _GLIBCPP_NUM_FACETS; ++__i)
297               f[__i] = 0;
298
299             _S_classic = new (&c_locale_impl) _Impl(f, 2, true);
300             _S_global = _S_classic;         
301             new (&c_locale) locale(_S_classic);
302           }
303         catch(...) 
304           {
305             // Just call destructor, so that locale_impl_c's memory is
306             // not deallocated via a call to delete.
307             if (_S_classic)
308               _S_classic->~_Impl();
309             _S_classic = _S_global = 0;
310             __throw_exception_again;
311           }
312       }
313     return c_locale;
314   }
315
316   locale::category
317   locale::_S_normalize_category(category __cat) 
318   {
319     int __ret = 0;
320     if (__cat == none || (__cat & all) && !(__cat & ~all))
321       __ret = __cat;
322     else
323       {
324         // NB: May be a C-style "LC_ALL" category; convert.
325         switch (__cat)
326           {
327           case LC_COLLATE:  
328             __ret = collate; 
329             break;
330           case LC_CTYPE:    
331             __ret = ctype;
332             break;
333           case LC_MONETARY: 
334             __ret = monetary;
335             break;
336           case LC_NUMERIC:  
337             __ret = numeric;
338             break;
339           case LC_TIME:     
340             __ret = time; 
341             break;
342 #ifdef _GLIBCPP_HAVE_LC_MESSAGES
343           case LC_MESSAGES: 
344             __ret = messages;
345             break;
346 #endif  
347           case LC_ALL:      
348             __ret = all;
349             break;
350           default:
351             __throw_runtime_error("bad locale category");
352           }
353       }
354     return __ret;
355   }
356
357   __c_locale
358   locale::facet::_S_c_locale;
359   
360   locale::facet::
361   ~facet() { }
362
363   locale::facet::
364   facet(size_t __refs) throw() : _M_references(__refs) 
365   { 
366     if (!_S_c_locale)
367       _S_create_c_locale(_S_c_locale, "C");
368   }
369
370   void  
371   locale::facet::
372   _M_add_reference() throw()
373   { __atomic_add(&_M_references, 1); }
374
375   void  
376   locale::facet::
377   _M_remove_reference() throw()
378   {
379     if (__exchange_and_add(&_M_references, -1) == 0)
380       {
381         try 
382           { delete this; }  
383         catch (...) 
384           { }
385       }
386   }
387   
388   locale::id::id() 
389   { }
390
391   // Definitions for static const data members of ctype_base.
392   const ctype_base::mask ctype_base::space;
393   const ctype_base::mask ctype_base::print;
394   const ctype_base::mask ctype_base::cntrl;
395   const ctype_base::mask ctype_base::upper;
396   const ctype_base::mask ctype_base::lower;
397   const ctype_base::mask ctype_base::alpha;
398   const ctype_base::mask ctype_base::digit;
399   const ctype_base::mask ctype_base::punct;
400   const ctype_base::mask ctype_base::xdigit;
401   const ctype_base::mask ctype_base::alnum;
402   const ctype_base::mask ctype_base::graph;
403
404   // Platform-specific initialization code for ctype tables.
405   #include <bits/ctype_noninline.h>
406
407   const size_t ctype<char>::table_size;
408
409   ctype<char>::~ctype()
410   { 
411     if (_M_c_locale_ctype != _S_c_locale)
412       _S_destroy_c_locale(_M_c_locale_ctype);
413     if (_M_del) 
414       delete[] this->table(); 
415   }
416
417   // These are dummy placeholders as these virtual functions are never called.
418   bool 
419   ctype<char>::do_is(mask, char_type) const 
420   { return false; }
421   
422   const char*
423   ctype<char>::do_is(const char_type* __c, const char_type*, mask*) const 
424   { return __c; }
425   
426   const char*
427   ctype<char>::do_scan_is(mask, const char_type* __c, const char_type*) const 
428   { return __c; }
429
430   const char* 
431   ctype<char>::do_scan_not(mask, const char_type* __c, const char_type*) const
432   { return __c; }
433
434   char
435   ctype<char>::do_widen(char __c) const
436   { return __c; }
437   
438   const char* 
439   ctype<char>::do_widen(const char* __lo, const char* __hi, char* __dest) const
440   {
441     memcpy(__dest, __lo, __hi - __lo);
442     return __hi;
443   }
444   
445   char
446   ctype<char>::do_narrow(char __c, char /*__dfault*/) const
447   { return __c; }
448   
449   const char* 
450   ctype<char>::do_narrow(const char* __lo, const char* __hi, 
451                          char /*__dfault*/, char* __dest) const
452   {
453     memcpy(__dest, __lo, __hi - __lo);
454     return __hi;
455   }
456
457 #ifdef _GLIBCPP_USE_WCHAR_T
458   ctype<wchar_t>::ctype(size_t __refs) 
459   : __ctype_abstract_base<wchar_t>(__refs)
460   { _M_c_locale_ctype = _S_c_locale; }
461
462   ctype<wchar_t>::ctype(__c_locale __cloc, size_t __refs) 
463   : __ctype_abstract_base<wchar_t>(__refs) 
464   { _M_c_locale_ctype = _S_clone_c_locale(__cloc); }
465
466   ctype<wchar_t>::~ctype() 
467   { 
468     if (_M_c_locale_ctype != _S_c_locale)
469       _S_destroy_c_locale(_M_c_locale_ctype); 
470   }
471
472   template<>
473     ctype_byname<wchar_t>::ctype_byname(const char* __s, size_t __refs)
474     : ctype<wchar_t>(__refs) 
475     {   
476       if (_M_c_locale_ctype != _S_c_locale)
477         _S_destroy_c_locale(_M_c_locale_ctype);
478       _S_create_c_locale(_M_c_locale_ctype, __s); 
479     }
480 #endif
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   template<>
506     const ctype<char>&
507     use_facet<ctype<char> >(const locale& __loc)
508     {
509       size_t __i = ctype<char>::id._M_id();
510       const locale::_Impl* __tmp = __loc._M_impl;
511       return static_cast<const ctype<char>&>(*(__tmp->_M_facets[__i]));
512     }
513
514 #ifdef _GLIBCPP_USE_WCHAR_T
515   template<>
516     const ctype<wchar_t>&
517     use_facet<ctype<wchar_t> >(const locale& __loc)
518     {
519       size_t __i = ctype<wchar_t>::id._M_id();
520       const locale::_Impl* __tmp = __loc._M_impl;
521       return static_cast<const ctype<wchar_t>&>(*(__tmp->_M_facets[__i]));
522     }
523 #endif
524
525   const char __num_base::_S_atoms[] = "0123456789eEabcdfABCDF";
526
527   bool
528   __num_base::_S_format_float(const ios_base& __io, char* __fptr, char __mod,
529                               streamsize __prec)
530   {
531     bool __incl_prec = false;
532     ios_base::fmtflags __flags = __io.flags();
533     *__fptr++ = '%';
534     // [22.2.2.2.2] Table 60
535     if (__flags & ios_base::showpos)
536       *__fptr++ = '+';
537     if (__flags & ios_base::showpoint)
538       *__fptr++ = '#';
539     // As per [22.2.2.2.2.11]
540     if (__flags & ios_base::fixed || __prec > 0)
541       {
542         *__fptr++ = '.';
543         *__fptr++ = '*';
544         __incl_prec = true;
545       }
546     if (__mod)
547       *__fptr++ = __mod;
548     ios_base::fmtflags __fltfield = __flags & ios_base::floatfield;
549     // [22.2.2.2.2] Table 58
550     if (__fltfield == ios_base::fixed)
551       *__fptr++ = 'f';
552     else if (__fltfield == ios_base::scientific)
553       *__fptr++ = (__flags & ios_base::uppercase) ? 'E' : 'e';
554     else
555       *__fptr++ = (__flags & ios_base::uppercase) ? 'G' : 'g';
556     *__fptr = '\0';
557     return __incl_prec;
558   }
559   
560   void
561   __num_base::_S_format_int(const ios_base& __io, char* __fptr, char __mod, 
562                             char __modl)
563   {
564     ios_base::fmtflags __flags = __io.flags();
565     *__fptr++ = '%';
566     // [22.2.2.2.2] Table 60
567     if (__flags & ios_base::showpos)
568       *__fptr++ = '+';
569     if (__flags & ios_base::showbase)
570       *__fptr++ = '#';
571     *__fptr++ = 'l';
572
573     // For long long types.
574     if (__modl)
575       *__fptr++ = __modl;
576
577     ios_base::fmtflags __bsefield = __flags & ios_base::basefield;
578     if (__bsefield == ios_base::hex)
579       *__fptr++ = (__flags & ios_base::uppercase) ? 'X' : 'x';
580     else if (__bsefield == ios_base::oct)
581       *__fptr++ = 'o';
582     else
583       *__fptr++ = __mod;
584     *__fptr = '\0';
585   }
586 } // namespace std
587