1 /*
2 * Copyright (c) 1999
3 * Silicon Graphics Computer Systems, Inc.
4 *
5 * Copyright (c) 1999
6 * Boris Fomitchev
7 *
8 * This material is provided "as is", with absolutely no warranty expressed
9 * or implied. Any use is at your own risk.
10 *
11 * Permission to use or copy this software for any purpose is hereby granted
12 * without fee, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
16 *
17 */
18
19 // This file is #included into locale_impl.cpp, due to locale use many
20 // statics from locale_impl.cpp
21
22 _STLP_BEGIN_NAMESPACE
23
24 #if defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)
25 # define locale _STLP_NO_MEM_T_NAME(loc)
26 #endif
27
~facet()28 locale::facet::~facet() {}
29
30 #if !defined (_STLP_MEMBER_TEMPLATES) || defined (_STLP_INLINE_MEMBER_TEMPLATES)
31 // members that fail to be templates
operator ()(const string & __x,const string & __y) const32 bool locale::operator()(const string& __x,
33 const string& __y) const
34 { return __locale_do_operator_call(*this, __x, __y); }
35
36 # if !defined (_STLP_NO_WCHAR_T)
operator ()(const wstring & __x,const wstring & __y) const37 bool locale::operator()(const wstring& __x,
38 const wstring& __y) const
39 { return __locale_do_operator_call(*this, __x, __y); }
40 # endif
41 #endif
42
_M_throw_runtime_error(const char * name)43 void _STLP_CALL locale::_M_throw_runtime_error(const char* name) {
44 char buf[256];
45
46 if (name) {
47 const char* prefix = "bad locale name: ";
48 #if !defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
49 strcpy(buf, prefix);
50 strncat(buf, name, _STLP_ARRAY_SIZE(buf) - strlen(prefix));
51 buf[_STLP_ARRAY_SIZE(buf) - 1] = 0;
52 #else
53 strcpy_s(_STLP_ARRAY_AND_SIZE(buf), prefix);
54 strncat_s(_STLP_ARRAY_AND_SIZE(buf), name, _TRUNCATE);
55 #endif
56 }
57 else {
58 #if !defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
59 strcpy(buf, "locale error");
60 #else
61 strcpy_s(_STLP_ARRAY_AND_SIZE(buf), "locale error");
62 #endif
63 }
64 _STLP_THROW(runtime_error(buf));
65 }
66
67 // Takes a reference to a locale::id, and returns its numeric index.
68 // If no numeric index has yet been assigned, assigns one. The return
69 // value is always positive.
_Stl_loc_get_index(locale::id & id)70 static size_t _Stl_loc_get_index(locale::id& id) {
71 if (id._M_index == 0) {
72 #if defined (_STLP_ATOMIC_INCREMENT) && \
73 (!defined (_STLP_WIN32_VERSION) || (_STLP_WIN32_VERSION > 0x0400))
74 static _STLP_VOLATILE __stl_atomic_t _S_index = __STATIC_CAST(__stl_atomic_t, locale::id::_S_max);
75 id._M_index = _STLP_ATOMIC_INCREMENT(&_S_index);
76 #else
77 static _STLP_STATIC_MUTEX _Index_lock _STLP_MUTEX_INITIALIZER;
78 _STLP_auto_lock sentry(_Index_lock);
79 size_t new_index = locale::id::_S_max++;
80 id._M_index = new_index;
81 #endif
82 }
83 return id._M_index;
84 }
85
86 // Default constructor: create a copy of the global locale.
locale()87 locale::locale() _STLP_NOTHROW
88 : _M_impl(_get_Locale_impl(_Stl_get_global_locale()->_M_impl))
89 {}
90
91 // Copy constructor
locale(const locale & L)92 locale::locale(const locale& L) _STLP_NOTHROW
93 : _M_impl( _get_Locale_impl( L._M_impl ) )
94 {}
95
_M_insert(facet * f,locale::id & n)96 void locale::_M_insert(facet* f, locale::id& n) {
97 if (f)
98 _M_impl->insert(f, _Stl_loc_get_index(n));
99 }
100
locale(_Locale_impl * impl)101 locale::locale( _Locale_impl* impl ) :
102 _M_impl( _get_Locale_impl( impl ) )
103 {}
104
105 // Create a locale from a name.
locale(const char * name)106 locale::locale(const char* name)
107 : _M_impl(0) {
108 if (!name)
109 _M_throw_runtime_error(0);
110
111 if (is_C_locale_name(name)) {
112 _M_impl = _get_Locale_impl( locale::classic()._M_impl );
113 return;
114 }
115
116 _Locale_impl* impl = 0;
117 _STLP_TRY {
118 impl = _CHECK_PTR(new _Locale_impl(locale::id::_S_max, name));
119
120 // Insert categories one at a time.
121 _Locale_name_hint *hint = 0;
122 hint = impl->insert_ctype_facets(name, hint);
123 hint = impl->insert_numeric_facets(name, hint);
124 hint = impl->insert_time_facets(name, hint);
125 hint = impl->insert_collate_facets(name, hint);
126 hint = impl->insert_monetary_facets(name, hint);
127 impl->insert_messages_facets(name, hint);
128 // reassign impl
129 _M_impl = _get_Locale_impl( impl );
130 }
131 _STLP_UNWIND(delete impl);
132 }
133
134 // Give L a name where all facets except those in category c
135 // are taken from name1, and those in category c are taken from name2.
_Stl_loc_combine_names(_Locale_impl * L,const char * name1,const char * name2,locale::category c)136 static void _Stl_loc_combine_names(_Locale_impl* L,
137 const char* name1, const char* name2,
138 locale::category c) {
139 if ((c & locale::all) == 0 || strcmp(name1, name2) == 0)
140 L->name = name1;
141 else if ((c & locale::all) == locale::all)
142 L->name = name2;
143 else {
144 // Decompose the names.
145 char ctype_buf[_Locale_MAX_SIMPLE_NAME];
146 char numeric_buf[_Locale_MAX_SIMPLE_NAME];
147 char time_buf[_Locale_MAX_SIMPLE_NAME];
148 char collate_buf[_Locale_MAX_SIMPLE_NAME];
149 char monetary_buf[_Locale_MAX_SIMPLE_NAME];
150 char messages_buf[_Locale_MAX_SIMPLE_NAME];
151
152 // TODO: check returnvalues?
153 _Locale_extract_ctype_name((c & locale::ctype) ? name2 : name1, ctype_buf, 0);
154 _Locale_extract_numeric_name((c & locale::numeric) ? name2 : name1, numeric_buf, 0);
155 _Locale_extract_time_name((c & locale::time) ? name2 : name1, time_buf, 0);
156 _Locale_extract_collate_name((c & locale::collate) ? name2 : name1, collate_buf, 0);
157 _Locale_extract_monetary_name((c & locale::monetary) ? name2 : name1, monetary_buf, 0);
158 _Locale_extract_messages_name((c & locale::messages) ? name2 : name1, messages_buf, 0);
159
160 // Construct a new composite name.
161 char composite_buf[_Locale_MAX_COMPOSITE_NAME];
162 // TODO: check returnvalue?
163 _Locale_compose_name(composite_buf,
164 ctype_buf, numeric_buf, time_buf,
165 collate_buf, monetary_buf, messages_buf,
166 name1);
167 L->name = composite_buf;
168 }
169 }
170
171 // Create a locale that's a copy of L, except that all of the facets
172 // in category c are instead constructed by name.
locale(const locale & L,const char * name,locale::category c)173 locale::locale(const locale& L, const char* name, locale::category c)
174 : _M_impl(0) {
175 if (name == 0 || (_Nameless == name))
176 _M_throw_runtime_error(name);
177
178 _Locale_impl* impl = 0;
179
180 _STLP_TRY {
181 impl = _CHECK_PTR(new _Locale_impl(*L._M_impl));
182 _Stl_loc_combine_names(impl, L._M_impl->name.c_str(), name, c);
183
184 _Locale_name_hint *hint = 0;
185 if (c & locale::ctype)
186 hint = impl->insert_ctype_facets(name, hint);
187 if (c & locale::numeric)
188 hint = impl->insert_numeric_facets(name, hint);
189 if (c & locale::time)
190 hint = impl->insert_time_facets(name, hint);
191 if (c & locale::collate)
192 hint = impl->insert_collate_facets(name, hint);
193 if (c & locale::monetary)
194 hint = impl->insert_monetary_facets(name, hint);
195 if (c & locale::messages)
196 impl->insert_messages_facets(name, hint);
197 _M_impl = _get_Locale_impl( impl );
198 }
199 _STLP_UNWIND(delete impl)
200 }
201
202 // Contruct a new locale where all facets that aren't in category c
203 // come from L1, and all those that are in category c come from L2.
locale(const locale & L1,const locale & L2,category c)204 locale::locale(const locale& L1, const locale& L2, category c)
205 : _M_impl(0) {
206 _Locale_impl* impl = _CHECK_PTR(new _Locale_impl(*L1._M_impl));
207
208 _Locale_impl* i2 = L2._M_impl;
209
210 if (L1.name() != _Nameless && L2.name() != _Nameless)
211 _Stl_loc_combine_names(impl, L1._M_impl->name.c_str(), L2._M_impl->name.c_str(), c);
212 else {
213 impl->name = _Nameless;
214 }
215
216 if (c & collate) {
217 impl->insert( i2, _STLP_STD::collate<char>::id);
218 # ifndef _STLP_NO_WCHAR_T
219 impl->insert( i2, _STLP_STD::collate<wchar_t>::id);
220 # endif
221 }
222 if (c & ctype) {
223 impl->insert( i2, _STLP_STD::ctype<char>::id);
224 impl->insert( i2, _STLP_STD::codecvt<char, char, mbstate_t>::id);
225 # ifndef _STLP_NO_WCHAR_T
226 impl->insert( i2, _STLP_STD::ctype<wchar_t>::id);
227 impl->insert( i2, _STLP_STD::codecvt<wchar_t, char, mbstate_t>::id);
228 # endif
229 }
230 if (c & monetary) {
231 impl->insert( i2, _STLP_STD::moneypunct<char, true>::id);
232 impl->insert( i2, _STLP_STD::moneypunct<char, false>::id);
233 impl->insert( i2, _STLP_STD::money_get<char, istreambuf_iterator<char, char_traits<char> > >::id);
234 impl->insert( i2, _STLP_STD::money_put<char, ostreambuf_iterator<char, char_traits<char> > >::id);
235 # ifndef _STLP_NO_WCHAR_T
236 impl->insert( i2, _STLP_STD::moneypunct<wchar_t, true>::id);
237 impl->insert( i2, _STLP_STD::moneypunct<wchar_t, false>::id);
238 impl->insert( i2, _STLP_STD::money_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id);
239 impl->insert( i2, _STLP_STD::money_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id);
240 # endif
241 }
242 if (c & numeric) {
243 impl->insert( i2, _STLP_STD::numpunct<char>::id);
244 impl->insert( i2, _STLP_STD::num_get<char, istreambuf_iterator<char, char_traits<char> > >::id);
245 impl->insert( i2, _STLP_STD::num_put<char, ostreambuf_iterator<char, char_traits<char> > >::id);
246 # ifndef _STLP_NO_WCHAR_T
247 impl->insert( i2, _STLP_STD::numpunct<wchar_t>::id);
248 impl->insert( i2, num_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id);
249 impl->insert( i2, num_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id);
250 # endif
251 }
252 if (c & time) {
253 impl->insert( i2, _STLP_STD::time_get<char, istreambuf_iterator<char, char_traits<char> > >::id);
254 impl->insert( i2, _STLP_STD::time_put<char, ostreambuf_iterator<char, char_traits<char> > >::id);
255 # ifndef _STLP_NO_WCHAR_T
256 impl->insert( i2, _STLP_STD::time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id);
257 impl->insert( i2, _STLP_STD::time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id);
258 # endif
259 }
260 if (c & messages) {
261 impl->insert( i2, _STLP_STD::messages<char>::id);
262 # ifndef _STLP_NO_WCHAR_T
263 impl->insert( i2, _STLP_STD::messages<wchar_t>::id);
264 # endif
265 }
266 _M_impl = _get_Locale_impl( impl );
267 }
268
269 // Destructor.
~locale()270 locale::~locale() _STLP_NOTHROW {
271 if (_M_impl)
272 _release_Locale_impl(_M_impl);
273 }
274
275 // Assignment operator. Much like the copy constructor: just a bit of
276 // pointer twiddling.
operator =(const locale & L)277 const locale& locale::operator=(const locale& L) _STLP_NOTHROW {
278 if (this->_M_impl != L._M_impl) {
279 if (this->_M_impl)
280 _release_Locale_impl(this->_M_impl);
281 this->_M_impl = _get_Locale_impl(L._M_impl);
282 }
283 return *this;
284 }
285
_M_get_facet(const locale::id & n) const286 locale::facet* locale::_M_get_facet(const locale::id& n) const {
287 return n._M_index < _M_impl->size() ? _M_impl->facets_vec[n._M_index] : 0;
288 }
289
_M_use_facet(const locale::id & n) const290 locale::facet* locale::_M_use_facet(const locale::id& n) const {
291 locale::facet* f = (n._M_index < _M_impl->size() ? _M_impl->facets_vec[n._M_index] : 0);
292 if (!f)
293 _M_impl->_M_throw_bad_cast();
294 return f;
295 }
296
name() const297 string locale::name() const {
298 return _M_impl->name;
299 }
300
301 // Compare two locales for equality.
operator ==(const locale & L) const302 bool locale::operator==(const locale& L) const {
303 return this->_M_impl == L._M_impl ||
304 (this->name() == L.name() && this->name() != _Nameless);
305 }
306
operator !=(const locale & L) const307 bool locale::operator!=(const locale& L) const {
308 return !(*this == L);
309 }
310
311 // static data members.
312
classic()313 const locale& _STLP_CALL locale::classic() {
314 return *_Stl_get_classic_locale();
315 }
316
global(const locale & L)317 locale _STLP_CALL locale::global(const locale& L) {
318 locale old(_Stl_get_global_locale()->_M_impl);
319 if (_Stl_get_global_locale()->_M_impl != L._M_impl) {
320 _release_Locale_impl(_Stl_get_global_locale()->_M_impl);
321 // this assign should be atomic, should be fixed here:
322 _Stl_get_global_locale()->_M_impl = _get_Locale_impl(L._M_impl);
323
324 // Set the global C locale, if appropriate.
325 #if !defined(_STLP_NO_LOCALE_SUPPORT)
326 if (L.name() != _Nameless)
327 setlocale(LC_ALL, L.name().c_str());
328 #endif
329 }
330
331 return old;
332 }
333
334 # if !defined (_STLP_STATIC_CONST_INIT_BUG) && ! defined (_STLP_USE_DECLSPEC)
335
336 const locale::category locale::none;
337 const locale::category locale::collate;
338 const locale::category locale::ctype;
339 const locale::category locale::monetary;
340 const locale::category locale::numeric;
341 const locale::category locale::time;
342 const locale::category locale::messages;
343 const locale::category locale::all;
344
345 # endif
346
347 _STLP_END_NAMESPACE
348
349