| 1 | // -*- C++ -*- |
| 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_LOCALE_BASE_API_OPENBSD_H |
| 11 | #define _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_OPENBSD_H |
| 12 | |
| 13 | #include <__config> |
| 14 | #include <__cstddef/size_t.h> |
| 15 | #include <__std_mbstate_t.h> |
| 16 | #include <__utility/forward.h> |
| 17 | #include <clocale> // std::lconv |
| 18 | #include <ctype.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <time.h> |
| 23 | #if _LIBCPP_HAS_WIDE_CHARACTERS |
| 24 | # include <wchar.h> |
| 25 | # include <wctype.h> |
| 26 | #endif |
| 27 | #if defined(_LIBCPP_BUILDING_LIBRARY) |
| 28 | # include <langinfo.h> |
| 29 | #endif |
| 30 | |
| 31 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 32 | # pragma GCC system_header |
| 33 | #endif |
| 34 | |
| 35 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 36 | namespace __locale { |
| 37 | |
| 38 | struct __locale_guard { |
| 39 | __locale_guard(locale_t& __loc) : __old_loc_(::uselocale(__loc)) {} |
| 40 | |
| 41 | ~__locale_guard() { |
| 42 | if (__old_loc_) |
| 43 | ::uselocale(__old_loc_); |
| 44 | } |
| 45 | |
| 46 | locale_t __old_loc_; |
| 47 | |
| 48 | __locale_guard(__locale_guard const&) = delete; |
| 49 | __locale_guard& operator=(__locale_guard const&) = delete; |
| 50 | }; |
| 51 | |
| 52 | // |
| 53 | // Locale management |
| 54 | // |
| 55 | #define _LIBCPP_COLLATE_MASK LC_COLLATE_MASK |
| 56 | #define _LIBCPP_CTYPE_MASK LC_CTYPE_MASK |
| 57 | #define _LIBCPP_MONETARY_MASK LC_MONETARY_MASK |
| 58 | #define _LIBCPP_NUMERIC_MASK LC_NUMERIC_MASK |
| 59 | #define _LIBCPP_TIME_MASK LC_TIME_MASK |
| 60 | #define _LIBCPP_MESSAGES_MASK LC_MESSAGES_MASK |
| 61 | #define _LIBCPP_ALL_MASK LC_ALL_MASK |
| 62 | #define _LIBCPP_LC_ALL LC_ALL |
| 63 | |
| 64 | using __locale_t _LIBCPP_NODEBUG = ::locale_t; |
| 65 | #if defined(_LIBCPP_BUILDING_LIBRARY) |
| 66 | using __lconv_t _LIBCPP_NODEBUG = std::lconv; |
| 67 | |
| 68 | inline __locale_t __newlocale(int __category_mask, const char* __locale, __locale_t __base) { |
| 69 | return ::newlocale(__category_mask, __locale, __base); |
| 70 | } |
| 71 | |
| 72 | inline void __freelocale(__locale_t __loc) { ::freelocale(__loc); } |
| 73 | |
| 74 | inline char* __setlocale(int __category, char const* __locale) { return ::setlocale(__category, __locale); } |
| 75 | |
| 76 | inline __lconv_t* __localeconv(__locale_t& __loc) { |
| 77 | __locale_guard __current(__loc); |
| 78 | return ::localeconv(); |
| 79 | } |
| 80 | |
| 81 | inline _LIBCPP_HIDE_FROM_ABI const char* __get_locale_encoding(__locale_t __loc) { |
| 82 | return ::nl_langinfo_l(CODESET, __loc); |
| 83 | } |
| 84 | #endif // _LIBCPP_BUILDING_LIBRARY |
| 85 | |
| 86 | // |
| 87 | // Strtonum functions |
| 88 | // |
| 89 | inline float __strtof(const char* __nptr, char** __endptr, __locale_t) { return ::strtof(__nptr, __endptr); } |
| 90 | |
| 91 | inline double __strtod(const char* __nptr, char** __endptr, __locale_t) { return ::strtod(__nptr, __endptr); } |
| 92 | |
| 93 | inline long double __strtold(const char* __nptr, char** __endptr, __locale_t) { return ::strtold(__nptr, __endptr); } |
| 94 | |
| 95 | // |
| 96 | // Character manipulation functions |
| 97 | // |
| 98 | #if defined(_LIBCPP_BUILDING_LIBRARY) |
| 99 | inline int __toupper(int __c, __locale_t __loc) { return ::toupper_l(__c, __loc); } |
| 100 | |
| 101 | inline int __tolower(int __c, __locale_t __loc) { return ::tolower_l(__c, __loc); } |
| 102 | |
| 103 | inline int __strcoll(const char* __s1, const char* __s2, __locale_t __loc) { return ::strcoll_l(__s1, __s2, __loc); } |
| 104 | |
| 105 | inline size_t __strxfrm(char* __dest, const char* __src, size_t __n, __locale_t __loc) { |
| 106 | return ::strxfrm_l(__dest, __src, __n, __loc); |
| 107 | } |
| 108 | |
| 109 | # if _LIBCPP_HAS_WIDE_CHARACTERS |
| 110 | inline int __iswctype(wint_t __c, wctype_t __type, __locale_t __loc) { return ::iswctype_l(__c, __type, __loc); } |
| 111 | |
| 112 | inline int __iswspace(wint_t __c, __locale_t __loc) { return ::iswspace_l(__c, __loc); } |
| 113 | |
| 114 | inline int __iswprint(wint_t __c, __locale_t __loc) { return ::iswprint_l(__c, __loc); } |
| 115 | |
| 116 | inline int __iswcntrl(wint_t __c, __locale_t __loc) { return ::iswcntrl_l(__c, __loc); } |
| 117 | |
| 118 | inline int __iswupper(wint_t __c, __locale_t __loc) { return ::iswupper_l(__c, __loc); } |
| 119 | |
| 120 | inline int __iswlower(wint_t __c, __locale_t __loc) { return ::iswlower_l(__c, __loc); } |
| 121 | |
| 122 | inline int __iswalpha(wint_t __c, __locale_t __loc) { return ::iswalpha_l(__c, __loc); } |
| 123 | |
| 124 | inline int __iswblank(wint_t __c, __locale_t __loc) { return ::iswblank_l(__c, __loc); } |
| 125 | |
| 126 | inline int __iswdigit(wint_t __c, __locale_t __loc) { return ::iswdigit_l(__c, __loc); } |
| 127 | |
| 128 | inline int __iswpunct(wint_t __c, __locale_t __loc) { return ::iswpunct_l(__c, __loc); } |
| 129 | |
| 130 | inline int __iswxdigit(wint_t __c, __locale_t __loc) { return ::iswxdigit_l(__c, __loc); } |
| 131 | |
| 132 | inline wint_t __towupper(wint_t __c, __locale_t __loc) { return ::towupper_l(__c, __loc); } |
| 133 | |
| 134 | inline wint_t __towlower(wint_t __c, __locale_t __loc) { return ::towlower_l(__c, __loc); } |
| 135 | |
| 136 | inline int __wcscoll(const wchar_t* __ws1, const wchar_t* __ws2, __locale_t __loc) { |
| 137 | return ::wcscoll_l(__ws1, __ws2, __loc); |
| 138 | } |
| 139 | |
| 140 | inline size_t __wcsxfrm(wchar_t* __dest, const wchar_t* __src, size_t __n, __locale_t __loc) { |
| 141 | return ::wcsxfrm_l(__dest, __src, __n, __loc); |
| 142 | } |
| 143 | # endif // _LIBCPP_HAS_WIDE_CHARACTERS |
| 144 | |
| 145 | inline _LIBCPP_ATTRIBUTE_FORMAT(__strftime__, 3, 0) size_t |
| 146 | __strftime(char* __s, size_t __max, const char* __format, const struct tm* __tm, __locale_t __loc) { |
| 147 | return ::strftime_l(__s, __max, __format, __tm, __loc); |
| 148 | } |
| 149 | |
| 150 | // |
| 151 | // Other functions |
| 152 | // |
| 153 | inline decltype(MB_CUR_MAX) __mb_len_max(__locale_t __loc) { |
| 154 | __locale_guard __current(__loc); |
| 155 | return MB_CUR_MAX; |
| 156 | } |
| 157 | |
| 158 | # if _LIBCPP_HAS_WIDE_CHARACTERS |
| 159 | inline wint_t __btowc(int __c, __locale_t __loc) { |
| 160 | __locale_guard __current(__loc); |
| 161 | return btowc(__c); |
| 162 | } |
| 163 | |
| 164 | inline int __wctob(wint_t __c, __locale_t __loc) { |
| 165 | __locale_guard __current(__loc); |
| 166 | return wctob(__c); |
| 167 | } |
| 168 | |
| 169 | inline size_t |
| 170 | __wcsnrtombs(char* __dest, const wchar_t** __src, size_t __nwc, size_t __len, mbstate_t* __ps, __locale_t __loc) { |
| 171 | __locale_guard __current(__loc); |
| 172 | return wcsnrtombs(__dest, __src, __nwc, __len, __ps); // wcsnrtombs is a POSIX extension |
| 173 | } |
| 174 | |
| 175 | inline size_t __wcrtomb(char* __s, wchar_t __wc, mbstate_t* __ps, __locale_t __loc) { |
| 176 | __locale_guard __current(__loc); |
| 177 | return wcrtomb(__s, __wc, __ps); |
| 178 | } |
| 179 | |
| 180 | inline size_t |
| 181 | __mbsnrtowcs(wchar_t* __dest, const char** __src, size_t __nms, size_t __len, mbstate_t* __ps, __locale_t __loc) { |
| 182 | __locale_guard __current(__loc); |
| 183 | return mbsnrtowcs(__dest, __src, __nms, __len, __ps); // mbsnrtowcs is a POSIX extension |
| 184 | } |
| 185 | |
| 186 | inline size_t __mbrtowc(wchar_t* __pwc, const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) { |
| 187 | __locale_guard __current(__loc); |
| 188 | return mbrtowc(__pwc, __s, __n, __ps); |
| 189 | } |
| 190 | |
| 191 | inline int __mbtowc(wchar_t* __pwc, const char* __pmb, size_t __max, __locale_t __loc) { |
| 192 | __locale_guard __current(__loc); |
| 193 | return mbtowc(__pwc, __pmb, __max); |
| 194 | } |
| 195 | |
| 196 | inline size_t __mbrlen(const char* __s, size_t __n, mbstate_t* __ps, __locale_t __loc) { |
| 197 | __locale_guard __current(__loc); |
| 198 | return mbrlen(__s, __n, __ps); |
| 199 | } |
| 200 | |
| 201 | inline size_t __mbsrtowcs(wchar_t* __dest, const char** __src, size_t __len, mbstate_t* __ps, __locale_t __loc) { |
| 202 | __locale_guard __current(__loc); |
| 203 | return mbsrtowcs(__dest, __src, __len, __ps); |
| 204 | } |
| 205 | # endif // _LIBCPP_HAS_WIDE_CHARACTERS |
| 206 | #endif // _LIBCPP_BUILDING_LIBRARY |
| 207 | |
| 208 | _LIBCPP_DIAGNOSTIC_PUSH |
| 209 | _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wgcc-compat") |
| 210 | _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wformat-nonliteral") // GCC doesn't support [[gnu::format]] on variadic templates |
| 211 | #ifdef _LIBCPP_COMPILER_CLANG_BASED |
| 212 | # define _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(...) _LIBCPP_ATTRIBUTE_FORMAT(__VA_ARGS__) |
| 213 | #else |
| 214 | # define _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(...) /* nothing */ |
| 215 | #endif |
| 216 | |
| 217 | template <class... _Args> |
| 218 | _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(__printf__, 4, 5) |
| 219 | int __snprintf(char* __s, size_t __n, __locale_t __loc, const char* __format, _Args&&... __args) { |
| 220 | __locale_guard __current(__loc); |
| 221 | return ::snprintf(__s, __n, __format, std::forward<_Args>(__args)...); |
| 222 | } |
| 223 | |
| 224 | template <class... _Args> |
| 225 | _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(__printf__, 3, 4) |
| 226 | int __asprintf(char** __s, __locale_t __loc, const char* __format, _Args&&... __args) { |
| 227 | __locale_guard __current(__loc); |
| 228 | return ::asprintf(__s, __format, std::forward<_Args>(__args)...); // non-standard |
| 229 | } |
| 230 | _LIBCPP_DIAGNOSTIC_POP |
| 231 | #undef _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT |
| 232 | |
| 233 | } // namespace __locale |
| 234 | _LIBCPP_END_NAMESPACE_STD |
| 235 | |
| 236 | #endif // _LIBCPP___LOCALE_DIR_LOCALE_BASE_API_OPENBSD_H |