1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef _LIBCPP___LOCALE_LOCALE_BASE_API_LOCALE_GUARD_H 10 #define _LIBCPP___LOCALE_LOCALE_BASE_API_LOCALE_GUARD_H 11 12 #include <__config> 13 #include <__locale> // for locale_t 14 #include <clocale> 15 16 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 17 # pragma GCC system_header 18 #endif 19 20 _LIBCPP_BEGIN_NAMESPACE_STD 21 22 #if !defined(_LIBCPP_LOCALE__L_EXTENSIONS) 23 struct __libcpp_locale_guard { __libcpp_locale_guard__libcpp_locale_guard24 _LIBCPP_HIDE_FROM_ABI __libcpp_locale_guard(locale_t& __loc) : __old_loc_(uselocale(__loc)) {} 25 ~__libcpp_locale_guard__libcpp_locale_guard26 _LIBCPP_HIDE_FROM_ABI ~__libcpp_locale_guard() { 27 if (__old_loc_) 28 uselocale(__old_loc_); 29 } 30 31 locale_t __old_loc_; 32 33 private: 34 __libcpp_locale_guard(__libcpp_locale_guard const&); 35 __libcpp_locale_guard& operator=(__libcpp_locale_guard const&); 36 }; 37 #elif defined(_LIBCPP_MSVCRT_LIKE) 38 struct __libcpp_locale_guard { 39 __libcpp_locale_guard(locale_t __l) : __status(_configthreadlocale(_ENABLE_PER_THREAD_LOCALE)) { 40 // Setting the locale can be expensive even when the locale given is 41 // already the current locale, so do an explicit check to see if the 42 // current locale is already the one we want. 43 const char* __lc = __setlocale(nullptr); 44 // If every category is the same, the locale string will simply be the 45 // locale name, otherwise it will be a semicolon-separated string listing 46 // each category. In the second case, we know at least one category won't 47 // be what we want, so we only have to check the first case. 48 if (std::strcmp(__l.__get_locale(), __lc) != 0) { 49 __locale_all = _strdup(__lc); 50 if (__locale_all == nullptr) 51 __throw_bad_alloc(); 52 __setlocale(__l.__get_locale()); 53 } 54 } 55 ~__libcpp_locale_guard() { 56 // The CRT documentation doesn't explicitly say, but setlocale() does the 57 // right thing when given a semicolon-separated list of locale settings 58 // for the different categories in the same format as returned by 59 // setlocale(LC_ALL, nullptr). 60 if (__locale_all != nullptr) { 61 __setlocale(__locale_all); 62 free(__locale_all); 63 } 64 _configthreadlocale(__status); 65 } 66 static const char* __setlocale(const char* __locale) { 67 const char* __new_locale = setlocale(LC_ALL, __locale); 68 if (__new_locale == nullptr) 69 __throw_bad_alloc(); 70 return __new_locale; 71 } 72 int __status; 73 char* __locale_all = nullptr; 74 }; 75 #endif 76 77 _LIBCPP_END_NAMESPACE_STD 78 79 #endif // _LIBCPP___LOCALE_LOCALE_BASE_API_LOCALE_GUARD_H 80