3 * Silicon Graphics Computer Systems, Inc.
8 * This material is provided "as is", with absolutely no warranty expressed
9 * or implied. Any use is at your own risk.
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.
19 // This file is #included into locale_impl.cpp, due to locale use many
20 // statics from locale_impl.cpp
24 #if defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)
25 # define locale _STLP_NO_MEM_T_NAME(loc)
28 locale::facet::~facet() {}
30 #if !defined (_STLP_MEMBER_TEMPLATES) || defined (_STLP_INLINE_MEMBER_TEMPLATES)
31 // members that fail to be templates
32 bool locale::operator()(const string& __x,
33 const string& __y) const
34 { return __locale_do_operator_call(*this, __x, __y); }
36 # if !defined (_STLP_NO_WCHAR_T)
37 bool locale::operator()(const wstring& __x,
38 const wstring& __y) const
39 { return __locale_do_operator_call(*this, __x, __y); }
43 void _STLP_CALL locale::_M_throw_runtime_error(const char* name) {
47 const char* prefix = "bad locale name: ";
48 #if !defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
50 strncat(buf, name, _STLP_ARRAY_SIZE(buf) - strlen(prefix));
51 buf[_STLP_ARRAY_SIZE(buf) - 1] = 0;
53 strcpy_s(_STLP_ARRAY_AND_SIZE(buf), prefix);
54 strncat_s(_STLP_ARRAY_AND_SIZE(buf), name, _TRUNCATE);
58 #if !defined (_STLP_USE_SAFE_STRING_FUNCTIONS)
59 strcpy(buf, "locale error");
61 strcpy_s(_STLP_ARRAY_AND_SIZE(buf), "locale error");
64 _STLP_THROW(runtime_error(buf));
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.
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);
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;
86 // Default constructor: create a copy of the global locale.
87 locale::locale() _STLP_NOTHROW
88 : _M_impl(_get_Locale_impl(_Stl_get_global_locale()->_M_impl))
92 locale::locale(const locale& L) _STLP_NOTHROW
93 : _M_impl( _get_Locale_impl( L._M_impl ) )
96 void locale::_M_insert(facet* f, locale::id& n) {
98 _M_impl->insert(f, _Stl_loc_get_index(n));
101 locale::locale( _Locale_impl* impl ) :
102 _M_impl( _get_Locale_impl( impl ) )
105 // Create a locale from a name.
106 locale::locale(const char* name)
109 _M_throw_runtime_error(0);
111 if (is_C_locale_name(name)) {
112 _M_impl = _get_Locale_impl( locale::classic()._M_impl );
116 _Locale_impl* impl = 0;
118 impl = new _Locale_impl(locale::id::_S_max, name);
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);
129 _M_impl = _get_Locale_impl( impl );
131 _STLP_UNWIND(delete impl);
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.
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)
141 else if ((c & locale::all) == locale::all)
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];
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);
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,
167 L->name = composite_buf;
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.
173 locale::locale(const locale& L, const char* name, locale::category c)
175 if (name == 0 || (_Nameless == name))
176 _M_throw_runtime_error(name);
178 _Locale_impl* impl = 0;
181 impl = new _Locale_impl(*L._M_impl);
182 _Stl_loc_combine_names(impl, L._M_impl->name.c_str(), name, c);
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 );
199 _STLP_UNWIND(delete impl)
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.
204 locale::locale(const locale& L1, const locale& L2, category c)
206 _Locale_impl* impl = new _Locale_impl(*L1._M_impl);
208 _Locale_impl* i2 = L2._M_impl;
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);
213 impl->name = _Nameless;
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);
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);
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);
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);
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);
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);
266 _M_impl = _get_Locale_impl( impl );
270 locale::~locale() _STLP_NOTHROW {
272 _release_Locale_impl(_M_impl);
275 // Assignment operator. Much like the copy constructor: just a bit of
276 // pointer twiddling.
277 const locale& locale::operator=(const locale& L) _STLP_NOTHROW {
278 if (this->_M_impl != L._M_impl) {
280 _release_Locale_impl(this->_M_impl);
281 this->_M_impl = _get_Locale_impl(L._M_impl);
286 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;
290 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);
293 _M_impl->_M_throw_bad_cast();
297 string locale::name() const {
298 return _M_impl->name;
301 // Compare two locales for equality.
302 bool locale::operator==(const locale& L) const {
303 return this->_M_impl == L._M_impl ||
304 (this->name() == L.name() && this->name() != _Nameless);
307 bool locale::operator!=(const locale& L) const {
308 return !(*this == L);
311 // static data members.
313 const locale& _STLP_CALL locale::classic() {
314 return *_Stl_get_classic_locale();
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);
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());
334 # if !defined (_STLP_STATIC_CONST_INIT_BUG) && ! defined (_STLP_USE_DECLSPEC)
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;