1 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2 // 2006, 2007, 2008, 2009, 2010 3 // Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the 7 // terms of the GNU General Public License as published by the 8 // Free Software Foundation; either version 3, or (at your option) 9 // any later version. 10 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 16 // Under Section 7 of GPL version 3, you are granted additional 17 // permissions described in the GCC Runtime Library Exception, version 18 // 3.1, as published by the Free Software Foundation. 19 20 // You should have received a copy of the GNU General Public License and 21 // a copy of the GCC Runtime Library Exception along with this program; 22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 // <http://www.gnu.org/licenses/>. 24 25 #include <clocale> 26 #include <cstring> 27 #include <cstdlib> 28 #include <locale> 29 30 namespace std _GLIBCXX_VISIBILITY(default) 31 { 32 _GLIBCXX_BEGIN_NAMESPACE_VERSION 33 34 using namespace __gnu_cxx; 35 36 locale::locale(const char* __s) : _M_impl(0) 37 { 38 if (__s) 39 { 40 _S_initialize(); 41 if (std::strcmp(__s, "C") == 0 || std::strcmp(__s, "POSIX") == 0) 42 (_M_impl = _S_classic)->_M_add_reference(); 43 else if (std::strcmp(__s, "") != 0) 44 _M_impl = new _Impl(__s, 1); 45 else 46 { 47 // Get it from the environment. 48 char* __env = std::getenv("LC_ALL"); 49 // If LC_ALL is set we are done. 50 if (__env && std::strcmp(__env, "") != 0) 51 { 52 if (std::strcmp(__env, "C") == 0 53 || std::strcmp(__env, "POSIX") == 0) 54 (_M_impl = _S_classic)->_M_add_reference(); 55 else 56 _M_impl = new _Impl(__env, 1); 57 } 58 else 59 { 60 // LANG may set a default different from "C". 61 string __lang; 62 __env = std::getenv("LANG"); 63 if (!__env || std::strcmp(__env, "") == 0 64 || std::strcmp(__env, "C") == 0 65 || std::strcmp(__env, "POSIX") == 0) 66 __lang = "C"; 67 else 68 __lang = __env; 69 70 // Scan the categories looking for the first one 71 // different from LANG. 72 size_t __i = 0; 73 if (__lang == "C") 74 for (; __i < _S_categories_size; ++__i) 75 { 76 __env = std::getenv(_S_categories[__i]); 77 if (__env && std::strcmp(__env, "") != 0 78 && std::strcmp(__env, "C") != 0 79 && std::strcmp(__env, "POSIX") != 0) 80 break; 81 } 82 else 83 for (; __i < _S_categories_size; ++__i) 84 { 85 __env = std::getenv(_S_categories[__i]); 86 if (__env && std::strcmp(__env, "") != 0 87 && __lang != __env) 88 break; 89 } 90 91 // If one is found, build the complete string of 92 // the form LC_CTYPE=xxx;LC_NUMERIC=yyy; and so on... 93 if (__i < _S_categories_size) 94 { 95 string __str; 96 __str.reserve(128); 97 for (size_t __j = 0; __j < __i; ++__j) 98 { 99 __str += _S_categories[__j]; 100 __str += '='; 101 __str += __lang; 102 __str += ';'; 103 } 104 __str += _S_categories[__i]; 105 __str += '='; 106 __str += __env; 107 __str += ';'; 108 ++__i; 109 for (; __i < _S_categories_size; ++__i) 110 { 111 __env = std::getenv(_S_categories[__i]); 112 __str += _S_categories[__i]; 113 if (!__env || std::strcmp(__env, "") == 0) 114 { 115 __str += '='; 116 __str += __lang; 117 __str += ';'; 118 } 119 else if (std::strcmp(__env, "C") == 0 120 || std::strcmp(__env, "POSIX") == 0) 121 __str += "=C;"; 122 else 123 { 124 __str += '='; 125 __str += __env; 126 __str += ';'; 127 } 128 } 129 __str.erase(__str.end() - 1); 130 _M_impl = new _Impl(__str.c_str(), 1); 131 } 132 // ... otherwise either an additional instance of 133 // the "C" locale or LANG. 134 else if (__lang == "C") 135 (_M_impl = _S_classic)->_M_add_reference(); 136 else 137 _M_impl = new _Impl(__lang.c_str(), 1); 138 } 139 } 140 } 141 else 142 __throw_runtime_error(__N("locale::locale null not valid")); 143 } 144 145 locale::locale(const locale& __base, const char* __s, category __cat) 146 : _M_impl(0) 147 { 148 // NB: There are complicated, yet more efficient ways to do 149 // this. Building up locales on a per-category way is tedious, so 150 // let's do it this way until people complain. 151 locale __add(__s); 152 _M_coalesce(__base, __add, __cat); 153 } 154 155 locale::locale(const locale& __base, const locale& __add, category __cat) 156 : _M_impl(0) 157 { _M_coalesce(__base, __add, __cat); } 158 159 void 160 locale::_M_coalesce(const locale& __base, const locale& __add, 161 category __cat) 162 { 163 __cat = _S_normalize_category(__cat); 164 _M_impl = new _Impl(*__base._M_impl, 1); 165 166 __try 167 { _M_impl->_M_replace_categories(__add._M_impl, __cat); } 168 __catch(...) 169 { 170 _M_impl->_M_remove_reference(); 171 __throw_exception_again; 172 } 173 } 174 175 // Construct named _Impl. 176 locale::_Impl:: 177 _Impl(const char* __s, size_t __refs) 178 : _M_refcount(__refs), _M_facets(0), _M_facets_size(_GLIBCXX_NUM_FACETS), 179 _M_caches(0), _M_names(0) 180 { 181 // Initialize the underlying locale model, which also checks to 182 // see if the given name is valid. 183 __c_locale __cloc; 184 locale::facet::_S_create_c_locale(__cloc, __s); 185 __c_locale __clocm = __cloc; 186 187 __try 188 { 189 _M_facets = new const facet*[_M_facets_size]; 190 for (size_t __i = 0; __i < _M_facets_size; ++__i) 191 _M_facets[__i] = 0; 192 _M_caches = new const facet*[_M_facets_size]; 193 for (size_t __j = 0; __j < _M_facets_size; ++__j) 194 _M_caches[__j] = 0; 195 _M_names = new char*[_S_categories_size]; 196 for (size_t __k = 0; __k < _S_categories_size; ++__k) 197 _M_names[__k] = 0; 198 199 // Name the categories. 200 const char* __smon = __s; 201 const size_t __len = std::strlen(__s); 202 if (!std::memchr(__s, ';', __len)) 203 { 204 _M_names[0] = new char[__len + 1]; 205 std::memcpy(_M_names[0], __s, __len + 1); 206 } 207 else 208 { 209 const char* __end = __s; 210 bool __found_ctype = false; 211 bool __found_monetary = false; 212 size_t __ci = 0, __mi = 0; 213 for (size_t __i = 0; __i < _S_categories_size; ++__i) 214 { 215 const char* __beg = std::strchr(__end + 1, '=') + 1; 216 __end = std::strchr(__beg, ';'); 217 if (!__end) 218 __end = __s + __len; 219 _M_names[__i] = new char[__end - __beg + 1]; 220 std::memcpy(_M_names[__i], __beg, __end - __beg); 221 _M_names[__i][__end - __beg] = '\0'; 222 if (!__found_ctype 223 && *(__beg - 2) == 'E' && *(__beg - 3) == 'P') 224 { 225 __found_ctype = true; 226 __ci = __i; 227 } 228 else if (!__found_monetary && *(__beg - 2) == 'Y') 229 { 230 __found_monetary = true; 231 __mi = __i; 232 } 233 } 234 235 if (std::strcmp(_M_names[__ci], _M_names[__mi])) 236 { 237 __smon = _M_names[__mi]; 238 __clocm = locale::facet::_S_lc_ctype_c_locale(__cloc, 239 __smon); 240 } 241 } 242 243 // Construct all standard facets and add them to _M_facets. 244 _M_init_facet(new std::ctype<char>(__cloc, 0, false)); 245 _M_init_facet(new codecvt<char, char, mbstate_t>(__cloc)); 246 _M_init_facet(new numpunct<char>(__cloc)); 247 _M_init_facet(new num_get<char>); 248 _M_init_facet(new num_put<char>); 249 _M_init_facet(new std::collate<char>(__cloc)); 250 _M_init_facet(new moneypunct<char, false>(__cloc, 0)); 251 _M_init_facet(new moneypunct<char, true>(__cloc, 0)); 252 _M_init_facet(new money_get<char>); 253 _M_init_facet(new money_put<char>); 254 _M_init_facet(new __timepunct<char>(__cloc, __s)); 255 _M_init_facet(new time_get<char>); 256 _M_init_facet(new time_put<char>); 257 _M_init_facet(new std::messages<char>(__cloc, __s)); 258 259 #ifdef _GLIBCXX_USE_WCHAR_T 260 _M_init_facet(new std::ctype<wchar_t>(__cloc)); 261 _M_init_facet(new codecvt<wchar_t, char, mbstate_t>(__cloc)); 262 _M_init_facet(new numpunct<wchar_t>(__cloc)); 263 _M_init_facet(new num_get<wchar_t>); 264 _M_init_facet(new num_put<wchar_t>); 265 _M_init_facet(new std::collate<wchar_t>(__cloc)); 266 _M_init_facet(new moneypunct<wchar_t, false>(__clocm, __smon)); 267 _M_init_facet(new moneypunct<wchar_t, true>(__clocm, __smon)); 268 _M_init_facet(new money_get<wchar_t>); 269 _M_init_facet(new money_put<wchar_t>); 270 _M_init_facet(new __timepunct<wchar_t>(__cloc, __s)); 271 _M_init_facet(new time_get<wchar_t>); 272 _M_init_facet(new time_put<wchar_t>); 273 _M_init_facet(new std::messages<wchar_t>(__cloc, __s)); 274 #endif 275 locale::facet::_S_destroy_c_locale(__cloc); 276 if (__clocm != __cloc) 277 locale::facet::_S_destroy_c_locale(__clocm); 278 } 279 __catch(...) 280 { 281 locale::facet::_S_destroy_c_locale(__cloc); 282 if (__clocm != __cloc) 283 locale::facet::_S_destroy_c_locale(__clocm); 284 this->~_Impl(); 285 __throw_exception_again; 286 } 287 } 288 289 void 290 locale::_Impl:: 291 _M_replace_categories(const _Impl* __imp, category __cat) 292 { 293 category __mask = 1; 294 if (!_M_names[0] || !__imp->_M_names[0]) 295 { 296 if (_M_names[0]) 297 { 298 delete [] _M_names[0]; 299 _M_names[0] = 0; // Unnamed. 300 } 301 302 for (size_t __ix = 0; __ix < _S_categories_size; ++__ix, __mask <<= 1) 303 { 304 if (__mask & __cat) 305 // Need to replace entry in _M_facets with other locale's info. 306 _M_replace_category(__imp, _S_facet_categories[__ix]); 307 } 308 } 309 else 310 { 311 if (!_M_names[1]) 312 { 313 // A full set of _M_names must be prepared, all identical 314 // to _M_names[0] to begin with. Then, below, a few will 315 // be replaced by the corresponding __imp->_M_names. I.e., 316 // not a "simple" locale anymore (see locale::operator==). 317 const size_t __len = std::strlen(_M_names[0]) + 1; 318 for (size_t __i = 1; __i < _S_categories_size; ++__i) 319 { 320 _M_names[__i] = new char[__len]; 321 std::memcpy(_M_names[__i], _M_names[0], __len); 322 } 323 } 324 325 for (size_t __ix = 0; __ix < _S_categories_size; ++__ix, __mask <<= 1) 326 { 327 if (__mask & __cat) 328 { 329 // Need to replace entry in _M_facets with other locale's info. 330 _M_replace_category(__imp, _S_facet_categories[__ix]); 331 332 // FIXME: Hack for libstdc++/29217: the numerical encodings 333 // of the time and collate categories are swapped vs the 334 // order of the names in locale::_S_categories. We'd like to 335 // adjust the former (the latter is dictated by compatibility 336 // with glibc) but we can't for binary compatibility. 337 size_t __ix_name = __ix; 338 if (__ix == 2 || __ix == 3) 339 __ix_name = 5 - __ix; 340 341 char* __src = __imp->_M_names[__ix_name] ? 342 __imp->_M_names[__ix_name] : __imp->_M_names[0]; 343 const size_t __len = std::strlen(__src) + 1; 344 char* __new = new char[__len]; 345 std::memcpy(__new, __src, __len); 346 delete [] _M_names[__ix_name]; 347 _M_names[__ix_name] = __new; 348 } 349 } 350 } 351 } 352 353 _GLIBCXX_END_NAMESPACE_VERSION 354 } // namespace 355