OSDN Git Service

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