* c-pretty-print.h (pp_type_specifier_seq): Fix thinko.
[official-gcc.git] / libstdc++-v3 / src / localename.cc
blobd428290b438b3638001fddc49c1561ab379aafc1
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 <locale>
33 namespace __gnu_cxx
35 using namespace std;
37 // Defined in globals.cc.
38 extern locale::facet* facet_vec[_GLIBCXX_NUM_FACETS];
39 extern char* name_vec[6 + _GLIBCXX_NUM_CATEGORIES];
40 extern char name_c[6 + _GLIBCXX_NUM_CATEGORIES][2];
42 extern std::ctype<char> ctype_c;
43 extern std::collate<char> collate_c;
44 extern numpunct<char> numpunct_c;
45 extern num_get<char> num_get_c;
46 extern num_put<char> num_put_c;
47 extern codecvt<char, char, mbstate_t> codecvt_c;
48 extern moneypunct<char, false> moneypunct_cf;
49 extern moneypunct<char, true> moneypunct_ct;
50 extern money_get<char> money_get_c;
51 extern money_put<char> money_put_c;
52 extern __timepunct<char> timepunct_c;
53 extern time_get<char> time_get_c;
54 extern time_put<char> time_put_c;
55 extern std::messages<char> messages_c;
56 #ifdef _GLIBCXX_USE_WCHAR_T
57 extern std::ctype<wchar_t> ctype_w;
58 extern std::collate<wchar_t> collate_w;
59 extern numpunct<wchar_t> numpunct_w;
60 extern num_get<wchar_t> num_get_w;
61 extern num_put<wchar_t> num_put_w;
62 extern codecvt<wchar_t, char, mbstate_t> codecvt_w;
63 extern moneypunct<wchar_t, false> moneypunct_wf;
64 extern moneypunct<wchar_t, true> moneypunct_wt;
65 extern money_get<wchar_t> money_get_w;
66 extern money_put<wchar_t> money_put_w;
67 extern __timepunct<wchar_t> timepunct_w;
68 extern time_get<wchar_t> time_get_w;
69 extern time_put<wchar_t> time_put_w;
70 extern std::messages<wchar_t> messages_w;
71 #endif
73 // And the caches....
74 extern locale::facet* cache_vec[_GLIBCXX_NUM_FACETS];
75 extern __numpunct_cache<char> numpunct_cache_c;
76 extern __moneypunct_cache<char> moneypunct_cache_cf;
77 extern __moneypunct_cache<char> moneypunct_cache_ct;
78 extern __timepunct_cache<char> timepunct_cache_c;
79 #ifdef _GLIBCXX_USE_WCHAR_T
80 extern __numpunct_cache<wchar_t> numpunct_cache_w;
81 extern __moneypunct_cache<wchar_t> moneypunct_cache_wf;
82 extern __moneypunct_cache<wchar_t> moneypunct_cache_wt;
83 extern __timepunct_cache<wchar_t> timepunct_cache_w;
84 #endif
85 } // namespace __gnu_cxx
87 namespace std
89 using namespace __gnu_cxx;
91 locale::_Impl::
92 ~_Impl() throw()
94 for (size_t __i = 0; __i < _M_facets_size; ++__i)
95 if (_M_facets[__i])
96 _M_facets[__i]->_M_remove_reference();
97 delete [] _M_facets;
99 for (size_t __i = 0; __i < _M_facets_size; ++__i)
100 if (_M_caches[__i])
101 _M_caches[__i]->_M_remove_reference();
102 delete [] _M_caches;
104 for (size_t __i = 0; __i < _S_categories_size; ++__i)
105 delete [] _M_names[__i];
106 delete [] _M_names;
109 // Clone existing _Impl object.
110 locale::_Impl::
111 _Impl(const _Impl& __imp, size_t __refs)
112 : _M_references(__refs), _M_facets_size(__imp._M_facets_size)
116 _M_facets = new const facet*[_M_facets_size];
117 for (size_t __i = 0; __i < _M_facets_size; ++__i)
118 _M_facets[__i] = 0;
120 catch(...)
122 delete [] _M_facets;
123 __throw_exception_again;
125 for (size_t __i = 0; __i < _M_facets_size; ++__i)
127 _M_facets[__i] = __imp._M_facets[__i];
128 if (_M_facets[__i])
129 _M_facets[__i]->_M_add_reference();
132 try
134 _M_caches = new const facet*[_M_facets_size];
136 catch(...)
138 delete [] _M_caches;
139 __throw_exception_again;
141 for (size_t __i = 0; __i < _M_facets_size; ++__i)
143 _M_caches[__i] = __imp._M_caches[__i];
144 if (_M_caches[__i])
145 _M_caches[__i]->_M_add_reference();
148 try
150 _M_names = new char*[_S_categories_size];
152 catch(...)
154 delete [] _M_names;
155 __throw_exception_again;
157 for (size_t __i = 0; __i < _S_categories_size; ++__i)
159 char* __new = new char[strlen(__imp._M_names[__i]) + 1];
160 std::strcpy(__new, __imp._M_names[__i]);
161 _M_names[__i] = __new;
165 // Construct named _Impl.
166 locale::_Impl::
167 _Impl(const char* __s, size_t __refs)
168 : _M_references(__refs), _M_facets_size(_GLIBCXX_NUM_FACETS)
170 // Initialize the underlying locale model, which also checks to
171 // see if the given name is valid.
172 __c_locale __cloc;
173 locale::facet::_S_create_c_locale(__cloc, __s);
177 _M_facets = new const facet*[_M_facets_size];
178 for (size_t __i = 0; __i < _M_facets_size; ++__i)
179 _M_facets[__i] = 0;
181 catch(...)
183 delete [] _M_facets;
184 __throw_exception_again;
187 try
189 _M_caches = new const facet*[_M_facets_size];
190 for (size_t __i = 0; __i < _M_facets_size; ++__i)
191 _M_caches[__i] = 0;
193 catch(...)
195 delete [] _M_caches;
196 __throw_exception_again;
199 // Name all the categories.
200 try
202 _M_names = new char*[_S_categories_size];
204 catch(...)
206 delete [] _M_names;
207 __throw_exception_again;
209 size_t __len = std::strlen(__s);
210 if (!std::strchr(__s, ';'))
212 for (size_t __i = 0; __i < _S_categories_size; ++__i)
214 _M_names[__i] = new char[__len + 1];
215 std::strcpy(_M_names[__i], __s);
218 else
220 const char* __beg = __s;
221 for (size_t __i = 0; __i < _S_categories_size; ++__i)
223 __beg = std::strchr(__beg, '=') + 1;
224 const char* __end = std::strchr(__beg, ';');
225 if (!__end)
226 __end = __s + __len;
227 char* __new = new char[__end - __beg + 1];
228 std::memcpy(__new, __beg, __end - __beg);
229 __new[__end - __beg] = '\0';
230 _M_names[__i] = __new;
234 // Construct all standard facets and add them to _M_facets.
235 _M_init_facet(new std::ctype<char>(__cloc, 0, false));
236 _M_init_facet(new codecvt<char, char, mbstate_t>(__cloc));
237 _M_init_facet(new numpunct<char>(__cloc));
238 _M_init_facet(new num_get<char>);
239 _M_init_facet(new num_put<char>);
240 _M_init_facet(new std::collate<char>(__cloc));
241 _M_init_facet(new moneypunct<char, false>(__cloc, __s));
242 _M_init_facet(new moneypunct<char, true>(__cloc, __s));
243 _M_init_facet(new money_get<char>);
244 _M_init_facet(new money_put<char>);
245 _M_init_facet(new __timepunct<char>(__cloc, __s));
246 _M_init_facet(new time_get<char>);
247 _M_init_facet(new time_put<char>);
248 _M_init_facet(new std::messages<char>(__cloc, __s));
250 #ifdef _GLIBCXX_USE_WCHAR_T
251 _M_init_facet(new std::ctype<wchar_t>(__cloc));
252 _M_init_facet(new codecvt<wchar_t, char, mbstate_t>(__cloc));
253 _M_init_facet(new numpunct<wchar_t>(__cloc));
254 _M_init_facet(new num_get<wchar_t>);
255 _M_init_facet(new num_put<wchar_t>);
256 _M_init_facet(new std::collate<wchar_t>(__cloc));
257 _M_init_facet(new moneypunct<wchar_t, false>(__cloc, __s));
258 _M_init_facet(new moneypunct<wchar_t, true>(__cloc, __s));
259 _M_init_facet(new money_get<wchar_t>);
260 _M_init_facet(new money_put<wchar_t>);
261 _M_init_facet(new __timepunct<wchar_t>(__cloc, __s));
262 _M_init_facet(new time_get<wchar_t>);
263 _M_init_facet(new time_put<wchar_t>);
264 _M_init_facet(new std::messages<wchar_t>(__cloc, __s));
265 #endif
266 locale::facet::_S_destroy_c_locale(__cloc);
269 // Construct "C" _Impl.
270 locale::_Impl::
271 _Impl(facet**, size_t __refs, bool)
272 : _M_references(__refs), _M_facets_size(_GLIBCXX_NUM_FACETS)
274 // Initialize the underlying locale model.
275 locale::facet::_S_c_name[0] = 'C';
276 locale::facet::_S_c_name[1] = '\0';
277 locale::facet::_S_create_c_locale(locale::facet::_S_c_locale,
278 locale::facet::_S_c_name);
280 _M_facets = new (&facet_vec) const facet*[_M_facets_size];
281 for (size_t __i = 0; __i < _M_facets_size; ++__i)
282 _M_facets[__i] = 0;
284 _M_caches = new (&cache_vec) const facet*[_M_facets_size];
285 for (size_t __i = 0; __i < _M_facets_size; ++__i)
286 _M_caches[__i] = 0;
288 // Name all the categories.
289 _M_names = new (&name_vec) char*[_S_categories_size];
290 for (size_t __i = 0; __i < _S_categories_size; ++__i)
292 _M_names[__i] = new (&name_c[__i]) char[2];
293 strcpy(_M_names[__i], locale::facet::_S_c_name);
296 // This is needed as presently the C++ version of "C" locales
297 // != data in the underlying locale model for __timepunct,
298 // numpunct, and moneypunct. Also, the "C" locales must be
299 // constructed in a way such that they are pre-allocated.
300 // NB: Set locale::facets(ref) count to one so that each individual
301 // facet is not destroyed when the locale (and thus locale::_Impl) is
302 // destroyed.
303 _M_init_facet(new (&ctype_c) std::ctype<char>(0, false, 1));
304 _M_init_facet(new (&codecvt_c) codecvt<char, char, mbstate_t>(1));
306 typedef __numpunct_cache<char> num_cache_c;
307 num_cache_c* __npc = new (&numpunct_cache_c) num_cache_c(2);
308 _M_init_facet(new (&numpunct_c) numpunct<char>(__npc, 1));
310 _M_init_facet(new (&num_get_c) num_get<char>(1));
311 _M_init_facet(new (&num_put_c) num_put<char>(1));
312 _M_init_facet(new (&collate_c) std::collate<char>(1));
314 typedef __moneypunct_cache<char> money_cache_c;
315 money_cache_c* __mpcf = new (&moneypunct_cache_cf) money_cache_c(2);
316 _M_init_facet(new (&moneypunct_cf) moneypunct<char, false>(__mpcf, 1));
317 money_cache_c* __mpct = new (&moneypunct_cache_ct) money_cache_c(2);
318 _M_init_facet(new (&moneypunct_ct) moneypunct<char, true>(__mpct, 1));
320 _M_init_facet(new (&money_get_c) money_get<char>(1));
321 _M_init_facet(new (&money_put_c) money_put<char>(1));
323 typedef __timepunct_cache<char> time_cache_c;
324 time_cache_c* __tpc = new (&timepunct_cache_c) time_cache_c(2);
325 _M_init_facet(new (&timepunct_c) __timepunct<char>(__tpc, 1));
327 _M_init_facet(new (&time_get_c) time_get<char>(1));
328 _M_init_facet(new (&time_put_c) time_put<char>(1));
329 _M_init_facet(new (&messages_c) std::messages<char>(1));
331 #ifdef _GLIBCXX_USE_WCHAR_T
332 _M_init_facet(new (&ctype_w) std::ctype<wchar_t>(1));
333 _M_init_facet(new (&codecvt_w) codecvt<wchar_t, char, mbstate_t>(1));
335 typedef __numpunct_cache<wchar_t> num_cache_w;
336 num_cache_w* __npw = new (&numpunct_cache_w) num_cache_w(2);
337 _M_init_facet(new (&numpunct_w) numpunct<wchar_t>(__npw, 1));
339 _M_init_facet(new (&num_get_w) num_get<wchar_t>(1));
340 _M_init_facet(new (&num_put_w) num_put<wchar_t>(1));
341 _M_init_facet(new (&collate_w) std::collate<wchar_t>(1));
343 typedef __moneypunct_cache<wchar_t> money_cache_w;
344 money_cache_w* __mpwf = new (&moneypunct_cache_wf) money_cache_w(2);
345 _M_init_facet(new (&moneypunct_wf) moneypunct<wchar_t, false>(__mpwf, 1));
346 money_cache_w* __mpwt = new (&moneypunct_cache_wt) money_cache_w(2);
347 _M_init_facet(new (&moneypunct_wt) moneypunct<wchar_t, true>(__mpwt, 1));
349 _M_init_facet(new (&money_get_w) money_get<wchar_t>(1));
350 _M_init_facet(new (&money_put_w) money_put<wchar_t>(1));
352 typedef __timepunct_cache<wchar_t> time_cache_w;
353 time_cache_w* __tpw = new (&timepunct_cache_w) time_cache_w(2);
354 _M_init_facet(new (&timepunct_w) __timepunct<wchar_t>(__tpw, 1));
356 _M_init_facet(new (&time_get_w) time_get<wchar_t>(1));
357 _M_init_facet(new (&time_put_w) time_put<wchar_t>(1));
358 _M_init_facet(new (&messages_w) std::messages<wchar_t>(1));
359 #endif
361 // This locale is safe to pre-cache, after all the facets have
362 // been created and installed.
363 _M_caches[numpunct<char>::id._M_id()] = __npc;
364 _M_caches[moneypunct<char, false>::id._M_id()] = __mpcf;
365 _M_caches[moneypunct<char, true>::id._M_id()] = __mpct;
366 _M_caches[__timepunct<char>::id._M_id()] = __tpc;
367 #ifdef _GLIBCXX_USE_WCHAR_T
368 _M_caches[numpunct<wchar_t>::id._M_id()] = __npw;
369 _M_caches[moneypunct<wchar_t, false>::id._M_id()] = __mpwf;
370 _M_caches[moneypunct<wchar_t, true>::id._M_id()] = __mpwt;
371 _M_caches[__timepunct<wchar_t>::id._M_id()] = __tpw;
372 #endif
375 void
376 locale::_Impl::
377 _M_replace_categories(const _Impl* __imp, category __cat)
379 category __mask;
380 for (size_t __ix = 0; __ix < _S_categories_size; ++__ix)
382 __mask = 1 << __ix;
383 if (__mask & __cat)
385 // Need to replace entry in _M_facets with other locale's info.
386 _M_replace_category(__imp, _S_facet_categories[__ix]);
387 // If both have names, go ahead and mangle.
388 if (std::strcmp(_M_names[__ix], "*") != 0
389 && std::strcmp(__imp->_M_names[__ix], "*") != 0)
391 delete [] _M_names[__ix];
392 char* __new = new char[std::strlen(__imp->_M_names[__ix]) + 1];
393 std::strcpy(__new, __imp->_M_names[__ix]);
394 _M_names[__ix] = __new;
400 void
401 locale::_Impl::
402 _M_replace_category(const _Impl* __imp, const locale::id* const* __idpp)
404 for (; *__idpp; ++__idpp)
405 _M_replace_facet(__imp, *__idpp);
408 void
409 locale::_Impl::
410 _M_replace_facet(const _Impl* __imp, const locale::id* __idp)
412 size_t __index = __idp->_M_id();
413 if ((__index > (__imp->_M_facets_size - 1)) || !__imp->_M_facets[__index])
414 __throw_runtime_error("locale::_Impl::_M_replace_facet");
415 _M_install_facet(__idp, __imp->_M_facets[__index]);
418 void
419 locale::_Impl::
420 _M_install_facet(const locale::id* __idp, const facet* __fp)
422 if (__fp)
424 size_t __index = __idp->_M_id();
426 // Check size of facet vector to ensure adequate room.
427 if (__index > _M_facets_size - 1)
429 const size_t __new_size = __index + 4;
431 // New facet array.
432 const facet** __oldf = _M_facets;
433 const facet** __newf;
434 __newf = new const facet*[__new_size];
435 for (size_t __i = 0; __i < _M_facets_size; ++__i)
436 __newf[__i] = _M_facets[__i];
437 for (size_t __i2 = _M_facets_size; __i2 < __new_size; ++__i2)
438 __newf[__i2] = 0;
440 // New cache array.
441 const facet** __oldc = _M_caches;
442 const facet** __newc;
443 __newc = new const facet*[__new_size];
444 for (size_t __i = 0; __i < _M_facets_size; ++__i)
445 __newc[__i] = _M_caches[__i];
446 for (size_t __i2 = _M_facets_size; __i2 < __new_size; ++__i2)
447 __newc[__i2] = 0;
449 _M_facets_size = __new_size;
450 _M_facets = __newf;
451 _M_caches = __newc;
452 delete [] __oldf;
453 delete [] __oldc;
456 __fp->_M_add_reference();
457 const facet*& __fpr = _M_facets[__index];
458 if (__fpr)
460 // Replacing an existing facet. Order matters.
461 __fpr->_M_remove_reference();
462 __fpr = __fp;
464 else
466 // Installing a newly created facet into an empty
467 // _M_facets container, say a newly-constructed,
468 // swanky-fresh _Impl.
469 _M_facets[__index] = __fp;
472 // Ideally, it would be nice to only remove the caches that
473 // are now incorrect. However, some of the caches depend on
474 // multiple facets, and we only know about one facet
475 // here. It's no great loss: the first use of the new facet
476 // will create a new, correctly cached facet anyway.
477 for (size_t __i = 0; __i < _M_facets_size; ++__i)
479 const facet* __cpr = _M_caches[__i];
480 if (__cpr)
482 __cpr->_M_remove_reference();
483 _M_caches[__i] = 0;
488 } // namespace std