OSDN Git Service

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