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