OSDN Git Service

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