1 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 // Free Software Foundation, Inc.
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)
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,
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.
35 _GLIBCXX_BEGIN_NAMESPACE(std
)
37 using namespace __gnu_cxx
;
39 locale::locale(const char* __s
) : _M_impl(0)
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);
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();
59 _M_impl
= new _Impl(__env
, 1);
63 // LANG may set a default different from "C".
65 __env
= std::getenv("LANG");
66 if (!__env
|| std::strcmp(__env
, "") == 0
67 || std::strcmp(__env
, "C") == 0
68 || std::strcmp(__env
, "POSIX") == 0)
73 // Scan the categories looking for the first one
74 // different from LANG.
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)
86 for (; __i
< _S_categories_size
; ++__i
)
88 __env
= std::getenv(_S_categories
[__i
]);
89 if (__env
&& std::strcmp(__env
, "") != 0
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
)
100 for (size_t __j
= 0; __j
< __i
; ++__j
)
102 __str
+= _S_categories
[__j
];
107 __str
+= _S_categories
[__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)
122 else if (std::strcmp(__env
, "C") == 0
123 || std::strcmp(__env
, "POSIX") == 0)
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();
140 _M_impl
= new _Impl(__lang
.c_str(), 1);
145 __throw_runtime_error(__N("locale::locale NULL not valid"));
148 locale::locale(const locale
& __base
, const char* __s
, category __cat
)
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.
155 _M_coalesce(__base
, __add
, __cat
);
158 locale::locale(const locale
& __base
, const locale
& __add
, category __cat
)
160 { _M_coalesce(__base
, __add
, __cat
); }
163 locale::_M_coalesce(const locale
& __base
, const locale
& __add
,
166 __cat
= _S_normalize_category(__cat
);
167 _M_impl
= new _Impl(*__base
._M_impl
, 1);
170 { _M_impl
->_M_replace_categories(__add
._M_impl
, __cat
); }
173 _M_impl
->_M_remove_reference();
174 __throw_exception_again
;
178 // Construct named _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.
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
)
194 _M_caches
= new const facet
*[_M_facets_size
];
195 for (size_t __j
= 0; __j
< _M_facets_size
; ++__j
)
197 _M_names
= new char*[_S_categories_size
];
198 for (size_t __k
= 0; __k
< _S_categories_size
; ++__k
)
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);
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
, ';');
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
));
255 locale::facet::_S_destroy_c_locale(__cloc
);
259 locale::facet::_S_destroy_c_locale(__cloc
);
261 __throw_exception_again
;
267 _M_replace_categories(const _Impl
* __imp
, category __cat
)
270 if (!_M_names
[0] || !__imp
->_M_names
[0])
274 delete [] _M_names
[0];
275 _M_names
[0] = 0; // Unnamed.
278 for (size_t __ix
= 0; __ix
< _S_categories_size
; ++__ix
, __mask
<<= 1)
281 // Need to replace entry in _M_facets with other locale's info.
282 _M_replace_category(__imp
, _S_facet_categories
[__ix
]);
289 // A full set of _M_names must be prepared, all identical
290 // to _M_names[0] to begin with. Then, below, a few will
291 // be replaced by the corresponding __imp->_M_names. I.e.,
292 // not a "simple" locale anymore (see locale::operator==).
293 const size_t __len
= std::strlen(_M_names
[0]) + 1;
294 for (size_t __i
= 1; __i
< _S_categories_size
; ++__i
)
296 _M_names
[__i
] = new char[__len
];
297 std::memcpy(_M_names
[__i
], _M_names
[0], __len
);
301 for (size_t __ix
= 0; __ix
< _S_categories_size
; ++__ix
, __mask
<<= 1)
305 // Need to replace entry in _M_facets with other locale's info.
306 _M_replace_category(__imp
, _S_facet_categories
[__ix
]);
308 // FIXME: Hack for libstdc++/29217: the numerical encodings
309 // of the time and collate categories are swapped vs the
310 // order of the names in locale::_S_categories. We'd like to
311 // adjust the former (the latter is dictated by compatibility
312 // with glibc) but we can't for binary compatibility.
313 size_t __ix_name
= __ix
;
314 if (__ix
== 2 || __ix
== 3)
315 __ix_name
= 5 - __ix
;
317 char* __src
= __imp
->_M_names
[__ix_name
] ?
318 __imp
->_M_names
[__ix_name
] : __imp
->_M_names
[0];
319 const size_t __len
= std::strlen(__src
) + 1;
320 char* __new
= new char[__len
];
321 std::memcpy(__new
, __src
, __len
);
322 delete [] _M_names
[__ix_name
];
323 _M_names
[__ix_name
] = __new
;
329 _GLIBCXX_END_NAMESPACE