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