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: #include "stlport_prefix.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "c_locale.h" michael@0: #include "locale_impl.h" michael@0: #include "acquire_release.h" michael@0: michael@0: _STLP_BEGIN_NAMESPACE michael@0: _STLP_MOVE_TO_PRIV_NAMESPACE michael@0: michael@0: // Those wrappers are needed to use locale functions in __acquire_category, michael@0: // all functions have to present the same prototype. michael@0: michael@0: static void* _Loc_ctype_create(const char * s, _Locale_name_hint* hint, int *__err_code) michael@0: { return _Locale_ctype_create(s, hint, __err_code); } michael@0: static void* _Loc_codecvt_create(const char * s, _Locale_name_hint* hint, int *__err_code) michael@0: { return _Locale_codecvt_create(s, hint, __err_code); } michael@0: static void* _Loc_numeric_create(const char * s, _Locale_name_hint* hint, int *__err_code) michael@0: { return _Locale_numeric_create(s, hint, __err_code); } michael@0: static void* _Loc_time_create(const char * s, _Locale_name_hint* hint, int *__err_code) michael@0: { return _Locale_time_create(s, hint, __err_code); } michael@0: static void* _Loc_collate_create(const char * s, _Locale_name_hint* hint, int *__err_code) michael@0: { return _Locale_collate_create(s, hint, __err_code); } michael@0: static void* _Loc_monetary_create(const char * s, _Locale_name_hint* hint, int *__err_code) michael@0: { return _Locale_monetary_create(s, hint, __err_code); } michael@0: static void* _Loc_messages_create(const char * s, _Locale_name_hint* hint, int *__err_code) michael@0: { return _Locale_messages_create(s, hint, __err_code); } michael@0: michael@0: static char const* _Loc_ctype_name(void* l, char* s) michael@0: { return _Locale_ctype_name((_Locale_ctype*)l, s); } michael@0: static char const* _Loc_codecvt_name(void* l, char* s) michael@0: { return _Locale_codecvt_name((_Locale_codecvt*)l, s); } michael@0: static char const* _Loc_numeric_name(void* l, char* s) michael@0: { return _Locale_numeric_name((_Locale_numeric*)l, s); } michael@0: static char const* _Loc_time_name(void* l, char* s) michael@0: { return _Locale_time_name((_Locale_time*)l, s); } michael@0: static char const* _Loc_collate_name(void* l, char* s) michael@0: { return _Locale_collate_name((_Locale_collate*)l, s); } michael@0: static char const* _Loc_monetary_name(void* l, char* s) michael@0: { return _Locale_monetary_name((_Locale_monetary*)l, s); } michael@0: static char const* _Loc_messages_name(void* l, char* s) michael@0: { return _Locale_messages_name((_Locale_messages*)l, s); } michael@0: michael@0: static const char* _Loc_ctype_default(char* p) michael@0: { return _Locale_ctype_default(p); } michael@0: static const char* _Loc_numeric_default(char * p) michael@0: { return _Locale_numeric_default(p); } michael@0: static const char* _Loc_time_default(char* p) michael@0: { return _Locale_time_default(p); } michael@0: static const char* _Loc_collate_default(char* p) michael@0: { return _Locale_collate_default(p); } michael@0: static const char* _Loc_monetary_default(char* p) michael@0: { return _Locale_monetary_default(p); } michael@0: static const char* _Loc_messages_default(char* p) michael@0: { return _Locale_messages_default(p); } michael@0: michael@0: static void _Loc_ctype_destroy(void* p) {_Locale_ctype_destroy((_Locale_ctype*)p); } michael@0: static void _Loc_codecvt_destroy(void* p) {_Locale_codecvt_destroy((_Locale_codecvt*)p); } michael@0: static void _Loc_numeric_destroy(void* p) {_Locale_numeric_destroy((_Locale_numeric*)p); } michael@0: static void _Loc_time_destroy(void* p) {_Locale_time_destroy((_Locale_time*)p);} michael@0: static void _Loc_collate_destroy(void* p) {_Locale_collate_destroy((_Locale_collate*)p);} michael@0: static void _Loc_monetary_destroy(void* p) {_Locale_monetary_destroy((_Locale_monetary*)p);} michael@0: static void _Loc_messages_destroy(void* p) {_Locale_messages_destroy((_Locale_messages*)p);} michael@0: michael@0: typedef void* (*loc_create_func_t)(const char *, _Locale_name_hint*, int *__err_code); michael@0: typedef char const* (*loc_name_func_t)(void* l, char* s); michael@0: typedef void (*loc_destroy_func_t)(void* l); michael@0: typedef const char* (*loc_default_name_func_t)(char* s); michael@0: typedef char const* (*loc_extract_name_func_t)(const char*, char*, _Locale_name_hint*, int *__err_code); michael@0: michael@0: //---------------------------------------------------------------------- michael@0: // Acquire and release low-level category objects. The whole point of michael@0: // this is so that we don't allocate (say) four different _Locale_ctype michael@0: // objects for a single locale. michael@0: michael@0: // Global hash tables for category objects. michael@0: typedef hash_map, hash, equal_to > Category_Map; michael@0: michael@0: // Look up a category by name michael@0: static Category_Map** ctype_hash() { michael@0: static Category_Map *_S_ctype_hash = 0; michael@0: return &_S_ctype_hash; michael@0: } michael@0: static Category_Map** codecvt_hash() { michael@0: static Category_Map *_S_codecvt_hash = 0; michael@0: return &_S_codecvt_hash; michael@0: } michael@0: static Category_Map** numeric_hash() { michael@0: static Category_Map *_S_numeric_hash = 0; michael@0: return &_S_numeric_hash; michael@0: } michael@0: static Category_Map** time_hash() { michael@0: static Category_Map *_S_time_hash = 0; michael@0: return &_S_time_hash; michael@0: } michael@0: static Category_Map** collate_hash() { michael@0: static Category_Map *_S_collate_hash = 0; michael@0: return &_S_collate_hash; michael@0: } michael@0: static Category_Map** monetary_hash() { michael@0: static Category_Map *_S_monetary_hash = 0; michael@0: return &_S_monetary_hash; michael@0: } michael@0: static Category_Map** messages_hash() { michael@0: static Category_Map *_S_messages_hash; michael@0: return &_S_messages_hash; michael@0: } michael@0: michael@0: // We have a single lock for all of the hash tables. We may wish to michael@0: // replace it with six different locks. michael@0: /* REFERENCED */ michael@0: static _STLP_STATIC_MUTEX& category_hash_mutex() { michael@0: static _STLP_STATIC_MUTEX lock _STLP_MUTEX_INITIALIZER; michael@0: return lock; michael@0: } michael@0: michael@0: static void* michael@0: __acquire_category(const char* &name, char *buf, _Locale_name_hint* hint, michael@0: loc_extract_name_func_t extract_name, michael@0: loc_create_func_t create_obj, loc_default_name_func_t default_name, michael@0: Category_Map ** M, int *__err_code) { michael@0: #if !defined (__BORLANDC__) || (__BORLANDC__ >= 0x564) michael@0: typedef Category_Map::iterator Category_iterator; michael@0: pair result; michael@0: #else michael@0: # if !defined(_STLP_DEBUG) michael@0: pair<_Ht_iterator<_Slist_iterator >,_Nonconst_traits > > >,_NonLocalHashMapTraitsT > > >, bool> result; michael@0: # else michael@0: pair<_DBG_iter<_NonDbg_hashtable >,string,hash,_HashMapTraitsT > >,_Select1st > >,_DbgEqual >,allocator > > >,_DbgTraits<_NonLocalHashMapTraitsT > > > >, bool> result; michael@0: # endif michael@0: #endif michael@0: michael@0: *__err_code = _STLP_LOC_UNDEFINED; michael@0: michael@0: // Find what name to look for. Be careful if user requests the default. michael@0: if (name[0] == 0) { michael@0: name = default_name(buf); michael@0: if (name == 0 || name[0] == 0) michael@0: name = "C"; michael@0: } michael@0: else { michael@0: const char* cname = extract_name(name, buf, hint, __err_code); michael@0: if (cname == 0) { michael@0: return 0; michael@0: } michael@0: name = cname; michael@0: } michael@0: michael@0: Category_Map::value_type __e(name, pair((void*)0,size_t(0))); michael@0: michael@0: _STLP_auto_lock sentry(category_hash_mutex()); michael@0: michael@0: if (!*M) michael@0: *M = new Category_Map(); michael@0: michael@0: // Look for an existing entry with that name. michael@0: result = (*M)->insert_noresize(__e); michael@0: michael@0: if (result.second) { michael@0: // There was no entry in the map already. Create the category. michael@0: (*result.first).second.first = create_obj(name, hint, __err_code); michael@0: if (!(*result.first).second.first) { michael@0: (*M)->erase(result.first); michael@0: #if defined (_STLP_LEAKS_PEDANTIC) michael@0: if ((*M)->empty()) { michael@0: delete *M; michael@0: *M = 0; michael@0: } michael@0: #endif michael@0: return 0; michael@0: } michael@0: } michael@0: michael@0: // Increment the reference count. michael@0: ++((*result.first).second.second); michael@0: michael@0: return (*result.first).second.first; michael@0: } michael@0: michael@0: static void michael@0: __release_category(void* cat, michael@0: loc_destroy_func_t destroy_fun, michael@0: loc_name_func_t get_name, michael@0: Category_Map** M) { michael@0: Category_Map *pM = *M; michael@0: michael@0: if (cat && pM) { michael@0: // Find the name of the category object. michael@0: char buf[_Locale_MAX_SIMPLE_NAME + 1]; michael@0: char const* name = get_name(cat, buf); michael@0: michael@0: if (name != 0) { michael@0: _STLP_auto_lock sentry(category_hash_mutex()); michael@0: Category_Map::iterator it = pM->find(name); michael@0: if (it != pM->end()) { michael@0: // Decrement the ref count. If it goes to zero, delete this category michael@0: // from the map. michael@0: if (--((*it).second.second) == 0) { michael@0: void* cat1 = (*it).second.first; michael@0: destroy_fun(cat1); michael@0: pM->erase(it); michael@0: #if defined (_STLP_LEAKS_PEDANTIC) michael@0: if (pM->empty()) { michael@0: delete pM; michael@0: *M = 0; michael@0: } michael@0: #endif michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: _Locale_ctype* _STLP_CALL __acquire_ctype(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) { michael@0: return __REINTERPRET_CAST(_Locale_ctype*, __acquire_category(name, buf, hint, michael@0: _Locale_extract_ctype_name, _Loc_ctype_create, _Loc_ctype_default, michael@0: ctype_hash(), __err_code)); michael@0: } michael@0: _Locale_codecvt* _STLP_CALL __acquire_codecvt(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) { michael@0: return __REINTERPRET_CAST(_Locale_codecvt*, __acquire_category(name, buf, hint, michael@0: _Locale_extract_ctype_name, _Loc_codecvt_create, _Loc_ctype_default, michael@0: codecvt_hash(), __err_code)); michael@0: } michael@0: _Locale_numeric* _STLP_CALL __acquire_numeric(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) { michael@0: return __REINTERPRET_CAST(_Locale_numeric*, __acquire_category(name, buf, hint, michael@0: _Locale_extract_numeric_name, _Loc_numeric_create, _Loc_numeric_default, michael@0: numeric_hash(), __err_code)); michael@0: } michael@0: _Locale_time* _STLP_CALL __acquire_time(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) { michael@0: return __REINTERPRET_CAST(_Locale_time*, __acquire_category(name, buf, hint, michael@0: _Locale_extract_time_name, _Loc_time_create, _Loc_time_default, michael@0: time_hash(), __err_code)); michael@0: } michael@0: _Locale_collate* _STLP_CALL __acquire_collate(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) { michael@0: return __REINTERPRET_CAST(_Locale_collate*, __acquire_category(name, buf, hint, michael@0: _Locale_extract_collate_name, _Loc_collate_create, _Loc_collate_default, michael@0: collate_hash(), __err_code)); michael@0: } michael@0: _Locale_monetary* _STLP_CALL __acquire_monetary(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) { michael@0: return __REINTERPRET_CAST(_Locale_monetary*, __acquire_category(name, buf, hint, michael@0: _Locale_extract_monetary_name, _Loc_monetary_create, _Loc_monetary_default, michael@0: monetary_hash(), __err_code)); michael@0: } michael@0: _Locale_messages* _STLP_CALL __acquire_messages(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) { michael@0: return __REINTERPRET_CAST(_Locale_messages*, __acquire_category(name, buf, hint, michael@0: _Locale_extract_messages_name, _Loc_messages_create, _Loc_messages_default, michael@0: messages_hash(), __err_code)); michael@0: } michael@0: michael@0: void _STLP_CALL __release_ctype(_Locale_ctype* cat) michael@0: { __release_category(cat, _Loc_ctype_destroy, _Loc_ctype_name, ctype_hash()); } michael@0: void _STLP_CALL __release_codecvt(_Locale_codecvt* cat) michael@0: { __release_category(cat, _Loc_codecvt_destroy, _Loc_codecvt_name, codecvt_hash()); } michael@0: void _STLP_CALL __release_numeric(_Locale_numeric* cat) michael@0: { __release_category(cat, _Loc_numeric_destroy, _Loc_numeric_name, numeric_hash()); } michael@0: void _STLP_CALL __release_time(_Locale_time* cat) michael@0: { __release_category(cat, _Loc_time_destroy, _Loc_time_name, time_hash()); } michael@0: void _STLP_CALL __release_collate(_Locale_collate* cat) michael@0: { __release_category(cat, _Loc_collate_destroy, _Loc_collate_name, collate_hash()); } michael@0: void _STLP_CALL __release_monetary(_Locale_monetary* cat) michael@0: { __release_category(cat, _Loc_monetary_destroy, _Loc_monetary_name, monetary_hash()); } michael@0: void _STLP_CALL __release_messages(_Locale_messages* cat) michael@0: { __release_category(cat, _Loc_messages_destroy, _Loc_messages_name, messages_hash()); } michael@0: michael@0: _STLP_MOVE_TO_STD_NAMESPACE michael@0: _STLP_END_NAMESPACE