OSDN Git Service

2006-07-27 Benjamin Kosnik <bkoz@wells.artheist.org>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / src / locale_init.cc
1 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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.
9
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.
14
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18 // USA.
19
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.
28
29 #include <clocale>
30 #include <cstring>
31 #include <cstdlib>     // For getenv, free.
32 #include <cctype>
33 #include <cwctype>     // For towupper, etc.
34 #include <locale>
35 #include <bits/atomicity.h>
36 #include <bits/concurrence.h>
37
38 namespace 
39 {
40   using namespace std;
41
42   typedef char fake_locale_Impl[sizeof(locale::_Impl)]
43   __attribute__ ((aligned(__alignof__(locale::_Impl))));
44   fake_locale_Impl c_locale_impl;
45
46   typedef char fake_locale[sizeof(locale)]
47   __attribute__ ((aligned(__alignof__(locale))));
48   fake_locale c_locale;
49
50   typedef char fake_name_vec[sizeof(char*)]
51   __attribute__ ((aligned(__alignof__(char*))));
52   fake_name_vec name_vec[6 + _GLIBCXX_NUM_CATEGORIES];
53
54   typedef char fake_names[sizeof(char[2])]
55   __attribute__ ((aligned(__alignof__(char[2]))));
56   fake_names name_c[6 + _GLIBCXX_NUM_CATEGORIES];
57
58   typedef char fake_facet_vec[sizeof(locale::facet*)]
59   __attribute__ ((aligned(__alignof__(locale::facet*))));
60   fake_facet_vec facet_vec[_GLIBCXX_NUM_FACETS];
61
62   typedef char fake_cache_vec[sizeof(locale::facet*)]
63   __attribute__ ((aligned(__alignof__(locale::facet*))));
64   fake_cache_vec cache_vec[_GLIBCXX_NUM_FACETS];
65
66   typedef char fake_ctype_c[sizeof(std::ctype<char>)]
67   __attribute__ ((aligned(__alignof__(std::ctype<char>))));
68   fake_ctype_c ctype_c;
69
70   typedef char fake_collate_c[sizeof(std::collate<char>)]
71   __attribute__ ((aligned(__alignof__(std::collate<char>))));
72   fake_collate_c collate_c;
73
74   typedef char fake_numpunct_c[sizeof(numpunct<char>)]
75   __attribute__ ((aligned(__alignof__(numpunct<char>))));
76   fake_numpunct_c numpunct_c;
77
78   typedef char fake_num_get_c[sizeof(num_get<char>)]
79   __attribute__ ((aligned(__alignof__(num_get<char>))));
80   fake_num_get_c num_get_c;
81
82   typedef char fake_num_put_c[sizeof(num_put<char>)]
83   __attribute__ ((aligned(__alignof__(num_put<char>))));
84   fake_num_put_c num_put_c;
85
86   typedef char fake_codecvt_c[sizeof(codecvt<char, char, mbstate_t>)]
87   __attribute__ ((aligned(__alignof__(codecvt<char, char, mbstate_t>))));
88   fake_codecvt_c codecvt_c;
89
90   typedef char fake_moneypunct_c[sizeof(moneypunct<char, true>)]
91   __attribute__ ((aligned(__alignof__(moneypunct<char, true>))));
92   fake_moneypunct_c moneypunct_ct;
93   fake_moneypunct_c moneypunct_cf;
94
95   typedef char fake_money_get_c[sizeof(money_get<char>)]
96   __attribute__ ((aligned(__alignof__(money_get<char>))));
97   fake_money_get_c money_get_c;
98   
99   typedef char fake_money_put_c[sizeof(money_put<char>)]
100   __attribute__ ((aligned(__alignof__(money_put<char>))));
101   fake_money_put_c money_put_c;
102
103   typedef char fake_timepunct_c[sizeof(__timepunct<char>)]
104   __attribute__ ((aligned(__alignof__(__timepunct<char>))));
105   fake_timepunct_c timepunct_c;
106
107   typedef char fake_time_get_c[sizeof(time_get<char>)]
108   __attribute__ ((aligned(__alignof__(time_get<char>))));
109   fake_time_get_c time_get_c;
110
111   typedef char fake_time_put_c[sizeof(time_put<char>)]
112   __attribute__ ((aligned(__alignof__(time_put<char>))));
113   fake_time_put_c time_put_c;
114
115   typedef char fake_messages_c[sizeof(messages<char>)]
116   __attribute__ ((aligned(__alignof__(messages<char>))));
117   fake_messages_c messages_c;
118
119 #ifdef  _GLIBCXX_USE_WCHAR_T
120   typedef char fake_wtype_w[sizeof(std::ctype<wchar_t>)]
121   __attribute__ ((aligned(__alignof__(std::ctype<wchar_t>))));
122   fake_wtype_w ctype_w;
123
124   typedef char fake_wollate_w[sizeof(std::collate<wchar_t>)]
125   __attribute__ ((aligned(__alignof__(std::collate<wchar_t>))));
126   fake_wollate_w collate_w;
127
128   typedef char fake_numpunct_w[sizeof(numpunct<wchar_t>)]
129   __attribute__ ((aligned(__alignof__(numpunct<wchar_t>))));
130   fake_numpunct_w numpunct_w;
131
132   typedef char fake_num_get_w[sizeof(num_get<wchar_t>)]
133   __attribute__ ((aligned(__alignof__(num_get<wchar_t>))));
134   fake_num_get_w num_get_w;
135
136   typedef char fake_num_put_w[sizeof(num_put<wchar_t>)]
137   __attribute__ ((aligned(__alignof__(num_put<wchar_t>))));
138   fake_num_put_w num_put_w;
139
140   typedef char fake_wodecvt_w[sizeof(codecvt<wchar_t, char, mbstate_t>)]
141   __attribute__ ((aligned(__alignof__(codecvt<wchar_t, char, mbstate_t>))));
142   fake_wodecvt_w codecvt_w;
143
144   typedef char fake_moneypunct_w[sizeof(moneypunct<wchar_t, true>)]
145   __attribute__ ((aligned(__alignof__(moneypunct<wchar_t, true>))));
146   fake_moneypunct_w moneypunct_wt;
147   fake_moneypunct_w moneypunct_wf;
148
149   typedef char fake_money_get_w[sizeof(money_get<wchar_t>)]
150   __attribute__ ((aligned(__alignof__(money_get<wchar_t>))));
151   fake_money_get_w money_get_w;
152   
153   typedef char fake_money_put_w[sizeof(money_put<wchar_t>)]
154   __attribute__ ((aligned(__alignof__(money_put<wchar_t>))));
155   fake_money_put_w money_put_w;
156
157   typedef char fake_timepunct_w[sizeof(__timepunct<wchar_t>)]
158   __attribute__ ((aligned(__alignof__(__timepunct<wchar_t>))));
159   fake_timepunct_w timepunct_w;
160
161   typedef char fake_time_get_w[sizeof(time_get<wchar_t>)]
162   __attribute__ ((aligned(__alignof__(time_get<wchar_t>))));
163   fake_time_get_w time_get_w;
164
165   typedef char fake_time_put_w[sizeof(time_put<wchar_t>)]
166   __attribute__ ((aligned(__alignof__(time_put<wchar_t>))));
167   fake_time_put_w time_put_w;
168
169   typedef char fake_messages_w[sizeof(messages<wchar_t>)]
170   __attribute__ ((aligned(__alignof__(messages<wchar_t>))));
171   fake_messages_w messages_w;
172 #endif
173
174   // Storage for "C" locale caches.
175   typedef char fake_num_cache_c[sizeof(std::__numpunct_cache<char>)]
176   __attribute__ ((aligned(__alignof__(std::__numpunct_cache<char>))));
177   fake_num_cache_c numpunct_cache_c;
178
179   typedef char fake_money_cache_c[sizeof(std::__moneypunct_cache<char, true>)]
180   __attribute__ ((aligned(__alignof__(std::__moneypunct_cache<char, true>))));
181   fake_money_cache_c moneypunct_cache_ct;
182   fake_money_cache_c moneypunct_cache_cf;
183
184   typedef char fake_time_cache_c[sizeof(std::__timepunct_cache<char>)]
185   __attribute__ ((aligned(__alignof__(std::__timepunct_cache<char>))));
186   fake_time_cache_c timepunct_cache_c;
187
188 #ifdef _GLIBCXX_USE_WCHAR_T
189   typedef char fake_num_cache_w[sizeof(std::__numpunct_cache<wchar_t>)]
190   __attribute__ ((aligned(__alignof__(std::__numpunct_cache<wchar_t>))));
191   fake_num_cache_w numpunct_cache_w;
192
193   typedef char fake_money_cache_w[sizeof(std::__moneypunct_cache<wchar_t,true>)]
194   __attribute__ ((aligned(__alignof__(std::__moneypunct_cache<wchar_t,true>))));
195   fake_money_cache_w moneypunct_cache_wt;
196   fake_money_cache_w moneypunct_cache_wf;
197
198   typedef char fake_time_cache_w[sizeof(std::__timepunct_cache<wchar_t>)]
199   __attribute__ ((aligned(__alignof__(std::__timepunct_cache<wchar_t>))));
200   fake_time_cache_w timepunct_cache_w;
201 #endif
202
203   // Mutex object for locale initialization.
204   static __glibcxx_mutex_define_initialized(locale_mutex);
205 } // anonymous namespace
206
207 _GLIBCXX_BEGIN_NAMESPACE(std)
208
209   locale::locale() throw() : _M_impl(0)
210   { 
211     _S_initialize();
212     __gnu_cxx::lock sentry(locale_mutex);
213     _S_global->_M_add_reference();
214     _M_impl = _S_global;
215   }
216
217   locale
218   locale::global(const locale& __other)
219   {
220     _S_initialize();
221     _Impl* __old;
222     {
223       __gnu_cxx::lock sentry(locale_mutex);
224       __old = _S_global;
225       __other._M_impl->_M_add_reference();
226       _S_global = __other._M_impl;
227       const string __other_name = __other.name();
228       if (__other_name != "*")
229         setlocale(LC_ALL, __other_name.c_str());
230     }
231
232     // Reference count sanity check: one reference removed for the
233     // subsition of __other locale, one added by return-by-value. Net
234     // difference: zero. When the returned locale object's destrutor
235     // is called, then the reference count is decremented and possibly
236     // destroyed.
237     return locale(__old);
238   }
239
240   const locale&
241   locale::classic()
242   {
243     _S_initialize();
244     return reinterpret_cast<const locale&>(c_locale);
245   }
246
247   void
248   locale::_S_initialize_once()
249   {
250     // 2 references.
251     // One reference for _S_classic, one for _S_global
252     _S_classic = new (&c_locale_impl) _Impl(2);
253     _S_global = _S_classic;         
254     new (&c_locale) locale(_S_classic);
255   }
256
257   void  
258   locale::_S_initialize()
259   {
260 #ifdef __GTHREADS
261     if (__gthread_active_p())
262       __gthread_once(&_S_once, _S_initialize_once);
263 #endif
264     if (!_S_classic)
265       _S_initialize_once();
266   }
267
268   // Definitions for static const data members of locale::_Impl
269   const locale::id* const
270   locale::_Impl::_S_id_ctype[] =
271   {
272     &std::ctype<char>::id, 
273     &codecvt<char, char, mbstate_t>::id,
274 #ifdef _GLIBCXX_USE_WCHAR_T
275     &std::ctype<wchar_t>::id,
276     &codecvt<wchar_t, char, mbstate_t>::id,
277 #endif
278     0
279   };
280
281   const locale::id* const
282   locale::_Impl::_S_id_numeric[] =
283   {
284     &num_get<char>::id,  
285     &num_put<char>::id,  
286     &numpunct<char>::id, 
287 #ifdef _GLIBCXX_USE_WCHAR_T
288     &num_get<wchar_t>::id,
289     &num_put<wchar_t>::id,
290     &numpunct<wchar_t>::id,
291 #endif
292     0
293   };
294   
295   const locale::id* const
296   locale::_Impl::_S_id_collate[] =
297   {
298     &std::collate<char>::id,
299 #ifdef _GLIBCXX_USE_WCHAR_T
300     &std::collate<wchar_t>::id,
301 #endif
302     0
303   };
304
305   const locale::id* const
306   locale::_Impl::_S_id_time[] =
307   {
308     &__timepunct<char>::id, 
309     &time_get<char>::id, 
310     &time_put<char>::id, 
311 #ifdef _GLIBCXX_USE_WCHAR_T
312     &__timepunct<wchar_t>::id, 
313     &time_get<wchar_t>::id,
314     &time_put<wchar_t>::id,
315 #endif
316     0
317   };
318   
319   const locale::id* const
320   locale::_Impl::_S_id_monetary[] =
321   {
322     &money_get<char>::id,        
323     &money_put<char>::id,        
324     &moneypunct<char, false>::id, 
325     &moneypunct<char, true >::id, 
326 #ifdef _GLIBCXX_USE_WCHAR_T
327     &money_get<wchar_t>::id,
328     &money_put<wchar_t>::id,
329     &moneypunct<wchar_t, false>::id,
330     &moneypunct<wchar_t, true >::id,
331 #endif
332     0
333   };
334
335   const locale::id* const
336   locale::_Impl::_S_id_messages[] =
337   {
338     &std::messages<char>::id, 
339 #ifdef _GLIBCXX_USE_WCHAR_T
340     &std::messages<wchar_t>::id,
341 #endif
342     0
343   };
344   
345   const locale::id* const* const
346   locale::_Impl::_S_facet_categories[] =
347   {
348     // Order must match the decl order in class locale.
349     locale::_Impl::_S_id_ctype,
350     locale::_Impl::_S_id_numeric,
351     locale::_Impl::_S_id_collate,
352     locale::_Impl::_S_id_time,
353     locale::_Impl::_S_id_monetary,
354     locale::_Impl::_S_id_messages,
355     0
356   };
357
358   // Construct "C" _Impl.
359   locale::_Impl::
360   _Impl(size_t __refs) throw() 
361   : _M_refcount(__refs), _M_facets(0), _M_facets_size(_GLIBCXX_NUM_FACETS),
362   _M_caches(0), _M_names(0)    
363   {
364     _M_facets = new (&facet_vec) const facet*[_M_facets_size];
365     _M_caches = new (&cache_vec) const facet*[_M_facets_size];
366     for (size_t __i = 0; __i < _M_facets_size; ++__i)
367       _M_facets[__i] = _M_caches[__i] = 0;
368
369     // Name the categories.
370     _M_names = new (&name_vec) char*[_S_categories_size];
371     _M_names[0] = new (&name_c[0]) char[2];
372     std::memcpy(_M_names[0], locale::facet::_S_get_c_name(), 2);
373     for (size_t __j = 1; __j < _S_categories_size; ++__j)
374       _M_names[__j] = 0;
375
376     // This is needed as presently the C++ version of "C" locales
377     // != data in the underlying locale model for __timepunct,
378     // numpunct, and moneypunct. Also, the "C" locales must be
379     // constructed in a way such that they are pre-allocated.
380     // NB: Set locale::facets(ref) count to one so that each individual
381     // facet is not destroyed when the locale (and thus locale::_Impl) is
382     // destroyed.
383     _M_init_facet(new (&ctype_c) std::ctype<char>(0, false, 1));
384     _M_init_facet(new (&codecvt_c) codecvt<char, char, mbstate_t>(1));
385
386     typedef __numpunct_cache<char> num_cache_c;
387     num_cache_c* __npc = new (&numpunct_cache_c) num_cache_c(2);
388     _M_init_facet(new (&numpunct_c) numpunct<char>(__npc, 1));
389
390     _M_init_facet(new (&num_get_c) num_get<char>(1));
391     _M_init_facet(new (&num_put_c) num_put<char>(1));
392     _M_init_facet(new (&collate_c) std::collate<char>(1));
393
394     typedef __moneypunct_cache<char, false> money_cache_cf;
395     typedef __moneypunct_cache<char, true> money_cache_ct;
396     money_cache_cf* __mpcf = new (&moneypunct_cache_cf) money_cache_cf(2);
397     _M_init_facet(new (&moneypunct_cf) moneypunct<char, false>(__mpcf, 1));
398     money_cache_ct* __mpct = new (&moneypunct_cache_ct) money_cache_ct(2);
399     _M_init_facet(new (&moneypunct_ct) moneypunct<char, true>(__mpct, 1));
400
401     _M_init_facet(new (&money_get_c) money_get<char>(1));
402     _M_init_facet(new (&money_put_c) money_put<char>(1));
403
404     typedef __timepunct_cache<char> time_cache_c;
405     time_cache_c* __tpc = new (&timepunct_cache_c) time_cache_c(2);
406     _M_init_facet(new (&timepunct_c) __timepunct<char>(__tpc, 1));
407
408     _M_init_facet(new (&time_get_c) time_get<char>(1));
409     _M_init_facet(new (&time_put_c) time_put<char>(1));
410     _M_init_facet(new (&messages_c) std::messages<char>(1));    
411
412 #ifdef  _GLIBCXX_USE_WCHAR_T
413     _M_init_facet(new (&ctype_w) std::ctype<wchar_t>(1));
414     _M_init_facet(new (&codecvt_w) codecvt<wchar_t, char, mbstate_t>(1));
415
416     typedef __numpunct_cache<wchar_t> num_cache_w;
417     num_cache_w* __npw = new (&numpunct_cache_w) num_cache_w(2);
418     _M_init_facet(new (&numpunct_w) numpunct<wchar_t>(__npw, 1));
419
420     _M_init_facet(new (&num_get_w) num_get<wchar_t>(1));
421     _M_init_facet(new (&num_put_w) num_put<wchar_t>(1));
422     _M_init_facet(new (&collate_w) std::collate<wchar_t>(1));
423
424     typedef __moneypunct_cache<wchar_t, false> money_cache_wf;
425     typedef __moneypunct_cache<wchar_t, true> money_cache_wt;
426     money_cache_wf* __mpwf = new (&moneypunct_cache_wf) money_cache_wf(2);
427     _M_init_facet(new (&moneypunct_wf) moneypunct<wchar_t, false>(__mpwf, 1));
428     money_cache_wt* __mpwt = new (&moneypunct_cache_wt) money_cache_wt(2);
429     _M_init_facet(new (&moneypunct_wt) moneypunct<wchar_t, true>(__mpwt, 1));
430
431     _M_init_facet(new (&money_get_w) money_get<wchar_t>(1));
432     _M_init_facet(new (&money_put_w) money_put<wchar_t>(1));
433
434     typedef __timepunct_cache<wchar_t> time_cache_w;
435     time_cache_w* __tpw = new (&timepunct_cache_w) time_cache_w(2);
436     _M_init_facet(new (&timepunct_w) __timepunct<wchar_t>(__tpw, 1));
437
438     _M_init_facet(new (&time_get_w) time_get<wchar_t>(1));
439     _M_init_facet(new (&time_put_w) time_put<wchar_t>(1));
440     _M_init_facet(new (&messages_w) std::messages<wchar_t>(1));
441 #endif 
442      
443     // This locale is safe to pre-cache, after all the facets have
444     // been created and installed.
445     _M_caches[numpunct<char>::id._M_id()] = __npc;
446     _M_caches[moneypunct<char, false>::id._M_id()] = __mpcf;
447     _M_caches[moneypunct<char, true>::id._M_id()] = __mpct;
448     _M_caches[__timepunct<char>::id._M_id()] = __tpc;
449 #ifdef  _GLIBCXX_USE_WCHAR_T
450     _M_caches[numpunct<wchar_t>::id._M_id()] = __npw;
451     _M_caches[moneypunct<wchar_t, false>::id._M_id()] = __mpwf;
452     _M_caches[moneypunct<wchar_t, true>::id._M_id()] = __mpwt;
453     _M_caches[__timepunct<wchar_t>::id._M_id()] = __tpw;
454 #endif
455   }
456
457 _GLIBCXX_END_NAMESPACE