1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/build/stlport/src/locale_catalog.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,288 @@ 1.4 +/* 1.5 + * Copyright (c) 1999 1.6 + * Silicon Graphics Computer Systems, Inc. 1.7 + * 1.8 + * Copyright (c) 1999 1.9 + * Boris Fomitchev 1.10 + * 1.11 + * This material is provided "as is", with absolutely no warranty expressed 1.12 + * or implied. Any use is at your own risk. 1.13 + * 1.14 + * Permission to use or copy this software for any purpose is hereby granted 1.15 + * without fee, provided the above notices are retained on all copies. 1.16 + * Permission to modify the code and to distribute modified code is granted, 1.17 + * provided the above notices are retained, and a notice that the code was 1.18 + * modified is included with the above copyright notice. 1.19 + * 1.20 + */ 1.21 +#include "stlport_prefix.h" 1.22 + 1.23 +#include <hash_map> 1.24 +#include <string> 1.25 + 1.26 +#include <locale> 1.27 +#include <istream> 1.28 + 1.29 +#include "c_locale.h" 1.30 +#include "locale_impl.h" 1.31 +#include "acquire_release.h" 1.32 + 1.33 +_STLP_BEGIN_NAMESPACE 1.34 +_STLP_MOVE_TO_PRIV_NAMESPACE 1.35 + 1.36 +// Those wrappers are needed to use locale functions in __acquire_category, 1.37 +// all functions have to present the same prototype. 1.38 + 1.39 +static void* _Loc_ctype_create(const char * s, _Locale_name_hint* hint, int *__err_code) 1.40 +{ return _Locale_ctype_create(s, hint, __err_code); } 1.41 +static void* _Loc_codecvt_create(const char * s, _Locale_name_hint* hint, int *__err_code) 1.42 +{ return _Locale_codecvt_create(s, hint, __err_code); } 1.43 +static void* _Loc_numeric_create(const char * s, _Locale_name_hint* hint, int *__err_code) 1.44 +{ return _Locale_numeric_create(s, hint, __err_code); } 1.45 +static void* _Loc_time_create(const char * s, _Locale_name_hint* hint, int *__err_code) 1.46 +{ return _Locale_time_create(s, hint, __err_code); } 1.47 +static void* _Loc_collate_create(const char * s, _Locale_name_hint* hint, int *__err_code) 1.48 +{ return _Locale_collate_create(s, hint, __err_code); } 1.49 +static void* _Loc_monetary_create(const char * s, _Locale_name_hint* hint, int *__err_code) 1.50 +{ return _Locale_monetary_create(s, hint, __err_code); } 1.51 +static void* _Loc_messages_create(const char * s, _Locale_name_hint* hint, int *__err_code) 1.52 +{ return _Locale_messages_create(s, hint, __err_code); } 1.53 + 1.54 +static char const* _Loc_ctype_name(void* l, char* s) 1.55 +{ return _Locale_ctype_name((_Locale_ctype*)l, s); } 1.56 +static char const* _Loc_codecvt_name(void* l, char* s) 1.57 +{ return _Locale_codecvt_name((_Locale_codecvt*)l, s); } 1.58 +static char const* _Loc_numeric_name(void* l, char* s) 1.59 +{ return _Locale_numeric_name((_Locale_numeric*)l, s); } 1.60 +static char const* _Loc_time_name(void* l, char* s) 1.61 +{ return _Locale_time_name((_Locale_time*)l, s); } 1.62 +static char const* _Loc_collate_name(void* l, char* s) 1.63 +{ return _Locale_collate_name((_Locale_collate*)l, s); } 1.64 +static char const* _Loc_monetary_name(void* l, char* s) 1.65 +{ return _Locale_monetary_name((_Locale_monetary*)l, s); } 1.66 +static char const* _Loc_messages_name(void* l, char* s) 1.67 +{ return _Locale_messages_name((_Locale_messages*)l, s); } 1.68 + 1.69 +static const char* _Loc_ctype_default(char* p) 1.70 +{ return _Locale_ctype_default(p); } 1.71 +static const char* _Loc_numeric_default(char * p) 1.72 +{ return _Locale_numeric_default(p); } 1.73 +static const char* _Loc_time_default(char* p) 1.74 +{ return _Locale_time_default(p); } 1.75 +static const char* _Loc_collate_default(char* p) 1.76 +{ return _Locale_collate_default(p); } 1.77 +static const char* _Loc_monetary_default(char* p) 1.78 +{ return _Locale_monetary_default(p); } 1.79 +static const char* _Loc_messages_default(char* p) 1.80 +{ return _Locale_messages_default(p); } 1.81 + 1.82 +static void _Loc_ctype_destroy(void* p) {_Locale_ctype_destroy((_Locale_ctype*)p); } 1.83 +static void _Loc_codecvt_destroy(void* p) {_Locale_codecvt_destroy((_Locale_codecvt*)p); } 1.84 +static void _Loc_numeric_destroy(void* p) {_Locale_numeric_destroy((_Locale_numeric*)p); } 1.85 +static void _Loc_time_destroy(void* p) {_Locale_time_destroy((_Locale_time*)p);} 1.86 +static void _Loc_collate_destroy(void* p) {_Locale_collate_destroy((_Locale_collate*)p);} 1.87 +static void _Loc_monetary_destroy(void* p) {_Locale_monetary_destroy((_Locale_monetary*)p);} 1.88 +static void _Loc_messages_destroy(void* p) {_Locale_messages_destroy((_Locale_messages*)p);} 1.89 + 1.90 +typedef void* (*loc_create_func_t)(const char *, _Locale_name_hint*, int *__err_code); 1.91 +typedef char const* (*loc_name_func_t)(void* l, char* s); 1.92 +typedef void (*loc_destroy_func_t)(void* l); 1.93 +typedef const char* (*loc_default_name_func_t)(char* s); 1.94 +typedef char const* (*loc_extract_name_func_t)(const char*, char*, _Locale_name_hint*, int *__err_code); 1.95 + 1.96 +//---------------------------------------------------------------------- 1.97 +// Acquire and release low-level category objects. The whole point of 1.98 +// this is so that we don't allocate (say) four different _Locale_ctype 1.99 +// objects for a single locale. 1.100 + 1.101 +// Global hash tables for category objects. 1.102 +typedef hash_map<string, pair<void*, size_t>, hash<string>, equal_to<string> > Category_Map; 1.103 + 1.104 +// Look up a category by name 1.105 +static Category_Map** ctype_hash() { 1.106 + static Category_Map *_S_ctype_hash = 0; 1.107 + return &_S_ctype_hash; 1.108 +} 1.109 +static Category_Map** codecvt_hash() { 1.110 + static Category_Map *_S_codecvt_hash = 0; 1.111 + return &_S_codecvt_hash; 1.112 +} 1.113 +static Category_Map** numeric_hash() { 1.114 + static Category_Map *_S_numeric_hash = 0; 1.115 + return &_S_numeric_hash; 1.116 +} 1.117 +static Category_Map** time_hash() { 1.118 + static Category_Map *_S_time_hash = 0; 1.119 + return &_S_time_hash; 1.120 +} 1.121 +static Category_Map** collate_hash() { 1.122 + static Category_Map *_S_collate_hash = 0; 1.123 + return &_S_collate_hash; 1.124 +} 1.125 +static Category_Map** monetary_hash() { 1.126 + static Category_Map *_S_monetary_hash = 0; 1.127 + return &_S_monetary_hash; 1.128 +} 1.129 +static Category_Map** messages_hash() { 1.130 + static Category_Map *_S_messages_hash; 1.131 + return &_S_messages_hash; 1.132 +} 1.133 + 1.134 +// We have a single lock for all of the hash tables. We may wish to 1.135 +// replace it with six different locks. 1.136 +/* REFERENCED */ 1.137 +static _STLP_STATIC_MUTEX& category_hash_mutex() { 1.138 + static _STLP_STATIC_MUTEX lock _STLP_MUTEX_INITIALIZER; 1.139 + return lock; 1.140 +} 1.141 + 1.142 +static void* 1.143 +__acquire_category(const char* &name, char *buf, _Locale_name_hint* hint, 1.144 + loc_extract_name_func_t extract_name, 1.145 + loc_create_func_t create_obj, loc_default_name_func_t default_name, 1.146 + Category_Map ** M, int *__err_code) { 1.147 +#if !defined (__BORLANDC__) || (__BORLANDC__ >= 0x564) 1.148 + typedef Category_Map::iterator Category_iterator; 1.149 + pair<Category_iterator, bool> result; 1.150 +#else 1.151 +# if !defined(_STLP_DEBUG) 1.152 + pair<_Ht_iterator<_Slist_iterator<pair<const string,pair<void *,unsigned int> >,_Nonconst_traits<pair<const string,pair<void *,unsigned int> > > >,_NonLocalHashMapTraitsT<pair<const string,pair<void *,unsigned int> > > >, bool> result; 1.153 +# else 1.154 + pair<_DBG_iter<_NonDbg_hashtable<pair<const string,pair<void *,unsigned int> >,string,hash<string>,_HashMapTraitsT<pair<const string,pair<void *,unsigned int> > >,_Select1st<pair<const string,pair<void *,unsigned int> > >,_DbgEqual<string,equal_to<string> >,allocator<pair<const string,pair<void *,unsigned int> > > >,_DbgTraits<_NonLocalHashMapTraitsT<pair<const string,pair<void *,unsigned int> > > > >, bool> result; 1.155 +# endif 1.156 +#endif 1.157 + 1.158 + *__err_code = _STLP_LOC_UNDEFINED; 1.159 + 1.160 + // Find what name to look for. Be careful if user requests the default. 1.161 + if (name[0] == 0) { 1.162 + name = default_name(buf); 1.163 + if (name == 0 || name[0] == 0) 1.164 + name = "C"; 1.165 + } 1.166 + else { 1.167 + const char* cname = extract_name(name, buf, hint, __err_code); 1.168 + if (cname == 0) { 1.169 + return 0; 1.170 + } 1.171 + name = cname; 1.172 + } 1.173 + 1.174 + Category_Map::value_type __e(name, pair<void*,size_t>((void*)0,size_t(0))); 1.175 + 1.176 + _STLP_auto_lock sentry(category_hash_mutex()); 1.177 + 1.178 + if (!*M) 1.179 + *M = new Category_Map(); 1.180 + 1.181 + // Look for an existing entry with that name. 1.182 + result = (*M)->insert_noresize(__e); 1.183 + 1.184 + if (result.second) { 1.185 + // There was no entry in the map already. Create the category. 1.186 + (*result.first).second.first = create_obj(name, hint, __err_code); 1.187 + if (!(*result.first).second.first) { 1.188 + (*M)->erase(result.first); 1.189 +#if defined (_STLP_LEAKS_PEDANTIC) 1.190 + if ((*M)->empty()) { 1.191 + delete *M; 1.192 + *M = 0; 1.193 + } 1.194 +#endif 1.195 + return 0; 1.196 + } 1.197 + } 1.198 + 1.199 + // Increment the reference count. 1.200 + ++((*result.first).second.second); 1.201 + 1.202 + return (*result.first).second.first; 1.203 +} 1.204 + 1.205 +static void 1.206 +__release_category(void* cat, 1.207 + loc_destroy_func_t destroy_fun, 1.208 + loc_name_func_t get_name, 1.209 + Category_Map** M) { 1.210 + Category_Map *pM = *M; 1.211 + 1.212 + if (cat && pM) { 1.213 + // Find the name of the category object. 1.214 + char buf[_Locale_MAX_SIMPLE_NAME + 1]; 1.215 + char const* name = get_name(cat, buf); 1.216 + 1.217 + if (name != 0) { 1.218 + _STLP_auto_lock sentry(category_hash_mutex()); 1.219 + Category_Map::iterator it = pM->find(name); 1.220 + if (it != pM->end()) { 1.221 + // Decrement the ref count. If it goes to zero, delete this category 1.222 + // from the map. 1.223 + if (--((*it).second.second) == 0) { 1.224 + void* cat1 = (*it).second.first; 1.225 + destroy_fun(cat1); 1.226 + pM->erase(it); 1.227 +#if defined (_STLP_LEAKS_PEDANTIC) 1.228 + if (pM->empty()) { 1.229 + delete pM; 1.230 + *M = 0; 1.231 + } 1.232 +#endif 1.233 + } 1.234 + } 1.235 + } 1.236 + } 1.237 +} 1.238 + 1.239 +_Locale_ctype* _STLP_CALL __acquire_ctype(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) { 1.240 + return __REINTERPRET_CAST(_Locale_ctype*, __acquire_category(name, buf, hint, 1.241 + _Locale_extract_ctype_name, _Loc_ctype_create, _Loc_ctype_default, 1.242 + ctype_hash(), __err_code)); 1.243 +} 1.244 +_Locale_codecvt* _STLP_CALL __acquire_codecvt(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) { 1.245 + return __REINTERPRET_CAST(_Locale_codecvt*, __acquire_category(name, buf, hint, 1.246 + _Locale_extract_ctype_name, _Loc_codecvt_create, _Loc_ctype_default, 1.247 + codecvt_hash(), __err_code)); 1.248 +} 1.249 +_Locale_numeric* _STLP_CALL __acquire_numeric(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) { 1.250 + return __REINTERPRET_CAST(_Locale_numeric*, __acquire_category(name, buf, hint, 1.251 + _Locale_extract_numeric_name, _Loc_numeric_create, _Loc_numeric_default, 1.252 + numeric_hash(), __err_code)); 1.253 +} 1.254 +_Locale_time* _STLP_CALL __acquire_time(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) { 1.255 + return __REINTERPRET_CAST(_Locale_time*, __acquire_category(name, buf, hint, 1.256 + _Locale_extract_time_name, _Loc_time_create, _Loc_time_default, 1.257 + time_hash(), __err_code)); 1.258 +} 1.259 +_Locale_collate* _STLP_CALL __acquire_collate(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) { 1.260 + return __REINTERPRET_CAST(_Locale_collate*, __acquire_category(name, buf, hint, 1.261 + _Locale_extract_collate_name, _Loc_collate_create, _Loc_collate_default, 1.262 + collate_hash(), __err_code)); 1.263 +} 1.264 +_Locale_monetary* _STLP_CALL __acquire_monetary(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) { 1.265 + return __REINTERPRET_CAST(_Locale_monetary*, __acquire_category(name, buf, hint, 1.266 + _Locale_extract_monetary_name, _Loc_monetary_create, _Loc_monetary_default, 1.267 + monetary_hash(), __err_code)); 1.268 +} 1.269 +_Locale_messages* _STLP_CALL __acquire_messages(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) { 1.270 + return __REINTERPRET_CAST(_Locale_messages*, __acquire_category(name, buf, hint, 1.271 + _Locale_extract_messages_name, _Loc_messages_create, _Loc_messages_default, 1.272 + messages_hash(), __err_code)); 1.273 +} 1.274 + 1.275 +void _STLP_CALL __release_ctype(_Locale_ctype* cat) 1.276 +{ __release_category(cat, _Loc_ctype_destroy, _Loc_ctype_name, ctype_hash()); } 1.277 +void _STLP_CALL __release_codecvt(_Locale_codecvt* cat) 1.278 +{ __release_category(cat, _Loc_codecvt_destroy, _Loc_codecvt_name, codecvt_hash()); } 1.279 +void _STLP_CALL __release_numeric(_Locale_numeric* cat) 1.280 +{ __release_category(cat, _Loc_numeric_destroy, _Loc_numeric_name, numeric_hash()); } 1.281 +void _STLP_CALL __release_time(_Locale_time* cat) 1.282 +{ __release_category(cat, _Loc_time_destroy, _Loc_time_name, time_hash()); } 1.283 +void _STLP_CALL __release_collate(_Locale_collate* cat) 1.284 +{ __release_category(cat, _Loc_collate_destroy, _Loc_collate_name, collate_hash()); } 1.285 +void _STLP_CALL __release_monetary(_Locale_monetary* cat) 1.286 +{ __release_category(cat, _Loc_monetary_destroy, _Loc_monetary_name, monetary_hash()); } 1.287 +void _STLP_CALL __release_messages(_Locale_messages* cat) 1.288 +{ __release_category(cat, _Loc_messages_destroy, _Loc_messages_name, messages_hash()); } 1.289 + 1.290 +_STLP_MOVE_TO_STD_NAMESPACE 1.291 +_STLP_END_NAMESPACE