OSDN Git Service

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