Warning: This file is not a C or C++ file. It does not have highlighting.
| 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___CXX03___LOCALE_LOCALE_BASE_API_LOCALE_GUARD_H |
| 10 | #define _LIBCPP___CXX03___LOCALE_LOCALE_BASE_API_LOCALE_GUARD_H |
| 11 | |
| 12 | #include <__cxx03/__config> |
| 13 | #include <__cxx03/__locale> // for locale_t |
| 14 | #include <__cxx03/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 { |
| 24 | _LIBCPP_HIDE_FROM_ABI __libcpp_locale_guard(locale_t& __loc) : __old_loc_(uselocale(__loc)) {} |
| 25 | |
| 26 | _LIBCPP_HIDE_FROM_ABI ~__libcpp_locale_guard() { |
| 27 | if (__old_loc_) |
| 28 | uselocale(__old_loc_); |
| 29 | } |
| 30 | |
| 31 | locale_t __old_loc_; |
| 32 | |
| 33 | __libcpp_locale_guard(__libcpp_locale_guard const&) = delete; |
| 34 | __libcpp_locale_guard& operator=(__libcpp_locale_guard const&) = delete; |
| 35 | }; |
| 36 | #elif defined(_LIBCPP_MSVCRT_LIKE) |
| 37 | struct __libcpp_locale_guard { |
| 38 | __libcpp_locale_guard(locale_t __l) : __status(_configthreadlocale(_ENABLE_PER_THREAD_LOCALE)) { |
| 39 | // Setting the locale can be expensive even when the locale given is |
| 40 | // already the current locale, so do an explicit check to see if the |
| 41 | // current locale is already the one we want. |
| 42 | const char* __lc = __setlocale(nullptr); |
| 43 | // If every category is the same, the locale string will simply be the |
| 44 | // locale name, otherwise it will be a semicolon-separated string listing |
| 45 | // each category. In the second case, we know at least one category won't |
| 46 | // be what we want, so we only have to check the first case. |
| 47 | if (std::strcmp(__l.__get_locale(), __lc) != 0) { |
| 48 | __locale_all = _strdup(__lc); |
| 49 | if (__locale_all == nullptr) |
| 50 | __throw_bad_alloc(); |
| 51 | __setlocale(__l.__get_locale()); |
| 52 | } |
| 53 | } |
| 54 | ~__libcpp_locale_guard() { |
| 55 | // The CRT documentation doesn't explicitly say, but setlocale() does the |
| 56 | // right thing when given a semicolon-separated list of locale settings |
| 57 | // for the different categories in the same format as returned by |
| 58 | // setlocale(LC_ALL, nullptr). |
| 59 | if (__locale_all != nullptr) { |
| 60 | __setlocale(__locale_all); |
| 61 | free(__locale_all); |
| 62 | } |
| 63 | _configthreadlocale(__status); |
| 64 | } |
| 65 | static const char* __setlocale(const char* __locale) { |
| 66 | const char* __new_locale = setlocale(LC_ALL, __locale); |
| 67 | if (__new_locale == nullptr) |
| 68 | __throw_bad_alloc(); |
| 69 | return __new_locale; |
| 70 | } |
| 71 | int __status; |
| 72 | char* __locale_all = nullptr; |
| 73 | }; |
| 74 | #endif |
| 75 | |
| 76 | _LIBCPP_END_NAMESPACE_STD |
| 77 | |
| 78 | #endif // _LIBCPP___CXX03___LOCALE_LOCALE_BASE_API_LOCALE_GUARD_H |
| 79 |
Warning: This file is not a C or C++ file. It does not have highlighting.
