1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/build/stlport/src/messages.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,247 @@ 1.4 +/* 1.5 + * Copyright (c) 1999 1.6 + * Silicon Graphics Computer Systems, Inc. 1.7 + * 1.8 + * Copyright (c) 1999 1.9 + * Boris Fomitchev 1.10 + * 1.11 + * This material is provided "as is", with absolutely no warranty expressed 1.12 + * or implied. Any use is at your own risk. 1.13 + * 1.14 + * Permission to use or copy this software for any purpose is hereby granted 1.15 + * without fee, provided the above notices are retained on all copies. 1.16 + * Permission to modify the code and to distribute modified code is granted, 1.17 + * provided the above notices are retained, and a notice that the code was 1.18 + * modified is included with the above copyright notice. 1.19 + * 1.20 + */ 1.21 +#include "stlport_prefix.h" 1.22 + 1.23 +#include <typeinfo> 1.24 + 1.25 +#include "message_facets.h" 1.26 +#include "acquire_release.h" 1.27 + 1.28 +_STLP_BEGIN_NAMESPACE 1.29 + 1.30 +_STLP_MOVE_TO_PRIV_NAMESPACE 1.31 + 1.32 +void _Catalog_locale_map::insert(nl_catd_type key, const locale& L) { 1.33 + _STLP_TRY { 1.34 +#if !defined (_STLP_NO_TYPEINFO) && !defined (_STLP_NO_RTTI) 1.35 + // Don't bother to do anything unless we're using a non-default ctype facet 1.36 +# ifdef _STLP_NO_WCHAR_T 1.37 + typedef char _Char; 1.38 +# else 1.39 + typedef wchar_t _Char; 1.40 +# endif 1.41 + 1.42 + typedef ctype<_Char> wctype; 1.43 + wctype const& wct = use_facet<wctype>(L); 1.44 + if (typeid(wct) != typeid(wctype)) { 1.45 +#endif 1.46 + if (!M) 1.47 + M = new map_type; 1.48 + 1.49 + M->insert(map_type::value_type(key, L)); 1.50 +#if !defined (_STLP_NO_TYPEINFO) && !defined (_STLP_NO_RTTI) 1.51 + } 1.52 +#endif 1.53 + } 1.54 + _STLP_CATCH_ALL {} 1.55 +} 1.56 + 1.57 +void _Catalog_locale_map::erase(nl_catd_type key) { 1.58 + if (M) 1.59 + M->erase(key); 1.60 +} 1.61 + 1.62 +locale _Catalog_locale_map::lookup(nl_catd_type key) const { 1.63 + if (M) { 1.64 + map_type::const_iterator i = M->find(key); 1.65 + return i != M->end() ? (*i).second : locale::classic(); 1.66 + } 1.67 + else 1.68 + return locale::classic(); 1.69 +} 1.70 + 1.71 + 1.72 +#if defined (_STLP_USE_NL_CATD_MAPPING) 1.73 +_STLP_VOLATILE __stl_atomic_t _Catalog_nl_catd_map::_count = 0; 1.74 + 1.75 +messages_base::catalog _Catalog_nl_catd_map::insert(nl_catd_type cat) { 1.76 + messages_base::catalog &res = Mr[cat]; 1.77 + if ( res == 0 ) { 1.78 +#if defined (_STLP_ATOMIC_INCREMENT) 1.79 + res = __STATIC_CAST(int, _STLP_ATOMIC_INCREMENT(&_count)); 1.80 +#else 1.81 + static _STLP_STATIC_MUTEX _Count_lock _STLP_MUTEX_INITIALIZER; 1.82 + { 1.83 + _STLP_auto_lock sentry(_Count_lock); 1.84 + res = __STATIC_CAST(int, ++_count); 1.85 + } 1.86 +#endif 1.87 + M[res] = cat; 1.88 + } 1.89 + return res; 1.90 +} 1.91 + 1.92 +void _Catalog_nl_catd_map::erase(messages_base::catalog cat) { 1.93 + map_type::iterator mit(M.find(cat)); 1.94 + if (mit != M.end()) { 1.95 + Mr.erase((*mit).second); 1.96 + M.erase(mit); 1.97 + } 1.98 +} 1.99 +#endif 1.100 + 1.101 +//---------------------------------------------------------------------- 1.102 +// 1.103 +_Messages::_Messages(bool is_wide, const char *name) : 1.104 + _M_message_obj(0), _M_map(0) { 1.105 + if (!name) 1.106 + locale::_M_throw_on_null_name(); 1.107 + 1.108 + int __err_code; 1.109 + char buf[_Locale_MAX_SIMPLE_NAME]; 1.110 + _M_message_obj = _STLP_PRIV __acquire_messages(name, buf, 0, &__err_code); 1.111 + if (!_M_message_obj) 1.112 + locale::_M_throw_on_creation_failure(__err_code, name, "messages"); 1.113 + 1.114 + if (is_wide) 1.115 + _M_map = new _Catalog_locale_map; 1.116 +} 1.117 + 1.118 +_Messages::_Messages(bool is_wide, _Locale_messages* msg) : 1.119 + _M_message_obj(msg), _M_map(is_wide ? new _Catalog_locale_map() : 0) 1.120 +{} 1.121 + 1.122 +_Messages::~_Messages() { 1.123 + __release_messages(_M_message_obj); 1.124 + delete _M_map; 1.125 +} 1.126 + 1.127 +_Messages::catalog _Messages::do_open(const string& filename, const locale& L) const { 1.128 + nl_catd_type result = _M_message_obj ? _Locale_catopen(_M_message_obj, filename.c_str()) 1.129 + : (nl_catd_type)(-1); 1.130 + 1.131 + if ( result != (nl_catd_type)(-1) ) { 1.132 + if ( _M_map != 0 ) { 1.133 + _M_map->insert(result, L); 1.134 + } 1.135 + return _STLP_MUTABLE(_Messages_impl, _M_cat).insert( result ); 1.136 + } 1.137 + 1.138 + return -1; 1.139 +} 1.140 + 1.141 +string _Messages::do_get(catalog cat, 1.142 + int set, int p_id, const string& dfault) const { 1.143 + return _M_message_obj != 0 && cat >= 0 1.144 + ? string(_Locale_catgets(_M_message_obj, _STLP_MUTABLE(_Messages_impl, _M_cat)[cat], 1.145 + set, p_id, dfault.c_str())) 1.146 + : dfault; 1.147 +} 1.148 + 1.149 +#if !defined (_STLP_NO_WCHAR_T) 1.150 + 1.151 +wstring 1.152 +_Messages::do_get(catalog thecat, 1.153 + int set, int p_id, const wstring& dfault) const { 1.154 + typedef ctype<wchar_t> wctype; 1.155 + const wctype& ct = use_facet<wctype>(_M_map->lookup(_STLP_MUTABLE(_Messages_impl, _M_cat)[thecat])); 1.156 + 1.157 + const char* str = _Locale_catgets(_M_message_obj, _STLP_MUTABLE(_Messages_impl, _M_cat)[thecat], set, p_id, ""); 1.158 + 1.159 + // Verify that the lookup failed; an empty string might represent success. 1.160 + if (!str) 1.161 + return dfault; 1.162 + else if (str[0] == '\0') { 1.163 + const char* str2 = _Locale_catgets(_M_message_obj, _STLP_MUTABLE(_Messages_impl, _M_cat)[thecat], set, p_id, "*"); 1.164 + if (!str2 || ((str2[0] == '*') && (str2[1] == '\0'))) 1.165 + return dfault; 1.166 + } 1.167 + 1.168 + // str is correct. Now we must widen it to get a wstring. 1.169 + size_t n = strlen(str); 1.170 + 1.171 + // NOT PORTABLE. What we're doing relies on internal details of the 1.172 + // string implementation. (Contiguity of string elements.) 1.173 + wstring result(n, wchar_t(0)); 1.174 + ct.widen(str, str + n, &*result.begin()); 1.175 + return result; 1.176 +} 1.177 + 1.178 +#endif 1.179 + 1.180 +void _Messages::do_close(catalog thecat) const { 1.181 + if (_M_message_obj) 1.182 + _Locale_catclose(_M_message_obj, _STLP_MUTABLE(_Messages_impl, _M_cat)[thecat]); 1.183 + if (_M_map) _M_map->erase(_STLP_MUTABLE(_Messages_impl, _M_cat)[thecat]); 1.184 + _STLP_MUTABLE(_Messages_impl, _M_cat).erase( thecat ); 1.185 +} 1.186 + 1.187 +_STLP_MOVE_TO_STD_NAMESPACE 1.188 + 1.189 +//---------------------------------------------------------------------- 1.190 +// messages<char> 1.191 +messages<char>::messages(size_t refs) 1.192 + : locale::facet(refs) {} 1.193 + 1.194 +messages_byname<char>::messages_byname(const char *name, size_t refs) 1.195 + : messages<char>(refs), _M_impl(new _STLP_PRIV _Messages(false, name)) {} 1.196 + 1.197 +messages_byname<char>::messages_byname(_Locale_messages* msg) 1.198 + : messages<char>(0), _M_impl(new _STLP_PRIV _Messages(false, msg)) {} 1.199 + 1.200 +messages_byname<char>::~messages_byname() 1.201 +{ delete _M_impl; } 1.202 + 1.203 +messages_byname<char>::catalog 1.204 +messages_byname<char>::do_open(const string& filename, const locale& l) const 1.205 +{ return _M_impl->do_open(filename, l); } 1.206 + 1.207 +string 1.208 +messages_byname<char>::do_get(catalog cat, int set, int p_id, 1.209 + const string& dfault) const 1.210 +{ return _M_impl->do_get(cat, set, p_id, dfault); } 1.211 + 1.212 +void messages_byname<char>::do_close(catalog cat) const 1.213 +{ _M_impl->do_close(cat); } 1.214 + 1.215 +#if !defined (_STLP_NO_WCHAR_T) 1.216 + 1.217 +//---------------------------------------------------------------------- 1.218 +// messages<wchar_t> 1.219 + 1.220 +messages<wchar_t>::messages(size_t refs) 1.221 + : locale::facet(refs) {} 1.222 + 1.223 +messages_byname<wchar_t>::messages_byname(const char *name, size_t refs) 1.224 + : messages<wchar_t>(refs), _M_impl(new _STLP_PRIV _Messages(true, name)) {} 1.225 + 1.226 +messages_byname<wchar_t>::messages_byname(_Locale_messages* msg) 1.227 + : messages<wchar_t>(0), _M_impl(new _STLP_PRIV _Messages(true, msg)) {} 1.228 + 1.229 +messages_byname<wchar_t>::~messages_byname() 1.230 +{ delete _M_impl; } 1.231 + 1.232 +messages_byname<wchar_t>::catalog 1.233 +messages_byname<wchar_t>::do_open(const string& filename, const locale& L) const 1.234 +{ return _M_impl->do_open(filename, L); } 1.235 + 1.236 +wstring 1.237 +messages_byname<wchar_t>::do_get(catalog thecat, 1.238 + int set, int p_id, const wstring& dfault) const 1.239 +{ return _M_impl->do_get(thecat, set, p_id, dfault); } 1.240 + 1.241 +void messages_byname<wchar_t>::do_close(catalog cat) const 1.242 +{ _M_impl->do_close(cat); } 1.243 + 1.244 +#endif 1.245 + 1.246 +_STLP_END_NAMESPACE 1.247 + 1.248 +// Local Variables: 1.249 +// mode:C++ 1.250 +// End: