2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / src / locale.cc
blobc9562e213a7d4643695d200fe5dfef174d62b071
1 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
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.
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.
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.
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.
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>
37 namespace std
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;
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;
54 #ifdef __GTHREADS
55 __gthread_once_t locale::_S_once = __GTHREAD_ONCE_INIT;
56 #endif
58 locale::locale(const locale& __other) throw()
59 { (_M_impl = __other._M_impl)->_M_add_reference(); }
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 { }
67 locale::~locale() throw()
68 { _M_impl->_M_remove_reference(); }
70 bool
71 locale::operator==(const locale& __rhs) const throw()
73 string __name = this->name();
74 return (_M_impl == __rhs._M_impl
75 || (__name != "*" && __name == __rhs.name()));
78 const locale&
79 locale::operator=(const locale& __other) throw()
81 __other._M_impl->_M_add_reference();
82 _M_impl->_M_remove_reference();
83 _M_impl = __other._M_impl;
84 return *this;
87 string
88 locale::name() const
90 string __ret;
91 if (_M_impl->_M_check_same_name())
92 __ret = _M_impl->_M_names[0];
93 else
95 __ret += _S_categories[0];
96 __ret += '=';
97 __ret += _M_impl->_M_names[0];
98 for (size_t __i = 1; __i < _S_categories_size; ++__i)
100 __ret += ';';
101 __ret += _S_categories[__i];
102 __ret += '=';
103 __ret += _M_impl->_M_names[__i];
106 return __ret;
109 locale::category
110 locale::_S_normalize_category(category __cat)
112 int __ret = 0;
113 if (__cat == none || (__cat & all) && !(__cat & ~all))
114 __ret = __cat;
115 else
117 // NB: May be a C-style "LC_ALL" category; convert.
118 switch (__cat)
120 case LC_COLLATE:
121 __ret = collate;
122 break;
123 case LC_CTYPE:
124 __ret = ctype;
125 break;
126 case LC_MONETARY:
127 __ret = monetary;
128 break;
129 case LC_NUMERIC:
130 __ret = numeric;
131 break;
132 case LC_TIME:
133 __ret = time;
134 break;
135 #ifdef _GLIBCXX_HAVE_LC_MESSAGES
136 case LC_MESSAGES:
137 __ret = messages;
138 break;
139 #endif
140 case LC_ALL:
141 __ret = all;
142 break;
143 default:
144 __throw_runtime_error("locale::_S_normalize_category "
145 "category not found");
148 return __ret;
151 // locale::facet
152 __c_locale locale::facet::_S_c_locale;
154 const char locale::facet::_S_c_name[2] = "C";
156 #ifdef __GTHREADS
157 __gthread_once_t locale::facet::_S_once = __GTHREAD_ONCE_INIT;
158 #endif
160 void
161 locale::facet::_S_initialize_once()
163 // Initialize the underlying locale model.
164 _S_create_c_locale(_S_c_locale, _S_c_name);
167 __c_locale
168 locale::facet::_S_get_c_locale()
170 #ifdef __GHTREADS
171 if (__gthread_active_p())
172 __gthread_once(&_S_once, _S_initialize_once);
173 else
174 #endif
176 if (!_S_c_locale)
177 _S_initialize_once();
179 return _S_c_locale;
182 const char*
183 locale::facet::_S_get_c_name()
184 { return _S_c_name; }
186 locale::facet::
187 ~facet() { }
189 // locale::_Impl
190 locale::_Impl::
191 ~_Impl() throw()
193 if (_M_facets)
194 for (size_t __i = 0; __i < _M_facets_size; ++__i)
195 if (_M_facets[__i])
196 _M_facets[__i]->_M_remove_reference();
197 delete [] _M_facets;
199 if (_M_caches)
200 for (size_t __i = 0; __i < _M_facets_size; ++__i)
201 if (_M_caches[__i])
202 _M_caches[__i]->_M_remove_reference();
203 delete [] _M_caches;
205 if (_M_names)
206 for (size_t __i = 0; __i < _S_categories_size; ++__i)
207 delete [] _M_names[__i];
208 delete [] _M_names;
211 // Clone existing _Impl object.
212 locale::_Impl::
213 _Impl(const _Impl& __imp, size_t __refs)
214 : _M_refcount(__refs), _M_facets_size(__imp._M_facets_size)
216 _M_facets = _M_caches = 0;
217 _M_names = 0;
220 _M_facets = new const facet*[_M_facets_size];
221 for (size_t __i = 0; __i < _M_facets_size; ++__i)
223 _M_facets[__i] = __imp._M_facets[__i];
224 if (_M_facets[__i])
225 _M_facets[__i]->_M_add_reference();
227 _M_caches = new const facet*[_M_facets_size];
228 for (size_t __i = 0; __i < _M_facets_size; ++__i)
230 _M_caches[__i] = __imp._M_caches[__i];
231 if (_M_caches[__i])
232 _M_caches[__i]->_M_add_reference();
234 _M_names = new char*[_S_categories_size];
235 for (size_t __i = 0; __i < _S_categories_size; ++__i)
236 _M_names[__i] = 0;
238 // Name all the categories.
239 for (size_t __i = 0; __i < _S_categories_size; ++__i)
241 char* __new = new char[std::strlen(__imp._M_names[__i]) + 1];
242 std::strcpy(__new, __imp._M_names[__i]);
243 _M_names[__i] = __new;
246 catch(...)
248 this->~_Impl();
249 __throw_exception_again;
253 void
254 locale::_Impl::
255 _M_replace_category(const _Impl* __imp, const locale::id* const* __idpp)
257 for (; *__idpp; ++__idpp)
258 _M_replace_facet(__imp, *__idpp);
261 void
262 locale::_Impl::
263 _M_replace_facet(const _Impl* __imp, const locale::id* __idp)
265 size_t __index = __idp->_M_id();
266 if ((__index > (__imp->_M_facets_size - 1)) || !__imp->_M_facets[__index])
267 __throw_runtime_error("locale::_Impl::_M_replace_facet");
268 _M_install_facet(__idp, __imp->_M_facets[__index]);
271 void
272 locale::_Impl::
273 _M_install_facet(const locale::id* __idp, const facet* __fp)
275 if (__fp)
277 size_t __index = __idp->_M_id();
279 // Check size of facet vector to ensure adequate room.
280 if (__index > _M_facets_size - 1)
282 const size_t __new_size = __index + 4;
284 // New facet array.
285 const facet** __oldf = _M_facets;
286 const facet** __newf;
287 __newf = new const facet*[__new_size];
288 for (size_t __i = 0; __i < _M_facets_size; ++__i)
289 __newf[__i] = _M_facets[__i];
290 for (size_t __i2 = _M_facets_size; __i2 < __new_size; ++__i2)
291 __newf[__i2] = 0;
293 // New cache array.
294 const facet** __oldc = _M_caches;
295 const facet** __newc;
298 __newc = new const facet*[__new_size];
300 catch(...)
302 delete [] __newf;
303 __throw_exception_again;
305 for (size_t __i = 0; __i < _M_facets_size; ++__i)
306 __newc[__i] = _M_caches[__i];
307 for (size_t __i2 = _M_facets_size; __i2 < __new_size; ++__i2)
308 __newc[__i2] = 0;
310 _M_facets_size = __new_size;
311 _M_facets = __newf;
312 _M_caches = __newc;
313 delete [] __oldf;
314 delete [] __oldc;
317 __fp->_M_add_reference();
318 const facet*& __fpr = _M_facets[__index];
319 if (__fpr)
321 // Replacing an existing facet. Order matters.
322 __fpr->_M_remove_reference();
323 __fpr = __fp;
325 else
327 // Installing a newly created facet into an empty
328 // _M_facets container, say a newly-constructed,
329 // swanky-fresh _Impl.
330 _M_facets[__index] = __fp;
333 // Ideally, it would be nice to only remove the caches that
334 // are now incorrect. However, some of the caches depend on
335 // multiple facets, and we only know about one facet
336 // here. It's no great loss: the first use of the new facet
337 // will create a new, correctly cached facet anyway.
338 for (size_t __i = 0; __i < _M_facets_size; ++__i)
340 const facet* __cpr = _M_caches[__i];
341 if (__cpr)
343 __cpr->_M_remove_reference();
344 _M_caches[__i] = 0;
351 // locale::id
352 // Definitions for static const data members of locale::id
353 _Atomic_word locale::id::_S_refcount; // init'd to 0 by linker
355 size_t
356 locale::id::_M_id() const
358 if (!_M_index)
359 _M_index = 1 + __exchange_and_add(&_S_refcount, 1);
360 return _M_index - 1;
362 } // namespace std