build/stlport/src/locale_catalog.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * Copyright (c) 1999
michael@0 3 * Silicon Graphics Computer Systems, Inc.
michael@0 4 *
michael@0 5 * Copyright (c) 1999
michael@0 6 * Boris Fomitchev
michael@0 7 *
michael@0 8 * This material is provided "as is", with absolutely no warranty expressed
michael@0 9 * or implied. Any use is at your own risk.
michael@0 10 *
michael@0 11 * Permission to use or copy this software for any purpose is hereby granted
michael@0 12 * without fee, provided the above notices are retained on all copies.
michael@0 13 * Permission to modify the code and to distribute modified code is granted,
michael@0 14 * provided the above notices are retained, and a notice that the code was
michael@0 15 * modified is included with the above copyright notice.
michael@0 16 *
michael@0 17 */
michael@0 18 #include "stlport_prefix.h"
michael@0 19
michael@0 20 #include <hash_map>
michael@0 21 #include <string>
michael@0 22
michael@0 23 #include <locale>
michael@0 24 #include <istream>
michael@0 25
michael@0 26 #include "c_locale.h"
michael@0 27 #include "locale_impl.h"
michael@0 28 #include "acquire_release.h"
michael@0 29
michael@0 30 _STLP_BEGIN_NAMESPACE
michael@0 31 _STLP_MOVE_TO_PRIV_NAMESPACE
michael@0 32
michael@0 33 // Those wrappers are needed to use locale functions in __acquire_category,
michael@0 34 // all functions have to present the same prototype.
michael@0 35
michael@0 36 static void* _Loc_ctype_create(const char * s, _Locale_name_hint* hint, int *__err_code)
michael@0 37 { return _Locale_ctype_create(s, hint, __err_code); }
michael@0 38 static void* _Loc_codecvt_create(const char * s, _Locale_name_hint* hint, int *__err_code)
michael@0 39 { return _Locale_codecvt_create(s, hint, __err_code); }
michael@0 40 static void* _Loc_numeric_create(const char * s, _Locale_name_hint* hint, int *__err_code)
michael@0 41 { return _Locale_numeric_create(s, hint, __err_code); }
michael@0 42 static void* _Loc_time_create(const char * s, _Locale_name_hint* hint, int *__err_code)
michael@0 43 { return _Locale_time_create(s, hint, __err_code); }
michael@0 44 static void* _Loc_collate_create(const char * s, _Locale_name_hint* hint, int *__err_code)
michael@0 45 { return _Locale_collate_create(s, hint, __err_code); }
michael@0 46 static void* _Loc_monetary_create(const char * s, _Locale_name_hint* hint, int *__err_code)
michael@0 47 { return _Locale_monetary_create(s, hint, __err_code); }
michael@0 48 static void* _Loc_messages_create(const char * s, _Locale_name_hint* hint, int *__err_code)
michael@0 49 { return _Locale_messages_create(s, hint, __err_code); }
michael@0 50
michael@0 51 static char const* _Loc_ctype_name(void* l, char* s)
michael@0 52 { return _Locale_ctype_name((_Locale_ctype*)l, s); }
michael@0 53 static char const* _Loc_codecvt_name(void* l, char* s)
michael@0 54 { return _Locale_codecvt_name((_Locale_codecvt*)l, s); }
michael@0 55 static char const* _Loc_numeric_name(void* l, char* s)
michael@0 56 { return _Locale_numeric_name((_Locale_numeric*)l, s); }
michael@0 57 static char const* _Loc_time_name(void* l, char* s)
michael@0 58 { return _Locale_time_name((_Locale_time*)l, s); }
michael@0 59 static char const* _Loc_collate_name(void* l, char* s)
michael@0 60 { return _Locale_collate_name((_Locale_collate*)l, s); }
michael@0 61 static char const* _Loc_monetary_name(void* l, char* s)
michael@0 62 { return _Locale_monetary_name((_Locale_monetary*)l, s); }
michael@0 63 static char const* _Loc_messages_name(void* l, char* s)
michael@0 64 { return _Locale_messages_name((_Locale_messages*)l, s); }
michael@0 65
michael@0 66 static const char* _Loc_ctype_default(char* p)
michael@0 67 { return _Locale_ctype_default(p); }
michael@0 68 static const char* _Loc_numeric_default(char * p)
michael@0 69 { return _Locale_numeric_default(p); }
michael@0 70 static const char* _Loc_time_default(char* p)
michael@0 71 { return _Locale_time_default(p); }
michael@0 72 static const char* _Loc_collate_default(char* p)
michael@0 73 { return _Locale_collate_default(p); }
michael@0 74 static const char* _Loc_monetary_default(char* p)
michael@0 75 { return _Locale_monetary_default(p); }
michael@0 76 static const char* _Loc_messages_default(char* p)
michael@0 77 { return _Locale_messages_default(p); }
michael@0 78
michael@0 79 static void _Loc_ctype_destroy(void* p) {_Locale_ctype_destroy((_Locale_ctype*)p); }
michael@0 80 static void _Loc_codecvt_destroy(void* p) {_Locale_codecvt_destroy((_Locale_codecvt*)p); }
michael@0 81 static void _Loc_numeric_destroy(void* p) {_Locale_numeric_destroy((_Locale_numeric*)p); }
michael@0 82 static void _Loc_time_destroy(void* p) {_Locale_time_destroy((_Locale_time*)p);}
michael@0 83 static void _Loc_collate_destroy(void* p) {_Locale_collate_destroy((_Locale_collate*)p);}
michael@0 84 static void _Loc_monetary_destroy(void* p) {_Locale_monetary_destroy((_Locale_monetary*)p);}
michael@0 85 static void _Loc_messages_destroy(void* p) {_Locale_messages_destroy((_Locale_messages*)p);}
michael@0 86
michael@0 87 typedef void* (*loc_create_func_t)(const char *, _Locale_name_hint*, int *__err_code);
michael@0 88 typedef char const* (*loc_name_func_t)(void* l, char* s);
michael@0 89 typedef void (*loc_destroy_func_t)(void* l);
michael@0 90 typedef const char* (*loc_default_name_func_t)(char* s);
michael@0 91 typedef char const* (*loc_extract_name_func_t)(const char*, char*, _Locale_name_hint*, int *__err_code);
michael@0 92
michael@0 93 //----------------------------------------------------------------------
michael@0 94 // Acquire and release low-level category objects. The whole point of
michael@0 95 // this is so that we don't allocate (say) four different _Locale_ctype
michael@0 96 // objects for a single locale.
michael@0 97
michael@0 98 // Global hash tables for category objects.
michael@0 99 typedef hash_map<string, pair<void*, size_t>, hash<string>, equal_to<string> > Category_Map;
michael@0 100
michael@0 101 // Look up a category by name
michael@0 102 static Category_Map** ctype_hash() {
michael@0 103 static Category_Map *_S_ctype_hash = 0;
michael@0 104 return &_S_ctype_hash;
michael@0 105 }
michael@0 106 static Category_Map** codecvt_hash() {
michael@0 107 static Category_Map *_S_codecvt_hash = 0;
michael@0 108 return &_S_codecvt_hash;
michael@0 109 }
michael@0 110 static Category_Map** numeric_hash() {
michael@0 111 static Category_Map *_S_numeric_hash = 0;
michael@0 112 return &_S_numeric_hash;
michael@0 113 }
michael@0 114 static Category_Map** time_hash() {
michael@0 115 static Category_Map *_S_time_hash = 0;
michael@0 116 return &_S_time_hash;
michael@0 117 }
michael@0 118 static Category_Map** collate_hash() {
michael@0 119 static Category_Map *_S_collate_hash = 0;
michael@0 120 return &_S_collate_hash;
michael@0 121 }
michael@0 122 static Category_Map** monetary_hash() {
michael@0 123 static Category_Map *_S_monetary_hash = 0;
michael@0 124 return &_S_monetary_hash;
michael@0 125 }
michael@0 126 static Category_Map** messages_hash() {
michael@0 127 static Category_Map *_S_messages_hash;
michael@0 128 return &_S_messages_hash;
michael@0 129 }
michael@0 130
michael@0 131 // We have a single lock for all of the hash tables. We may wish to
michael@0 132 // replace it with six different locks.
michael@0 133 /* REFERENCED */
michael@0 134 static _STLP_STATIC_MUTEX& category_hash_mutex() {
michael@0 135 static _STLP_STATIC_MUTEX lock _STLP_MUTEX_INITIALIZER;
michael@0 136 return lock;
michael@0 137 }
michael@0 138
michael@0 139 static void*
michael@0 140 __acquire_category(const char* &name, char *buf, _Locale_name_hint* hint,
michael@0 141 loc_extract_name_func_t extract_name,
michael@0 142 loc_create_func_t create_obj, loc_default_name_func_t default_name,
michael@0 143 Category_Map ** M, int *__err_code) {
michael@0 144 #if !defined (__BORLANDC__) || (__BORLANDC__ >= 0x564)
michael@0 145 typedef Category_Map::iterator Category_iterator;
michael@0 146 pair<Category_iterator, bool> result;
michael@0 147 #else
michael@0 148 # if !defined(_STLP_DEBUG)
michael@0 149 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;
michael@0 150 # else
michael@0 151 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;
michael@0 152 # endif
michael@0 153 #endif
michael@0 154
michael@0 155 *__err_code = _STLP_LOC_UNDEFINED;
michael@0 156
michael@0 157 // Find what name to look for. Be careful if user requests the default.
michael@0 158 if (name[0] == 0) {
michael@0 159 name = default_name(buf);
michael@0 160 if (name == 0 || name[0] == 0)
michael@0 161 name = "C";
michael@0 162 }
michael@0 163 else {
michael@0 164 const char* cname = extract_name(name, buf, hint, __err_code);
michael@0 165 if (cname == 0) {
michael@0 166 return 0;
michael@0 167 }
michael@0 168 name = cname;
michael@0 169 }
michael@0 170
michael@0 171 Category_Map::value_type __e(name, pair<void*,size_t>((void*)0,size_t(0)));
michael@0 172
michael@0 173 _STLP_auto_lock sentry(category_hash_mutex());
michael@0 174
michael@0 175 if (!*M)
michael@0 176 *M = new Category_Map();
michael@0 177
michael@0 178 // Look for an existing entry with that name.
michael@0 179 result = (*M)->insert_noresize(__e);
michael@0 180
michael@0 181 if (result.second) {
michael@0 182 // There was no entry in the map already. Create the category.
michael@0 183 (*result.first).second.first = create_obj(name, hint, __err_code);
michael@0 184 if (!(*result.first).second.first) {
michael@0 185 (*M)->erase(result.first);
michael@0 186 #if defined (_STLP_LEAKS_PEDANTIC)
michael@0 187 if ((*M)->empty()) {
michael@0 188 delete *M;
michael@0 189 *M = 0;
michael@0 190 }
michael@0 191 #endif
michael@0 192 return 0;
michael@0 193 }
michael@0 194 }
michael@0 195
michael@0 196 // Increment the reference count.
michael@0 197 ++((*result.first).second.second);
michael@0 198
michael@0 199 return (*result.first).second.first;
michael@0 200 }
michael@0 201
michael@0 202 static void
michael@0 203 __release_category(void* cat,
michael@0 204 loc_destroy_func_t destroy_fun,
michael@0 205 loc_name_func_t get_name,
michael@0 206 Category_Map** M) {
michael@0 207 Category_Map *pM = *M;
michael@0 208
michael@0 209 if (cat && pM) {
michael@0 210 // Find the name of the category object.
michael@0 211 char buf[_Locale_MAX_SIMPLE_NAME + 1];
michael@0 212 char const* name = get_name(cat, buf);
michael@0 213
michael@0 214 if (name != 0) {
michael@0 215 _STLP_auto_lock sentry(category_hash_mutex());
michael@0 216 Category_Map::iterator it = pM->find(name);
michael@0 217 if (it != pM->end()) {
michael@0 218 // Decrement the ref count. If it goes to zero, delete this category
michael@0 219 // from the map.
michael@0 220 if (--((*it).second.second) == 0) {
michael@0 221 void* cat1 = (*it).second.first;
michael@0 222 destroy_fun(cat1);
michael@0 223 pM->erase(it);
michael@0 224 #if defined (_STLP_LEAKS_PEDANTIC)
michael@0 225 if (pM->empty()) {
michael@0 226 delete pM;
michael@0 227 *M = 0;
michael@0 228 }
michael@0 229 #endif
michael@0 230 }
michael@0 231 }
michael@0 232 }
michael@0 233 }
michael@0 234 }
michael@0 235
michael@0 236 _Locale_ctype* _STLP_CALL __acquire_ctype(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) {
michael@0 237 return __REINTERPRET_CAST(_Locale_ctype*, __acquire_category(name, buf, hint,
michael@0 238 _Locale_extract_ctype_name, _Loc_ctype_create, _Loc_ctype_default,
michael@0 239 ctype_hash(), __err_code));
michael@0 240 }
michael@0 241 _Locale_codecvt* _STLP_CALL __acquire_codecvt(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) {
michael@0 242 return __REINTERPRET_CAST(_Locale_codecvt*, __acquire_category(name, buf, hint,
michael@0 243 _Locale_extract_ctype_name, _Loc_codecvt_create, _Loc_ctype_default,
michael@0 244 codecvt_hash(), __err_code));
michael@0 245 }
michael@0 246 _Locale_numeric* _STLP_CALL __acquire_numeric(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) {
michael@0 247 return __REINTERPRET_CAST(_Locale_numeric*, __acquire_category(name, buf, hint,
michael@0 248 _Locale_extract_numeric_name, _Loc_numeric_create, _Loc_numeric_default,
michael@0 249 numeric_hash(), __err_code));
michael@0 250 }
michael@0 251 _Locale_time* _STLP_CALL __acquire_time(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) {
michael@0 252 return __REINTERPRET_CAST(_Locale_time*, __acquire_category(name, buf, hint,
michael@0 253 _Locale_extract_time_name, _Loc_time_create, _Loc_time_default,
michael@0 254 time_hash(), __err_code));
michael@0 255 }
michael@0 256 _Locale_collate* _STLP_CALL __acquire_collate(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) {
michael@0 257 return __REINTERPRET_CAST(_Locale_collate*, __acquire_category(name, buf, hint,
michael@0 258 _Locale_extract_collate_name, _Loc_collate_create, _Loc_collate_default,
michael@0 259 collate_hash(), __err_code));
michael@0 260 }
michael@0 261 _Locale_monetary* _STLP_CALL __acquire_monetary(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) {
michael@0 262 return __REINTERPRET_CAST(_Locale_monetary*, __acquire_category(name, buf, hint,
michael@0 263 _Locale_extract_monetary_name, _Loc_monetary_create, _Loc_monetary_default,
michael@0 264 monetary_hash(), __err_code));
michael@0 265 }
michael@0 266 _Locale_messages* _STLP_CALL __acquire_messages(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) {
michael@0 267 return __REINTERPRET_CAST(_Locale_messages*, __acquire_category(name, buf, hint,
michael@0 268 _Locale_extract_messages_name, _Loc_messages_create, _Loc_messages_default,
michael@0 269 messages_hash(), __err_code));
michael@0 270 }
michael@0 271
michael@0 272 void _STLP_CALL __release_ctype(_Locale_ctype* cat)
michael@0 273 { __release_category(cat, _Loc_ctype_destroy, _Loc_ctype_name, ctype_hash()); }
michael@0 274 void _STLP_CALL __release_codecvt(_Locale_codecvt* cat)
michael@0 275 { __release_category(cat, _Loc_codecvt_destroy, _Loc_codecvt_name, codecvt_hash()); }
michael@0 276 void _STLP_CALL __release_numeric(_Locale_numeric* cat)
michael@0 277 { __release_category(cat, _Loc_numeric_destroy, _Loc_numeric_name, numeric_hash()); }
michael@0 278 void _STLP_CALL __release_time(_Locale_time* cat)
michael@0 279 { __release_category(cat, _Loc_time_destroy, _Loc_time_name, time_hash()); }
michael@0 280 void _STLP_CALL __release_collate(_Locale_collate* cat)
michael@0 281 { __release_category(cat, _Loc_collate_destroy, _Loc_collate_name, collate_hash()); }
michael@0 282 void _STLP_CALL __release_monetary(_Locale_monetary* cat)
michael@0 283 { __release_category(cat, _Loc_monetary_destroy, _Loc_monetary_name, monetary_hash()); }
michael@0 284 void _STLP_CALL __release_messages(_Locale_messages* cat)
michael@0 285 { __release_category(cat, _Loc_messages_destroy, _Loc_messages_name, messages_hash()); }
michael@0 286
michael@0 287 _STLP_MOVE_TO_STD_NAMESPACE
michael@0 288 _STLP_END_NAMESPACE

mercurial