locale_catalog.cpp 13 KB

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