OSDN Git Service

2002-09-10 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 static const data members of locale::id
63   _Atomic_word locale::id::_S_highwater;  // init'd to 0 by linker
64
65   // Definitions for static const data members of locale::_Impl
66   const locale::id* const
67   locale::_Impl::_S_id_ctype[] =
68   {
69     &std::ctype<char>::id, 
70     &codecvt<char, char, mbstate_t>::id,
71 #ifdef _GLIBCPP_USE_WCHAR_T
72     &std::ctype<wchar_t>::id,
73     &codecvt<wchar_t, char, mbstate_t>::id,
74 #endif
75     0
76   };
77
78   const locale::id* const
79   locale::_Impl::_S_id_numeric[] =
80   {
81     &num_get<char>::id,  
82     &num_put<char>::id,  
83     &numpunct<char>::id, 
84 #ifdef _GLIBCPP_USE_WCHAR_T
85     &num_get<wchar_t>::id,
86     &num_put<wchar_t>::id,
87     &numpunct<wchar_t>::id,
88 #endif
89     0
90   };
91   
92   const locale::id* const
93   locale::_Impl::_S_id_collate[] =
94   {
95     &std::collate<char>::id,
96 #ifdef _GLIBCPP_USE_WCHAR_T
97     &std::collate<wchar_t>::id,
98 #endif
99     0
100   };
101
102   const locale::id* const
103   locale::_Impl::_S_id_time[] =
104   {
105     &__timepunct<char>::id, 
106     &time_get<char>::id, 
107     &time_put<char>::id, 
108 #ifdef _GLIBCPP_USE_WCHAR_T
109     &__timepunct<wchar_t>::id, 
110     &time_get<wchar_t>::id,
111     &time_put<wchar_t>::id,
112 #endif
113     0
114   };
115   
116   const locale::id* const
117   locale::_Impl::_S_id_monetary[] =
118   {
119     &money_get<char>::id,        
120     &money_put<char>::id,        
121     &moneypunct<char, false>::id, 
122     &moneypunct<char, true >::id, 
123 #ifdef _GLIBCPP_USE_WCHAR_T
124     &money_get<wchar_t>::id,
125     &money_put<wchar_t>::id,
126     &moneypunct<wchar_t, false>::id,
127     &moneypunct<wchar_t, true >::id,
128 #endif
129     0
130   };
131
132   const locale::id* const
133   locale::_Impl::_S_id_messages[] =
134   {
135     &std::messages<char>::id, 
136 #ifdef _GLIBCPP_USE_WCHAR_T
137     &std::messages<wchar_t>::id,
138 #endif
139     0
140   };
141   
142   const locale::id* const* const
143   locale::_Impl::_S_facet_categories[] =
144   {
145     // Order must match the decl order in class locale.
146     locale::_Impl::_S_id_ctype,
147     locale::_Impl::_S_id_numeric,
148     locale::_Impl::_S_id_collate,
149     locale::_Impl::_S_id_monetary,
150     locale::_Impl::_S_id_time,
151     locale::_Impl::_S_id_messages,
152     0
153   };
154
155   locale::~locale() throw()
156   { _M_impl->_M_remove_reference(); }
157
158   void
159   locale::_M_coalesce(const locale& __base, const locale& __add, 
160                       category __cat)
161   {
162     __cat = _S_normalize_category(__cat);  
163     _M_impl = new _Impl(*__base._M_impl, 1);  
164
165     try 
166       { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
167     catch (...) 
168       { 
169         _M_impl->_M_remove_reference(); 
170         __throw_exception_again;
171       }
172   }
173
174   locale::locale() throw()
175   { 
176     _S_initialize(); 
177     (_M_impl = _S_global)->_M_add_reference(); 
178   }
179
180   locale::locale(const locale& __other) throw()
181   { (_M_impl = __other._M_impl)->_M_add_reference(); }
182
183   // This is used to initialize global and classic locales, and
184   // assumes that the _Impl objects are constructed correctly.
185   locale::locale(_Impl* __ip) throw() : _M_impl(__ip)
186   { }
187
188   locale::locale(const char* __s)
189   {
190     if (__s)
191       {
192         _S_initialize(); 
193         if (strcmp(__s, "C") == 0 || strcmp(__s, "POSIX") == 0)
194           (_M_impl = _S_classic)->_M_add_reference();
195         else if (strcmp(__s, "") == 0)
196           {
197             char* __env = getenv("LC_ALL");
198             if (__env)
199               _M_impl = new _Impl(__env, 1);
200             else if ((__env = getenv("LANG")))
201               _M_impl = new _Impl(__env, 1);
202             else
203               (_M_impl = _S_classic)->_M_add_reference();
204           }
205         else
206           _M_impl = new _Impl(__s, 1);
207       }
208     else
209       __throw_runtime_error("attempt to create locale from NULL name");
210   }
211
212   locale::locale(const locale& __base, const char* __s, category __cat)
213   { 
214     // NB: There are complicated, yet more efficient ways to do
215     // this. Building up locales on a per-category way is tedious, so
216     // let's do it this way until people complain.
217     locale __add(__s);
218     _M_coalesce(__base, __add, __cat);
219   }
220
221   locale::locale(const locale& __base, const locale& __add, category __cat)
222   { _M_coalesce(__base, __add, __cat); }
223
224   bool
225   locale::operator==(const locale& __rhs) const throw()
226   {
227     string __name = this->name();
228     return (_M_impl == __rhs._M_impl 
229             || (__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 time_base
392   template<> 
393     const char*
394     __timepunct<char>::_S_timezones[14] =
395     { 
396       "GMT", "HST", "AKST", "PST", "MST", "CST", "EST", "AST", "NST", "CET", 
397       "IST", "EET", "CST", "JST"  
398     };
399  
400 #ifdef _GLIBCPP_USE_WCHAR_T
401   template<> 
402     const wchar_t*
403     __timepunct<wchar_t>::_S_timezones[14] =
404     { 
405       L"GMT", L"HST", L"AKST", L"PST", L"MST", L"CST", L"EST", L"AST", 
406       L"NST", L"CET", L"IST", L"EET", L"CST", L"JST"  
407     };
408 #endif
409
410   // Definitions for static const data members of money_base
411   const money_base::pattern 
412   money_base::_S_default_pattern =  { {symbol, sign, none, value} };
413
414   const char __num_base::_S_atoms[] = "0123456789eEabcdfABCDF";
415
416   bool
417   __num_base::_S_format_float(const ios_base& __io, char* __fptr, char __mod,
418                               streamsize __prec)
419   {
420     bool __incl_prec = false;
421     ios_base::fmtflags __flags = __io.flags();
422     *__fptr++ = '%';
423     // [22.2.2.2.2] Table 60
424     if (__flags & ios_base::showpos)
425       *__fptr++ = '+';
426     if (__flags & ios_base::showpoint)
427       *__fptr++ = '#';
428     // As per [22.2.2.2.2.11]
429     if (__flags & ios_base::fixed || __prec > 0)
430       {
431         *__fptr++ = '.';
432         *__fptr++ = '*';
433         __incl_prec = true;
434       }
435     if (__mod)
436       *__fptr++ = __mod;
437     ios_base::fmtflags __fltfield = __flags & ios_base::floatfield;
438     // [22.2.2.2.2] Table 58
439     if (__fltfield == ios_base::fixed)
440       *__fptr++ = 'f';
441     else if (__fltfield == ios_base::scientific)
442       *__fptr++ = (__flags & ios_base::uppercase) ? 'E' : 'e';
443     else
444       *__fptr++ = (__flags & ios_base::uppercase) ? 'G' : 'g';
445     *__fptr = '\0';
446     return __incl_prec;
447   }
448   
449   void
450   __num_base::_S_format_int(const ios_base& __io, char* __fptr, char __mod, 
451                             char __modl)
452   {
453     ios_base::fmtflags __flags = __io.flags();
454     *__fptr++ = '%';
455     // [22.2.2.2.2] Table 60
456     if (__flags & ios_base::showpos)
457       *__fptr++ = '+';
458     if (__flags & ios_base::showbase)
459       *__fptr++ = '#';
460     *__fptr++ = 'l';
461
462     // For long long types.
463     if (__modl)
464       *__fptr++ = __modl;
465
466     ios_base::fmtflags __bsefield = __flags & ios_base::basefield;
467     if (__bsefield == ios_base::hex)
468       *__fptr++ = (__flags & ios_base::uppercase) ? 'X' : 'x';
469     else if (__bsefield == ios_base::oct)
470       *__fptr++ = 'o';
471     else
472       *__fptr++ = __mod;
473     *__fptr = '\0';
474   }
475 } // namespace std
476