|
| 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_LINUX_H |
| 10 | +#define _LIBCPP___LOCALE_DIR_SUPPORT_LINUX_H |
| 11 | + |
| 12 | +#include <__config> |
| 13 | +#include <__cstddef/size_t.h> |
| 14 | +#include <__std_mbstate_t.h> |
| 15 | +#include <__utility/forward.h> |
| 16 | +#include <clocale> // std::lconv |
| 17 | +#include <cstdio> |
| 18 | +#include <cstdlib> |
| 19 | +#include <ctype.h> |
| 20 | +#include <stdarg.h> |
| 21 | +#include <string.h> |
| 22 | +#include <time.h> |
| 23 | +#if _LIBCPP_HAS_WIDE_CHARACTERS |
| 24 | +# include <cwchar> |
| 25 | +# include <wctype.h> |
| 26 | +#endif |
| 27 | + |
| 28 | +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 29 | +# pragma GCC system_header |
| 30 | +#endif |
| 31 | + |
| 32 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 33 | +namespace __locale { |
| 34 | + |
| 35 | +struct __locale_guard { |
| 36 | + _LIBCPP_HIDE_FROM_ABI __locale_guard(locale_t& __loc) : __old_loc_(::uselocale(__loc)) {} |
| 37 | + |
| 38 | + _LIBCPP_HIDE_FROM_ABI ~__locale_guard() { |
| 39 | + if (__old_loc_) |
| 40 | + ::uselocale(__old_loc_); |
| 41 | + } |
| 42 | + |
| 43 | + locale_t __old_loc_; |
| 44 | + |
| 45 | + __locale_guard(__locale_guard const&) = delete; |
| 46 | + __locale_guard& operator=(__locale_guard const&) = delete; |
| 47 | +}; |
| 48 | + |
| 49 | +// |
| 50 | +// Locale management |
| 51 | +// |
| 52 | +#define _LIBCPP_COLLATE_MASK LC_COLLATE_MASK |
| 53 | +#define _LIBCPP_CTYPE_MASK LC_CTYPE_MASK |
| 54 | +#define _LIBCPP_MONETARY_MASK LC_MONETARY_MASK |
| 55 | +#define _LIBCPP_NUMERIC_MASK LC_NUMERIC_MASK |
| 56 | +#define _LIBCPP_TIME_MASK LC_TIME_MASK |
| 57 | +#define _LIBCPP_MESSAGES_MASK LC_MESSAGES_MASK |
| 58 | +#define _LIBCPP_ALL_MASK LC_ALL_MASK |
| 59 | +#define _LIBCPP_LC_ALL LC_ALL |
| 60 | + |
| 61 | +using __locale_t _LIBCPP_NODEBUG = ::locale_t; |
| 62 | + |
| 63 | +#if defined(_LIBCPP_BUILDING_LIBRARY) |
| 64 | +using __lconv_t _LIBCPP_NODEBUG = std::lconv; |
| 65 | + |
| 66 | +inline _LIBCPP_HIDE_FROM_ABI __locale_t __newlocale(int __category_mask, const char* __locale, __locale_t __base) { |
| 67 | + return ::newlocale(__category_mask, __locale, __base); |
| 68 | +} |
| 69 | + |
| 70 | +inline _LIBCPP_HIDE_FROM_ABI void __freelocale(__locale_t __loc) { ::freelocale(__loc); } |
| 71 | + |
| 72 | +inline _LIBCPP_HIDE_FROM_ABI char* __setlocale(int __category, char const* __locale) { |
| 73 | + return ::setlocale(__category, __locale); |
| 74 | +} |
| 75 | + |
| 76 | +inline _LIBCPP_HIDE_FROM_ABI __lconv_t* __localeconv(__locale_t& __loc) { |
| 77 | + __locale_guard __current(__loc); |
| 78 | + return std::localeconv(); |
| 79 | +} |
| 80 | +#endif // _LIBCPP_BUILDING_LIBRARY |
| 81 | + |
| 82 | +// |
| 83 | +// Strtonum functions |
| 84 | +// |
| 85 | +inline _LIBCPP_HIDE_FROM_ABI float __strtof(const char* __nptr, char** __endptr, __locale_t __loc) { |
| 86 | + return ::strtof_l(__nptr, __endptr, __loc); |
| 87 | +} |
| 88 | + |
| 89 | +inline _LIBCPP_HIDE_FROM_ABI double __strtod(const char* __nptr, char** __endptr, __locale_t __loc) { |
| 90 | + return ::strtod_l(__nptr, __endptr, __loc); |
| 91 | +} |
| 92 | + |
| 93 | +inline _LIBCPP_HIDE_FROM_ABI long double __strtold(const char* __nptr, char** __endptr, __locale_t __loc) { |
| 94 | + return ::strtold_l(__nptr, __endptr, __loc); |
| 95 | +} |
| 96 | + |
| 97 | +inline _LIBCPP_HIDE_FROM_ABI long long __strtoll(const char* __nptr, char** __endptr, int __base, __locale_t __loc) { |
| 98 | + return ::strtoll_l(__nptr, __endptr, __base, __loc); |
| 99 | +} |
| 100 | + |
| 101 | +inline _LIBCPP_HIDE_FROM_ABI unsigned long long |
| 102 | +__strtoull(const char* __nptr, char** __endptr, int __base, __locale_t __loc) { |
| 103 | + return ::strtoull_l(__nptr, __endptr, __base, __loc); |
| 104 | +} |
| 105 | + |
| 106 | +// |
| 107 | +// Character manipulation functions |
| 108 | +// |
| 109 | +#if defined(_LIBCPP_BUILDING_LIBRARY) |
| 110 | +inline _LIBCPP_HIDE_FROM_ABI int __islower(int __c, __locale_t __loc) { return islower_l(__c, __loc); } |
| 111 | + |
| 112 | +inline _LIBCPP_HIDE_FROM_ABI int __isupper(int __c, __locale_t __loc) { return isupper_l(__c, __loc); } |
| 113 | +#endif |
| 114 | + |
| 115 | +inline _LIBCPP_HIDE_FROM_ABI int __isdigit(int __c, __locale_t __loc) { return isdigit_l(__c, __loc); } |
| 116 | + |
| 117 | +inline _LIBCPP_HIDE_FROM_ABI int __isxdigit(int __c, __locale_t __loc) { return isxdigit_l(__c, __loc); } |
| 118 | + |
| 119 | +#if defined(_LIBCPP_BUILDING_LIBRARY) |
| 120 | +inline _LIBCPP_HIDE_FROM_ABI int __toupper(int __c, __locale_t __loc) { return toupper_l(__c, __loc); } |
| 121 | + |
| 122 | +inline _LIBCPP_HIDE_FROM_ABI int __tolower(int __c, __locale_t __loc) { return tolower_l(__c, __loc); } |
| 123 | + |
| 124 | +inline _LIBCPP_HIDE_FROM_ABI int __strcoll(const char* __s1, const char* __s2, __locale_t __loc) { |
| 125 | + return strcoll_l(__s1, __s2, __loc); |
| 126 | +} |
| 127 | + |
| 128 | +inline _LIBCPP_HIDE_FROM_ABI size_t __strxfrm(char* __dest, const char* __src, size_t __n, __locale_t __loc) { |
| 129 | + return strxfrm_l(__dest, __src, __n, __loc); |
| 130 | +} |
| 131 | + |
| 132 | +# if _LIBCPP_HAS_WIDE_CHARACTERS |
| 133 | +inline _LIBCPP_HIDE_FROM_ABI int __iswctype(wint_t __c, wctype_t __type, __locale_t __loc) { |
| 134 | + return iswctype_l(__c, __type, __loc); |
| 135 | +} |
| 136 | + |
| 137 | +inline _LIBCPP_HIDE_FROM_ABI int __iswspace(wint_t __c, __locale_t __loc) { return iswspace_l(__c, __loc); } |
| 138 | + |
| 139 | +inline _LIBCPP_HIDE_FROM_ABI int __iswprint(wint_t __c, __locale_t __loc) { return iswprint_l(__c, __loc); } |
| 140 | + |
| 141 | +inline _LIBCPP_HIDE_FROM_ABI int __iswcntrl(wint_t __c, __locale_t __loc) { return iswcntrl_l(__c, __loc); } |
| 142 | + |
| 143 | +inline _LIBCPP_HIDE_FROM_ABI int __iswupper(wint_t __c, __locale_t __loc) { return iswupper_l(__c, __loc); } |
| 144 | + |
| 145 | +inline _LIBCPP_HIDE_FROM_ABI int __iswlower(wint_t __c, __locale_t __loc) { return iswlower_l(__c, __loc); } |
| 146 | + |
| 147 | +inline _LIBCPP_HIDE_FROM_ABI int __iswalpha(wint_t __c, __locale_t __loc) { return iswalpha_l(__c, __loc); } |
| 148 | + |
| 149 | +inline _LIBCPP_HIDE_FROM_ABI int __iswblank(wint_t __c, __locale_t __loc) { return iswblank_l(__c, __loc); } |
| 150 | + |
| 151 | +inline _LIBCPP_HIDE_FROM_ABI int __iswdigit(wint_t __c, __locale_t __loc) { return iswdigit_l(__c, __loc); } |
| 152 | + |
| 153 | +inline _LIBCPP_HIDE_FROM_ABI int __iswpunct(wint_t __c, __locale_t __loc) { return iswpunct_l(__c, __loc); } |
| 154 | + |
| 155 | +inline _LIBCPP_HIDE_FROM_ABI int __iswxdigit(wint_t __c, __locale_t __loc) { return iswxdigit_l(__c, __loc); } |
| 156 | + |
| 157 | +inline _LIBCPP_HIDE_FROM_ABI wint_t __towupper(wint_t __c, __locale_t __loc) { return towupper_l(__c, __loc); } |
| 158 | + |
| 159 | +inline _LIBCPP_HIDE_FROM_ABI wint_t __towlower(wint_t __c, __locale_t __loc) { return towlower_l(__c, __loc); } |
| 160 | + |
| 161 | +inline _LIBCPP_HIDE_FROM_ABI int __wcscoll(const wchar_t* __ws1, const wchar_t* __ws2, __locale_t __loc) { |
| 162 | + return wcscoll_l(__ws1, __ws2, __loc); |
| 163 | +} |
| 164 | + |
| 165 | +inline _LIBCPP_HIDE_FROM_ABI size_t __wcsxfrm(wchar_t* __dest, const wchar_t* __src, size_t __n, __locale_t __loc) { |
| 166 | + return wcsxfrm_l(__dest, __src, __n, __loc); |
| 167 | +} |
| 168 | +# endif // _LIBCPP_HAS_WIDE_CHARACTERS |
| 169 | + |
| 170 | +inline _LIBCPP_HIDE_FROM_ABI size_t |
| 171 | +__strftime(char* __s, size_t __max, const char* __format, const struct tm* __tm, __locale_t __loc) { |
| 172 | + return strftime_l(__s, __max, __format, __tm, __loc); |
| 173 | +} |
| 174 | + |
| 175 | +// |
| 176 | +// Other functions |
| 177 | +// |
| 178 | +inline _LIBCPP_HIDE_FROM_ABI decltype(MB_CUR_MAX) __mb_len_max(__locale_t __loc) { |
| 179 | + __locale_guard __current(__loc); |
| 180 | + return MB_CUR_MAX; |
| 181 | +} |
| 182 | + |
| 183 | +# if _LIBCPP_HAS_WIDE_CHARACTERS |
| 184 | +inline _LIBCPP_HIDE_FROM_ABI wint_t __btowc(int __c, __locale_t __loc) { |
| 185 | + __locale_guard __current(__loc); |
| 186 | + return std::btowc(__c); |
| 187 | +} |
| 188 | + |
| 189 | +inline _LIBCPP_HIDE_FROM_ABI int __wctob(wint_t __c, __locale_t __loc) { |
| 190 | + __locale_guard __current(__loc); |
| 191 | + return std::wctob(__c); |
| 192 | +} |
| 193 | + |
| 194 | +inline _LIBCPP_HIDE_FROM_ABI size_t |
| 195 | +__wcsnrtombs(char* __dest, const wchar_t** __src, size_t __nwc, size_t __len, mbstate_t* __ps, __locale_t __loc) { |
| 196 | + __locale_guard __current(__loc); |
| 197 | + return ::wcsnrtombs(__dest, __src, __nwc, __len, __ps); // non-standard |
| 198 | +} |
| 199 | + |
| 200 | +inline _LIBCPP_HIDE_FROM_ABI size_t __wcrtomb(char* __s, wchar_t __wc, mbstate_t* __ps, __locale_t __loc) { |
| 201 | + __locale_guard __current(__loc); |
| 202 | + return std::wcrtomb(__s, __wc, __ps); |
| 203 | +} |
| 204 | + |
| 205 | +inline _LIBCPP_HIDE_FROM_ABI size_t |
| 206 | +__mbsnrtowcs(wchar_t* __dest, const char** __src, size_t __nms, size_t __len, mbstate_t* __ps, __locale_t __loc) { |
| 207 | + __locale_guard __current(__loc); |
| 208 | + return ::mbsnrtowcs(__dest, __src, __nms, __len, __ps); // non-standard |
| 209 | +} |
| 210 | + |
| 211 | +inline _LIBCPP_HIDE_FROM_ABI size_t |
| 212 | +__mbrtowc(wchar_t* __pwc, const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) { |
| 213 | + __locale_guard __current(__loc); |
| 214 | + return std::mbrtowc(__pwc, __s, __n, __ps); |
| 215 | +} |
| 216 | + |
| 217 | +inline _LIBCPP_HIDE_FROM_ABI int __mbtowc(wchar_t* __pwc, const char* __pmb, size_t __max, __locale_t __loc) { |
| 218 | + __locale_guard __current(__loc); |
| 219 | + return std::mbtowc(__pwc, __pmb, __max); |
| 220 | +} |
| 221 | + |
| 222 | +inline _LIBCPP_HIDE_FROM_ABI size_t __mbrlen(const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) { |
| 223 | + __locale_guard __current(__loc); |
| 224 | + return std::mbrlen(__s, __n, __ps); |
| 225 | +} |
| 226 | + |
| 227 | +inline _LIBCPP_HIDE_FROM_ABI size_t |
| 228 | +__mbsrtowcs(wchar_t* __dest, const char** __src, size_t __len, mbstate_t* __ps, __locale_t __loc) { |
| 229 | + __locale_guard __current(__loc); |
| 230 | + return std::mbsrtowcs(__dest, __src, __len, __ps); |
| 231 | +} |
| 232 | +# endif // _LIBCPP_HAS_WIDE_CHARACTERS |
| 233 | +#endif // _LIBCPP_BUILDING_LIBRARY |
| 234 | + |
| 235 | +#ifndef _LIBCPP_COMPILER_GCC // GCC complains that this can't be always_inline due to C-style varargs |
| 236 | +_LIBCPP_HIDE_FROM_ABI |
| 237 | +#endif |
| 238 | +inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 4, 5) int __snprintf( |
| 239 | + char* __s, size_t __n, __locale_t __loc, const char* __format, ...) { |
| 240 | + va_list __va; |
| 241 | + va_start(__va, __format); |
| 242 | + __locale_guard __current(__loc); |
| 243 | + int __res = std::vsnprintf(__s, __n, __format, __va); |
| 244 | + va_end(__va); |
| 245 | + return __res; |
| 246 | +} |
| 247 | + |
| 248 | +#ifndef _LIBCPP_COMPILER_GCC // GCC complains that this can't be always_inline due to C-style varargs |
| 249 | +_LIBCPP_HIDE_FROM_ABI |
| 250 | +#endif |
| 251 | +inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 3, 4) int __asprintf( |
| 252 | + char** __s, __locale_t __loc, const char* __format, ...) { |
| 253 | + va_list __va; |
| 254 | + va_start(__va, __format); |
| 255 | + __locale_guard __current(__loc); |
| 256 | + int __res = ::vasprintf(__s, __format, __va); // non-standard |
| 257 | + va_end(__va); |
| 258 | + return __res; |
| 259 | +} |
| 260 | + |
| 261 | +#ifndef _LIBCPP_COMPILER_GCC // GCC complains that this can't be always_inline due to C-style varargs |
| 262 | +_LIBCPP_HIDE_FROM_ABI |
| 263 | +#endif |
| 264 | +inline _LIBCPP_ATTRIBUTE_FORMAT(__scanf__, 3, 4) int __sscanf( |
| 265 | + const char* __s, __locale_t __loc, const char* __format, ...) { |
| 266 | + va_list __va; |
| 267 | + va_start(__va, __format); |
| 268 | + __locale_guard __current(__loc); |
| 269 | + int __res = std::vsscanf(__s, __format, __va); |
| 270 | + va_end(__va); |
| 271 | + return __res; |
| 272 | +} |
| 273 | + |
| 274 | +} // namespace __locale |
| 275 | +_LIBCPP_END_NAMESPACE_STD |
| 276 | + |
| 277 | +#endif // _LIBCPP___LOCALE_DIR_SUPPORT_LINUX_H |
0 commit comments