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