OSDN Git Service

2001-12-06 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 <bits/std_clocale.h>
29 #include <bits/std_cstring.h>
30 #include <bits/std_cassert.h>
31 #include <bits/std_cctype.h>
32 #include <bits/std_limits.h>
33 #include <exception>
34 #include <bits/std_stdexcept.h>
35 #include <bits/std_locale.h>
36 #include <bits/std_istream.h>
37 #include <bits/std_ostream.h>
38 #include <bits/std_vector.h>
39 #include <bits/std_memory.h>      // for auto_ptr
40 #ifdef _GLIBCPP_USE_WCHAR_T  
41 # include <bits/std_cwctype.h>     // 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   const char __num_base::_S_atoms[] = "0123456789eEabcdfABCDF";
78
79   // Definitions for static const data members of locale::_Impl
80   const locale::id* const
81   locale::_Impl::_S_id_ctype[] =
82   {
83     &std::ctype<char>::id, 
84     &codecvt<char, char, mbstate_t>::id,
85 #ifdef _GLIBCPP_USE_WCHAR_T
86     &std::ctype<wchar_t>::id,
87     &codecvt<wchar_t, char, mbstate_t>::id,
88 #endif
89     0
90   };
91
92   const locale::id* const
93   locale::_Impl::_S_id_numeric[] =
94   {
95     &num_get<char>::id,  
96     &num_put<char>::id,  
97     &numpunct<char>::id, 
98 #ifdef _GLIBCPP_USE_WCHAR_T
99     &num_get<wchar_t>::id,
100     &num_put<wchar_t>::id,
101     &numpunct<wchar_t>::id,
102 #endif
103     0
104   };
105   
106   const locale::id* const
107   locale::_Impl::_S_id_collate[] =
108   {
109     &std::collate<char>::id,
110 #ifdef _GLIBCPP_USE_WCHAR_T
111     &std::collate<wchar_t>::id,
112 #endif
113     0
114   };
115
116   const locale::id* const
117   locale::_Impl::_S_id_time[] =
118   {
119     &__timepunct<char>::id, 
120     &time_get<char>::id, 
121     &time_put<char>::id, 
122 #ifdef _GLIBCPP_USE_WCHAR_T
123     &__timepunct<wchar_t>::id, 
124     &time_get<wchar_t>::id,
125     &time_put<wchar_t>::id,
126 #endif
127     0
128   };
129   
130   const locale::id* const
131   locale::_Impl::_S_id_monetary[] =
132   {
133     &money_get<char>::id,        
134     &money_put<char>::id,        
135     &moneypunct<char, false>::id, 
136     &moneypunct<char, true >::id, 
137 #ifdef _GLIBCPP_USE_WCHAR_T
138     &money_get<wchar_t>::id,
139     &money_put<wchar_t>::id,
140     &moneypunct<wchar_t, false>::id,
141     &moneypunct<wchar_t, true >::id,
142 #endif
143     0
144   };
145
146   const locale::id* const
147   locale::_Impl::_S_id_messages[] =
148   {
149     &std::messages<char>::id, 
150 #ifdef _GLIBCPP_USE_WCHAR_T
151     &std::messages<wchar_t>::id,
152 #endif
153     0
154   };
155   
156   const locale::id* const* const
157   locale::_Impl::_S_facet_categories[] =
158   {
159     // Order must match the decl order in class locale.
160     locale::_Impl::_S_id_ctype,
161     locale::_Impl::_S_id_numeric,
162     locale::_Impl::_S_id_collate,
163     locale::_Impl::_S_id_time,
164     locale::_Impl::_S_id_monetary,
165     locale::_Impl::_S_id_messages,
166     0
167   };
168
169   locale::~locale() throw()
170   { _M_impl->_M_remove_reference(); }
171
172   void
173   locale::_M_coalesce(const locale& __base, const locale& __add, 
174                       category __cat)
175   {
176     __cat = _S_normalize_category(__cat);  
177     _M_impl = new _Impl(*__base._M_impl, 1);  
178
179     try 
180       { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
181     catch (...) 
182       { 
183         _M_impl->_M_remove_reference(); 
184         __throw_exception_again;
185       }
186   }
187
188   locale::locale() throw()
189   { 
190     _S_initialize(); 
191     (_M_impl = _S_global)->_M_add_reference(); 
192   } // XXX MT
193
194   locale::locale(const locale& __other) throw()
195   { (_M_impl = __other._M_impl)->_M_add_reference(); }
196
197   // This is used to initialize global and classic locales, and
198   // assumes that the _Impl objects are constructed correctly.
199   locale::locale(_Impl* __ip) throw() : _M_impl(__ip)
200   { }
201
202   locale::locale(const char* __s)
203   {
204     if (__s)
205       {
206         _S_initialize(); 
207         if (strcmp(__s, "C") == 0 || strcmp(__s, "POSIX") == 0)
208           (_M_impl = _S_classic)->_M_add_reference();
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   locale::facet::
352   facet(size_t __refs) throw() : _M_references(__refs) 
353   { }
354
355   void  
356   locale::facet::
357   _M_add_reference() throw()
358   { ++_M_references; }  // XXX MT
359
360   void  
361   locale::facet::
362   _M_remove_reference() throw()
363   {
364     if (_M_references-- == 0)
365       {
366         try 
367           { delete this; }  
368         catch (...) 
369           { }
370       }
371   }
372   
373   // Definitions for static const data members of ctype_base.
374   const ctype_base::mask ctype_base::space;
375   const ctype_base::mask ctype_base::print;
376   const ctype_base::mask ctype_base::cntrl;
377   const ctype_base::mask ctype_base::upper;
378   const ctype_base::mask ctype_base::lower;
379   const ctype_base::mask ctype_base::alpha;
380   const ctype_base::mask ctype_base::digit;
381   const ctype_base::mask ctype_base::punct;
382   const ctype_base::mask ctype_base::xdigit;
383   const ctype_base::mask ctype_base::alnum;
384   const ctype_base::mask ctype_base::graph;
385
386   // Platform-specific initialization code for ctype tables.
387   #include <bits/ctype_noninline.h>
388
389   const size_t ctype<char>::table_size;
390
391   ctype<char>::~ctype()
392   { if (_M_del) delete[] this->table(); }
393
394   // These are dummy placeholders as these virtual functions are never called.
395   bool 
396   ctype<char>::do_is(mask, char_type) const 
397   { return false; }
398   
399   const char*
400   ctype<char>::do_is(const char_type* __c, const char_type*, mask*) const 
401   { return __c; }
402   
403   const char*
404   ctype<char>::do_scan_is(mask, const char_type* __c, const char_type*) const 
405   { return __c; }
406
407   const char* 
408   ctype<char>::do_scan_not(mask, const char_type* __c, const char_type*) const
409   { return __c; }
410
411   char
412   ctype<char>::do_widen(char __c) const
413   { return __c; }
414   
415   const char* 
416   ctype<char>::do_widen(const char* __lo, const char* __hi, char* __dest) const
417   {
418     memcpy(__dest, __lo, __hi - __lo);
419     return __hi;
420   }
421   
422   char
423   ctype<char>::do_narrow(char __c, char /*__dfault*/) const
424   { return __c; }
425   
426   const char* 
427   ctype<char>::do_narrow(const char* __lo, const char* __hi, 
428                          char /*__dfault*/, char* __dest) const
429   {
430     memcpy(__dest, __lo, __hi - __lo);
431     return __hi;
432   }
433
434   template<>
435   ctype_byname<char>::ctype_byname(const char* /*__s*/, size_t __refs)
436   : ctype<char>(new mask[table_size], true, __refs)
437   { }
438
439   // Definitions for static const data members of time_base
440   template<> 
441     const char*
442     __timepunct<char>::_S_timezones[14] =
443     { 
444       "GMT", "HST", "AKST", "PST", "MST", "CST", "EST", "AST", "NST", "CET", 
445       "IST", "EET", "CST", "JST"  
446     };
447  
448 #ifdef _GLIBCPP_USE_WCHAR_T
449   template<> 
450     const wchar_t*
451     __timepunct<wchar_t>::_S_timezones[14] =
452     { 
453       L"GMT", L"HST", L"AKST", L"PST", L"MST", L"CST", L"EST", L"AST", 
454       L"NST", L"CET", L"IST", L"EET", L"CST", L"JST"  
455     };
456 #endif
457
458   // Definitions for static const data members of money_base
459   const money_base::pattern 
460   money_base::_S_default_pattern =  {{symbol, sign, none, value}};
461
462   template<>
463     const ctype<char>&
464     use_facet<ctype<char> >(const locale& __loc)
465     {
466       size_t __i = ctype<char>::id._M_index;
467       const locale::_Impl* __tmp = __loc._M_impl;
468       return static_cast<const ctype<char>&>(* (*(__tmp->_M_facets))[__i]);
469     }
470
471 #ifdef _GLIBCPP_USE_WCHAR_T
472   template<>
473     const ctype<wchar_t>&
474     use_facet<ctype<wchar_t> >(const locale& __loc)
475     {
476       size_t __i = ctype<wchar_t>::id._M_index;
477       const locale::_Impl* __tmp = __loc._M_impl;
478       return static_cast<const ctype<wchar_t>&>(* (*(__tmp->_M_facets))[__i]);
479     }
480 #endif
481
482   bool
483   __num_base::_S_format_float(const ios_base& __io, char* __fptr, char __mod,
484                               streamsize __prec)
485   {
486     bool __incl_prec = false;
487     ios_base::fmtflags __flags = __io.flags();
488     *__fptr++ = '%';
489     // [22.2.2.2.2] Table 60
490     if (__flags & ios_base::showpos)
491       *__fptr++ = '+';
492     if (__flags & ios_base::showpoint)
493       *__fptr++ = '#';
494     // As per [22.2.2.2.2.11]
495     if (__flags & ios_base::fixed || __prec > 0)
496       {
497         *__fptr++ = '.';
498         *__fptr++ = '*';
499         __incl_prec = true;
500       }
501     if (__mod)
502       *__fptr++ = __mod;
503     ios_base::fmtflags __fltfield = __flags & ios_base::floatfield;
504     // [22.2.2.2.2] Table 58
505     if (__fltfield == ios_base::fixed)
506       *__fptr++ = 'f';
507     else if (__fltfield == ios_base::scientific)
508       *__fptr++ = (__flags & ios_base::uppercase) ? 'E' : 'e';
509     else
510       *__fptr++ = (__flags & ios_base::uppercase) ? 'G' : 'g';
511     *__fptr = '\0';
512     return __incl_prec;
513   }
514   
515   void
516   __num_base::_S_format_int(const ios_base& __io, char* __fptr, char __mod, 
517                             char __modl)
518   {
519     ios_base::fmtflags __flags = __io.flags();
520     *__fptr++ = '%';
521     // [22.2.2.2.2] Table 60
522     if (__flags & ios_base::showpos)
523       *__fptr++ = '+';
524     if (__flags & ios_base::showbase)
525       *__fptr++ = '#';
526     *__fptr++ = 'l';
527
528     // For long long types.
529     if (__modl)
530       *__fptr++ = __modl;
531
532     ios_base::fmtflags __bsefield = __flags & ios_base::basefield;
533     if (__bsefield == ios_base::hex)
534       *__fptr++ = (__flags & ios_base::uppercase) ? 'X' : 'x';
535     else if (__bsefield == ios_base::oct)
536       *__fptr++ = 'o';
537     else
538       *__fptr++ = __mod;
539     *__fptr = '\0';
540   }
541   
542   template<>
543     moneypunct_byname<char, false>::moneypunct_byname(const char* /*__s*/, 
544                                                       size_t __refs)
545     : moneypunct<char, false>(__refs) { }
546   
547   template<>
548     moneypunct_byname<char, true>::moneypunct_byname(const char* /*__s*/, 
549                                                      size_t __refs)
550     : moneypunct<char, true>(__refs) { }
551   
552 #ifdef _GLIBCPP_USE_WCHAR_T  
553   ctype<wchar_t>::__wmask_type
554   ctype<wchar_t>::_M_convert_to_wmask(const mask __m) const
555   {
556     __wmask_type __ret;
557     switch (__m)
558       {
559       case space:
560         __ret = wctype("space");
561         break;
562       case print:
563         __ret = wctype("print");
564         break;
565       case cntrl:
566         __ret = wctype("cntrl");
567         break;
568       case upper:
569         __ret = wctype("upper");
570         break;
571       case lower:
572         __ret = wctype("lower");
573         break;
574       case alpha:
575         __ret = wctype("alpha");
576         break;
577       case digit:
578         __ret = wctype("digit");
579         break;
580       case punct:
581         __ret = wctype("punct");
582         break;
583       case xdigit:
584         __ret = wctype("xdigit");
585         break;
586       case alnum:
587         __ret = wctype("alnum");
588         break;
589       case graph:
590         __ret = wctype("graph");
591         break;
592       default:
593         __ret = 0;
594       }
595     return __ret;
596   };
597   
598   ctype<wchar_t>::~ctype() { }
599
600   // NB: These ctype<wchar_t> methods are not configuration-specific,
601   // unlike the ctype<char> bits.
602   ctype<wchar_t>::ctype(size_t __refs) : __ctype_abstract_base<wchar_t>(__refs)
603   { }
604
605   wchar_t
606   ctype<wchar_t>::do_toupper(wchar_t __c) const
607   { return towupper(__c); }
608
609   const wchar_t*
610   ctype<wchar_t>::do_toupper(wchar_t* __lo, const wchar_t* __hi) const
611   {
612     while (__lo < __hi)
613       {
614         *__lo = towupper(*__lo);
615         ++__lo;
616       }
617     return __hi;
618   }
619   
620   wchar_t
621   ctype<wchar_t>::do_tolower(wchar_t __c) const
622   { return towlower(__c); }
623   
624   const wchar_t*
625   ctype<wchar_t>::do_tolower(wchar_t* __lo, const wchar_t* __hi) const
626   {
627     while (__lo < __hi)
628       {
629         *__lo = towlower(*__lo);
630         ++__lo;
631       }
632     return __hi;
633   }
634
635   bool
636   ctype<wchar_t>::
637   do_is(mask __m, char_type __c) const
638   { return static_cast<bool>(iswctype(__c, _M_convert_to_wmask(__m))); }
639   
640   const wchar_t* 
641   ctype<wchar_t>::
642   do_is(const wchar_t* __lo, const wchar_t* __hi, mask* __m) const
643   {
644     while (__lo < __hi && !this->is(*__m, *__lo))
645       ++__lo;
646     return __lo;
647   }
648   
649   const wchar_t* 
650   ctype<wchar_t>::
651   do_scan_is(mask __m, const wchar_t* __lo, const wchar_t* __hi) const
652   {
653     while (__lo < __hi && !this->is(__m, *__lo))
654       ++__lo;
655     return __lo;
656   }
657
658   const wchar_t*
659   ctype<wchar_t>::
660   do_scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
661   {
662     while (__lo < __hi && this->is(__m, *__lo) != 0)
663       ++__lo;
664     return __lo;
665   }
666
667   wchar_t
668   ctype<wchar_t>::
669   do_widen(char __c) const
670   { return btowc(__c); }
671   
672   const char* 
673   ctype<wchar_t>::
674   do_widen(const char* __lo, const char* __hi, wchar_t* __dest) const
675   {
676     mbstate_t __state;
677     memset(static_cast<void*>(&__state), 0, sizeof(mbstate_t));
678     mbsrtowcs(__dest, &__lo, __hi - __lo, &__state);
679     return __hi;
680   }
681
682   char
683   ctype<wchar_t>::
684   do_narrow(wchar_t __wc, char __dfault) const
685   { 
686     int __c = wctob(__wc);
687     return (__c == EOF ? __dfault : static_cast<char>(__c)); 
688   }
689
690   const wchar_t*
691   ctype<wchar_t>::
692   do_narrow(const wchar_t* __lo, const wchar_t* __hi, char __dfault, 
693             char* __dest) const
694   {
695     mbstate_t __state;
696     memset(static_cast<void*>(&__state), 0, sizeof(mbstate_t));
697     size_t __len = __hi - __lo;
698     size_t __conv = wcsrtombs(__dest, &__lo, __len, &__state);
699     if (__conv == __len)
700       *__dest = __dfault;
701     return __hi;
702   }
703
704   template<>
705   ctype_byname<wchar_t>::
706   ctype_byname(const char* /*__s*/, size_t __refs)
707   : ctype<wchar_t>(__refs) { }
708 #endif //  _GLIBCPP_USE_WCHAR_T
709 } // namespace std