/home/ntakagi/work/STLport-5.1.5/src/locale_catalog.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1999
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Copyright (c) 1999
00006  * Boris Fomitchev
00007  *
00008  * This material is provided "as is", with absolutely no warranty expressed
00009  * or implied. Any use is at your own risk.
00010  *
00011  * Permission to use or copy this software for any purpose is hereby granted
00012  * without fee, provided the above notices are retained on all copies.
00013  * Permission to modify the code and to distribute modified code is granted,
00014  * provided the above notices are retained, and a notice that the code was
00015  * modified is included with the above copyright notice.
00016  *
00017  */
00018 #include "stlport_prefix.h"
00019 
00020 #include <hash_map>
00021 #include <string>
00022 
00023 #include <locale>
00024 #include <istream>
00025 
00026 #include "c_locale.h"
00027 #include "locale_impl.h"
00028 #include "acquire_release.h"
00029 
00030 _STLP_BEGIN_NAMESPACE
00031 _STLP_MOVE_TO_PRIV_NAMESPACE
00032 
00033 // those wrappers are needed to avoid extern "C"
00034 
00035 static void* _Loc_ctype_create(const char * s, _Locale_name_hint* hint)
00036 { return _Locale_ctype_create(s, hint); }
00037 static void* _Loc_numeric_create(const char * s, _Locale_name_hint* hint)
00038 { return _Locale_numeric_create(s, hint); }
00039 static void* _Loc_time_create(const char * s, _Locale_name_hint* hint)
00040 { return _Locale_time_create(s, hint); }
00041 static void* _Loc_collate_create(const char * s, _Locale_name_hint* hint)
00042 { return _Locale_collate_create(s, hint); }
00043 static void* _Loc_monetary_create(const char * s, _Locale_name_hint* hint)
00044 { return _Locale_monetary_create(s, hint); }
00045 static void* _Loc_messages_create(const char * s, _Locale_name_hint* hint)
00046 { return _Locale_messages_create(s, hint); }
00047 
00048 static char const* _Loc_ctype_name(const void* l, char* s)
00049 { return _Locale_ctype_name(l, s); }
00050 static char const* _Loc_numeric_name(const void* l, char* s)
00051 { return _Locale_numeric_name(l, s); }
00052 static char const* _Loc_time_name(const void* l, char* s)
00053 { return _Locale_time_name(l,s); }
00054 static char const* _Loc_collate_name( const void* l, char* s)
00055 { return _Locale_collate_name(l,s); }
00056 static char const* _Loc_monetary_name(const void* l, char* s)
00057 { return _Locale_monetary_name(l,s); }
00058 static char const* _Loc_messages_name(const void* l, char* s)
00059 { return _Locale_messages_name(l,s); }
00060 
00061 static const char* _Loc_ctype_default(char* p)
00062 { return _Locale_ctype_default(p); }
00063 static const char* _Loc_numeric_default(char * p)
00064 { return _Locale_numeric_default(p); }
00065 static const char* _Loc_time_default(char* p)
00066 { return _Locale_time_default(p); }
00067 static const char* _Loc_collate_default(char* p)
00068 { return _Locale_collate_default(p); }
00069 static const char* _Loc_monetary_default(char* p)
00070 { return _Locale_monetary_default(p); }
00071 static const char* _Loc_messages_default(char* p)
00072 { return _Locale_messages_default(p); }
00073 
00074 static void _Loc_ctype_destroy(void* p)    {_Locale_ctype_destroy(p); }
00075 static void _Loc_numeric_destroy(void* p)  {_Locale_numeric_destroy(p); }
00076 static void _Loc_time_destroy(void* p)     {_Locale_time_destroy(p);}
00077 static void _Loc_collate_destroy(void* p)  {_Locale_collate_destroy(p);}
00078 static void _Loc_monetary_destroy(void* p) {_Locale_monetary_destroy(p);}
00079 static void _Loc_messages_destroy(void* p) {_Locale_messages_destroy(p);}
00080 
00081 typedef void* (*loc_create_func_t)(const char *, _Locale_name_hint*);
00082 typedef char const* (*loc_name_func_t)(const void* l, char* s);
00083 typedef void (*loc_destroy_func_t)(void* l);
00084 typedef const char* (*loc_default_name_func_t)(char* s);
00085 typedef char const* (*loc_extract_name_func_t)(const char*, char*, _Locale_name_hint*);
00086 
00087 //----------------------------------------------------------------------
00088 // Acquire and release low-level category objects.  The whole point of
00089 // this is so that we don't allocate (say) four different _Locale_ctype
00090 // objects for a single locale.
00091 
00092 // Global hash tables for category objects.
00093 typedef hash_map<string, pair<void*, size_t>, hash<string>, equal_to<string> > Category_Map;
00094 
00095 // Look up a category by name
00096 static Category_Map** ctype_hash() {
00097   static Category_Map *_S_ctype_hash = 0;
00098   return &_S_ctype_hash;
00099 }
00100 static Category_Map** numeric_hash() {
00101   static Category_Map *_S_numeric_hash = 0;
00102   return &_S_numeric_hash;
00103 }
00104 static Category_Map** time_hash() {
00105   static Category_Map *_S_time_hash = 0;
00106   return &_S_time_hash;
00107 }
00108 static Category_Map** collate_hash() {
00109   static Category_Map *_S_collate_hash = 0;
00110   return &_S_collate_hash;
00111 }
00112 static Category_Map** monetary_hash() {
00113   static Category_Map *_S_monetary_hash = 0;
00114   return &_S_monetary_hash;
00115 }
00116 static Category_Map** messages_hash() {
00117   static Category_Map *_S_messages_hash;
00118   return &_S_messages_hash;
00119 }
00120 
00121 // We have a single lock for all of the hash tables.  We may wish to
00122 // replace it with six different locks.
00123 /* REFERENCED */
00124 static _STLP_STATIC_MUTEX __category_hash_lock _STLP_MUTEX_INITIALIZER;
00125 
00126 static void*
00127 __acquire_category(const char* name, _Locale_name_hint* hint,
00128                    loc_extract_name_func_t extract_name,
00129                    loc_create_func_t create_obj, loc_default_name_func_t default_obj,
00130                    Category_Map ** M) {
00131 #if !defined (__BORLANDC__) || (__BORLANDC__ >= 0x564)
00132   typedef Category_Map::iterator Category_iterator;
00133   pair<Category_iterator, bool> result;
00134 #else
00135   pair<Category_Map::iterator, bool> result;
00136 #endif
00137 
00138   // Find what name to look for. Be careful if user requests the default.
00139   const char *cname;
00140   char buf[_Locale_MAX_SIMPLE_NAME];
00141   if (name == 0 || name[0] == 0) {
00142     cname = default_obj(buf);
00143     if (cname == 0 || cname[0] == 0)
00144       cname = "C";
00145   }
00146   else {
00147     cname = extract_name(name, buf, hint);
00148     if (cname == 0) {
00149       return 0;
00150     }
00151   }
00152 
00153   Category_Map::value_type __e(cname, pair<void*,size_t>((void*)0,size_t(0)));
00154 
00155   _STLP_auto_lock sentry(__category_hash_lock);
00156 
00157   if (!*M)
00158     *M = new Category_Map();
00159 
00160 #if defined(__SC__)    //*TY 06/01/2000 - added workaround for SCpp
00161   if(!*M) delete *M;  //*TY 06/01/2000 - it forgets to generate dtor for Category_Map class. This fake code forces to generate one.
00162 #endif          //*TY 06/01/2000 -
00163 
00164   // Look for an existing entry with that name.
00165   result = (*M)->insert_noresize(__e);
00166 
00167   // There was no entry in the map already.  Create the category.
00168   if (result.second)
00169     (*result.first).second.first = create_obj(cname, hint);
00170 
00171   // Increment the reference count.
00172   ++((*result.first).second.second);
00173 
00174   return (*result.first).second.first;
00175 }
00176 
00177 static void
00178 __release_category(void* cat,
00179                    loc_destroy_func_t destroy_fun,
00180                    loc_name_func_t get_name,
00181                    Category_Map** M) {
00182   Category_Map *pM = *M;
00183 
00184   if (cat && pM) {
00185     // Find the name of the category object.
00186     char buf[_Locale_MAX_SIMPLE_NAME + 1];
00187     char const* name = get_name(cat, buf);
00188 
00189     if (name != 0) {
00190       _STLP_auto_lock sentry(__category_hash_lock);
00191       Category_Map::iterator it = pM->find(name);
00192       if (it != pM->end()) {
00193         // Decrement the ref count.  If it goes to zero, delete this category
00194         // from the map.
00195         if (--((*it).second.second) == 0) {
00196           void* cat1 = (*it).second.first;
00197           destroy_fun(cat1);
00198           pM->erase(it);
00199 #if defined (_STLP_LEAKS_PEDANTIC)
00200           if (pM->empty()) {
00201             delete pM;
00202             *M = 0;
00203           }
00204 #endif /* _STLP_LEAKS_PEDANTIC */
00205         }
00206       }
00207     }
00208   }
00209 }
00210 
00211 _Locale_ctype* _STLP_CALL __acquire_ctype(const char* name, _Locale_name_hint* hint) {
00212   return __REINTERPRET_CAST(_Locale_ctype*, __acquire_category(name, hint,
00213                                                                _Locale_extract_ctype_name, _Loc_ctype_create, _Loc_ctype_default,
00214                                                                ctype_hash()));
00215 }
00216 _Locale_numeric* _STLP_CALL __acquire_numeric(const char* name, _Locale_name_hint* hint) {
00217   return __REINTERPRET_CAST(_Locale_numeric*, __acquire_category(name, hint,
00218                                                                  _Locale_extract_numeric_name, _Loc_numeric_create, _Loc_numeric_default,
00219                                                                  numeric_hash()));
00220 }
00221 _Locale_time* _STLP_CALL __acquire_time(const char* name, _Locale_name_hint* hint) {
00222   return __REINTERPRET_CAST(_Locale_time*, __acquire_category(name, hint,
00223                                                               _Locale_extract_time_name, _Loc_time_create, _Loc_time_default,
00224                                                               time_hash()));
00225 }
00226 _Locale_collate* _STLP_CALL __acquire_collate(const char* name, _Locale_name_hint* hint) {
00227   return __REINTERPRET_CAST(_Locale_collate*, __acquire_category(name, hint,
00228                                                                  _Locale_extract_collate_name, _Loc_collate_create, _Loc_collate_default,
00229                                                                  collate_hash()));
00230 }
00231 _Locale_monetary* _STLP_CALL __acquire_monetary(const char* name, _Locale_name_hint* hint) {
00232   return __REINTERPRET_CAST(_Locale_monetary*, __acquire_category(name, hint,
00233                                                                   _Locale_extract_monetary_name, _Loc_monetary_create, _Loc_monetary_default,
00234                                                                   monetary_hash()));
00235 }
00236 _Locale_messages* _STLP_CALL __acquire_messages(const char* name, _Locale_name_hint* hint) {
00237   return __REINTERPRET_CAST(_Locale_messages*, __acquire_category(name, hint,
00238                                                                   _Locale_extract_messages_name, _Loc_messages_create, _Loc_messages_default,
00239                                                                   messages_hash()));
00240 }
00241 
00242 void _STLP_CALL __release_ctype(_Locale_ctype* cat)
00243 { __release_category(cat, _Loc_ctype_destroy, _Loc_ctype_name, ctype_hash()); }
00244 void _STLP_CALL __release_numeric(_Locale_numeric* cat)
00245 { __release_category(cat, _Loc_numeric_destroy, _Loc_numeric_name, numeric_hash()); }
00246 void _STLP_CALL __release_time(_Locale_time* cat)
00247 { __release_category(cat, _Loc_time_destroy, _Loc_time_name, time_hash()); }
00248 void _STLP_CALL __release_collate(_Locale_collate* cat)
00249 { __release_category(cat, _Loc_collate_destroy, _Loc_collate_name, collate_hash()); }
00250 void _STLP_CALL __release_monetary(_Locale_monetary* cat)
00251 { __release_category(cat, _Loc_monetary_destroy, _Loc_monetary_name, monetary_hash()); }
00252 void _STLP_CALL __release_messages(_Locale_messages* cat)
00253 { __release_category(cat, _Loc_messages_destroy, _Loc_messages_name, messages_hash()); }
00254 
00255 _STLP_MOVE_TO_STD_NAMESPACE
00256 _STLP_END_NAMESPACE



Generated on Mon Mar 10 15:32:16 2008 by  doxygen 1.5.1