* config/xtensa/lib1funcs.asm (__umodsi3, __modsi3): Rearrange so that
[official-gcc.git] / libstdc++-v3 / src / localename.cc
blob5394d9a99c69f408ffef8fb0d21d003c7fa82ab3
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.
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 // 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.
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.
30 #include <clocale>
31 #include <cstring>
32 #include <cstdlib>
33 #include <locale>
35 _GLIBCXX_BEGIN_NAMESPACE(std)
37 using namespace __gnu_cxx;
39 locale::locale(const char* __s) : _M_impl(0)
41 if (__s)
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
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)
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);
61 else
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;
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)
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;
85 else
86 for (; __i < _S_categories_size; ++__i)
88 __env = std::getenv(_S_categories[__i]);
89 if (__env && std::strcmp(__env, "") != 0
90 && __lang != __env)
91 break;
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)
98 string __str;
99 __str.reserve(128);
100 for (size_t __j = 0; __j < __i; ++__j)
102 __str += _S_categories[__j];
103 __str += '=';
104 __str += __lang;
105 __str += ';';
107 __str += _S_categories[__i];
108 __str += '=';
109 __str += __env;
110 __str += ';';
111 ++__i;
112 for (; __i < _S_categories_size; ++__i)
114 __env = std::getenv(_S_categories[__i]);
115 __str += _S_categories[__i];
116 if (!__env || std::strcmp(__env, "") == 0)
118 __str += '=';
119 __str += __lang;
120 __str += ';';
122 else if (std::strcmp(__env, "C") == 0
123 || std::strcmp(__env, "POSIX") == 0)
124 __str += "=C;";
125 else
127 __str += '=';
128 __str += __env;
129 __str += ';';
132 __str.erase(__str.end() - 1);
133 _M_impl = new _Impl(__str.c_str(), 1);
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);
144 else
145 __throw_runtime_error(__N("locale::locale NULL not valid"));
148 locale::locale(const locale& __base, const char* __s, category __cat)
149 : _M_impl(0)
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);
158 locale::locale(const locale& __base, const locale& __add, category __cat)
159 : _M_impl(0)
160 { _M_coalesce(__base, __add, __cat); }
162 void
163 locale::_M_coalesce(const locale& __base, const locale& __add,
164 category __cat)
166 __cat = _S_normalize_category(__cat);
167 _M_impl = new _Impl(*__base._M_impl, 1);
169 try
170 { _M_impl->_M_replace_categories(__add._M_impl, __cat); }
171 catch (...)
173 _M_impl->_M_remove_reference();
174 __throw_exception_again;
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)
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);
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;
201 // Name the categories.
202 const size_t __len = std::strlen(__s);
203 if (!std::memchr(__s, ';', __len))
205 _M_names[0] = new char[__len + 1];
206 std::memcpy(_M_names[0], __s, __len + 1);
208 else
210 const char* __end = __s;
211 for (size_t __i = 0; __i < _S_categories_size; ++__i)
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';
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));
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);
257 catch(...)
259 locale::facet::_S_destroy_c_locale(__cloc);
260 this->~_Impl();
261 __throw_exception_again;
265 void
266 locale::_Impl::
267 _M_replace_categories(const _Impl* __imp, category __cat)
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)
273 if (__mask & __cat)
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)
280 if (!_M_names[1])
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)
289 _M_names[__i] = new char[__len];
290 std::memcpy(_M_names[__i], _M_names[0], __len);
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;
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;
315 _GLIBCXX_END_NAMESPACE