Mostly work by Pedro to port libstdc++
[cegcc.git] / cegcc / src / gcc-4.4.0 / libstdc++-v3 / src / locale.cc
blob3e2da3d288a060033293dfb1b4584461dbbf0f74
1 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
2 // 2006, 2007, 2008, 2009
3 // Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 #ifdef _GLIBCXX_HAVE_LOCALE_H
26 #include <clocale>
27 #endif
28 #include <cstring>
29 #include <cstdlib> // For getenv
30 #include <cctype>
31 #include <cwctype> // For towupper, etc.
32 #include <locale>
33 #include <ext/concurrence.h>
35 namespace
37 __gnu_cxx::__mutex&
38 get_locale_cache_mutex()
40 static __gnu_cxx::__mutex locale_cache_mutex;
41 return locale_cache_mutex;
43 } // anonymous namespace
45 // XXX GLIBCXX_ABI Deprecated
46 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
47 # define _GLIBCXX_LOC_ID(mangled) extern std::locale::id mangled
48 _GLIBCXX_LOC_ID (_ZNSt7num_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
49 _GLIBCXX_LOC_ID (_ZNSt7num_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
50 _GLIBCXX_LOC_ID (_ZNSt9money_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
51 _GLIBCXX_LOC_ID (_ZNSt9money_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
52 # ifdef _GLIBCXX_USE_WCHAR_T
53 _GLIBCXX_LOC_ID (_ZNSt7num_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
54 _GLIBCXX_LOC_ID (_ZNSt7num_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
55 _GLIBCXX_LOC_ID (_ZNSt9money_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
56 _GLIBCXX_LOC_ID (_ZNSt9money_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
57 # endif
58 #endif
60 _GLIBCXX_BEGIN_NAMESPACE(std)
62 // Definitions for static const data members of locale.
63 const locale::category locale::none;
64 const locale::category locale::ctype;
65 const locale::category locale::numeric;
66 const locale::category locale::collate;
67 const locale::category locale::time;
68 const locale::category locale::monetary;
69 const locale::category locale::messages;
70 const locale::category locale::all;
72 // These are no longer exported.
73 locale::_Impl* locale::_S_classic;
74 locale::_Impl* locale::_S_global;
76 #ifdef __GTHREADS
77 __gthread_once_t locale::_S_once = __GTHREAD_ONCE_INIT;
78 #endif
80 locale::locale(const locale& __other) throw()
81 : _M_impl(__other._M_impl)
82 { _M_impl->_M_add_reference(); }
84 // This is used to initialize global and classic locales, and
85 // assumes that the _Impl objects are constructed correctly.
86 // The lack of a reference increment is intentional.
87 locale::locale(_Impl* __ip) throw() : _M_impl(__ip)
88 { }
90 locale::~locale() throw()
91 { _M_impl->_M_remove_reference(); }
93 bool
94 locale::operator==(const locale& __rhs) const throw()
96 // Deal first with the common cases, fast to process: refcopies,
97 // unnamed (i.e., !_M_names[0]), "simple" (!_M_names[1] => all the
98 // categories same name, i.e., _M_names[0]). Otherwise fall back
99 // to the general locale::name().
100 bool __ret;
101 if (_M_impl == __rhs._M_impl)
102 __ret = true;
103 else if (!_M_impl->_M_names[0] || !__rhs._M_impl->_M_names[0]
104 || std::strcmp(_M_impl->_M_names[0],
105 __rhs._M_impl->_M_names[0]) != 0)
106 __ret = false;
107 else if (!_M_impl->_M_names[1] && !__rhs._M_impl->_M_names[1])
108 __ret = true;
109 else
110 __ret = this->name() == __rhs.name();
111 return __ret;
114 const locale&
115 locale::operator=(const locale& __other) throw()
117 __other._M_impl->_M_add_reference();
118 _M_impl->_M_remove_reference();
119 _M_impl = __other._M_impl;
120 return *this;
123 string
124 locale::name() const
126 string __ret;
127 if (!_M_impl->_M_names[0])
128 __ret = '*';
129 else if (_M_impl->_M_check_same_name())
130 __ret = _M_impl->_M_names[0];
131 else
133 __ret.reserve(128);
134 __ret += _S_categories[0];
135 __ret += '=';
136 __ret += _M_impl->_M_names[0];
137 for (size_t __i = 1; __i < _S_categories_size; ++__i)
139 __ret += ';';
140 __ret += _S_categories[__i];
141 __ret += '=';
142 __ret += _M_impl->_M_names[__i];
145 return __ret;
148 locale::category
149 locale::_S_normalize_category(category __cat)
151 #ifdef __MINGW32CE__
152 return __cat;
153 #else
154 int __ret = 0;
155 if (__cat == none || ((__cat & all) && !(__cat & ~all)))
156 __ret = __cat;
157 else
159 // NB: May be a C-style "LC_ALL" category; convert.
160 switch (__cat)
162 case LC_COLLATE:
163 __ret = collate;
164 break;
165 case LC_CTYPE:
166 __ret = ctype;
167 break;
168 case LC_MONETARY:
169 __ret = monetary;
170 break;
171 case LC_NUMERIC:
172 __ret = numeric;
173 break;
174 case LC_TIME:
175 __ret = time;
176 break;
177 #ifdef _GLIBCXX_HAVE_LC_MESSAGES
178 case LC_MESSAGES:
179 __ret = messages;
180 break;
181 #endif
182 case LC_ALL:
183 __ret = all;
184 break;
185 default:
186 __throw_runtime_error(__N("locale::_S_normalize_category "
187 "category not found"));
190 return __ret;
191 #endif
194 // locale::facet
195 __c_locale locale::facet::_S_c_locale;
197 const char locale::facet::_S_c_name[2] = "C";
199 #ifdef __GTHREADS
200 __gthread_once_t locale::facet::_S_once = __GTHREAD_ONCE_INIT;
201 #endif
203 void
204 locale::facet::_S_initialize_once()
206 // Initialize the underlying locale model.
207 _S_create_c_locale(_S_c_locale, _S_c_name);
210 __c_locale
211 locale::facet::_S_get_c_locale()
213 #ifdef __GHTREADS
214 if (__gthread_active_p())
215 __gthread_once(&_S_once, _S_initialize_once);
216 else
217 #endif
219 if (!_S_c_locale)
220 _S_initialize_once();
222 return _S_c_locale;
225 const char*
226 locale::facet::_S_get_c_name()
227 { return _S_c_name; }
229 locale::facet::
230 ~facet() { }
232 // locale::_Impl
233 locale::_Impl::
234 ~_Impl() throw()
236 if (_M_facets)
237 for (size_t __i = 0; __i < _M_facets_size; ++__i)
238 if (_M_facets[__i])
239 _M_facets[__i]->_M_remove_reference();
240 delete [] _M_facets;
242 if (_M_caches)
243 for (size_t __i = 0; __i < _M_facets_size; ++__i)
244 if (_M_caches[__i])
245 _M_caches[__i]->_M_remove_reference();
246 delete [] _M_caches;
248 if (_M_names)
249 for (size_t __i = 0; __i < _S_categories_size; ++__i)
250 delete [] _M_names[__i];
251 delete [] _M_names;
254 // Clone existing _Impl object.
255 locale::_Impl::
256 _Impl(const _Impl& __imp, size_t __refs)
257 : _M_refcount(__refs), _M_facets(0), _M_facets_size(__imp._M_facets_size),
258 _M_caches(0), _M_names(0)
260 __try
262 _M_facets = new const facet*[_M_facets_size];
263 for (size_t __i = 0; __i < _M_facets_size; ++__i)
265 _M_facets[__i] = __imp._M_facets[__i];
266 if (_M_facets[__i])
267 _M_facets[__i]->_M_add_reference();
269 _M_caches = new const facet*[_M_facets_size];
270 for (size_t __j = 0; __j < _M_facets_size; ++__j)
272 _M_caches[__j] = __imp._M_caches[__j];
273 if (_M_caches[__j])
274 _M_caches[__j]->_M_add_reference();
276 _M_names = new char*[_S_categories_size];
277 for (size_t __k = 0; __k < _S_categories_size; ++__k)
278 _M_names[__k] = 0;
280 // Name the categories.
281 for (size_t __l = 0; (__l < _S_categories_size
282 && __imp._M_names[__l]); ++__l)
284 const size_t __len = std::strlen(__imp._M_names[__l]) + 1;
285 _M_names[__l] = new char[__len];
286 std::memcpy(_M_names[__l], __imp._M_names[__l], __len);
289 __catch(...)
291 this->~_Impl();
292 __throw_exception_again;
296 void
297 locale::_Impl::
298 _M_replace_category(const _Impl* __imp,
299 const locale::id* const* __idpp)
301 for (; *__idpp; ++__idpp)
302 _M_replace_facet(__imp, *__idpp);
305 void
306 locale::_Impl::
307 _M_replace_facet(const _Impl* __imp, const locale::id* __idp)
309 size_t __index = __idp->_M_id();
310 if ((__index > (__imp->_M_facets_size - 1))
311 || !__imp->_M_facets[__index])
312 __throw_runtime_error(__N("locale::_Impl::_M_replace_facet"));
313 _M_install_facet(__idp, __imp->_M_facets[__index]);
316 void
317 locale::_Impl::
318 _M_install_facet(const locale::id* __idp, const facet* __fp)
320 if (__fp)
322 size_t __index = __idp->_M_id();
324 // Check size of facet vector to ensure adequate room.
325 if (__index > _M_facets_size - 1)
327 const size_t __new_size = __index + 4;
329 // New facet array.
330 const facet** __oldf = _M_facets;
331 const facet** __newf;
332 __newf = new const facet*[__new_size];
333 for (size_t __i = 0; __i < _M_facets_size; ++__i)
334 __newf[__i] = _M_facets[__i];
335 for (size_t __l = _M_facets_size; __l < __new_size; ++__l)
336 __newf[__l] = 0;
338 // New cache array.
339 const facet** __oldc = _M_caches;
340 const facet** __newc;
341 __try
343 __newc = new const facet*[__new_size];
345 __catch(...)
347 delete [] __newf;
348 __throw_exception_again;
350 for (size_t __j = 0; __j < _M_facets_size; ++__j)
351 __newc[__j] = _M_caches[__j];
352 for (size_t __k = _M_facets_size; __k < __new_size; ++__k)
353 __newc[__k] = 0;
355 _M_facets_size = __new_size;
356 _M_facets = __newf;
357 _M_caches = __newc;
358 delete [] __oldf;
359 delete [] __oldc;
362 __fp->_M_add_reference();
363 const facet*& __fpr = _M_facets[__index];
364 if (__fpr)
366 // Replacing an existing facet. Order matters.
367 __fpr->_M_remove_reference();
368 __fpr = __fp;
370 else
372 // Installing a newly created facet into an empty
373 // _M_facets container, say a newly-constructed,
374 // swanky-fresh _Impl.
375 _M_facets[__index] = __fp;
378 // Ideally, it would be nice to only remove the caches that
379 // are now incorrect. However, some of the caches depend on
380 // multiple facets, and we only know about one facet
381 // here. It's no great loss: the first use of the new facet
382 // will create a new, correctly cached facet anyway.
383 for (size_t __i = 0; __i < _M_facets_size; ++__i)
385 const facet* __cpr = _M_caches[__i];
386 if (__cpr)
388 __cpr->_M_remove_reference();
389 _M_caches[__i] = 0;
395 void
396 locale::_Impl::
397 _M_install_cache(const facet* __cache, size_t __index)
399 __gnu_cxx::__scoped_lock sentry(get_locale_cache_mutex());
400 if (_M_caches[__index] != 0)
402 // Some other thread got in first.
403 delete __cache;
405 else
407 __cache->_M_add_reference();
408 _M_caches[__index] = __cache;
412 // locale::id
413 // Definitions for static const data members of locale::id
414 _Atomic_word locale::id::_S_refcount; // init'd to 0 by linker
416 size_t
417 locale::id::_M_id() const
419 if (!_M_index)
421 // XXX GLIBCXX_ABI Deprecated
422 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
423 locale::id *f = 0;
424 # define _GLIBCXX_SYNC_ID(facet, mangled) \
425 if (this == &::mangled) \
426 f = &facet::id
427 _GLIBCXX_SYNC_ID (num_get<char>, _ZNSt7num_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
428 _GLIBCXX_SYNC_ID (num_put<char>, _ZNSt7num_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
429 _GLIBCXX_SYNC_ID (money_get<char>, _ZNSt9money_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
430 _GLIBCXX_SYNC_ID (money_put<char>, _ZNSt9money_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
431 # ifdef _GLIBCXX_USE_WCHAR_T
432 _GLIBCXX_SYNC_ID (num_get<wchar_t>, _ZNSt7num_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
433 _GLIBCXX_SYNC_ID (num_put<wchar_t>, _ZNSt7num_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
434 _GLIBCXX_SYNC_ID (money_get<wchar_t>, _ZNSt9money_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
435 _GLIBCXX_SYNC_ID (money_put<wchar_t>, _ZNSt9money_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
436 # endif
437 if (f)
438 _M_index = 1 + f->_M_id();
439 else
440 #endif
441 _M_index = 1 + __gnu_cxx::__exchange_and_add_dispatch(&_S_refcount,
444 return _M_index - 1;
447 _GLIBCXX_END_NAMESPACE