OSDN Git Service

2002-01-04 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / src / locale.cc
1 // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library.  This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 2, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING.  If not, write to the Free
16 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 // USA.
18
19 // As a special exception, you may use this file as part of a free software
20 // library without restriction.  Specifically, if other files instantiate
21 // templates or use macros or inline functions from this file, or you compile
22 // this file and link it with other files to produce an executable, this
23 // file does not by itself cause the resulting executable to be covered by
24 // the GNU General Public License.  This exception does not however
25 // invalidate any other reasons why the executable file might be covered by
26 // the GNU General Public License.
27
28 #include <clocale>
29 #include <cstring>
30 #include <cassert>
31 #include <cctype>
32 #include <limits>
33 #include <exception>
34 #include <stdexcept>
35 #include <locale>
36 #include <istream>
37 #include <ostream>
38 #include <vector>
39 #include <memory>      // for auto_ptr
40 #ifdef _GLIBCPP_USE_WCHAR_T  
41 # include <cwctype>     // for towupper, etc.
42 #endif
43
44 namespace std 
45 {
46   // Defined in globals.cc.
47   extern locale::_Impl locale_impl_c;
48   extern locale locale_c;
49
50   // Definitions for static const data members of locale.
51   const locale::category        locale::none;
52   const locale::category        locale::ctype;
53   const locale::category        locale::numeric;
54   const locale::category        locale::collate;
55   const locale::category        locale::time;
56   const locale::category        locale::monetary;
57   const locale::category        locale::messages;
58   const locale::category        locale::all;
59
60   locale::_Impl*                locale::_S_classic;
61   locale::_Impl*                locale::_S_global; 
62   const size_t                  locale::_S_num_categories;
63   const size_t                  locale::_S_num_facets;
64
65   // Definitions for locale::id of standard facets. 
66   locale::id ctype<char>::id;
67   locale::id codecvt<char, char, mbstate_t>::id;
68
69 #ifdef _GLIBCPP_USE_WCHAR_T  
70   locale::id ctype<wchar_t>::id;
71   locale::id codecvt<wchar_t, char, mbstate_t>::id;
72 #endif
73
74   // Definitions for static const data members of locale::id
75   size_t locale::id::_S_highwater;  // init'd to 0 by linker
76
77   // Definitions for static const data members of locale::_Impl
78   const locale::id* const
79   locale::_Impl::_S_id_ctype[] =
80   {
81     &std::ctype<char>::id, 
82     &codecvt<char, char, mbstate_t>::id,
83 #ifdef _GLIBCPP_USE_WCHAR_T
84     &std::ctype<wchar_t>::id,
85     &codecvt<wchar_t, char, mbstate_t>::id,
86 #endif
87     0
88   };
89
90   const locale::id* const
91   locale::_Impl::_S_id_numeric[] =
92   {
93     &num_get<char>::id,  
94     &num_put<char>::id,  
95     &numpunct<char>::id, 
96 #ifdef _GLIBCPP_USE_WCHAR_T
97     &num_get<wchar_t>::id,
98     &num_put<wchar_t>::id,
99     &numpunct<wchar_t>::id,
100 #endif
101     0
102   };
103   
104   const locale::id* const
105   locale::_Impl::_S_id_collate[] =
106   {
107     &std::collate<char>::id,
108 #ifdef _GLIBCPP_USE_WCHAR_T
109     &std::collate<wchar_t>::id,
110 #endif
111     0
112   };
113
114   const locale::id* const
115   locale::_Impl::_S_id_time[] =
116   {
117     &__timepunct<char>::id, 
118     &time_get<char>::id, 
119     &time_put<char>::id, 
120 #ifdef _GLIBCPP_USE_WCHAR_T
121     &__timepunct<wchar_t>::id, 
122     &time_get<wchar_t>::id,
123     &time_put<wchar_t>::id,
124 #endif
125     0
126   };
127   
128   const locale::id* const
129   locale::_Impl::_S_id_monetary[] =
130   {
131     &money_get<char>::id,        
132     &money_put<char>::id,        
133     &moneypunct<char, false>::id, 
134     &moneypunct<char, true >::id, 
135 #ifdef _GLIBCPP_USE_WCHAR_T
136     &money_get<wchar_t>::id,
137     &money_put<wchar_t>::id,
138     &moneypunct<wchar_t, false>::id,
139     &moneypunct<wchar_t, true >::id,
140 #endif
141     0
142   };
143
144   const locale::id* const
145   locale::_Impl::_S_id_messages[] =
146   {
147     &std::messages<char>::id, 
148 #ifdef _GLIBCPP_USE_WCHAR_T
149     &std::messages<wchar_t>::id,
150 #endif
151     0
152   };
153   
154   const locale::id* const* const
155   locale::_Impl::_S_facet_categories[] =
156   {
157     // Order must match the decl order in class locale.
158     locale::_Impl::_S_id_ctype,
159     locale::_Impl::_S_id_numeric,
160     locale::_Impl::_S_id_collate,
161     locale::_Impl::_S_id_time,
162     locale::_Impl::_S_id_monetary,
163     locale::_Impl::_S_id_messages,
164     0
165   };
166
167   locale::~locale() throw()
168   { _M_impl->_M_remove_reference(); }
169
170   void
171   locale::_M_coalesce(const locale& __base, const locale& __add, 
172                       category __cat)
173   {
174     __cat = _S_normalize_category(__cat);  
175     _M_impl = new _Impl(*__base._M_impl, 1);  
176
177     try 
178       { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
179     catch (...) 
180       { 
181         _M_impl->_M_remove_reference(); 
182         __throw_exception_again;
183       }
184   }
185
186   locale::locale() throw()
187   { 
188     _S_initialize(); 
189     (_M_impl = _S_global)->_M_add_reference(); 
190   } // XXX MT
191
192   locale::locale(const locale& __other) throw()
193   { (_M_impl = __other._M_impl)->_M_add_reference(); }
194
195   // This is used to initialize global and classic locales, and
196   // assumes that the _Impl objects are constructed correctly.
197   locale::locale(_Impl* __ip) throw() : _M_impl(__ip)
198   { }
199
200   locale::locale(const char* __s)
201   {
202     if (__s)
203       {
204         _S_initialize(); 
205         if (strcmp(__s, "C") == 0 || strcmp(__s, "POSIX") == 0)
206           (_M_impl = _S_classic)->_M_add_reference();
207         else if (strcmp(__s, "") == 0)
208           _M_impl = new _Impl(setlocale(LC_ALL, __s), 1);
209         else
210           _M_impl = new _Impl(__s, 1);
211       }
212     else
213       __throw_runtime_error("attempt to create locale from NULL name");
214   }
215
216   locale::locale(const locale& __base, const char* __s, category __cat)
217   { 
218     // NB: There are complicated, yet more efficient ways to do
219     // this. Building up locales on a per-category way is tedious, so
220     // let's do it this way until people complain.
221     locale __add(__s);
222     _M_coalesce(__base, __add, __cat);
223   }
224
225   locale::locale(const locale& __base, const locale& __add, category __cat)
226   { _M_coalesce(__base, __add, __cat); }
227
228   bool
229   locale::operator==(const locale& __rhs) const throw()
230   {
231     string __name = this->name();
232     return (_M_impl == __rhs._M_impl 
233             || (__name != "*" && __name == __rhs.name()));
234   }
235
236   const locale&
237   locale::operator=(const locale& __other) throw()
238   {
239     __other._M_impl->_M_add_reference();
240     _M_impl->_M_remove_reference();
241     _M_impl = __other._M_impl;
242     return *this;
243   }
244
245   locale
246   locale::global(const locale& __other)
247   {
248     // XXX MT
249     _S_initialize();
250     _Impl* __old = _S_global;
251     __other._M_impl->_M_add_reference();
252     _S_global = __other._M_impl; 
253     if (_S_global->_M_check_same_name() && _S_global->_M_names[0] != "*")
254       setlocale(LC_ALL, __other.name().c_str());
255
256     // Reference count sanity check: one reference removed for the
257     // subsition of __other locale, one added by return-by-value. Net
258     // difference: zero. When the returned locale object's destrutor
259     // is called, then the reference count is decremented and possibly
260     // destroyed.
261     return locale(__old);
262   }
263
264   string
265   locale::name() const
266   {
267     string __ret;
268     // Need some kind of separator character. This one was pretty much
269     // arbitrarily chosen as to not conflict with glibc locales: the
270     // exact formatting is not set in stone.
271     const char __separator = '|';
272
273     if (_M_impl->_M_check_same_name())
274       __ret = _M_impl->_M_names[0];
275     else
276       {
277         for (size_t i = 0; i < _S_num_categories; ++i)
278           __ret += __separator + _M_impl->_M_names[i];
279       }
280     return __ret;
281   }
282
283   locale const&
284   locale::classic()
285   {
286     // XXX MT
287     if (!_S_classic)
288       {
289         try 
290           {
291             // 26 Standard facets, 2 references.
292             // One reference for _M_classic, one for _M_global
293             _S_classic = new (&locale_impl_c) _Impl("C", 2);
294             _S_global = _S_classic;         
295             new (&locale_c) locale(_S_classic);
296           }
297         catch(...) 
298           {
299             // Just call destructor, so that locale_impl_c's memory is
300             // not deallocated via a call to delete.
301             if (_S_classic)
302               _S_classic->~_Impl();
303             _S_classic = _S_global = 0;
304             __throw_exception_again;
305           }
306       }
307     return locale_c;
308   }
309
310   locale::category
311   locale::_S_normalize_category(category __cat) 
312   {
313     int __ret = 0;
314     if (__cat == none || (__cat & all) && !(__cat & ~all))
315       __ret = __cat;
316     else
317       {
318         // NB: May be a C-style "LC_ALL" category; convert.
319         switch (__cat)
320           {
321           case LC_COLLATE:  
322             __ret = collate; 
323             break;
324           case LC_CTYPE:    
325             __ret = ctype;
326             break;
327           case LC_MONETARY: 
328             __ret = monetary;
329             break;
330           case LC_NUMERIC:  
331             __ret = numeric;
332             break;
333           case LC_TIME:     
334             __ret = time; 
335             break;
336 #ifdef _GLIBCPP_HAVE_LC_MESSAGES
337           case LC_MESSAGES: 
338             __ret = messages;
339             break;
340 #endif  
341           case LC_ALL:      
342             __ret = all;
343             break;
344           default:
345             __throw_runtime_error("bad locale category");
346           }
347       }
348     return __ret;
349   }
350
351   __c_locale
352   locale::facet::_S_c_locale;
353   
354   locale::facet::
355   ~facet() { }
356
357   locale::facet::
358   facet(size_t __refs) throw() : _M_references(__refs) 
359   { 
360     if (!_S_c_locale)
361       _S_create_c_locale(_S_c_locale, "C");
362   }
363
364   void  
365   locale::facet::
366   _M_add_reference() throw()
367   { ++_M_references; }  // XXX MT
368
369   void  
370   locale::facet::
371   _M_remove_reference() throw()
372   {
373     if (_M_references-- == 0)
374       {
375         try 
376           { delete this; }  
377         catch (...) 
378           { }
379       }
380   }
381   
382   locale::id::id() { }
383
384   // Definitions for static const data members of ctype_base.
385   const ctype_base::mask ctype_base::space;
386   const ctype_base::mask ctype_base::print;
387   const ctype_base::mask ctype_base::cntrl;
388   const ctype_base::mask ctype_base::upper;
389   const ctype_base::mask ctype_base::lower;
390   const ctype_base::mask ctype_base::alpha;
391   const ctype_base::mask ctype_base::digit;
392   const ctype_base::mask ctype_base::punct;
393   const ctype_base::mask ctype_base::xdigit;
394   const ctype_base::mask ctype_base::alnum;
395   const ctype_base::mask ctype_base::graph;
396
397   // Platform-specific initialization code for ctype tables.
398   #include <bits/ctype_noninline.h>
399
400   const size_t ctype<char>::table_size;
401
402   ctype<char>::~ctype()
403   { 
404     if (_M_c_locale_ctype)
405       _S_destroy_c_locale(_M_c_locale_ctype);
406     if (_M_del) 
407       delete[] this->table(); 
408   }
409
410   // These are dummy placeholders as these virtual functions are never called.
411   bool 
412   ctype<char>::do_is(mask, char_type) const 
413   { return false; }
414   
415   const char*
416   ctype<char>::do_is(const char_type* __c, const char_type*, mask*) const 
417   { return __c; }
418   
419   const char*
420   ctype<char>::do_scan_is(mask, const char_type* __c, const char_type*) const 
421   { return __c; }
422
423   const char* 
424   ctype<char>::do_scan_not(mask, const char_type* __c, const char_type*) const
425   { return __c; }
426
427   char
428   ctype<char>::do_widen(char __c) const
429   { return __c; }
430   
431   const char* 
432   ctype<char>::do_widen(const char* __lo, const char* __hi, char* __dest) const
433   {
434     memcpy(__dest, __lo, __hi - __lo);
435     return __hi;
436   }
437   
438   char
439   ctype<char>::do_narrow(char __c, char /*__dfault*/) const
440   { return __c; }
441   
442   const char* 
443   ctype<char>::do_narrow(const char* __lo, const char* __hi, 
444                          char /*__dfault*/, char* __dest) const
445   {
446     memcpy(__dest, __lo, __hi - __lo);
447     return __hi;
448   }
449
450 #ifdef _GLIBCPP_USE_WCHAR_T
451   ctype<wchar_t>::ctype(size_t __refs) 
452   : __ctype_abstract_base<wchar_t>(__refs)
453   { _M_c_locale_ctype = _S_clone_c_locale(_S_c_locale); }
454
455   ctype<wchar_t>::ctype(__c_locale __cloc, size_t __refs) 
456   : __ctype_abstract_base<wchar_t>(__refs) 
457   { _M_c_locale_ctype = _S_clone_c_locale(__cloc); }
458
459   ctype<wchar_t>::~ctype() 
460   { _S_destroy_c_locale(_M_c_locale_ctype); }
461
462   template<>
463     ctype_byname<wchar_t>::ctype_byname(const char* __s, size_t __refs)
464     : ctype<wchar_t>(__refs) 
465     {   
466       _S_destroy_c_locale(_M_c_locale_ctype);
467       _S_create_c_locale(_M_c_locale_ctype, __s); 
468     }
469 #endif
470
471   // Definitions for static const data members of time_base
472   template<> 
473     const char*
474     __timepunct<char>::_S_timezones[14] =
475     { 
476       "GMT", "HST", "AKST", "PST", "MST", "CST", "EST", "AST", "NST", "CET", 
477       "IST", "EET", "CST", "JST"  
478     };
479  
480 #ifdef _GLIBCPP_USE_WCHAR_T
481   template<> 
482     const wchar_t*
483     __timepunct<wchar_t>::_S_timezones[14] =
484     { 
485       L"GMT", L"HST", L"AKST", L"PST", L"MST", L"CST", L"EST", L"AST", 
486       L"NST", L"CET", L"IST", L"EET", L"CST", L"JST"  
487     };
488 #endif
489
490   // Definitions for static const data members of money_base
491   const money_base::pattern 
492   money_base::_S_default_pattern =  { {symbol, sign, none, value} };
493
494   template<>
495     const ctype<char>&
496     use_facet<ctype<char> >(const locale& __loc)
497     {
498       size_t __i = ctype<char>::id._M_index;
499       const locale::_Impl* __tmp = __loc._M_impl;
500       return static_cast<const ctype<char>&>(* (*(__tmp->_M_facets))[__i]);
501     }
502
503 #ifdef _GLIBCPP_USE_WCHAR_T
504   template<>
505     const ctype<wchar_t>&
506     use_facet<ctype<wchar_t> >(const locale& __loc)
507     {
508       size_t __i = ctype<wchar_t>::id._M_index;
509       const locale::_Impl* __tmp = __loc._M_impl;
510       return static_cast<const ctype<wchar_t>&>(* (*(__tmp->_M_facets))[__i]);
511     }
512 #endif
513
514
515   const char __num_base::_S_atoms[] = "0123456789eEabcdfABCDF";
516
517   const double __num_base::_S_scale_hex = log(10.0)/log(16.0);
518
519   const double __num_base::_S_scale_oct = log(10.0)/log(8.0);
520
521   bool
522   __num_base::_S_format_float(const ios_base& __io, char* __fptr, char __mod,
523                               streamsize __prec)
524   {
525     bool __incl_prec = false;
526     ios_base::fmtflags __flags = __io.flags();
527     *__fptr++ = '%';
528     // [22.2.2.2.2] Table 60
529     if (__flags & ios_base::showpos)
530       *__fptr++ = '+';
531     if (__flags & ios_base::showpoint)
532       *__fptr++ = '#';
533     // As per [22.2.2.2.2.11]
534     if (__flags & ios_base::fixed || __prec > 0)
535       {
536         *__fptr++ = '.';
537         *__fptr++ = '*';
538         __incl_prec = true;
539       }
540     if (__mod)
541       *__fptr++ = __mod;
542     ios_base::fmtflags __fltfield = __flags & ios_base::floatfield;
543     // [22.2.2.2.2] Table 58
544     if (__fltfield == ios_base::fixed)
545       *__fptr++ = 'f';
546     else if (__fltfield == ios_base::scientific)
547       *__fptr++ = (__flags & ios_base::uppercase) ? 'E' : 'e';
548     else
549       *__fptr++ = (__flags & ios_base::uppercase) ? 'G' : 'g';
550     *__fptr = '\0';
551     return __incl_prec;
552   }
553   
554   void
555   __num_base::_S_format_int(const ios_base& __io, char* __fptr, char __mod, 
556                             char __modl)
557   {
558     ios_base::fmtflags __flags = __io.flags();
559     *__fptr++ = '%';
560     // [22.2.2.2.2] Table 60
561     if (__flags & ios_base::showpos)
562       *__fptr++ = '+';
563     if (__flags & ios_base::showbase)
564       *__fptr++ = '#';
565     *__fptr++ = 'l';
566
567     // For long long types.
568     if (__modl)
569       *__fptr++ = __modl;
570
571     ios_base::fmtflags __bsefield = __flags & ios_base::basefield;
572     if (__bsefield == ios_base::hex)
573       *__fptr++ = (__flags & ios_base::uppercase) ? 'X' : 'x';
574     else if (__bsefield == ios_base::oct)
575       *__fptr++ = 'o';
576     else
577       *__fptr++ = __mod;
578     *__fptr = '\0';
579   }
580   
581   template<>
582     moneypunct_byname<char, false>::moneypunct_byname(const char*, 
583                                                       size_t __refs)
584     : moneypunct<char, false>(__refs) { }
585   
586   template<>
587     moneypunct_byname<char, true>::moneypunct_byname(const char*, 
588                                                      size_t __refs)
589     : moneypunct<char, true>(__refs) { }
590 } // namespace std