]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.3.3/src/localename.cc
update
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.3.3 / src / localename.cc
1 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
2 // 2006, 2007
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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 #include <clocale>
31 #include <cstring>
32 #include <cstdlib>
33 #include <locale>
34
35 _GLIBCXX_BEGIN_NAMESPACE(std)
36
37   using namespace __gnu_cxx;
38
39   locale::locale(const char* __s) : _M_impl(0)
40   {
41     if (__s)
42       {
43         _S_initialize(); 
44         if (std::strcmp(__s, "C") == 0 || std::strcmp(__s, "POSIX") == 0)
45           (_M_impl = _S_classic)->_M_add_reference();
46         else if (std::strcmp(__s, "") != 0)
47           _M_impl = new _Impl(__s, 1);
48         else
49           {
50             // Get it from the environment.
51             char* __env = std::getenv("LC_ALL");
52             // If LC_ALL is set we are done.
53             if (__env && std::strcmp(__env, "") != 0)
54               {
55                 if (std::strcmp(__env, "C") == 0 
56                     || std::strcmp(__env, "POSIX") == 0)
57                   (_M_impl = _S_classic)->_M_add_reference();
58                 else
59                   _M_impl = new _Impl(__env, 1);
60               }
61             else
62               {
63                 // LANG may set a default different from "C".
64                 string __lang;
65                 __env = std::getenv("LANG");
66                 if (!__env || std::strcmp(__env, "") == 0 
67                     || std::strcmp(__env, "C") == 0 
68                     || std::strcmp(__env, "POSIX") == 0)
69                   __lang = "C";
70                 else 
71                   __lang = __env;
72                 
73                 // Scan the categories looking for the first one
74                 // different from LANG.
75                 size_t __i = 0;
76                 if (__lang == "C")
77                   for (; __i < _S_categories_size; ++__i)
78                     {
79                       __env = std::getenv(_S_categories[__i]);
80                       if (__env && std::strcmp(__env, "") != 0 
81                           && std::strcmp(__env, "C") != 0 
82                           && std::strcmp(__env, "POSIX") != 0)
83                         break;
84                     }
85                 else
86                   for (; __i < _S_categories_size; ++__i)
87                     {
88                       __env = std::getenv(_S_categories[__i]);
89                       if (__env && std::strcmp(__env, "") != 0
90                           && __lang != __env)
91                         break;
92                     }
93         
94                 // If one is found, build the complete string of
95                 // the form LC_CTYPE=xxx;LC_NUMERIC=yyy; and so on...
96                 if (__i < _S_categories_size)
97                   {
98                     string __str;
99                     __str.reserve(128);
100                     for (size_t __j = 0; __j < __i; ++__j)
101                       {
102                         __str += _S_categories[__j];
103                         __str += '=';
104                         __str += __lang;
105                         __str += ';';
106                       }
107                     __str += _S_categories[__i];
108                     __str += '=';
109                     __str += __env;
110                     __str += ';';
111                     ++__i;
112                     for (; __i < _S_categories_size; ++__i)
113                       {
114                         __env = std::getenv(_S_categories[__i]);
115                         __str += _S_categories[__i];
116                         if (!__env || std::strcmp(__env, "") == 0)
117                           {
118                             __str += '=';
119                             __str += __lang;
120                             __str += ';';
121                           }
122                         else if (std::strcmp(__env, "C") == 0
123                                  || std::strcmp(__env, "POSIX") == 0)
124                           __str += "=C;";
125                         else
126                           {
127                             __str += '=';
128                             __str += __env;
129                             __str += ';';
130                           }
131                       }
132                     __str.erase(__str.end() - 1);
133                     _M_impl = new _Impl(__str.c_str(), 1);
134                   }
135                 // ... otherwise either an additional instance of
136                 // the "C" locale or LANG.
137                 else if (__lang == "C")
138                   (_M_impl = _S_classic)->_M_add_reference();
139                 else
140                   _M_impl = new _Impl(__lang.c_str(), 1);
141               }
142           }
143       }
144     else
145       __throw_runtime_error(__N("locale::locale NULL not valid"));
146   }
147
148   locale::locale(const locale& __base, const char* __s, category __cat)
149   : _M_impl(0)
150   { 
151     // NB: There are complicated, yet more efficient ways to do
152     // this. Building up locales on a per-category way is tedious, so
153     // let's do it this way until people complain.
154     locale __add(__s);
155     _M_coalesce(__base, __add, __cat);
156   }
157
158   locale::locale(const locale& __base, const locale& __add, category __cat)
159   : _M_impl(0)
160   { _M_coalesce(__base, __add, __cat); }
161
162   void
163   locale::_M_coalesce(const locale& __base, const locale& __add, 
164                       category __cat)
165   {
166     __cat = _S_normalize_category(__cat);  
167     _M_impl = new _Impl(*__base._M_impl, 1);  
168
169     try 
170       { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
171     catch (...) 
172       { 
173         _M_impl->_M_remove_reference(); 
174         __throw_exception_again;
175       }
176   }
177
178   // Construct named _Impl.
179   locale::_Impl::
180   _Impl(const char* __s, size_t __refs)
181   : _M_refcount(__refs), _M_facets(0), _M_facets_size(_GLIBCXX_NUM_FACETS),
182     _M_caches(0), _M_names(0)
183   {
184     // Initialize the underlying locale model, which also checks to
185     // see if the given name is valid.
186     __c_locale __cloc;
187     locale::facet::_S_create_c_locale(__cloc, __s);
188
189     try
190       {
191         _M_facets = new const facet*[_M_facets_size];
192         for (size_t __i = 0; __i < _M_facets_size; ++__i)
193           _M_facets[__i] = 0;
194         _M_caches = new const facet*[_M_facets_size];
195         for (size_t __j = 0; __j < _M_facets_size; ++__j)
196           _M_caches[__j] = 0;
197         _M_names = new char*[_S_categories_size];
198         for (size_t __k = 0; __k < _S_categories_size; ++__k)
199           _M_names[__k] = 0;
200
201         // Name the categories.
202         const size_t __len = std::strlen(__s);
203         if (!std::memchr(__s, ';', __len))
204           {
205             _M_names[0] = new char[__len + 1];
206             std::memcpy(_M_names[0], __s, __len + 1);       
207           }
208         else
209           {
210             const char* __end = __s;
211             for (size_t __i = 0; __i < _S_categories_size; ++__i)
212               {
213                 const char* __beg = std::strchr(__end + 1, '=') + 1;
214                 __end = std::strchr(__beg, ';');
215                 if (!__end)
216                   __end = __s + __len;
217                 _M_names[__i] = new char[__end - __beg + 1];
218                 std::memcpy(_M_names[__i], __beg, __end - __beg);
219                 _M_names[__i][__end - __beg] = '\0';
220               }
221           }
222  
223         // Construct all standard facets and add them to _M_facets.
224         _M_init_facet(new std::ctype<char>(__cloc, 0, false));
225         _M_init_facet(new codecvt<char, char, mbstate_t>(__cloc));
226         _M_init_facet(new numpunct<char>(__cloc));
227         _M_init_facet(new num_get<char>);
228         _M_init_facet(new num_put<char>);
229         _M_init_facet(new std::collate<char>(__cloc));
230         _M_init_facet(new moneypunct<char, false>(__cloc, __s));
231         _M_init_facet(new moneypunct<char, true>(__cloc, __s));
232         _M_init_facet(new money_get<char>);
233         _M_init_facet(new money_put<char>);
234         _M_init_facet(new __timepunct<char>(__cloc, __s));
235         _M_init_facet(new time_get<char>);
236         _M_init_facet(new time_put<char>);
237         _M_init_facet(new std::messages<char>(__cloc, __s));
238         
239 #ifdef  _GLIBCXX_USE_WCHAR_T
240         _M_init_facet(new std::ctype<wchar_t>(__cloc));
241         _M_init_facet(new codecvt<wchar_t, char, mbstate_t>(__cloc));
242         _M_init_facet(new numpunct<wchar_t>(__cloc));
243         _M_init_facet(new num_get<wchar_t>);
244         _M_init_facet(new num_put<wchar_t>);
245         _M_init_facet(new std::collate<wchar_t>(__cloc));
246         _M_init_facet(new moneypunct<wchar_t, false>(__cloc, __s));
247         _M_init_facet(new moneypunct<wchar_t, true>(__cloc, __s));
248         _M_init_facet(new money_get<wchar_t>);
249         _M_init_facet(new money_put<wchar_t>);
250         _M_init_facet(new __timepunct<wchar_t>(__cloc, __s));
251         _M_init_facet(new time_get<wchar_t>);
252         _M_init_facet(new time_put<wchar_t>);
253         _M_init_facet(new std::messages<wchar_t>(__cloc, __s));
254 #endif    
255         locale::facet::_S_destroy_c_locale(__cloc);
256       }
257     catch(...)
258       {
259         locale::facet::_S_destroy_c_locale(__cloc);
260         this->~_Impl();
261         __throw_exception_again;
262       } 
263   }
264
265   void
266   locale::_Impl::
267   _M_replace_categories(const _Impl* __imp, category __cat)
268   {
269     category __mask = 1;
270     const bool __have_names = _M_names[0] && __imp->_M_names[0];
271     for (size_t __ix = 0; __ix < _S_categories_size; ++__ix, __mask <<= 1)
272       {
273         if (__mask & __cat)
274           {
275             // Need to replace entry in _M_facets with other locale's info.
276             _M_replace_category(__imp, _S_facet_categories[__ix]);
277             // If both have names, go ahead and mangle.
278             if (__have_names)
279               {
280                 if (!_M_names[1])
281                   {
282                     // A full set of _M_names must be prepared, all identical
283                     // to _M_names[0] to begin with. Then, below, a few will
284                     // be replaced by the corresponding __imp->_M_names. I.e.,
285                     // not a "simple" locale anymore (see locale::operator==).
286                     const size_t __len = std::strlen(_M_names[0]) + 1;
287                     for (size_t __i = 1; __i < _S_categories_size; ++__i)
288                       {
289                         _M_names[__i] = new char[__len];
290                         std::memcpy(_M_names[__i], _M_names[0], __len);
291                       }
292                   }
293
294                 // FIXME: Hack for libstdc++/29217: the numerical encodings
295                 // of the time and collate categories are swapped vs the
296                 // order of the names in locale::_S_categories.  We'd like to
297                 // adjust the former (the latter is dictated by compatibility
298                 // with glibc) but we can't for binary compatibility.
299                 size_t __ix_name = __ix;
300                 if (__ix == 2 || __ix == 3)
301                   __ix_name = 5 - __ix;
302
303                 char* __src = __imp->_M_names[__ix_name] ?
304                               __imp->_M_names[__ix_name] : __imp->_M_names[0];
305                 const size_t __len = std::strlen(__src) + 1;
306                 char* __new = new char[__len];
307                 std::memcpy(__new, __src, __len);
308                 delete [] _M_names[__ix_name];
309                 _M_names[__ix_name] = __new;
310               }
311           }
312       }
313   }
314
315 _GLIBCXX_END_NAMESPACE