OSDN Git Service

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