OSDN Git Service

2006-07-20 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / src / locale.cc
1 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 <cstdlib>     // For getenv
32 #include <cctype>
33 #include <cwctype>     // For towupper, etc.
34 #include <locale>
35 #include <bits/atomicity.h>
36 #include <bits/concurrence.h>
37
38 namespace __gnu_internal _GLIBCXX_VISIBILITY(hidden)
39 {
40   // Mutex object for cache access
41   static __glibcxx_mutex_define_initialized(locale_cache_mutex);
42 }
43
44 // XXX GLIBCXX_ABI Deprecated
45 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
46 # define _GLIBCXX_LOC_ID(mangled) extern std::locale::id mangled
47 _GLIBCXX_LOC_ID (_ZNSt7num_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
48 _GLIBCXX_LOC_ID (_ZNSt7num_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
49 _GLIBCXX_LOC_ID (_ZNSt9money_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
50 _GLIBCXX_LOC_ID (_ZNSt9money_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
51 # ifdef _GLIBCXX_USE_WCHAR_T
52 _GLIBCXX_LOC_ID (_ZNSt7num_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
53 _GLIBCXX_LOC_ID (_ZNSt7num_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
54 _GLIBCXX_LOC_ID (_ZNSt9money_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
55 _GLIBCXX_LOC_ID (_ZNSt9money_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
56 # endif
57 #endif
58
59 _GLIBCXX_BEGIN_NAMESPACE(std)
60
61   // Definitions for static const data members of locale.
62   const locale::category        locale::none;
63   const locale::category        locale::ctype;
64   const locale::category        locale::numeric;
65   const locale::category        locale::collate;
66   const locale::category        locale::time;
67   const locale::category        locale::monetary;
68   const locale::category        locale::messages;
69   const locale::category        locale::all;
70
71   // These are no longer exported.
72   locale::_Impl*                locale::_S_classic;
73   locale::_Impl*                locale::_S_global; 
74
75 #ifdef __GTHREADS
76   __gthread_once_t              locale::_S_once = __GTHREAD_ONCE_INIT;
77 #endif
78
79   locale::locale(const locale& __other) throw()
80   : _M_impl(__other._M_impl)
81   { _M_impl->_M_add_reference(); }
82
83   // This is used to initialize global and classic locales, and
84   // assumes that the _Impl objects are constructed correctly.
85   // The lack of a reference increment is intentional.
86   locale::locale(_Impl* __ip) throw() : _M_impl(__ip)
87   { }
88
89   locale::~locale() throw()
90   { _M_impl->_M_remove_reference(); }
91
92   bool
93   locale::operator==(const locale& __rhs) const throw()
94   {
95     // Deal first with the common cases, fast to process: refcopies,
96     // unnamed (i.e., !_M_names[0]), "simple" (!_M_names[1] => all the
97     // categories same name, i.e., _M_names[0]). Otherwise fall back
98     // to the general locale::name().
99     bool __ret;
100     if (_M_impl == __rhs._M_impl)
101       __ret = true;
102     else if (!_M_impl->_M_names[0] || !__rhs._M_impl->_M_names[0]
103              || std::strcmp(_M_impl->_M_names[0],
104                             __rhs._M_impl->_M_names[0]) != 0)
105       __ret = false;
106     else if (!_M_impl->_M_names[1] && !__rhs._M_impl->_M_names[1])
107       __ret = true;
108     else
109       __ret = this->name() == __rhs.name();
110     return __ret;
111   }
112
113   const locale&
114   locale::operator=(const locale& __other) throw()
115   {
116     __other._M_impl->_M_add_reference();
117     _M_impl->_M_remove_reference();
118     _M_impl = __other._M_impl;
119     return *this;
120   }
121
122   string
123   locale::name() const
124   {
125     string __ret;
126     if (!_M_impl->_M_names[0])
127       __ret = '*';
128     else if (_M_impl->_M_check_same_name())
129       __ret = _M_impl->_M_names[0];
130     else
131       {
132         __ret.reserve(128);
133         __ret += _S_categories[0];
134         __ret += '=';
135         __ret += _M_impl->_M_names[0]; 
136         for (size_t __i = 1; __i < _S_categories_size; ++__i)
137           {
138             __ret += ';';
139             __ret += _S_categories[__i];
140             __ret += '=';
141             __ret += _M_impl->_M_names[__i];
142           }
143       }
144     return __ret;
145   }
146
147   locale::category
148   locale::_S_normalize_category(category __cat) 
149   {
150     int __ret = 0;
151     if (__cat == none || (__cat & all) && !(__cat & ~all))
152       __ret = __cat;
153     else
154       {
155         // NB: May be a C-style "LC_ALL" category; convert.
156         switch (__cat)
157           {
158           case LC_COLLATE:  
159             __ret = collate; 
160             break;
161           case LC_CTYPE:    
162             __ret = ctype;
163             break;
164           case LC_MONETARY: 
165             __ret = monetary;
166             break;
167           case LC_NUMERIC:  
168             __ret = numeric;
169             break;
170           case LC_TIME:     
171             __ret = time; 
172             break;
173 #ifdef _GLIBCXX_HAVE_LC_MESSAGES
174           case LC_MESSAGES: 
175             __ret = messages;
176             break;
177 #endif  
178           case LC_ALL:      
179             __ret = all;
180             break;
181           default:
182             __throw_runtime_error(__N("locale::_S_normalize_category "
183                                   "category not found"));
184           }
185       }
186     return __ret;
187   }
188
189   // locale::facet
190   __c_locale locale::facet::_S_c_locale;
191
192   const char locale::facet::_S_c_name[2] = "C";
193
194 #ifdef __GTHREADS
195   __gthread_once_t locale::facet::_S_once = __GTHREAD_ONCE_INIT;
196 #endif
197
198   void
199   locale::facet::_S_initialize_once()
200   {
201     // Initialize the underlying locale model.
202     _S_create_c_locale(_S_c_locale, _S_c_name);
203   }
204
205   __c_locale
206   locale::facet::_S_get_c_locale()
207   {
208 #ifdef __GHTREADS
209     if (__gthread_active_p())
210       __gthread_once(&_S_once, _S_initialize_once);
211     else
212 #endif
213       {
214         if (!_S_c_locale)
215           _S_initialize_once();
216       }
217     return _S_c_locale;
218   }
219
220   const char*
221   locale::facet::_S_get_c_name()
222   { return _S_c_name; }
223
224   locale::facet::
225   ~facet() { }
226
227   // locale::_Impl
228   locale::_Impl::
229   ~_Impl() throw()
230   {
231     if (_M_facets)
232       for (size_t __i = 0; __i < _M_facets_size; ++__i)
233         if (_M_facets[__i])
234           _M_facets[__i]->_M_remove_reference();
235     delete [] _M_facets;
236
237     if (_M_caches)
238       for (size_t __i = 0; __i < _M_facets_size; ++__i)
239         if (_M_caches[__i])
240           _M_caches[__i]->_M_remove_reference(); 
241     delete [] _M_caches;
242
243     if (_M_names)
244       for (size_t __i = 0; __i < _S_categories_size; ++__i)
245         delete [] _M_names[__i];  
246     delete [] _M_names;
247   }
248
249   // Clone existing _Impl object.
250   locale::_Impl::
251   _Impl(const _Impl& __imp, size_t __refs)
252   : _M_refcount(__refs), _M_facets(0), _M_facets_size(__imp._M_facets_size),
253   _M_caches(0), _M_names(0)
254   {
255     try
256       {
257         _M_facets = new const facet*[_M_facets_size];
258         for (size_t __i = 0; __i < _M_facets_size; ++__i)
259           {
260             _M_facets[__i] = __imp._M_facets[__i];
261             if (_M_facets[__i])
262               _M_facets[__i]->_M_add_reference();
263           }
264         _M_caches = new const facet*[_M_facets_size];
265         for (size_t __j = 0; __j < _M_facets_size; ++__j)
266           {
267             _M_caches[__j] = __imp._M_caches[__j];
268             if (_M_caches[__j])
269               _M_caches[__j]->_M_add_reference();       
270           }
271         _M_names = new char*[_S_categories_size];
272         for (size_t __k = 0; __k < _S_categories_size; ++__k)
273           _M_names[__k] = 0;
274
275         // Name the categories.
276         for (size_t __l = 0; (__l < _S_categories_size
277                               && __imp._M_names[__l]); ++__l)
278           {
279             const size_t __len = std::strlen(__imp._M_names[__l]) + 1;
280             _M_names[__l] = new char[__len];
281             std::memcpy(_M_names[__l], __imp._M_names[__l], __len);
282           }
283       }
284     catch(...)
285       {
286         this->~_Impl();
287         __throw_exception_again;
288       }
289   }
290
291   void
292   locale::_Impl::
293   _M_replace_category(const _Impl* __imp, 
294                       const locale::id* const* __idpp)
295   {
296     for (; *__idpp; ++__idpp)
297       _M_replace_facet(__imp, *__idpp);
298   }
299   
300   void
301   locale::_Impl::
302   _M_replace_facet(const _Impl* __imp, const locale::id* __idp)
303   {
304     size_t __index = __idp->_M_id();
305     if ((__index > (__imp->_M_facets_size - 1)) 
306         || !__imp->_M_facets[__index])
307       __throw_runtime_error(__N("locale::_Impl::_M_replace_facet"));
308     _M_install_facet(__idp, __imp->_M_facets[__index]); 
309   }
310
311   void
312   locale::_Impl::
313   _M_install_facet(const locale::id* __idp, const facet* __fp)
314   {
315     if (__fp)
316       {
317         size_t __index = __idp->_M_id();
318
319         // Check size of facet vector to ensure adequate room.
320         if (__index > _M_facets_size - 1)
321           {
322             const size_t __new_size = __index + 4;
323
324             // New facet array.
325             const facet** __oldf = _M_facets;
326             const facet** __newf;
327             __newf = new const facet*[__new_size]; 
328             for (size_t __i = 0; __i < _M_facets_size; ++__i)
329               __newf[__i] = _M_facets[__i];
330             for (size_t __l = _M_facets_size; __l < __new_size; ++__l)
331               __newf[__l] = 0;
332
333             // New cache array.
334             const facet** __oldc = _M_caches;
335             const facet** __newc;
336             try
337               {
338                 __newc = new const facet*[__new_size];
339               }
340             catch(...)
341               {
342                 delete [] __newf;
343                 __throw_exception_again;
344               }
345             for (size_t __j = 0; __j < _M_facets_size; ++__j)
346               __newc[__j] = _M_caches[__j];
347             for (size_t __k = _M_facets_size; __k < __new_size; ++__k)
348               __newc[__k] = 0;
349
350             _M_facets_size = __new_size;
351             _M_facets = __newf;
352             _M_caches = __newc;
353             delete [] __oldf;
354             delete [] __oldc;
355           }
356
357         __fp->_M_add_reference();
358         const facet*& __fpr = _M_facets[__index];
359         if (__fpr)
360           {
361             // Replacing an existing facet. Order matters.
362             __fpr->_M_remove_reference();
363             __fpr = __fp;
364           }
365         else
366           {
367             // Installing a newly created facet into an empty
368             // _M_facets container, say a newly-constructed,
369             // swanky-fresh _Impl.
370             _M_facets[__index] = __fp;
371           }
372
373         // Ideally, it would be nice to only remove the caches that
374         // are now incorrect. However, some of the caches depend on
375         // multiple facets, and we only know about one facet
376         // here. It's no great loss: the first use of the new facet
377         // will create a new, correctly cached facet anyway.
378         for (size_t __i = 0; __i < _M_facets_size; ++__i)
379           {
380             const facet* __cpr = _M_caches[__i];
381             if (__cpr)
382               {
383                 __cpr->_M_remove_reference();
384                 _M_caches[__i] = 0;
385               }
386           }
387       }
388   }
389
390   void
391   locale::_Impl::
392   _M_install_cache(const facet* __cache, size_t __index)
393   {
394     __gnu_cxx::lock sentry(__gnu_internal::locale_cache_mutex);
395     if (_M_caches[__index] != 0)
396       {
397         // Some other thread got in first.
398         delete __cache;
399       }
400     else
401       {
402         __cache->_M_add_reference();
403         _M_caches[__index] = __cache;
404       }
405   }
406
407   // locale::id
408   // Definitions for static const data members of locale::id
409   _Atomic_word locale::id::_S_refcount;  // init'd to 0 by linker
410
411   size_t
412   locale::id::_M_id() const
413   {
414     if (!_M_index)
415       {
416         // XXX GLIBCXX_ABI Deprecated
417 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
418         locale::id *f = 0;
419 # define _GLIBCXX_SYNC_ID(facet, mangled) \
420         if (this == &::mangled)                         \
421           f = &facet::id
422         _GLIBCXX_SYNC_ID (num_get<char>, _ZNSt7num_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
423         _GLIBCXX_SYNC_ID (num_put<char>, _ZNSt7num_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
424         _GLIBCXX_SYNC_ID (money_get<char>, _ZNSt9money_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
425         _GLIBCXX_SYNC_ID (money_put<char>, _ZNSt9money_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
426 # ifdef _GLIBCXX_USE_WCHAR_T
427         _GLIBCXX_SYNC_ID (num_get<wchar_t>, _ZNSt7num_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
428         _GLIBCXX_SYNC_ID (num_put<wchar_t>, _ZNSt7num_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
429         _GLIBCXX_SYNC_ID (money_get<wchar_t>, _ZNSt9money_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
430         _GLIBCXX_SYNC_ID (money_put<wchar_t>, _ZNSt9money_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
431 # endif
432         if (f)
433           _M_index = 1 + f->_M_id();
434         else
435 #endif
436           _M_index = 1 + __gnu_cxx::__exchange_and_add_dispatch(&_S_refcount,
437                                                                 1);
438       }
439     return _M_index - 1;
440   }
441
442 _GLIBCXX_END_NAMESPACE