|
| 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_DIR_SUPPORT_FUCHSIA_H |
| 10 | +#define _LIBCPP___LOCALE_DIR_SUPPORT_FUCHSIA_H |
| 11 | + |
| 12 | +#include <__config> |
| 13 | +#include <__utility/forward.h> |
| 14 | +#include <clocale> // uselocale & friends |
| 15 | +#include <cstdio> |
| 16 | +#include <cstdlib> |
| 17 | +#include <cwchar> |
| 18 | + |
| 19 | +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 20 | +# pragma GCC system_header |
| 21 | +#endif |
| 22 | + |
| 23 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 24 | +namespace __locale { |
| 25 | + |
| 26 | +struct __locale_guard { |
| 27 | + _LIBCPP_HIDE_FROM_ABI __locale_guard(locale_t& __loc) : __old_loc_(::uselocale(__loc)) {} |
| 28 | + |
| 29 | + _LIBCPP_HIDE_FROM_ABI ~__locale_guard() { |
| 30 | + if (__old_loc_) |
| 31 | + ::uselocale(__old_loc_); |
| 32 | + } |
| 33 | + |
| 34 | + locale_t __old_loc_; |
| 35 | + |
| 36 | + __locale_guard(__locale_guard const&) = delete; |
| 37 | + __locale_guard& operator=(__locale_guard const&) = delete; |
| 38 | +}; |
| 39 | + |
| 40 | +// |
| 41 | +// Locale management |
| 42 | +// |
| 43 | +using __locale_t = locale_t; |
| 44 | + |
| 45 | +inline _LIBCPP_HIDE_FROM_ABI __locale_t __newlocale(int __category_mask, const char* __name, __locale_t __loc) { |
| 46 | + return ::newlocale(__category_mask, __name, __loc); |
| 47 | +} |
| 48 | + |
| 49 | +inline _LIBCPP_HIDE_FROM_ABI void __freelocale(__locale_t __loc) { ::freelocale(__loc); } |
| 50 | + |
| 51 | +inline _LIBCPP_HIDE_FROM_ABI lconv* __localeconv(__locale_t& __loc) { |
| 52 | + __locale_guard __current(__loc); |
| 53 | + return std::localeconv(); |
| 54 | +} |
| 55 | + |
| 56 | +// |
| 57 | +// Other functions |
| 58 | +// |
| 59 | +inline _LIBCPP_HIDE_FROM_ABI decltype(MB_CUR_MAX) __mb_len_max(__locale_t __loc) { |
| 60 | + __locale_guard __current(__loc); |
| 61 | + return MB_CUR_MAX; |
| 62 | +} |
| 63 | +#if _LIBCPP_HAS_WIDE_CHARACTERS |
| 64 | +inline _LIBCPP_HIDE_FROM_ABI wint_t __btowc(int __ch, __locale_t __loc) { |
| 65 | + __locale_guard __current(__loc); |
| 66 | + return std::btowc(__ch); |
| 67 | +} |
| 68 | +inline _LIBCPP_HIDE_FROM_ABI int __wctob(wint_t __ch, __locale_t __loc) { |
| 69 | + __locale_guard __current(__loc); |
| 70 | + return std::wctob(__ch); |
| 71 | +} |
| 72 | +inline _LIBCPP_HIDE_FROM_ABI size_t |
| 73 | +__wcsnrtombs(char* __dest, const wchar_t** __src, size_t __nwc, size_t __len, mbstate_t* __ps, __locale_t __loc) { |
| 74 | + __locale_guard __current(__loc); |
| 75 | + return ::wcsnrtombs(__dest, __src, __nwc, __len, __ps); // non-standard |
| 76 | +} |
| 77 | +inline _LIBCPP_HIDE_FROM_ABI size_t __wcrtomb(char* __s, wchar_t __ch, mbstate_t* __ps, __locale_t __loc) { |
| 78 | + __locale_guard __current(__loc); |
| 79 | + return std::wcrtomb(__s, __ch, __ps); |
| 80 | +} |
| 81 | +inline _LIBCPP_HIDE_FROM_ABI size_t |
| 82 | +__mbsnrtowcs(wchar_t* __dest, const char** __src, size_t __nms, size_t __len, mbstate_t* __ps, __locale_t __loc) { |
| 83 | + __locale_guard __current(__loc); |
| 84 | + return ::mbsnrtowcs(__dest, __src, __nms, __len, __ps); // non-standard |
| 85 | +} |
| 86 | +inline _LIBCPP_HIDE_FROM_ABI size_t |
| 87 | +__mbrtowc(wchar_t* __pwc, const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) { |
| 88 | + __locale_guard __current(__loc); |
| 89 | + return std::mbrtowc(__pwc, __s, __n, __ps); |
| 90 | +} |
| 91 | +inline _LIBCPP_HIDE_FROM_ABI int __mbtowc(wchar_t* __pwc, const char* __pmb, size_t __max, __locale_t __loc) { |
| 92 | + __locale_guard __current(__loc); |
| 93 | + return std::mbtowc(__pwc, __pmb, __max); |
| 94 | +} |
| 95 | +inline _LIBCPP_HIDE_FROM_ABI size_t __mbrlen(const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) { |
| 96 | + __locale_guard __current(__loc); |
| 97 | + return std::mbrlen(__s, __n, __ps); |
| 98 | +} |
| 99 | +inline _LIBCPP_HIDE_FROM_ABI size_t |
| 100 | +__mbsrtowcs(wchar_t* __dest, const char** __src, size_t __len, mbstate_t* __ps, __locale_t __loc) { |
| 101 | + __locale_guard __current(__loc); |
| 102 | + return ::mbsrtowcs(__dest, __src, __len, __ps); |
| 103 | +} |
| 104 | +#endif |
| 105 | + |
| 106 | +_LIBCPP_DIAGNOSTIC_PUSH |
| 107 | +_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wgcc-compat") |
| 108 | +_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wformat-nonliteral") // GCC doesn't support [[gnu::format]] on variadic templates |
| 109 | +#ifdef _LIBCPP_COMPILER_CLANG_BASED |
| 110 | +# define _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(...) _LIBCPP_ATTRIBUTE_FORMAT(__VA_ARGS__) |
| 111 | +#else |
| 112 | +# define _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(...) /* nothing */ |
| 113 | +#endif |
| 114 | + |
| 115 | +template <class... _Args> |
| 116 | +_LIBCPP_HIDE_FROM_ABI _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(__printf__, 4, 5) int __snprintf( |
| 117 | + char* __s, size_t __n, __locale_t __loc, const char* __format, _Args&&... __args) { |
| 118 | + __locale_guard __current(__loc); |
| 119 | + return std::snprintf(__s, __n, __format, std::forward<_Args>(__args)...); |
| 120 | +} |
| 121 | +template <class... _Args> |
| 122 | +_LIBCPP_HIDE_FROM_ABI _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(__printf__, 3, 4) int __asprintf( |
| 123 | + char** __s, __locale_t __loc, const char* __format, _Args&&... __args) { |
| 124 | + __locale_guard __current(__loc); |
| 125 | + return ::asprintf(__s, __format, std::forward<_Args>(__args)...); // non-standard |
| 126 | +} |
| 127 | +template <class... _Args> |
| 128 | +_LIBCPP_HIDE_FROM_ABI _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(__scanf__, 3, 4) int __sscanf( |
| 129 | + const char* __s, __locale_t __loc, const char* __format, _Args&&... __args) { |
| 130 | + __locale_guard __current(__loc); |
| 131 | + return std::sscanf(__s, __format, std::forward<_Args>(__args)...); |
| 132 | +} |
| 133 | + |
| 134 | +_LIBCPP_DIAGNOSTIC_POP |
| 135 | +#undef _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT |
| 136 | + |
| 137 | +} // namespace __locale |
| 138 | +_LIBCPP_END_NAMESPACE_STD |
| 139 | + |
| 140 | +#include <__locale_dir/support/no_locale/characters.h> |
| 141 | +#include <__locale_dir/support/no_locale/strtonum.h> |
| 142 | + |
| 143 | +#endif // _LIBCPP___LOCALE_DIR_SUPPORT_FUCHSIA_H |
0 commit comments