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_WINDOWS_H
 10#define _LIBCPP___LOCALE_DIR_SUPPORT_WINDOWS_H
 11
 12#include <__config>
 13#include <__cstddef/nullptr_t.h>
 14#include <__utility/forward.h>
 15#include <clocale> // std::lconv & friends
 16#include <cstddef>
 17#include <ctype.h>  // ::_isupper_l & friends
 18#include <locale.h> // ::_locale_t
 19#include <stdio.h>  // ::_sscanf_l
 20#include <stdlib.h> // ::_strtod_l & friends
 21#include <string.h> // ::_strcoll_l
 22#include <string>
 23#include <time.h> // ::_strftime_l
 24
 25#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 26#  pragma GCC system_header
 27#endif
 28
 29_LIBCPP_BEGIN_NAMESPACE_STD
 30namespace __locale {
 31
 32using __lconv_t _LIBCPP_NODEBUG = std::lconv;
 33
 34class __lconv_storage {
 35public:
 36  __lconv_storage(const __lconv_t* __lc_input) {
 37    __lc_ = *__lc_input;
 38
 39    __decimal_point_     = __lc_input->decimal_point;
 40    __thousands_sep_     = __lc_input->thousands_sep;
 41    __grouping_          = __lc_input->grouping;
 42    __int_curr_symbol_   = __lc_input->int_curr_symbol;
 43    __currency_symbol_   = __lc_input->currency_symbol;
 44    __mon_decimal_point_ = __lc_input->mon_decimal_point;
 45    __mon_thousands_sep_ = __lc_input->mon_thousands_sep;
 46    __mon_grouping_      = __lc_input->mon_grouping;
 47    __positive_sign_     = __lc_input->positive_sign;
 48    __negative_sign_     = __lc_input->negative_sign;
 49
 50    __lc_.decimal_point     = const_cast<char*>(__decimal_point_.c_str());
 51    __lc_.thousands_sep     = const_cast<char*>(__thousands_sep_.c_str());
 52    __lc_.grouping          = const_cast<char*>(__grouping_.c_str());
 53    __lc_.int_curr_symbol   = const_cast<char*>(__int_curr_symbol_.c_str());
 54    __lc_.currency_symbol   = const_cast<char*>(__currency_symbol_.c_str());
 55    __lc_.mon_decimal_point = const_cast<char*>(__mon_decimal_point_.c_str());
 56    __lc_.mon_thousands_sep = const_cast<char*>(__mon_thousands_sep_.c_str());
 57    __lc_.mon_grouping      = const_cast<char*>(__mon_grouping_.c_str());
 58    __lc_.positive_sign     = const_cast<char*>(__positive_sign_.c_str());
 59    __lc_.negative_sign     = const_cast<char*>(__negative_sign_.c_str());
 60  }
 61
 62  __lconv_t* __get() { return &__lc_; }
 63
 64private:
 65  __lconv_t __lc_;
 66  std::string __decimal_point_;
 67  std::string __thousands_sep_;
 68  std::string __grouping_;
 69  std::string __int_curr_symbol_;
 70  std::string __currency_symbol_;
 71  std::string __mon_decimal_point_;
 72  std::string __mon_thousands_sep_;
 73  std::string __mon_grouping_;
 74  std::string __positive_sign_;
 75  std::string __negative_sign_;
 76};
 77
 78//
 79// Locale management
 80//
 81#define _CATMASK(n) ((1 << (n)) >> 1)
 82#define _LIBCPP_COLLATE_MASK _CATMASK(LC_COLLATE)
 83#define _LIBCPP_CTYPE_MASK _CATMASK(LC_CTYPE)
 84#define _LIBCPP_MONETARY_MASK _CATMASK(LC_MONETARY)
 85#define _LIBCPP_NUMERIC_MASK _CATMASK(LC_NUMERIC)
 86#define _LIBCPP_TIME_MASK _CATMASK(LC_TIME)
 87#define _LIBCPP_MESSAGES_MASK _CATMASK(6)
 88#define _LIBCPP_ALL_MASK                                                                                               \
 89  (_LIBCPP_COLLATE_MASK | _LIBCPP_CTYPE_MASK | _LIBCPP_MESSAGES_MASK | _LIBCPP_MONETARY_MASK | _LIBCPP_NUMERIC_MASK |  \
 90   _LIBCPP_TIME_MASK)
 91#define _LIBCPP_LC_ALL LC_ALL
 92
 93class __locale_t {
 94public:
 95  __locale_t() : __locale_(nullptr), __locale_str_(nullptr), __lc_(nullptr) {}
 96  __locale_t(std::nullptr_t) : __locale_(nullptr), __locale_str_(nullptr), __lc_(nullptr) {}
 97  __locale_t(::_locale_t __loc, const char* __loc_str) : __locale_(__loc), __locale_str_(__loc_str), __lc_(nullptr) {}
 98  __locale_t(const __locale_t& __loc)
 99      : __locale_(__loc.__locale_), __locale_str_(__loc.__locale_str_), __lc_(nullptr) {}
100
101  ~__locale_t() { delete __lc_; }
102
103  __locale_t& operator=(const __locale_t& __loc) {
104    __locale_     = __loc.__locale_;
105    __locale_str_ = __loc.__locale_str_;
106    // __lc_ not copied
107    return *this;
108  }
109
110  friend bool operator==(const __locale_t& __left, const __locale_t& __right) {
111    return __left.__locale_ == __right.__locale_;
112  }
113
114  friend bool operator==(const __locale_t& __left, int __right) { return __left.__locale_ == nullptr && __right == 0; }
115
116  friend bool operator==(const __locale_t& __left, long long __right) {
117    return __left.__locale_ == nullptr && __right == 0;
118  }
119
120  friend bool operator==(const __locale_t& __left, std::nullptr_t) { return __left.__locale_ == nullptr; }
121
122  friend bool operator==(int __left, const __locale_t& __right) { return __left == 0 && nullptr == __right.__locale_; }
123
124  friend bool operator==(std::nullptr_t, const __locale_t& __right) { return nullptr == __right.__locale_; }
125
126  friend bool operator!=(const __locale_t& __left, const __locale_t& __right) { return !(__left == __right); }
127
128  friend bool operator!=(const __locale_t& __left, int __right) { return !(__left == __right); }
129
130  friend bool operator!=(const __locale_t& __left, long long __right) { return !(__left == __right); }
131
132  friend bool operator!=(const __locale_t& __left, std::nullptr_t __right) { return !(__left == __right); }
133
134  friend bool operator!=(int __left, const __locale_t& __right) { return !(__left == __right); }
135
136  friend bool operator!=(std::nullptr_t __left, const __locale_t& __right) { return !(__left == __right); }
137
138  operator bool() const { return __locale_ != nullptr; }
139
140  const char* __get_locale() const { return __locale_str_; }
141
142  operator ::_locale_t() const { return __locale_; }
143
144  __lconv_t* __store_lconv(const __lconv_t* __input_lc) {
145    delete __lc_;
146    __lc_ = new __lconv_storage(__input_lc);
147    return __lc_->__get();
148  }
149
150private:
151  ::_locale_t __locale_;
152  const char* __locale_str_;
153  __lconv_storage* __lc_ = nullptr;
154};
155
156#if defined(_LIBCPP_BUILDING_LIBRARY)
157_LIBCPP_EXPORTED_FROM_ABI __locale_t __newlocale(int __mask, const char* __locale, __locale_t __base);
158inline _LIBCPP_HIDE_FROM_ABI void __freelocale(__locale_t __loc) { ::_free_locale(__loc); }
159inline _LIBCPP_HIDE_FROM_ABI char* __setlocale(int __category, const char* __locale) {
160  char* __new_locale = ::setlocale(__category, __locale);
161  if (__new_locale == nullptr)
162    std::__throw_bad_alloc();
163  return __new_locale;
164}
165_LIBCPP_EXPORTED_FROM_ABI __lconv_t* __localeconv(__locale_t& __loc);
166#endif // _LIBCPP_BUILDING_LIBRARY
167
168//
169// Strtonum functions
170//
171
172// the *_l functions are prefixed on Windows, only available for msvcr80+, VS2005+
173#if defined(_LIBCPP_MSVCRT)
174inline _LIBCPP_HIDE_FROM_ABI float __strtof(const char* __nptr, char** __endptr, __locale_t __loc) {
175  return ::_strtof_l(__nptr, __endptr, __loc);
176}
177inline _LIBCPP_HIDE_FROM_ABI long double __strtold(const char* __nptr, char** __endptr, __locale_t __loc) {
178  return ::_strtold_l(__nptr, __endptr, __loc);
179}
180#else
181_LIBCPP_EXPORTED_FROM_ABI float __strtof(const char*, char**, __locale_t);
182_LIBCPP_EXPORTED_FROM_ABI long double __strtold(const char*, char**, __locale_t);
183#endif
184
185inline _LIBCPP_HIDE_FROM_ABI double __strtod(const char* __nptr, char** __endptr, __locale_t __loc) {
186  return ::_strtod_l(__nptr, __endptr, __loc);
187}
188
189inline _LIBCPP_HIDE_FROM_ABI long long __strtoll(const char* __nptr, char** __endptr, int __base, __locale_t __loc) {
190  return ::_strtoi64_l(__nptr, __endptr, __base, __loc);
191}
192inline _LIBCPP_HIDE_FROM_ABI unsigned long long
193__strtoull(const char* __nptr, char** __endptr, int __base, __locale_t __loc) {
194  return ::_strtoui64_l(__nptr, __endptr, __base, __loc);
195}
196
197//
198// Character manipulation functions
199//
200inline _LIBCPP_HIDE_FROM_ABI int __isdigit(int __c, __locale_t __loc) { return _isdigit_l(__c, __loc); }
201
202inline _LIBCPP_HIDE_FROM_ABI int __isxdigit(int __c, __locale_t __loc) { return _isxdigit_l(__c, __loc); }
203
204#if defined(_LIBCPP_BUILDING_LIBRARY)
205inline _LIBCPP_HIDE_FROM_ABI int __toupper(int __c, __locale_t __loc) { return ::_toupper_l(__c, __loc); }
206
207inline _LIBCPP_HIDE_FROM_ABI int __tolower(int __c, __locale_t __loc) { return ::_tolower_l(__c, __loc); }
208
209inline _LIBCPP_HIDE_FROM_ABI int __strcoll(const char* __s1, const char* __s2, __locale_t __loc) {
210  return ::_strcoll_l(__s1, __s2, __loc);
211}
212
213inline _LIBCPP_HIDE_FROM_ABI size_t __strxfrm(char* __dest, const char* __src, size_t __n, __locale_t __loc) {
214  return ::_strxfrm_l(__dest, __src, __n, __loc);
215}
216
217#  if _LIBCPP_HAS_WIDE_CHARACTERS
218inline _LIBCPP_HIDE_FROM_ABI int __iswctype(wint_t __c, wctype_t __type, __locale_t __loc) {
219  return ::_iswctype_l(__c, __type, __loc);
220}
221inline _LIBCPP_HIDE_FROM_ABI int __iswspace(wint_t __c, __locale_t __loc) { return ::_iswspace_l(__c, __loc); }
222inline _LIBCPP_HIDE_FROM_ABI int __iswprint(wint_t __c, __locale_t __loc) { return ::_iswprint_l(__c, __loc); }
223inline _LIBCPP_HIDE_FROM_ABI int __iswcntrl(wint_t __c, __locale_t __loc) { return ::_iswcntrl_l(__c, __loc); }
224inline _LIBCPP_HIDE_FROM_ABI int __iswupper(wint_t __c, __locale_t __loc) { return ::_iswupper_l(__c, __loc); }
225inline _LIBCPP_HIDE_FROM_ABI int __iswlower(wint_t __c, __locale_t __loc) { return ::_iswlower_l(__c, __loc); }
226inline _LIBCPP_HIDE_FROM_ABI int __iswalpha(wint_t __c, __locale_t __loc) { return ::_iswalpha_l(__c, __loc); }
227// TODO: use locale to determine blank characters
228inline _LIBCPP_HIDE_FROM_ABI int __iswblank(wint_t __c, __locale_t /*loc*/) { return (__c == L' ' || __c == L'\t'); }
229inline _LIBCPP_HIDE_FROM_ABI int __iswdigit(wint_t __c, __locale_t __loc) { return ::_iswdigit_l(__c, __loc); }
230inline _LIBCPP_HIDE_FROM_ABI int __iswpunct(wint_t __c, __locale_t __loc) { return ::_iswpunct_l(__c, __loc); }
231inline _LIBCPP_HIDE_FROM_ABI int __iswxdigit(wint_t __c, __locale_t __loc) { return ::_iswxdigit_l(__c, __loc); }
232inline _LIBCPP_HIDE_FROM_ABI wint_t __towupper(wint_t __c, __locale_t __loc) { return ::_towupper_l(__c, __loc); }
233inline _LIBCPP_HIDE_FROM_ABI wint_t __towlower(wint_t __c, __locale_t __loc) { return ::_towlower_l(__c, __loc); }
234
235inline _LIBCPP_HIDE_FROM_ABI int __wcscoll(const wchar_t* __ws1, const wchar_t* __ws2, __locale_t __loc) {
236  return ::_wcscoll_l(__ws1, __ws2, __loc);
237}
238
239inline _LIBCPP_HIDE_FROM_ABI size_t __wcsxfrm(wchar_t* __dest, const wchar_t* __src, size_t __n, __locale_t __loc) {
240  return ::_wcsxfrm_l(__dest, __src, __n, __loc);
241}
242#  endif // _LIBCPP_HAS_WIDE_CHARACTERS
243
244#  if defined(__MINGW32__) && __MSVCRT_VERSION__ < 0x0800
245_LIBCPP_EXPORTED_FROM_ABI size_t __strftime(char*, size_t, const char*, const struct tm*, __locale_t);
246#  else
247inline _LIBCPP_HIDE_FROM_ABI size_t
248__strftime(char* __ret, size_t __n, const char* __format, const struct tm* __tm, __locale_t __loc) {
249  return ::_strftime_l(__ret, __n, __format, __tm, __loc);
250}
251#  endif
252
253//
254// Other functions
255//
256_LIBCPP_EXPORTED_FROM_ABI decltype(MB_CUR_MAX) __mb_len_max(__locale_t);
257_LIBCPP_EXPORTED_FROM_ABI wint_t __btowc(int, __locale_t);
258_LIBCPP_EXPORTED_FROM_ABI int __wctob(wint_t, __locale_t);
259_LIBCPP_EXPORTED_FROM_ABI size_t
260__wcsnrtombs(char* __restrict, const wchar_t** __restrict, size_t, size_t, mbstate_t* __restrict, __locale_t);
261_LIBCPP_EXPORTED_FROM_ABI size_t __wcrtomb(char* __restrict, wchar_t, mbstate_t* __restrict, __locale_t);
262_LIBCPP_EXPORTED_FROM_ABI size_t
263__mbsnrtowcs(wchar_t* __restrict, const char** __restrict, size_t, size_t, mbstate_t* __restrict, __locale_t);
264_LIBCPP_EXPORTED_FROM_ABI size_t
265__mbrtowc(wchar_t* __restrict, const char* __restrict, size_t, mbstate_t* __restrict, __locale_t);
266
267inline _LIBCPP_HIDE_FROM_ABI int __mbtowc(wchar_t* __pwc, const char* __pmb, size_t __max, __locale_t __loc) {
268  return ::_mbtowc_l(__pwc, __pmb, __max, __loc);
269}
270
271_LIBCPP_EXPORTED_FROM_ABI size_t __mbrlen(const char* __restrict, size_t, mbstate_t* __restrict, __locale_t);
272
273_LIBCPP_EXPORTED_FROM_ABI size_t
274__mbsrtowcs(wchar_t* __restrict, const char** __restrict, size_t, mbstate_t* __restrict, __locale_t);
275#endif // _LIBCPP_BUILDING_LIBRARY
276
277_LIBCPP_EXPORTED_FROM_ABI _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 4, 5) int __snprintf(
278    char* __ret, size_t __n, __locale_t __loc, const char* __format, ...);
279
280_LIBCPP_EXPORTED_FROM_ABI
281_LIBCPP_ATTRIBUTE_FORMAT(__printf__, 3, 4) int __asprintf(char** __ret, __locale_t __loc, const char* __format, ...);
282
283_LIBCPP_DIAGNOSTIC_PUSH
284_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wgcc-compat")
285_LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Wformat-nonliteral") // GCC doesn't support [[gnu::format]] on variadic templates
286#ifdef _LIBCPP_COMPILER_CLANG_BASED
287#  define _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(...) _LIBCPP_ATTRIBUTE_FORMAT(__VA_ARGS__)
288#else
289#  define _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(...) /* nothing */
290#endif
291
292template <class... _Args>
293_LIBCPP_HIDE_FROM_ABI _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT(__scanf__, 3, 4) int __sscanf(
294    const char* __dest, __locale_t __loc, const char* __format, _Args&&... __args) {
295  return ::_sscanf_l(__dest, __format, __loc, std::forward<_Args>(__args)...);
296}
297_LIBCPP_DIAGNOSTIC_POP
298#undef _LIBCPP_VARIADIC_ATTRIBUTE_FORMAT
299
300#if defined(_LIBCPP_BUILDING_LIBRARY)
301struct __locale_guard {
302  _LIBCPP_HIDE_FROM_ABI __locale_guard(__locale_t __l) : __status(_configthreadlocale(_ENABLE_PER_THREAD_LOCALE)) {
303    // Setting the locale can be expensive even when the locale given is
304    // already the current locale, so do an explicit check to see if the
305    // current locale is already the one we want.
306    const char* __lc = __locale::__setlocale(LC_ALL, nullptr);
307    // If every category is the same, the locale string will simply be the
308    // locale name, otherwise it will be a semicolon-separated string listing
309    // each category.  In the second case, we know at least one category won't
310    // be what we want, so we only have to check the first case.
311    if (std::strcmp(__l.__get_locale(), __lc) != 0) {
312      __locale_all = _strdup(__lc);
313      if (__locale_all == nullptr)
314        std::__throw_bad_alloc();
315      __locale::__setlocale(LC_ALL, __l.__get_locale());
316    }
317  }
318  _LIBCPP_HIDE_FROM_ABI ~__locale_guard() {
319    // The CRT documentation doesn't explicitly say, but setlocale() does the
320    // right thing when given a semicolon-separated list of locale settings
321    // for the different categories in the same format as returned by
322    // setlocale(LC_ALL, nullptr).
323    if (__locale_all != nullptr) {
324      __locale::__setlocale(LC_ALL, __locale_all);
325      free(__locale_all);
326    }
327    _configthreadlocale(__status);
328  }
329  int __status;
330  char* __locale_all = nullptr;
331};
332#endif // _LIBCPP_BUILDING_LIBRARY
333
334} // namespace __locale
335_LIBCPP_END_NAMESPACE_STD
336
337#endif // _LIBCPP___LOCALE_DIR_SUPPORT_WINDOWS_H