1 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
2 // Free Software Foundation, Inc.
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)
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,
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.
37 // Defined in globals.cc.
38 extern locale::facet* facet_vec[_GLIBCPP_NUM_FACETS];
39 extern char* facet_name[6 + _GLIBCPP_NUM_CATEGORIES];
41 extern std::ctype<char> ctype_c;
42 extern std::collate<char> collate_c;
43 extern numpunct<char> numpunct_c;
44 extern num_get<char> num_get_c;
45 extern num_put<char> num_put_c;
46 extern codecvt<char, char, mbstate_t> codecvt_c;
47 extern moneypunct<char, false> moneypunct_fc;
48 extern moneypunct<char, true> moneypunct_tc;
49 extern money_get<char> money_get_c;
50 extern money_put<char> money_put_c;
51 extern __timepunct<char> timepunct_c;
52 extern time_get<char> time_get_c;
53 extern time_put<char> time_put_c;
54 extern std::messages<char> messages_c;
55 #ifdef _GLIBCPP_USE_WCHAR_T
56 extern std::ctype<wchar_t> ctype_w;
57 extern std::collate<wchar_t> collate_w;
58 extern numpunct<wchar_t> numpunct_w;
59 extern num_get<wchar_t> num_get_w;
60 extern num_put<wchar_t> num_put_w;
61 extern codecvt<wchar_t, char, mbstate_t> codecvt_w;
62 extern moneypunct<wchar_t, false> moneypunct_fw;
63 extern moneypunct<wchar_t, true> moneypunct_tw;
64 extern money_get<wchar_t> money_get_w;
65 extern money_put<wchar_t> money_put_w;
66 extern __timepunct<wchar_t> timepunct_w;
67 extern time_get<wchar_t> time_get_w;
68 extern time_put<wchar_t> time_put_w;
69 extern std::messages<wchar_t> messages_w;
71 } // namespace __gnu_cxx
75 using namespace __gnu_cxx;
80 for (size_t __i = 0; __i < _M_facets_size; ++__i)
82 _M_facets[__i]->_M_remove_reference();
85 for (size_t __i = 0; __i < _S_categories_size ; ++__i)
86 delete [] _M_names[__i];
89 // Clone existing _Impl object.
91 _Impl(const _Impl& __imp, size_t __refs)
92 : _M_references(__refs), _M_facets_size(__imp._M_facets_size) // XXX
96 _M_facets = new const facet*[_M_facets_size];
97 for (size_t __i = 0; __i < _M_facets_size; ++__i)
103 __throw_exception_again;
105 for (size_t __i = 0; __i < _M_facets_size; ++__i)
107 _M_facets[__i] = __imp._M_facets[__i];
109 _M_facets[__i]->_M_add_reference();
111 for (size_t __i = 0; __i < _S_categories_size ; ++__i)
113 char* __new = new char[strlen(__imp._M_names[__i]) + 1];
114 strcpy(__new, __imp._M_names[__i]);
115 _M_names[__i] = __new;
119 // Construct named _Impl.
121 _Impl(const char* __s, size_t __refs)
122 : _M_references(__refs), _M_facets_size(_GLIBCPP_NUM_FACETS)
124 // Initialize the underlying locale model, which also checks
125 // to see if the given name is valid.
127 locale::facet::_S_create_c_locale(__cloc, __s);
131 _M_facets = new const facet*[_M_facets_size];
132 for (size_t __i = 0; __i < _M_facets_size; ++__i)
138 __throw_exception_again;
141 // Name all the categories.
142 size_t __len = strlen(__s);
143 if (!strchr(__s, ';'))
145 for (size_t __i = 0; __i < _S_categories_size ; ++__i)
147 _M_names[__i] = new char[__len + 1];
148 strcpy(_M_names[__i], __s);
153 const char* __beg = __s;
154 for (size_t __i = 0; __i < _S_categories_size ; ++__i)
156 __beg = strchr(__beg, '=') + 1;
157 const char* __end = strchr(__beg, ';');
160 char* __new = new char[__end - __beg + 1];
161 memcpy(__new, __beg, __end - __beg);
162 __new[__end - __beg] = '\0';
163 _M_names[__i] = __new;
167 // Construct all standard facets and add them to _M_facets.
168 _M_init_facet(new std::ctype<char>(__cloc, 0, false));
169 _M_init_facet(new codecvt<char, char, mbstate_t>(__cloc));
170 _M_init_facet(new numpunct<char>(__cloc));
171 _M_init_facet(new num_get<char>);
172 _M_init_facet(new num_put<char>);
173 _M_init_facet(new std::collate<char>(__cloc));
174 _M_init_facet(new moneypunct<char, false>(__cloc, __s));
175 _M_init_facet(new moneypunct<char, true>(__cloc, __s));
176 _M_init_facet(new money_get<char>);
177 _M_init_facet(new money_put<char>);
178 _M_init_facet(new __timepunct<char>(__cloc, __s));
179 _M_init_facet(new time_get<char>);
180 _M_init_facet(new time_put<char>);
181 _M_init_facet(new std::messages<char>(__cloc, __s));
183 #ifdef _GLIBCPP_USE_WCHAR_T
184 _M_init_facet(new std::ctype<wchar_t>(__cloc));
185 _M_init_facet(new codecvt<wchar_t, char, mbstate_t>(__cloc));
186 _M_init_facet(new numpunct<wchar_t>(__cloc));
187 _M_init_facet(new num_get<wchar_t>);
188 _M_init_facet(new num_put<wchar_t>);
189 _M_init_facet(new std::collate<wchar_t>(__cloc));
190 _M_init_facet(new moneypunct<wchar_t, false>(__cloc, __s));
191 _M_init_facet(new moneypunct<wchar_t, true>(__cloc, __s));
192 _M_init_facet(new money_get<wchar_t>);
193 _M_init_facet(new money_put<wchar_t>);
194 _M_init_facet(new __timepunct<wchar_t>(__cloc, __s));
195 _M_init_facet(new time_get<wchar_t>);
196 _M_init_facet(new time_put<wchar_t>);
197 _M_init_facet(new std::messages<wchar_t>(__cloc, __s));
199 locale::facet::_S_destroy_c_locale(__cloc);
202 // Construct "C" _Impl.
204 _Impl(facet**, size_t __refs, bool)
205 : _M_references(__refs), _M_facets_size(_GLIBCPP_NUM_FACETS)
207 // Initialize the underlying locale model.
208 locale::facet::_S_c_name[0] = 'C';
209 locale::facet::_S_c_name[1] = '\0';
210 locale::facet::_S_create_c_locale(locale::facet::_S_c_locale,
211 locale::facet::_S_c_name);
213 _M_facets = new(&facet_vec) const facet*[_M_facets_size];
214 for (size_t __i = 0; __i < _M_facets_size; ++__i)
217 // Name all the categories.
218 for (size_t __i = 0; __i < _S_categories_size ; ++__i)
220 _M_names[__i] = new (&facet_name[__i]) char[2];
221 strcpy(_M_names[__i], locale::facet::_S_c_name);
224 // This is needed as presently the C++ version of "C" locales
225 // != data in the underlying locale model for __timepunct,
226 // numpunct, and moneypunct. Also, the "C" locales must be
227 // constructed in a way such that they are pre-allocated.
228 // NB: Set locale::facets(ref) count to one so that each individual
229 // facet is not destroyed when the locale (and thus locale::_Impl) is
231 _M_init_facet(new (&ctype_c) std::ctype<char>(0, false, 1));
232 _M_init_facet(new (&codecvt_c) codecvt<char, char, mbstate_t>(1));
233 _M_init_facet(new (&numpunct_c) numpunct<char>(1));
234 _M_init_facet(new (&num_get_c) num_get<char>(1));
235 _M_init_facet(new (&num_put_c) num_put<char>(1));
236 _M_init_facet(new (&collate_c) std::collate<char>(1));
237 _M_init_facet(new (&moneypunct_fc) moneypunct<char, false>(1));
238 _M_init_facet(new (&moneypunct_tc) moneypunct<char, true>(1));
239 _M_init_facet(new (&money_get_c) money_get<char>(1));
240 _M_init_facet(new (&money_put_c) money_put<char>(1));
241 _M_init_facet(new (&timepunct_c) __timepunct<char>(1));
242 _M_init_facet(new (&time_get_c) time_get<char>(1));
243 _M_init_facet(new (&time_put_c) time_put<char>(1));
244 _M_init_facet(new (&messages_c) std::messages<char>(1));
245 #ifdef _GLIBCPP_USE_WCHAR_T
246 _M_init_facet(new (&ctype_w) std::ctype<wchar_t>(1));
247 _M_init_facet(new (&codecvt_w) codecvt<wchar_t, char, mbstate_t>(1));
248 _M_init_facet(new (&numpunct_w) numpunct<wchar_t>(1));
249 _M_init_facet(new (&num_get_w) num_get<wchar_t>(1));
250 _M_init_facet(new (&num_put_w) num_put<wchar_t>(1));
251 _M_init_facet(new (&collate_w) std::collate<wchar_t>(1));
252 _M_init_facet(new (&moneypunct_fw) moneypunct<wchar_t, false>(1));
253 _M_init_facet(new (&moneypunct_tw) moneypunct<wchar_t, true>(1));
254 _M_init_facet(new (&money_get_w) money_get<wchar_t>(1));
255 _M_init_facet(new (&money_put_w) money_put<wchar_t>(1));
256 _M_init_facet(new (&timepunct_w) __timepunct<wchar_t>(1));
257 _M_init_facet(new (&time_get_w) time_get<wchar_t>(1));
258 _M_init_facet(new (&time_put_w) time_put<wchar_t>(1));
259 _M_init_facet(new (&messages_w) std::messages<wchar_t>(1));
265 _M_replace_categories(const _Impl* __imp, category __cat)
268 for (size_t __ix = 0; __ix < _S_categories_size; ++__ix)
273 // Need to replace entry in _M_facets with other locale's info.
274 _M_replace_category(__imp, _S_facet_categories[__ix]);
275 // If both have names, go ahead and mangle.
276 if (strcmp(_M_names[__ix], "*") != 0
277 && strcmp(__imp->_M_names[__ix], "*") != 0)
279 delete [] _M_names[__ix];
280 char* __new = new char[strlen(__imp->_M_names[__ix]) + 1];
281 strcpy(__new, __imp->_M_names[__ix]);
282 _M_names[__ix] = __new;
290 _M_replace_category(const _Impl* __imp, const locale::id* const* __idpp)
292 for (; *__idpp; ++__idpp)
293 _M_replace_facet(__imp, *__idpp);
298 _M_replace_facet(const _Impl* __imp, const locale::id* __idp)
300 size_t __index = __idp->_M_id();
301 if ((__index > (__imp->_M_facets_size - 1)) || !__imp->_M_facets[__index])
302 __throw_runtime_error("no locale facet");
303 _M_install_facet(__idp, __imp->_M_facets[__index]);
308 _M_install_facet(const locale::id* __idp, const facet* __fp)
312 size_t __index = __idp->_M_id();
314 // Check size of facet vector to ensure adequate room.
315 if (__index > _M_facets_size - 1)
317 const facet** __old = _M_facets;
319 const size_t __new_size = __index + 4;
320 __new = new const facet*[__new_size];
321 for (size_t __i = 0; __i < _M_facets_size; ++__i)
322 __new[__i] = _M_facets[__i];
323 for (size_t __i2 = _M_facets_size; __i2 < __new_size; ++__i2)
326 _M_facets_size = __new_size;
331 __fp->_M_add_reference();
332 const facet*& __fpr = _M_facets[__index];
335 // Replacing an existing facet. Order matters.
336 __fpr->_M_remove_reference();
341 // Installing a newly created facet into an empty
342 // _M_facets container, say a newly-constructed,
343 // swanky-fresh _Impl.
344 _M_facets[__index] = __fp;