1 |
424 |
jeremybenn |
// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
|
2 |
|
|
// 2006, 2007, 2008, 2009, 2010
|
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.
|
10 |
|
|
|
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.
|
15 |
|
|
|
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.
|
19 |
|
|
|
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/>.
|
24 |
|
|
|
25 |
|
|
#include <clocale>
|
26 |
|
|
#include <cstring>
|
27 |
|
|
#include <cstdlib> // For getenv
|
28 |
|
|
#include <cctype>
|
29 |
|
|
#include <cwctype> // For towupper, etc.
|
30 |
|
|
#include <locale>
|
31 |
|
|
#include <ext/concurrence.h>
|
32 |
|
|
|
33 |
|
|
namespace
|
34 |
|
|
{
|
35 |
|
|
__gnu_cxx::__mutex&
|
36 |
|
|
get_locale_cache_mutex()
|
37 |
|
|
{
|
38 |
|
|
static __gnu_cxx::__mutex locale_cache_mutex;
|
39 |
|
|
return locale_cache_mutex;
|
40 |
|
|
}
|
41 |
|
|
} // anonymous namespace
|
42 |
|
|
|
43 |
|
|
// XXX GLIBCXX_ABI Deprecated
|
44 |
|
|
#ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
|
45 |
|
|
# define _GLIBCXX_LOC_ID(mangled) extern std::locale::id mangled
|
46 |
|
|
_GLIBCXX_LOC_ID (_ZNSt7num_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
|
47 |
|
|
_GLIBCXX_LOC_ID (_ZNSt7num_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
|
48 |
|
|
_GLIBCXX_LOC_ID (_ZNSt9money_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
|
49 |
|
|
_GLIBCXX_LOC_ID (_ZNSt9money_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
|
50 |
|
|
# ifdef _GLIBCXX_USE_WCHAR_T
|
51 |
|
|
_GLIBCXX_LOC_ID (_ZNSt7num_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
|
52 |
|
|
_GLIBCXX_LOC_ID (_ZNSt7num_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
|
53 |
|
|
_GLIBCXX_LOC_ID (_ZNSt9money_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
|
54 |
|
|
_GLIBCXX_LOC_ID (_ZNSt9money_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
|
55 |
|
|
# endif
|
56 |
|
|
#endif
|
57 |
|
|
|
58 |
|
|
_GLIBCXX_BEGIN_NAMESPACE(std)
|
59 |
|
|
|
60 |
|
|
// Definitions for static const data members of locale.
|
61 |
|
|
const locale::category locale::none;
|
62 |
|
|
const locale::category locale::ctype;
|
63 |
|
|
const locale::category locale::numeric;
|
64 |
|
|
const locale::category locale::collate;
|
65 |
|
|
const locale::category locale::time;
|
66 |
|
|
const locale::category locale::monetary;
|
67 |
|
|
const locale::category locale::messages;
|
68 |
|
|
const locale::category locale::all;
|
69 |
|
|
|
70 |
|
|
// These are no longer exported.
|
71 |
|
|
locale::_Impl* locale::_S_classic;
|
72 |
|
|
locale::_Impl* locale::_S_global;
|
73 |
|
|
|
74 |
|
|
#ifdef __GTHREADS
|
75 |
|
|
__gthread_once_t locale::_S_once = __GTHREAD_ONCE_INIT;
|
76 |
|
|
#endif
|
77 |
|
|
|
78 |
|
|
locale::locale(const locale& __other) throw()
|
79 |
|
|
: _M_impl(__other._M_impl)
|
80 |
|
|
{ _M_impl->_M_add_reference(); }
|
81 |
|
|
|
82 |
|
|
// This is used to initialize global and classic locales, and
|
83 |
|
|
// assumes that the _Impl objects are constructed correctly.
|
84 |
|
|
// The lack of a reference increment is intentional.
|
85 |
|
|
locale::locale(_Impl* __ip) throw() : _M_impl(__ip)
|
86 |
|
|
{ }
|
87 |
|
|
|
88 |
|
|
locale::~locale() throw()
|
89 |
|
|
{ _M_impl->_M_remove_reference(); }
|
90 |
|
|
|
91 |
|
|
bool
|
92 |
|
|
locale::operator==(const locale& __rhs) const throw()
|
93 |
|
|
{
|
94 |
|
|
// Deal first with the common cases, fast to process: refcopies,
|
95 |
|
|
// unnamed (i.e., !_M_names[0]), "simple" (!_M_names[1] => all the
|
96 |
|
|
// categories same name, i.e., _M_names[0]). Otherwise fall back
|
97 |
|
|
// to the general locale::name().
|
98 |
|
|
bool __ret;
|
99 |
|
|
if (_M_impl == __rhs._M_impl)
|
100 |
|
|
__ret = true;
|
101 |
|
|
else if (!_M_impl->_M_names[0] || !__rhs._M_impl->_M_names[0]
|
102 |
|
|
|| std::strcmp(_M_impl->_M_names[0],
|
103 |
|
|
__rhs._M_impl->_M_names[0]) != 0)
|
104 |
|
|
__ret = false;
|
105 |
|
|
else if (!_M_impl->_M_names[1] && !__rhs._M_impl->_M_names[1])
|
106 |
|
|
__ret = true;
|
107 |
|
|
else
|
108 |
|
|
__ret = this->name() == __rhs.name();
|
109 |
|
|
return __ret;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
const locale&
|
113 |
|
|
locale::operator=(const locale& __other) throw()
|
114 |
|
|
{
|
115 |
|
|
__other._M_impl->_M_add_reference();
|
116 |
|
|
_M_impl->_M_remove_reference();
|
117 |
|
|
_M_impl = __other._M_impl;
|
118 |
|
|
return *this;
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
string
|
122 |
|
|
locale::name() const
|
123 |
|
|
{
|
124 |
|
|
string __ret;
|
125 |
|
|
if (!_M_impl->_M_names[0])
|
126 |
|
|
__ret = '*';
|
127 |
|
|
else if (_M_impl->_M_check_same_name())
|
128 |
|
|
__ret = _M_impl->_M_names[0];
|
129 |
|
|
else
|
130 |
|
|
{
|
131 |
|
|
__ret.reserve(128);
|
132 |
|
|
__ret += _S_categories[0];
|
133 |
|
|
__ret += '=';
|
134 |
|
|
__ret += _M_impl->_M_names[0];
|
135 |
|
|
for (size_t __i = 1; __i < _S_categories_size; ++__i)
|
136 |
|
|
{
|
137 |
|
|
__ret += ';';
|
138 |
|
|
__ret += _S_categories[__i];
|
139 |
|
|
__ret += '=';
|
140 |
|
|
__ret += _M_impl->_M_names[__i];
|
141 |
|
|
}
|
142 |
|
|
}
|
143 |
|
|
return __ret;
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
locale::category
|
147 |
|
|
locale::_S_normalize_category(category __cat)
|
148 |
|
|
{
|
149 |
|
|
int __ret = 0;
|
150 |
|
|
if (__cat == none || ((__cat & all) && !(__cat & ~all)))
|
151 |
|
|
__ret = __cat;
|
152 |
|
|
else
|
153 |
|
|
{
|
154 |
|
|
// NB: May be a C-style "LC_ALL" category; convert.
|
155 |
|
|
switch (__cat)
|
156 |
|
|
{
|
157 |
|
|
case LC_COLLATE:
|
158 |
|
|
__ret = collate;
|
159 |
|
|
break;
|
160 |
|
|
case LC_CTYPE:
|
161 |
|
|
__ret = ctype;
|
162 |
|
|
break;
|
163 |
|
|
case LC_MONETARY:
|
164 |
|
|
__ret = monetary;
|
165 |
|
|
break;
|
166 |
|
|
case LC_NUMERIC:
|
167 |
|
|
__ret = numeric;
|
168 |
|
|
break;
|
169 |
|
|
case LC_TIME:
|
170 |
|
|
__ret = time;
|
171 |
|
|
break;
|
172 |
|
|
#ifdef _GLIBCXX_HAVE_LC_MESSAGES
|
173 |
|
|
case LC_MESSAGES:
|
174 |
|
|
__ret = messages;
|
175 |
|
|
break;
|
176 |
|
|
#endif
|
177 |
|
|
case LC_ALL:
|
178 |
|
|
__ret = all;
|
179 |
|
|
break;
|
180 |
|
|
default:
|
181 |
|
|
__throw_runtime_error(__N("locale::_S_normalize_category "
|
182 |
|
|
"category not found"));
|
183 |
|
|
}
|
184 |
|
|
}
|
185 |
|
|
return __ret;
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
// locale::facet
|
189 |
|
|
__c_locale locale::facet::_S_c_locale;
|
190 |
|
|
|
191 |
|
|
const char locale::facet::_S_c_name[2] = "C";
|
192 |
|
|
|
193 |
|
|
#ifdef __GTHREADS
|
194 |
|
|
__gthread_once_t locale::facet::_S_once = __GTHREAD_ONCE_INIT;
|
195 |
|
|
#endif
|
196 |
|
|
|
197 |
|
|
void
|
198 |
|
|
locale::facet::_S_initialize_once()
|
199 |
|
|
{
|
200 |
|
|
// Initialize the underlying locale model.
|
201 |
|
|
_S_create_c_locale(_S_c_locale, _S_c_name);
|
202 |
|
|
}
|
203 |
|
|
|
204 |
|
|
__c_locale
|
205 |
|
|
locale::facet::_S_get_c_locale()
|
206 |
|
|
{
|
207 |
|
|
#ifdef __GHTREADS
|
208 |
|
|
if (__gthread_active_p())
|
209 |
|
|
__gthread_once(&_S_once, _S_initialize_once);
|
210 |
|
|
else
|
211 |
|
|
#endif
|
212 |
|
|
{
|
213 |
|
|
if (!_S_c_locale)
|
214 |
|
|
_S_initialize_once();
|
215 |
|
|
}
|
216 |
|
|
return _S_c_locale;
|
217 |
|
|
}
|
218 |
|
|
|
219 |
|
|
const char*
|
220 |
|
|
locale::facet::_S_get_c_name() throw()
|
221 |
|
|
{ return _S_c_name; }
|
222 |
|
|
|
223 |
|
|
locale::facet::
|
224 |
|
|
~facet() { }
|
225 |
|
|
|
226 |
|
|
// locale::_Impl
|
227 |
|
|
locale::_Impl::
|
228 |
|
|
~_Impl() throw()
|
229 |
|
|
{
|
230 |
|
|
if (_M_facets)
|
231 |
|
|
for (size_t __i = 0; __i < _M_facets_size; ++__i)
|
232 |
|
|
if (_M_facets[__i])
|
233 |
|
|
_M_facets[__i]->_M_remove_reference();
|
234 |
|
|
delete [] _M_facets;
|
235 |
|
|
|
236 |
|
|
if (_M_caches)
|
237 |
|
|
for (size_t __i = 0; __i < _M_facets_size; ++__i)
|
238 |
|
|
if (_M_caches[__i])
|
239 |
|
|
_M_caches[__i]->_M_remove_reference();
|
240 |
|
|
delete [] _M_caches;
|
241 |
|
|
|
242 |
|
|
if (_M_names)
|
243 |
|
|
for (size_t __i = 0; __i < _S_categories_size; ++__i)
|
244 |
|
|
delete [] _M_names[__i];
|
245 |
|
|
delete [] _M_names;
|
246 |
|
|
}
|
247 |
|
|
|
248 |
|
|
// Clone existing _Impl object.
|
249 |
|
|
locale::_Impl::
|
250 |
|
|
_Impl(const _Impl& __imp, size_t __refs)
|
251 |
|
|
: _M_refcount(__refs), _M_facets(0), _M_facets_size(__imp._M_facets_size),
|
252 |
|
|
_M_caches(0), _M_names(0)
|
253 |
|
|
{
|
254 |
|
|
__try
|
255 |
|
|
{
|
256 |
|
|
_M_facets = new const facet*[_M_facets_size];
|
257 |
|
|
for (size_t __i = 0; __i < _M_facets_size; ++__i)
|
258 |
|
|
{
|
259 |
|
|
_M_facets[__i] = __imp._M_facets[__i];
|
260 |
|
|
if (_M_facets[__i])
|
261 |
|
|
_M_facets[__i]->_M_add_reference();
|
262 |
|
|
}
|
263 |
|
|
_M_caches = new const facet*[_M_facets_size];
|
264 |
|
|
for (size_t __j = 0; __j < _M_facets_size; ++__j)
|
265 |
|
|
{
|
266 |
|
|
_M_caches[__j] = __imp._M_caches[__j];
|
267 |
|
|
if (_M_caches[__j])
|
268 |
|
|
_M_caches[__j]->_M_add_reference();
|
269 |
|
|
}
|
270 |
|
|
_M_names = new char*[_S_categories_size];
|
271 |
|
|
for (size_t __k = 0; __k < _S_categories_size; ++__k)
|
272 |
|
|
_M_names[__k] = 0;
|
273 |
|
|
|
274 |
|
|
// Name the categories.
|
275 |
|
|
for (size_t __l = 0; (__l < _S_categories_size
|
276 |
|
|
&& __imp._M_names[__l]); ++__l)
|
277 |
|
|
{
|
278 |
|
|
const size_t __len = std::strlen(__imp._M_names[__l]) + 1;
|
279 |
|
|
_M_names[__l] = new char[__len];
|
280 |
|
|
std::memcpy(_M_names[__l], __imp._M_names[__l], __len);
|
281 |
|
|
}
|
282 |
|
|
}
|
283 |
|
|
__catch(...)
|
284 |
|
|
{
|
285 |
|
|
this->~_Impl();
|
286 |
|
|
__throw_exception_again;
|
287 |
|
|
}
|
288 |
|
|
}
|
289 |
|
|
|
290 |
|
|
void
|
291 |
|
|
locale::_Impl::
|
292 |
|
|
_M_replace_category(const _Impl* __imp,
|
293 |
|
|
const locale::id* const* __idpp)
|
294 |
|
|
{
|
295 |
|
|
for (; *__idpp; ++__idpp)
|
296 |
|
|
_M_replace_facet(__imp, *__idpp);
|
297 |
|
|
}
|
298 |
|
|
|
299 |
|
|
void
|
300 |
|
|
locale::_Impl::
|
301 |
|
|
_M_replace_facet(const _Impl* __imp, const locale::id* __idp)
|
302 |
|
|
{
|
303 |
|
|
size_t __index = __idp->_M_id();
|
304 |
|
|
if ((__index > (__imp->_M_facets_size - 1))
|
305 |
|
|
|| !__imp->_M_facets[__index])
|
306 |
|
|
__throw_runtime_error(__N("locale::_Impl::_M_replace_facet"));
|
307 |
|
|
_M_install_facet(__idp, __imp->_M_facets[__index]);
|
308 |
|
|
}
|
309 |
|
|
|
310 |
|
|
void
|
311 |
|
|
locale::_Impl::
|
312 |
|
|
_M_install_facet(const locale::id* __idp, const facet* __fp)
|
313 |
|
|
{
|
314 |
|
|
if (__fp)
|
315 |
|
|
{
|
316 |
|
|
size_t __index = __idp->_M_id();
|
317 |
|
|
|
318 |
|
|
// Check size of facet vector to ensure adequate room.
|
319 |
|
|
if (__index > _M_facets_size - 1)
|
320 |
|
|
{
|
321 |
|
|
const size_t __new_size = __index + 4;
|
322 |
|
|
|
323 |
|
|
// New facet array.
|
324 |
|
|
const facet** __oldf = _M_facets;
|
325 |
|
|
const facet** __newf;
|
326 |
|
|
__newf = new const facet*[__new_size];
|
327 |
|
|
for (size_t __i = 0; __i < _M_facets_size; ++__i)
|
328 |
|
|
__newf[__i] = _M_facets[__i];
|
329 |
|
|
for (size_t __l = _M_facets_size; __l < __new_size; ++__l)
|
330 |
|
|
__newf[__l] = 0;
|
331 |
|
|
|
332 |
|
|
// New cache array.
|
333 |
|
|
const facet** __oldc = _M_caches;
|
334 |
|
|
const facet** __newc;
|
335 |
|
|
__try
|
336 |
|
|
{
|
337 |
|
|
__newc = new const facet*[__new_size];
|
338 |
|
|
}
|
339 |
|
|
__catch(...)
|
340 |
|
|
{
|
341 |
|
|
delete [] __newf;
|
342 |
|
|
__throw_exception_again;
|
343 |
|
|
}
|
344 |
|
|
for (size_t __j = 0; __j < _M_facets_size; ++__j)
|
345 |
|
|
__newc[__j] = _M_caches[__j];
|
346 |
|
|
for (size_t __k = _M_facets_size; __k < __new_size; ++__k)
|
347 |
|
|
__newc[__k] = 0;
|
348 |
|
|
|
349 |
|
|
_M_facets_size = __new_size;
|
350 |
|
|
_M_facets = __newf;
|
351 |
|
|
_M_caches = __newc;
|
352 |
|
|
delete [] __oldf;
|
353 |
|
|
delete [] __oldc;
|
354 |
|
|
}
|
355 |
|
|
|
356 |
|
|
__fp->_M_add_reference();
|
357 |
|
|
const facet*& __fpr = _M_facets[__index];
|
358 |
|
|
if (__fpr)
|
359 |
|
|
{
|
360 |
|
|
// Replacing an existing facet. Order matters.
|
361 |
|
|
__fpr->_M_remove_reference();
|
362 |
|
|
__fpr = __fp;
|
363 |
|
|
}
|
364 |
|
|
else
|
365 |
|
|
{
|
366 |
|
|
// Installing a newly created facet into an empty
|
367 |
|
|
// _M_facets container, say a newly-constructed,
|
368 |
|
|
// swanky-fresh _Impl.
|
369 |
|
|
_M_facets[__index] = __fp;
|
370 |
|
|
}
|
371 |
|
|
|
372 |
|
|
// Ideally, it would be nice to only remove the caches that
|
373 |
|
|
// are now incorrect. However, some of the caches depend on
|
374 |
|
|
// multiple facets, and we only know about one facet
|
375 |
|
|
// here. It's no great loss: the first use of the new facet
|
376 |
|
|
// will create a new, correctly cached facet anyway.
|
377 |
|
|
for (size_t __i = 0; __i < _M_facets_size; ++__i)
|
378 |
|
|
{
|
379 |
|
|
const facet* __cpr = _M_caches[__i];
|
380 |
|
|
if (__cpr)
|
381 |
|
|
{
|
382 |
|
|
__cpr->_M_remove_reference();
|
383 |
|
|
_M_caches[__i] = 0;
|
384 |
|
|
}
|
385 |
|
|
}
|
386 |
|
|
}
|
387 |
|
|
}
|
388 |
|
|
|
389 |
|
|
void
|
390 |
|
|
locale::_Impl::
|
391 |
|
|
_M_install_cache(const facet* __cache, size_t __index)
|
392 |
|
|
{
|
393 |
|
|
__gnu_cxx::__scoped_lock sentry(get_locale_cache_mutex());
|
394 |
|
|
if (_M_caches[__index] != 0)
|
395 |
|
|
{
|
396 |
|
|
// Some other thread got in first.
|
397 |
|
|
delete __cache;
|
398 |
|
|
}
|
399 |
|
|
else
|
400 |
|
|
{
|
401 |
|
|
__cache->_M_add_reference();
|
402 |
|
|
_M_caches[__index] = __cache;
|
403 |
|
|
}
|
404 |
|
|
}
|
405 |
|
|
|
406 |
|
|
// locale::id
|
407 |
|
|
// Definitions for static const data members of locale::id
|
408 |
|
|
_Atomic_word locale::id::_S_refcount; // init'd to 0 by linker
|
409 |
|
|
|
410 |
|
|
size_t
|
411 |
|
|
locale::id::_M_id() const throw()
|
412 |
|
|
{
|
413 |
|
|
if (!_M_index)
|
414 |
|
|
{
|
415 |
|
|
// XXX GLIBCXX_ABI Deprecated
|
416 |
|
|
#ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
|
417 |
|
|
locale::id *f = 0;
|
418 |
|
|
# define _GLIBCXX_SYNC_ID(facet, mangled) \
|
419 |
|
|
if (this == &::mangled) \
|
420 |
|
|
f = &facet::id
|
421 |
|
|
_GLIBCXX_SYNC_ID (num_get<char>, _ZNSt7num_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
|
422 |
|
|
_GLIBCXX_SYNC_ID (num_put<char>, _ZNSt7num_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
|
423 |
|
|
_GLIBCXX_SYNC_ID (money_get<char>, _ZNSt9money_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEE2idE);
|
424 |
|
|
_GLIBCXX_SYNC_ID (money_put<char>, _ZNSt9money_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE2idE);
|
425 |
|
|
# ifdef _GLIBCXX_USE_WCHAR_T
|
426 |
|
|
_GLIBCXX_SYNC_ID (num_get<wchar_t>, _ZNSt7num_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
|
427 |
|
|
_GLIBCXX_SYNC_ID (num_put<wchar_t>, _ZNSt7num_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
|
428 |
|
|
_GLIBCXX_SYNC_ID (money_get<wchar_t>, _ZNSt9money_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE);
|
429 |
|
|
_GLIBCXX_SYNC_ID (money_put<wchar_t>, _ZNSt9money_putIwSt19ostreambuf_iteratorIwSt11char_traitsIwEEE2idE);
|
430 |
|
|
# endif
|
431 |
|
|
if (f)
|
432 |
|
|
_M_index = 1 + f->_M_id();
|
433 |
|
|
else
|
434 |
|
|
#endif
|
435 |
|
|
_M_index = 1 + __gnu_cxx::__exchange_and_add_dispatch(&_S_refcount,
|
436 |
|
|
1);
|
437 |
|
|
}
|
438 |
|
|
return _M_index - 1;
|
439 |
|
|
}
|
440 |
|
|
|
441 |
|
|
_GLIBCXX_END_NAMESPACE
|