michael@0: /* michael@0: * Copyright (c) 1999 michael@0: * Silicon Graphics Computer Systems, Inc. michael@0: * michael@0: * Copyright (c) 1999 michael@0: * Boris Fomitchev michael@0: * michael@0: * This material is provided "as is", with absolutely no warranty expressed michael@0: * or implied. Any use is at your own risk. michael@0: * michael@0: * Permission to use or copy this software for any purpose is hereby granted michael@0: * without fee, provided the above notices are retained on all copies. michael@0: * Permission to modify the code and to distribute modified code is granted, michael@0: * provided the above notices are retained, and a notice that the code was michael@0: * modified is included with the above copyright notice. michael@0: * michael@0: */ michael@0: michael@0: #include "stlport_prefix.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "c_locale.h" michael@0: #include "locale_impl.h" michael@0: michael@0: _STLP_BEGIN_NAMESPACE michael@0: michael@0: #define _NAMELESS "*" michael@0: static const char _Nameless[] = _NAMELESS; michael@0: michael@0: static inline bool is_C_locale_name (const char* name) michael@0: { return ((name[0] == 'C') && (name[1] == 0)); } michael@0: michael@0: locale* _Stl_get_classic_locale(); michael@0: locale* _Stl_get_global_locale(); michael@0: michael@0: #if defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND) || \ michael@0: defined (_STLP_SIGNAL_RUNTIME_COMPATIBILITY) || defined (_STLP_CHECK_RUNTIME_COMPATIBILITY) michael@0: # define locale _STLP_NO_MEM_T_NAME(loc) michael@0: #endif michael@0: michael@0: locale::facet::~facet() {} michael@0: michael@0: #if !defined (_STLP_MEMBER_TEMPLATES) || defined (_STLP_INLINE_MEMBER_TEMPLATES) michael@0: // members that fail to be templates michael@0: bool locale::operator()(const string& __x, michael@0: const string& __y) const michael@0: { return __locale_do_operator_call(*this, __x, __y); } michael@0: michael@0: # if !defined (_STLP_NO_WCHAR_T) michael@0: bool locale::operator()(const wstring& __x, michael@0: const wstring& __y) const michael@0: { return __locale_do_operator_call(*this, __x, __y); } michael@0: # endif michael@0: #endif michael@0: michael@0: void _STLP_CALL locale::_M_throw_on_null_name() michael@0: { _STLP_THROW(runtime_error("Invalid null locale name")); } michael@0: michael@0: void _STLP_CALL locale::_M_throw_on_combine_error(const string& name) { michael@0: string what = "Unable to find facet"; michael@0: what += " in "; michael@0: what += name.empty() ? "system" : name.c_str(); michael@0: what += " locale"; michael@0: _STLP_THROW(runtime_error(what.c_str())); michael@0: } michael@0: michael@0: void _STLP_CALL locale::_M_throw_on_creation_failure(int __err_code, michael@0: const char* name, const char* facet) { michael@0: string what; michael@0: switch (__err_code) { michael@0: case _STLP_LOC_UNSUPPORTED_FACET_CATEGORY: michael@0: what = "No platform localization support for "; michael@0: what += facet; michael@0: what += " facet category, unable to create facet for "; michael@0: what += name[0] == 0 ? "system" : name; michael@0: what += " locale"; michael@0: break; michael@0: case _STLP_LOC_NO_PLATFORM_SUPPORT: michael@0: what = "No platform localization support, unable to create "; michael@0: what += name[0] == 0 ? "system" : name; michael@0: what += " locale"; michael@0: break; michael@0: default: michael@0: case _STLP_LOC_UNKNOWN_NAME: michael@0: what = "Unable to create facet "; michael@0: what += facet; michael@0: what += " from name '"; michael@0: what += name; michael@0: what += "'"; michael@0: break; michael@0: case _STLP_LOC_NO_MEMORY: michael@0: _STLP_THROW_BAD_ALLOC; michael@0: break; michael@0: } michael@0: michael@0: _STLP_THROW(runtime_error(what.c_str())); michael@0: } michael@0: michael@0: // Takes a reference to a locale::id, assign a numeric index if not already michael@0: // affected and returns it. The returned index is always positive. michael@0: static const locale::id& _Stl_loc_get_index(locale::id& id) { michael@0: if (id._M_index == 0) { michael@0: #if defined (_STLP_ATOMIC_INCREMENT) && !defined (_STLP_WIN95_LIKE) michael@0: static _STLP_VOLATILE __stl_atomic_t _S_index = __STATIC_CAST(__stl_atomic_t, locale::id::_S_max); michael@0: id._M_index = _STLP_ATOMIC_INCREMENT(&_S_index); michael@0: #else michael@0: static _STLP_STATIC_MUTEX _Index_lock _STLP_MUTEX_INITIALIZER; michael@0: _STLP_auto_lock sentry(_Index_lock); michael@0: size_t new_index = locale::id::_S_max++; michael@0: id._M_index = new_index; michael@0: #endif michael@0: } michael@0: return id; michael@0: } michael@0: michael@0: // Default constructor: create a copy of the global locale. michael@0: locale::locale() _STLP_NOTHROW michael@0: : _M_impl(_get_Locale_impl(_Stl_get_global_locale()->_M_impl)) michael@0: {} michael@0: michael@0: // Copy constructor michael@0: locale::locale(const locale& L) _STLP_NOTHROW michael@0: : _M_impl( _get_Locale_impl( L._M_impl ) ) michael@0: {} michael@0: michael@0: void locale::_M_insert(facet* f, locale::id& n) { michael@0: if (f) michael@0: _M_impl->insert(f, _Stl_loc_get_index(n)); michael@0: } michael@0: michael@0: locale::locale( _Locale_impl* impl ) : michael@0: _M_impl( _get_Locale_impl( impl ) ) michael@0: {} michael@0: michael@0: // Create a locale from a name. michael@0: locale::locale(const char* name) michael@0: : _M_impl(0) { michael@0: if (!name) michael@0: _M_throw_on_null_name(); michael@0: michael@0: if (is_C_locale_name(name)) { michael@0: _M_impl = _get_Locale_impl( locale::classic()._M_impl ); michael@0: return; michael@0: } michael@0: michael@0: _Locale_impl* impl = 0; michael@0: _STLP_TRY { michael@0: impl = new _Locale_impl(locale::id::_S_max, name); michael@0: michael@0: // Insert categories one at a time. michael@0: _Locale_name_hint *hint = 0; michael@0: const char* ctype_name = name; michael@0: char ctype_buf[_Locale_MAX_SIMPLE_NAME]; michael@0: const char* numeric_name = name; michael@0: char numeric_buf[_Locale_MAX_SIMPLE_NAME]; michael@0: const char* time_name = name; michael@0: char time_buf[_Locale_MAX_SIMPLE_NAME]; michael@0: const char* collate_name = name; michael@0: char collate_buf[_Locale_MAX_SIMPLE_NAME]; michael@0: const char* monetary_name = name; michael@0: char monetary_buf[_Locale_MAX_SIMPLE_NAME]; michael@0: const char* messages_name = name; michael@0: char messages_buf[_Locale_MAX_SIMPLE_NAME]; michael@0: hint = impl->insert_ctype_facets(ctype_name, ctype_buf, hint); michael@0: hint = impl->insert_numeric_facets(numeric_name, numeric_buf, hint); michael@0: hint = impl->insert_time_facets(time_name, time_buf, hint); michael@0: hint = impl->insert_collate_facets(collate_name, collate_buf, hint); michael@0: hint = impl->insert_monetary_facets(monetary_name, monetary_buf, hint); michael@0: impl->insert_messages_facets(messages_name, messages_buf, hint); michael@0: michael@0: // Try to use a normalize locale name in order to have the == operator michael@0: // to behave correctly: michael@0: if (strcmp(ctype_name, numeric_name) == 0 && michael@0: strcmp(ctype_name, time_name) == 0 && michael@0: strcmp(ctype_name, collate_name) == 0 && michael@0: strcmp(ctype_name, monetary_name) == 0 && michael@0: strcmp(ctype_name, messages_name) == 0) { michael@0: impl->name = ctype_name; michael@0: } michael@0: // else we keep current name. michael@0: michael@0: // reassign impl michael@0: _M_impl = _get_Locale_impl( impl ); michael@0: } michael@0: _STLP_UNWIND(delete impl); michael@0: } michael@0: michael@0: static void _Stl_loc_combine_names_aux(_Locale_impl* L, michael@0: const char* name, michael@0: const char* ctype_name, const char* time_name, const char* numeric_name, michael@0: const char* collate_name, const char* monetary_name, const char* messages_name, michael@0: locale::category c) { michael@0: // This function is only called when names has been validated so using _Locale_extract_*_name michael@0: // can't fail. michael@0: int __err_code; michael@0: char buf[_Locale_MAX_SIMPLE_NAME]; michael@0: L->name = string("LC_CTYPE=") + _Locale_extract_ctype_name((c & locale::ctype) ? ctype_name : name, buf, 0, &__err_code) + ";"; michael@0: L->name += string("LC_TIME=") + _Locale_extract_time_name((c & locale::time) ? time_name : name, buf, 0, &__err_code) + ";"; michael@0: L->name += string("LC_NUMERIC=") + _Locale_extract_numeric_name((c & locale::numeric) ? numeric_name : name, buf, 0, &__err_code) + ";"; michael@0: L->name += string("LC_COLLATE=") + _Locale_extract_collate_name((c & locale::collate) ? collate_name : name, buf, 0, &__err_code) + ";"; michael@0: L->name += string("LC_MONETARY=") + _Locale_extract_monetary_name((c & locale::monetary) ? monetary_name : name, buf, 0, &__err_code) + ";"; michael@0: L->name += string("LC_MESSAGES=") + _Locale_extract_messages_name((c & locale::messages) ? messages_name : name, buf, 0, &__err_code); michael@0: } michael@0: michael@0: // Give L a name where all facets except those in category c michael@0: // are taken from name1, and those in category c are taken from name2. michael@0: static void _Stl_loc_combine_names(_Locale_impl* L, michael@0: const char* name1, const char* name2, michael@0: locale::category c) { michael@0: if ((c & locale::all) == 0 || strcmp(name1, name1) == 0) michael@0: L->name = name1; michael@0: else if ((c & locale::all) == locale::all) michael@0: L->name = name2; michael@0: else { michael@0: _Stl_loc_combine_names_aux(L, name1, name2, name2, name2, name2, name2, name2, c); michael@0: } michael@0: } michael@0: michael@0: static void _Stl_loc_combine_names(_Locale_impl* L, michael@0: const char* name, michael@0: const char* ctype_name, const char* time_name, const char* numeric_name, michael@0: const char* collate_name, const char* monetary_name, const char* messages_name, michael@0: locale::category c) { michael@0: if ((c & locale::all) == 0 || (strcmp(name, ctype_name) == 0 && michael@0: strcmp(name, time_name) == 0 && michael@0: strcmp(name, numeric_name) == 0 && michael@0: strcmp(name, collate_name) == 0 && michael@0: strcmp(name, monetary_name) == 0 && michael@0: strcmp(name, messages_name) == 0)) michael@0: L->name = name; michael@0: else if ((c & locale::all) == locale::all && strcmp(ctype_name, time_name) == 0 && michael@0: strcmp(ctype_name, numeric_name) == 0 && michael@0: strcmp(ctype_name, collate_name) == 0 && michael@0: strcmp(ctype_name, monetary_name) == 0 && michael@0: strcmp(ctype_name, messages_name) == 0) michael@0: L->name = ctype_name; michael@0: else { michael@0: _Stl_loc_combine_names_aux(L, name, ctype_name, time_name, numeric_name, collate_name, monetary_name, messages_name, c); michael@0: } michael@0: } michael@0: michael@0: michael@0: // Create a locale that's a copy of L, except that all of the facets michael@0: // in category c are instead constructed by name. michael@0: locale::locale(const locale& L, const char* name, locale::category c) michael@0: : _M_impl(0) { michael@0: if (!name) michael@0: _M_throw_on_null_name(); michael@0: michael@0: if (!::strcmp(_Nameless, name)) michael@0: _STLP_THROW(runtime_error("Invalid locale name '" _NAMELESS "'")); michael@0: michael@0: _Locale_impl* impl = 0; michael@0: michael@0: _STLP_TRY { michael@0: impl = new _Locale_impl(*L._M_impl); michael@0: michael@0: _Locale_name_hint *hint = 0; michael@0: const char* ctype_name = name; michael@0: char ctype_buf[_Locale_MAX_SIMPLE_NAME]; michael@0: const char* numeric_name = name; michael@0: char numeric_buf[_Locale_MAX_SIMPLE_NAME]; michael@0: const char* time_name = name; michael@0: char time_buf[_Locale_MAX_SIMPLE_NAME]; michael@0: const char* collate_name = name; michael@0: char collate_buf[_Locale_MAX_SIMPLE_NAME]; michael@0: const char* monetary_name = name; michael@0: char monetary_buf[_Locale_MAX_SIMPLE_NAME]; michael@0: const char* messages_name = name; michael@0: char messages_buf[_Locale_MAX_SIMPLE_NAME]; michael@0: if (c & locale::ctype) michael@0: hint = impl->insert_ctype_facets(ctype_name, ctype_buf, hint); michael@0: if (c & locale::numeric) michael@0: hint = impl->insert_numeric_facets(numeric_name, numeric_buf, hint); michael@0: if (c & locale::time) michael@0: hint = impl->insert_time_facets(time_name, time_buf, hint); michael@0: if (c & locale::collate) michael@0: hint = impl->insert_collate_facets(collate_name, collate_buf, hint); michael@0: if (c & locale::monetary) michael@0: hint = impl->insert_monetary_facets(monetary_name, monetary_buf,hint); michael@0: if (c & locale::messages) michael@0: impl->insert_messages_facets(messages_name, messages_buf, hint); michael@0: michael@0: _Stl_loc_combine_names(impl, L._M_impl->name.c_str(), michael@0: ctype_name, time_name, numeric_name, michael@0: collate_name, monetary_name, messages_name, c); michael@0: _M_impl = _get_Locale_impl( impl ); michael@0: } michael@0: _STLP_UNWIND(delete impl) michael@0: } michael@0: michael@0: // Contruct a new locale where all facets that aren't in category c michael@0: // come from L1, and all those that are in category c come from L2. michael@0: locale::locale(const locale& L1, const locale& L2, category c) michael@0: : _M_impl(0) { michael@0: _Locale_impl* impl = new _Locale_impl(*L1._M_impl); michael@0: michael@0: _Locale_impl* i2 = L2._M_impl; michael@0: michael@0: if (L1.name() != _Nameless && L2.name() != _Nameless) michael@0: _Stl_loc_combine_names(impl, L1._M_impl->name.c_str(), L2._M_impl->name.c_str(), c); michael@0: else { michael@0: impl->name = _Nameless; michael@0: } michael@0: michael@0: if (c & collate) { michael@0: impl->insert( i2, _STLP_STD::collate::id); michael@0: # ifndef _STLP_NO_WCHAR_T michael@0: impl->insert( i2, _STLP_STD::collate::id); michael@0: # endif michael@0: } michael@0: if (c & ctype) { michael@0: impl->insert( i2, _STLP_STD::ctype::id); michael@0: impl->insert( i2, _STLP_STD::codecvt::id); michael@0: # ifndef _STLP_NO_WCHAR_T michael@0: impl->insert( i2, _STLP_STD::ctype::id); michael@0: impl->insert( i2, _STLP_STD::codecvt::id); michael@0: # endif michael@0: } michael@0: if (c & monetary) { michael@0: impl->insert( i2, _STLP_STD::moneypunct::id); michael@0: impl->insert( i2, _STLP_STD::moneypunct::id); michael@0: impl->insert( i2, _STLP_STD::money_get > >::id); michael@0: impl->insert( i2, _STLP_STD::money_put > >::id); michael@0: # ifndef _STLP_NO_WCHAR_T michael@0: impl->insert( i2, _STLP_STD::moneypunct::id); michael@0: impl->insert( i2, _STLP_STD::moneypunct::id); michael@0: impl->insert( i2, _STLP_STD::money_get > >::id); michael@0: impl->insert( i2, _STLP_STD::money_put > >::id); michael@0: # endif michael@0: } michael@0: if (c & numeric) { michael@0: impl->insert( i2, _STLP_STD::numpunct::id); michael@0: impl->insert( i2, _STLP_STD::num_get > >::id); michael@0: impl->insert( i2, _STLP_STD::num_put > >::id); michael@0: # ifndef _STLP_NO_WCHAR_T michael@0: impl->insert( i2, _STLP_STD::numpunct::id); michael@0: impl->insert( i2, _STLP_STD::num_get > >::id); michael@0: impl->insert( i2, _STLP_STD::num_put > >::id); michael@0: # endif michael@0: } michael@0: if (c & time) { michael@0: impl->insert( i2, _STLP_STD::time_get > >::id); michael@0: impl->insert( i2, _STLP_STD::time_put > >::id); michael@0: # ifndef _STLP_NO_WCHAR_T michael@0: impl->insert( i2, _STLP_STD::time_get > >::id); michael@0: impl->insert( i2, _STLP_STD::time_put > >::id); michael@0: # endif michael@0: } michael@0: if (c & messages) { michael@0: impl->insert( i2, _STLP_STD::messages::id); michael@0: # ifndef _STLP_NO_WCHAR_T michael@0: impl->insert( i2, _STLP_STD::messages::id); michael@0: # endif michael@0: } michael@0: _M_impl = _get_Locale_impl( impl ); michael@0: } michael@0: michael@0: // Destructor. michael@0: locale::~locale() _STLP_NOTHROW { michael@0: if (_M_impl) michael@0: _release_Locale_impl(_M_impl); michael@0: } michael@0: michael@0: // Assignment operator. Much like the copy constructor: just a bit of michael@0: // pointer twiddling. michael@0: const locale& locale::operator=(const locale& L) _STLP_NOTHROW { michael@0: if (this->_M_impl != L._M_impl) { michael@0: if (this->_M_impl) michael@0: _release_Locale_impl(this->_M_impl); michael@0: this->_M_impl = _get_Locale_impl(L._M_impl); michael@0: } michael@0: return *this; michael@0: } michael@0: michael@0: locale::facet* locale::_M_get_facet(const locale::id& n) const { michael@0: return n._M_index < _M_impl->size() ? _M_impl->facets_vec[n._M_index] : 0; michael@0: } michael@0: michael@0: locale::facet* locale::_M_use_facet(const locale::id& n) const { michael@0: locale::facet* f = (n._M_index < _M_impl->size() ? _M_impl->facets_vec[n._M_index] : 0); michael@0: if (!f) michael@0: _M_impl->_M_throw_bad_cast(); michael@0: return f; michael@0: } michael@0: michael@0: string locale::name() const { michael@0: return _M_impl->name; michael@0: } michael@0: michael@0: // Compare two locales for equality. michael@0: bool locale::operator==(const locale& L) const { michael@0: return this->_M_impl == L._M_impl || michael@0: (this->name() == L.name() && this->name() != _Nameless); michael@0: } michael@0: michael@0: bool locale::operator!=(const locale& L) const { michael@0: return !(*this == L); michael@0: } michael@0: michael@0: // static data members. michael@0: michael@0: const locale& _STLP_CALL locale::classic() { michael@0: return *_Stl_get_classic_locale(); michael@0: } michael@0: michael@0: #if !defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND) michael@0: locale _STLP_CALL locale::global(const locale& L) { michael@0: #else michael@0: _Locale_impl* _STLP_CALL locale::global(const locale& L) { michael@0: #endif michael@0: locale old(_Stl_get_global_locale()->_M_impl); michael@0: if (_Stl_get_global_locale()->_M_impl != L._M_impl) { michael@0: _release_Locale_impl(_Stl_get_global_locale()->_M_impl); michael@0: // this assign should be atomic, should be fixed here: michael@0: _Stl_get_global_locale()->_M_impl = _get_Locale_impl(L._M_impl); michael@0: michael@0: // Set the global C locale, if appropriate. michael@0: #if !defined(_STLP_NO_LOCALE_SUPPORT) michael@0: if (L.name() != _Nameless) michael@0: setlocale(LC_ALL, L.name().c_str()); michael@0: #endif michael@0: } michael@0: michael@0: #if !defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND) michael@0: return old; michael@0: #else michael@0: return old._M_impl; michael@0: #endif michael@0: } michael@0: michael@0: #if !defined (_STLP_STATIC_CONST_INIT_BUG) && !defined (_STLP_NO_STATIC_CONST_DEFINITION) michael@0: const locale::category locale::none; michael@0: const locale::category locale::collate; michael@0: const locale::category locale::ctype; michael@0: const locale::category locale::monetary; michael@0: const locale::category locale::numeric; michael@0: const locale::category locale::time; michael@0: const locale::category locale::messages; michael@0: const locale::category locale::all; michael@0: #endif michael@0: michael@0: _STLP_END_NAMESPACE michael@0: