Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[dragonfly.git] / contrib / gcc-3.4 / libstdc++-v3 / src / locale.cc
blob623eb26033e4a3dbdbd73ecc0c907dee185ef62c
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.
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)
60 { _M_impl->_M_add_reference(); }
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 { }
68 locale::~locale() throw()
69 { _M_impl->_M_remove_reference(); }
71 bool
72 locale::operator==(const locale& __rhs) const throw()
74 bool __ret = false;
75 if (_M_impl == __rhs._M_impl)
76 __ret = true;
77 else
79 const string __name = this->name();
80 if (__name != "*" && __name == __rhs.name())
81 __ret = true;
83 return __ret;
86 const locale&
87 locale::operator=(const locale& __other) throw()
89 __other._M_impl->_M_add_reference();
90 _M_impl->_M_remove_reference();
91 _M_impl = __other._M_impl;
92 return *this;
95 string
96 locale::name() const
98 string __ret;
99 if (_M_impl->_M_check_same_name())
100 __ret = _M_impl->_M_names[0];
101 else
103 __ret += _S_categories[0];
104 __ret += '=';
105 __ret += _M_impl->_M_names[0];
106 for (size_t __i = 1; __i < _S_categories_size; ++__i)
108 __ret += ';';
109 __ret += _S_categories[__i];
110 __ret += '=';
111 __ret += _M_impl->_M_names[__i];
114 return __ret;
117 locale::category
118 locale::_S_normalize_category(category __cat)
120 int __ret = 0;
121 if (__cat == none || (__cat & all) && !(__cat & ~all))
122 __ret = __cat;
123 else
125 // NB: May be a C-style "LC_ALL" category; convert.
126 switch (__cat)
128 case LC_COLLATE:
129 __ret = collate;
130 break;
131 case LC_CTYPE:
132 __ret = ctype;
133 break;
134 case LC_MONETARY:
135 __ret = monetary;
136 break;
137 case LC_NUMERIC:
138 __ret = numeric;
139 break;
140 case LC_TIME:
141 __ret = time;
142 break;
143 #ifdef _GLIBCXX_HAVE_LC_MESSAGES
144 case LC_MESSAGES:
145 __ret = messages;
146 break;
147 #endif
148 case LC_ALL:
149 __ret = all;
150 break;
151 default:
152 __throw_runtime_error(__N("locale::_S_normalize_category "
153 "category not found"));
156 return __ret;
159 // locale::facet
160 __c_locale locale::facet::_S_c_locale;
162 const char locale::facet::_S_c_name[2] = "C";
164 #ifdef __GTHREADS
165 __gthread_once_t locale::facet::_S_once = __GTHREAD_ONCE_INIT;
166 #endif
168 void
169 locale::facet::_S_initialize_once()
171 // Initialize the underlying locale model.
172 _S_create_c_locale(_S_c_locale, _S_c_name);
175 __c_locale
176 locale::facet::_S_get_c_locale()
178 #ifdef __GHTREADS
179 if (__gthread_active_p())
180 __gthread_once(&_S_once, _S_initialize_once);
181 else
182 #endif
184 if (!_S_c_locale)
185 _S_initialize_once();
187 return _S_c_locale;
190 const char*
191 locale::facet::_S_get_c_name()
192 { return _S_c_name; }
194 locale::facet::
195 ~facet() { }
197 // locale::_Impl
198 locale::_Impl::
199 ~_Impl() throw()
201 if (_M_facets)
202 for (size_t __i = 0; __i < _M_facets_size; ++__i)
203 if (_M_facets[__i])
204 _M_facets[__i]->_M_remove_reference();
205 delete [] _M_facets;
207 if (_M_caches)
208 for (size_t __i = 0; __i < _M_facets_size; ++__i)
209 if (_M_caches[__i])
210 _M_caches[__i]->_M_remove_reference();
211 delete [] _M_caches;
213 if (_M_names)
214 for (size_t __i = 0; __i < _S_categories_size; ++__i)
215 delete [] _M_names[__i];
216 delete [] _M_names;
219 // Clone existing _Impl object.
220 locale::_Impl::
221 _Impl(const _Impl& __imp, size_t __refs)
222 : _M_refcount(__refs), _M_facets(0), _M_facets_size(__imp._M_facets_size),
223 _M_caches(0), _M_names(0)
227 _M_facets = new const facet*[_M_facets_size];
228 for (size_t __i = 0; __i < _M_facets_size; ++__i)
230 _M_facets[__i] = __imp._M_facets[__i];
231 if (_M_facets[__i])
232 _M_facets[__i]->_M_add_reference();
234 _M_caches = new const facet*[_M_facets_size];
235 for (size_t __j = 0; __j < _M_facets_size; ++__j)
237 _M_caches[__j] = __imp._M_caches[__j];
238 if (_M_caches[__j])
239 _M_caches[__j]->_M_add_reference();
241 _M_names = new char*[_S_categories_size];
242 for (size_t __k = 0; __k < _S_categories_size; ++__k)
243 _M_names[__k] = 0;
245 // Name all the categories.
246 for (size_t __l = 0; __l < _S_categories_size; ++__l)
248 char* __new = new char[std::strlen(__imp._M_names[__l]) + 1];
249 std::strcpy(__new, __imp._M_names[__l]);
250 _M_names[__l] = __new;
253 catch(...)
255 this->~_Impl();
256 __throw_exception_again;
260 void
261 locale::_Impl::
262 _M_replace_category(const _Impl* __imp,
263 const locale::id* const* __idpp)
265 for (; *__idpp; ++__idpp)
266 _M_replace_facet(__imp, *__idpp);
269 void
270 locale::_Impl::
271 _M_replace_facet(const _Impl* __imp, const locale::id* __idp)
273 size_t __index = __idp->_M_id();
274 if ((__index > (__imp->_M_facets_size - 1))
275 || !__imp->_M_facets[__index])
276 __throw_runtime_error(__N("locale::_Impl::_M_replace_facet"));
277 _M_install_facet(__idp, __imp->_M_facets[__index]);
280 void
281 locale::_Impl::
282 _M_install_facet(const locale::id* __idp, const facet* __fp)
284 if (__fp)
286 size_t __index = __idp->_M_id();
288 // Check size of facet vector to ensure adequate room.
289 if (__index > _M_facets_size - 1)
291 const size_t __new_size = __index + 4;
293 // New facet array.
294 const facet** __oldf = _M_facets;
295 const facet** __newf;
296 __newf = new const facet*[__new_size];
297 for (size_t __i = 0; __i < _M_facets_size; ++__i)
298 __newf[__i] = _M_facets[__i];
299 for (size_t __l = _M_facets_size; __l < __new_size; ++__l)
300 __newf[__l] = 0;
302 // New cache array.
303 const facet** __oldc = _M_caches;
304 const facet** __newc;
307 __newc = new const facet*[__new_size];
309 catch(...)
311 delete [] __newf;
312 __throw_exception_again;
314 for (size_t __j = 0; __j < _M_facets_size; ++__j)
315 __newc[__j] = _M_caches[__j];
316 for (size_t __k = _M_facets_size; __k < __new_size; ++__k)
317 __newc[__k] = 0;
319 _M_facets_size = __new_size;
320 _M_facets = __newf;
321 _M_caches = __newc;
322 delete [] __oldf;
323 delete [] __oldc;
326 __fp->_M_add_reference();
327 const facet*& __fpr = _M_facets[__index];
328 if (__fpr)
330 // Replacing an existing facet. Order matters.
331 __fpr->_M_remove_reference();
332 __fpr = __fp;
334 else
336 // Installing a newly created facet into an empty
337 // _M_facets container, say a newly-constructed,
338 // swanky-fresh _Impl.
339 _M_facets[__index] = __fp;
342 // Ideally, it would be nice to only remove the caches that
343 // are now incorrect. However, some of the caches depend on
344 // multiple facets, and we only know about one facet
345 // here. It's no great loss: the first use of the new facet
346 // will create a new, correctly cached facet anyway.
347 for (size_t __i = 0; __i < _M_facets_size; ++__i)
349 const facet* __cpr = _M_caches[__i];
350 if (__cpr)
352 __cpr->_M_remove_reference();
353 _M_caches[__i] = 0;
360 // locale::id
361 // Definitions for static const data members of locale::id
362 _Atomic_word locale::id::_S_refcount; // init'd to 0 by linker
364 size_t
365 locale::id::_M_id() const
367 if (!_M_index)
368 _M_index = 1 + __gnu_cxx::__exchange_and_add(&_S_refcount, 1);
369 return _M_index - 1;
371 } // namespace std