ReactOS  0.4.14-dev-337-gf981a68
locale_catalog.cpp
Go to the documentation of this file.
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 
32 
33 // Those wrappers are needed to use locale functions in __acquire_category,
34 // all functions have to present the same prototype.
35 
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); }
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); }
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); }
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); }
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); }
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); }
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 
51 static char const* _Loc_ctype_name(void* l, char* s)
52 { return _Locale_ctype_name((_Locale_ctype*)l, s); }
53 static char const* _Loc_codecvt_name(void* l, char* s)
55 static char const* _Loc_numeric_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); }
59 static char const* _Loc_collate_name(void* l, char* s)
61 static char const* _Loc_monetary_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 
66 static const char* _Loc_ctype_default(char* p)
67 { return _Locale_ctype_default(p); }
68 static const char* _Loc_numeric_default(char * p)
69 { return _Locale_numeric_default(p); }
70 static const char* _Loc_time_default(char* p)
71 { return _Locale_time_default(p); }
72 static const char* _Loc_collate_default(char* p)
73 { return _Locale_collate_default(p); }
74 static const char* _Loc_monetary_default(char* p)
75 { return _Locale_monetary_default(p); }
76 static const char* _Loc_messages_default(char* p)
77 { return _Locale_messages_default(p); }
78 
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.
100 
101 // Look up a category by name
103  static Category_Map *_S_ctype_hash = 0;
104  return &_S_ctype_hash;
105 }
107  static Category_Map *_S_codecvt_hash = 0;
108  return &_S_codecvt_hash;
109 }
111  static Category_Map *_S_numeric_hash = 0;
112  return &_S_numeric_hash;
113 }
115  static Category_Map *_S_time_hash = 0;
116  return &_S_time_hash;
117 }
119  static Category_Map *_S_collate_hash = 0;
120  return &_S_collate_hash;
121 }
123  static Category_Map *_S_monetary_hash = 0;
124  return &_S_monetary_hash;
125 }
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 */
136  return lock;
137 }
138 
139 static void*
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;
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 
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
204  loc_destroy_func_t destroy_fun,
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) {
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 
236 _Locale_ctype* _STLP_CALL __acquire_ctype(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) {
239  ctype_hash(), __err_code));
240 }
241 _Locale_codecvt* _STLP_CALL __acquire_codecvt(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) {
244  codecvt_hash(), __err_code));
245 }
246 _Locale_numeric* _STLP_CALL __acquire_numeric(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) {
249  numeric_hash(), __err_code));
250 }
251 _Locale_time* _STLP_CALL __acquire_time(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) {
254  time_hash(), __err_code));
255 }
256 _Locale_collate* _STLP_CALL __acquire_collate(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) {
259  collate_hash(), __err_code));
260 }
261 _Locale_monetary* _STLP_CALL __acquire_monetary(const char* &name, char *buf, _Locale_name_hint* hint, int *__err_code) {
264  monetary_hash(), __err_code));
265 }
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,
269  messages_hash(), __err_code));
270 }
271 
284 void _STLP_CALL __release_messages(_Locale_messages* cat)
286 
void _STLP_CALL __release_numeric(_Locale_numeric *cat)
char const * _Locale_extract_monetary_name(const char *cname, char *__buf, struct _Locale_name_hint *__hint, int *__err_code)
void _STLP_CALL __release_ctype(_Locale_ctype *cat)
static char const * _Loc_ctype_name(void *l, char *s)
struct _Locale_collate * _Locale_collate_create(const char *, struct _Locale_name_hint *, int *)
static void * _Loc_collate_create(const char *s, _Locale_name_hint *hint, int *__err_code)
char const * _Locale_monetary_name(const struct _Locale_monetary *, char *__buf)
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
static const char * _Loc_collate_default(char *p)
#define _STLP_STATIC_MUTEX
Definition: features.h:267
static const char * _Loc_messages_default(char *p)
rwlock_t lock
Definition: tcpcore.h:1163
bool empty() const
Definition: _hash_map.h:168
void _Locale_collate_destroy(struct _Locale_collate *)
void *(* loc_create_func_t)(const char *, _Locale_name_hint *, int *__err_code)
static void _Loc_numeric_destroy(void *p)
static _STLP_STATIC_MUTEX & category_hash_mutex()
static char const * _Loc_messages_name(void *l, char *s)
void _Locale_monetary_destroy(struct _Locale_monetary *)
static const char * _Loc_monetary_default(char *p)
const char * _Locale_messages_default(char *__buf)
iterator end()
Definition: _hash_map.h:174
static void _Loc_collate_destroy(void *p)
static Category_Map ** time_hash()
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
void _Locale_messages_destroy(struct _Locale_messages *)
static void _Loc_time_destroy(void *p)
#define M(row, col)
struct _Locale_numeric * _Locale_numeric_create(const char *, struct _Locale_name_hint *, int *)
struct _Locale_messages * _Locale_messages_create(const char *, struct _Locale_name_hint *, int *)
void _STLP_CALL __release_time(_Locale_time *cat)
static void * _Loc_messages_create(const char *s, _Locale_name_hint *hint, int *__err_code)
void _STLP_CALL __release_messages(_Locale_messages *cat)
const char * _Locale_monetary_default(char *__buf)
static void * _Loc_numeric_create(const char *s, _Locale_name_hint *hint, int *__err_code)
const char * _Locale_time_default(char *__buf)
char const * _Locale_messages_name(const struct _Locale_messages *, char *__buf)
void _Locale_codecvt_destroy(struct _Locale_codecvt *)
static Category_Map ** codecvt_hash()
static void * _Loc_monetary_create(const char *s, _Locale_name_hint *hint, int *__err_code)
char const * _Locale_extract_time_name(const char *cname, char *__buf, struct _Locale_name_hint *__hint, int *__err_code)
struct _Locale_monetary * _Locale_monetary_create(const char *, struct _Locale_name_hint *, int *)
void _Locale_numeric_destroy(struct _Locale_numeric *)
#define _STLP_LOC_UNDEFINED
Definition: c_locale.h:99
_Locale_ctype *_STLP_CALL __acquire_ctype(const char *&name, char *buf, _Locale_name_hint *hint, int *__err_code)
_STLP_TEMPLATE_FOR_CONT_EXT iterator find(const _KT &__key)
Definition: _hash_map.h:195
#define _STLP_MOVE_TO_PRIV_NAMESPACE
Definition: features.h:524
static void * _Loc_time_create(const char *s, _Locale_name_hint *hint, int *__err_code)
char const * _Locale_ctype_name(const struct _Locale_ctype *, char *__buf)
static void _Loc_monetary_destroy(void *p)
char const * _Locale_extract_collate_name(const char *cname, char *__buf, struct _Locale_name_hint *__hint, int *__err_code)
const char * _Locale_numeric_default(char *__buf)
char const * _Locale_extract_ctype_name(const char *cname, char *__buf, struct _Locale_name_hint *__hint, int *__err_code)
static Category_Map ** monetary_hash()
_Locale_codecvt *_STLP_CALL __acquire_codecvt(const char *&name, char *buf, _Locale_name_hint *hint, int *__err_code)
void _Locale_time_destroy(struct _Locale_time *)
_Locale_time *_STLP_CALL __acquire_time(const char *&name, char *buf, _Locale_name_hint *hint, int *__err_code)
int get_name(unsigned char **pos, uint32_t *remaining, const char **out_name)
Definition: util.c:55
static const char * _Loc_time_default(char *p)
_Locale_messages *_STLP_CALL __acquire_messages(const char *&name, char *buf, _Locale_name_hint *hint, int *__err_code)
static char const * _Loc_collate_name(void *l, char *s)
char const * _Locale_codecvt_name(const struct _Locale_codecvt *, char *__buf)
static char const * _Loc_numeric_name(void *l, char *s)
_STLP_TEMPLATE_FOR_CONT_EXT size_type erase(const _KT &__key)
Definition: _hash_map.h:218
hash_map< string, pair< void *, size_t >, hash< string >, equal_to< string > > Category_Map
char const *(* loc_extract_name_func_t)(const char *, char *, _Locale_name_hint *, int *__err_code)
r l[0]
Definition: byte_order.h:167
static char const * _Loc_monetary_name(void *l, char *s)
const char * _Locale_ctype_default(char *__buf)
struct _Locale_codecvt * _Locale_codecvt_create(const char *, struct _Locale_name_hint *, int *)
#define _STLP_MOVE_TO_STD_NAMESPACE
Definition: features.h:525
void _STLP_CALL __release_monetary(_Locale_monetary *cat)
_Ht::iterator iterator
Definition: _hash_map.h:75
char const * _Locale_collate_name(const struct _Locale_collate *, char *__buf)
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)
static char const * _Loc_codecvt_name(void *l, char *s)
static const char * _Loc_numeric_default(char *p)
char const * _Locale_time_name(const struct _Locale_time *, char *__buf)
struct _Locale_time * _Locale_time_create(const char *, struct _Locale_name_hint *, int *)
_STLP_BEGIN_NAMESPACE static _STLP_MOVE_TO_PRIV_NAMESPACE void * _Loc_ctype_create(const char *s, _Locale_name_hint *hint, int *__err_code)
static void _Loc_messages_destroy(void *p)
char const * _Locale_extract_numeric_name(const char *cname, char *__buf, struct _Locale_name_hint *__hint, int *__err_code)
GLdouble s
Definition: gl.h:2039
_Locale_numeric *_STLP_CALL __acquire_numeric(const char *&name, char *buf, _Locale_name_hint *hint, int *__err_code)
void _Locale_ctype_destroy(struct _Locale_ctype *)
static void _Loc_ctype_destroy(void *p)
#define __REINTERPRET_CAST(__x, __y)
Definition: features.h:586
#define _Locale_MAX_SIMPLE_NAME
Definition: c_locale.h:54
char string[160]
Definition: util.h:11
static void * _Loc_codecvt_create(const char *s, _Locale_name_hint *hint, int *__err_code)
#define _STLP_MUTEX_INITIALIZER
Definition: _threads.h:241
char const * _Locale_numeric_name(const struct _Locale_numeric *, char *__buf)
static Category_Map ** collate_hash()
void _STLP_CALL __release_collate(_Locale_collate *cat)
char const *(* loc_name_func_t)(void *l, char *s)
DWORD hint
Definition: vfdcmd.c:88
void(* loc_destroy_func_t)(void *l)
#define _STLP_END_NAMESPACE
Definition: features.h:503
Definition: _pair.h:47
static void __release_category(void *cat, loc_destroy_func_t destroy_fun, loc_name_func_t get_name, Category_Map **M)
static Category_Map ** numeric_hash()
void _STLP_CALL __release_codecvt(_Locale_codecvt *cat)
_Locale_collate *_STLP_CALL __acquire_collate(const char *&name, char *buf, _Locale_name_hint *hint, int *__err_code)
const char * _Locale_collate_default(char *__buf)
Definition: name.c:36
char const * _Locale_extract_messages_name(const char *cname, char *__buf, struct _Locale_name_hint *__hint, int *__err_code)
struct _Locale_ctype * _Locale_ctype_create(const char *, struct _Locale_name_hint *, int *)
static void _Loc_codecvt_destroy(void *p)
_Locale_monetary *_STLP_CALL __acquire_monetary(const char *&name, char *buf, _Locale_name_hint *hint, int *__err_code)
static char const * _Loc_time_name(void *l, char *s)
#define _STLP_BEGIN_NAMESPACE
Definition: features.h:501
static const char * _Loc_ctype_default(char *p)
GLfloat GLfloat p
Definition: glext.h:8902
static Category_Map ** messages_hash()
static Category_Map ** ctype_hash()
GLuint64EXT * result
Definition: glext.h:11304
#define _STLP_CALL
Definition: _bc.h:131
const char *(* loc_default_name_func_t)(char *s)