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 {
24   _LIBCPP_INLINE_VISIBILITY __libcpp_locale_guard(locale_t& __loc) : __old_loc_(uselocale(__loc)) {}
25 
26   _LIBCPP_INLINE_VISIBILITY ~__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) :
40         __status(_configthreadlocale(_ENABLE_PER_THREAD_LOCALE)) {
41       // Setting the locale can be expensive even when the locale given is
42       // already the current locale, so do an explicit check to see if the
43       // current locale is already the one we want.
44       const char* __lc = __setlocale(nullptr);
45       // If every category is the same, the locale string will simply be the
46       // locale name, otherwise it will be a semicolon-separated string listing
47       // each category.  In the second case, we know at least one category won't
48       // be what we want, so we only have to check the first case.
49       if (_VSTD::strcmp(__l.__get_locale(), __lc) != 0) {
50         __locale_all = _strdup(__lc);
51         if (__locale_all == nullptr)
52           __throw_bad_alloc();
53         __setlocale(__l.__get_locale());
54       }
55     }
56     ~__libcpp_locale_guard() {
57       // The CRT documentation doesn't explicitly say, but setlocale() does the
58       // right thing when given a semicolon-separated list of locale settings
59       // for the different categories in the same format as returned by
60       // setlocale(LC_ALL, nullptr).
61       if (__locale_all != nullptr) {
62         __setlocale(__locale_all);
63         free(__locale_all);
64       }
65       _configthreadlocale(__status);
66     }
67     static const char* __setlocale(const char* __locale) {
68       const char* __new_locale = setlocale(LC_ALL, __locale);
69       if (__new_locale == nullptr)
70         __throw_bad_alloc();
71       return __new_locale;
72     }
73     int __status;
74     char* __locale_all = nullptr;
75 };
76 #endif
77 
78 _LIBCPP_END_NAMESPACE_STD
79 
80 #endif // _LIBCPP___LOCALE_LOCALE_BASE_API_LOCALE_GUARD_H
81