Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenmessages.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 <typeinfo> 00021 00022 #include "message_facets.h" 00023 #include "acquire_release.h" 00024 00025 _STLP_BEGIN_NAMESPACE 00026 00027 _STLP_MOVE_TO_PRIV_NAMESPACE 00028 00029 void _Catalog_locale_map::insert(nl_catd_type key, const locale& L) { 00030 _STLP_TRY { 00031 #if !defined (_STLP_NO_TYPEINFO) && !defined (_STLP_NO_RTTI) 00032 // Don't bother to do anything unless we're using a non-default ctype facet 00033 # ifdef _STLP_NO_WCHAR_T 00034 typedef char _Char; 00035 # else 00036 typedef wchar_t _Char; 00037 # endif 00038 00039 typedef ctype<_Char> wctype; 00040 wctype const& wct = use_facet<wctype>(L); 00041 if (typeid(wct) != typeid(wctype)) { 00042 #endif 00043 if (!M) 00044 M = new map_type; 00045 00046 M->insert(map_type::value_type(key, L)); 00047 #if !defined (_STLP_NO_TYPEINFO) && !defined (_STLP_NO_RTTI) 00048 } 00049 #endif 00050 } 00051 _STLP_CATCH_ALL {} 00052 } 00053 00054 void _Catalog_locale_map::erase(nl_catd_type key) { 00055 if (M) 00056 M->erase(key); 00057 } 00058 00059 locale _Catalog_locale_map::lookup(nl_catd_type key) const { 00060 if (M) { 00061 map_type::const_iterator i = M->find(key); 00062 return i != M->end() ? (*i).second : locale::classic(); 00063 } 00064 else 00065 return locale::classic(); 00066 } 00067 00068 00069 #if defined (_STLP_USE_NL_CATD_MAPPING) 00070 _STLP_VOLATILE __stl_atomic_t _Catalog_nl_catd_map::_count = 0; 00071 00072 messages_base::catalog _Catalog_nl_catd_map::insert(nl_catd_type cat) { 00073 messages_base::catalog &res = Mr[cat]; 00074 if ( res == 0 ) { 00075 #if defined (_STLP_ATOMIC_INCREMENT) 00076 res = __STATIC_CAST(int, _STLP_ATOMIC_INCREMENT(&_count)); 00077 #else 00078 static _STLP_STATIC_MUTEX _Count_lock _STLP_MUTEX_INITIALIZER; 00079 { 00080 _STLP_auto_lock sentry(_Count_lock); 00081 res = __STATIC_CAST(int, ++_count); 00082 } 00083 #endif 00084 M[res] = cat; 00085 } 00086 return res; 00087 } 00088 00089 void _Catalog_nl_catd_map::erase(messages_base::catalog cat) { 00090 map_type::iterator mit(M.find(cat)); 00091 if (mit != M.end()) { 00092 Mr.erase((*mit).second); 00093 M.erase(mit); 00094 } 00095 } 00096 #endif 00097 00098 //---------------------------------------------------------------------- 00099 // 00100 _Messages::_Messages(bool is_wide, const char *name) : 00101 _M_message_obj(0), _M_map(0) { 00102 if (!name) 00103 locale::_M_throw_on_null_name(); 00104 00105 int __err_code; 00106 char buf[_Locale_MAX_SIMPLE_NAME]; 00107 _M_message_obj = _STLP_PRIV __acquire_messages(name, buf, 0, &__err_code); 00108 if (!_M_message_obj) 00109 locale::_M_throw_on_creation_failure(__err_code, name, "messages"); 00110 00111 if (is_wide) 00112 _M_map = new _Catalog_locale_map; 00113 } 00114 00115 _Messages::_Messages(bool is_wide, _Locale_messages* msg) : 00116 _M_message_obj(msg), _M_map(is_wide ? new _Catalog_locale_map() : 0) 00117 {} 00118 00119 _Messages::~_Messages() { 00120 __release_messages(_M_message_obj); 00121 delete _M_map; 00122 } 00123 00124 _Messages::catalog _Messages::do_open(const string& filename, const locale& L) const { 00125 nl_catd_type result = _M_message_obj ? _Locale_catopen(_M_message_obj, filename.c_str()) 00126 : (nl_catd_type)(-1); 00127 00128 if ( result != (nl_catd_type)(-1) ) { 00129 if ( _M_map != 0 ) { 00130 _M_map->insert(result, L); 00131 } 00132 return _STLP_MUTABLE(_Messages_impl, _M_cat).insert( result ); 00133 } 00134 00135 return -1; 00136 } 00137 00138 string _Messages::do_get(catalog cat, 00139 int set, int p_id, const string& dfault) const { 00140 return _M_message_obj != 0 && cat >= 0 00141 ? string(_Locale_catgets(_M_message_obj, _STLP_MUTABLE(_Messages_impl, _M_cat)[cat], 00142 set, p_id, dfault.c_str())) 00143 : dfault; 00144 } 00145 00146 #if !defined (_STLP_NO_WCHAR_T) 00147 00148 wstring 00149 _Messages::do_get(catalog thecat, 00150 int set, int p_id, const wstring& dfault) const { 00151 typedef ctype<wchar_t> wctype; 00152 const wctype& ct = use_facet<wctype>(_M_map->lookup(_STLP_MUTABLE(_Messages_impl, _M_cat)[thecat])); 00153 00154 const char* str = _Locale_catgets(_M_message_obj, _STLP_MUTABLE(_Messages_impl, _M_cat)[thecat], set, p_id, ""); 00155 00156 // Verify that the lookup failed; an empty string might represent success. 00157 if (!str) 00158 return dfault; 00159 else if (str[0] == '\0') { 00160 const char* str2 = _Locale_catgets(_M_message_obj, _STLP_MUTABLE(_Messages_impl, _M_cat)[thecat], set, p_id, "*"); 00161 if (!str2 || ((str2[0] == '*') && (str2[1] == '\0'))) 00162 return dfault; 00163 } 00164 00165 // str is correct. Now we must widen it to get a wstring. 00166 size_t n = strlen(str); 00167 00168 // NOT PORTABLE. What we're doing relies on internal details of the 00169 // string implementation. (Contiguity of string elements.) 00170 wstring result(n, wchar_t(0)); 00171 ct.widen(str, str + n, &*result.begin()); 00172 return result; 00173 } 00174 00175 #endif 00176 00177 void _Messages::do_close(catalog thecat) const { 00178 if (_M_message_obj) 00179 _Locale_catclose(_M_message_obj, _STLP_MUTABLE(_Messages_impl, _M_cat)[thecat]); 00180 if (_M_map) _M_map->erase(_STLP_MUTABLE(_Messages_impl, _M_cat)[thecat]); 00181 _STLP_MUTABLE(_Messages_impl, _M_cat).erase( thecat ); 00182 } 00183 00184 _STLP_MOVE_TO_STD_NAMESPACE 00185 00186 //---------------------------------------------------------------------- 00187 // messages<char> 00188 messages<char>::messages(size_t refs) 00189 : locale::facet(refs) {} 00190 00191 messages_byname<char>::messages_byname(const char *name, size_t refs) 00192 : messages<char>(refs), _M_impl(new _STLP_PRIV _Messages(false, name)) {} 00193 00194 messages_byname<char>::messages_byname(_Locale_messages* msg) 00195 : messages<char>(0), _M_impl(new _STLP_PRIV _Messages(false, msg)) {} 00196 00197 messages_byname<char>::~messages_byname() 00198 { delete _M_impl; } 00199 00200 messages_byname<char>::catalog 00201 messages_byname<char>::do_open(const string& filename, const locale& l) const 00202 { return _M_impl->do_open(filename, l); } 00203 00204 string 00205 messages_byname<char>::do_get(catalog cat, int set, int p_id, 00206 const string& dfault) const 00207 { return _M_impl->do_get(cat, set, p_id, dfault); } 00208 00209 void messages_byname<char>::do_close(catalog cat) const 00210 { _M_impl->do_close(cat); } 00211 00212 #if !defined (_STLP_NO_WCHAR_T) 00213 00214 //---------------------------------------------------------------------- 00215 // messages<wchar_t> 00216 00217 messages<wchar_t>::messages(size_t refs) 00218 : locale::facet(refs) {} 00219 00220 messages_byname<wchar_t>::messages_byname(const char *name, size_t refs) 00221 : messages<wchar_t>(refs), _M_impl(new _STLP_PRIV _Messages(true, name)) {} 00222 00223 messages_byname<wchar_t>::messages_byname(_Locale_messages* msg) 00224 : messages<wchar_t>(0), _M_impl(new _STLP_PRIV _Messages(true, msg)) {} 00225 00226 messages_byname<wchar_t>::~messages_byname() 00227 { delete _M_impl; } 00228 00229 messages_byname<wchar_t>::catalog 00230 messages_byname<wchar_t>::do_open(const string& filename, const locale& L) const 00231 { return _M_impl->do_open(filename, L); } 00232 00233 wstring 00234 messages_byname<wchar_t>::do_get(catalog thecat, 00235 int set, int p_id, const wstring& dfault) const 00236 { return _M_impl->do_get(thecat, set, p_id, dfault); } 00237 00238 void messages_byname<wchar_t>::do_close(catalog cat) const 00239 { _M_impl->do_close(cat); } 00240 00241 #endif 00242 00243 _STLP_END_NAMESPACE 00244 00245 // Local Variables: 00246 // mode:C++ 00247 // End: Generated on Sun May 27 2012 04:35:13 for ReactOS by
1.7.6.1
|