xref: /openbsd-src/gnu/llvm/libcxx/src/support/win32/locale_win32.cpp (revision 4e1ee0786f11cc571bd0be17d38e46f635c719fc)
1 // -*- C++ -*-
2 //===-------------------- support/win32/locale_win32.cpp ------------------===//
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 #include <locale>
11 #include <cstdarg> // va_start, va_end
12 #include <memory>
13 #include <type_traits>
14 
15 int __libcpp_vasprintf(char **sptr, const char *__restrict fmt, va_list ap);
16 
17 using std::__libcpp_locale_guard;
18 
19 // FIXME: base currently unused. Needs manual work to construct the new locale
20 locale_t newlocale( int mask, const char * locale, locale_t /*base*/ )
21 {
22     return {_create_locale( LC_ALL, locale ), locale};
23 }
24 
25 decltype(MB_CUR_MAX) MB_CUR_MAX_L( locale_t __l )
26 {
27 #if defined(_LIBCPP_MSVCRT)
28   return ___mb_cur_max_l_func(__l);
29 #else
30   __libcpp_locale_guard __current(__l);
31   return MB_CUR_MAX;
32 #endif
33 }
34 
35 lconv *localeconv_l( locale_t &loc )
36 {
37     __libcpp_locale_guard __current(loc);
38     lconv *lc = localeconv();
39     if (!lc)
40         return lc;
41     return loc.__store_lconv(lc);
42 }
43 size_t mbrlen_l( const char *__restrict s, size_t n,
44                  mbstate_t *__restrict ps, locale_t loc )
45 {
46     __libcpp_locale_guard __current(loc);
47     return mbrlen( s, n, ps );
48 }
49 size_t mbsrtowcs_l( wchar_t *__restrict dst, const char **__restrict src,
50                     size_t len, mbstate_t *__restrict ps, locale_t loc )
51 {
52     __libcpp_locale_guard __current(loc);
53     return mbsrtowcs( dst, src, len, ps );
54 }
55 size_t wcrtomb_l( char *__restrict s, wchar_t wc, mbstate_t *__restrict ps,
56                   locale_t loc )
57 {
58     __libcpp_locale_guard __current(loc);
59     return wcrtomb( s, wc, ps );
60 }
61 size_t mbrtowc_l( wchar_t *__restrict pwc, const char *__restrict s,
62                   size_t n, mbstate_t *__restrict ps, locale_t loc )
63 {
64     __libcpp_locale_guard __current(loc);
65     return mbrtowc( pwc, s, n, ps );
66 }
67 size_t mbsnrtowcs_l( wchar_t *__restrict dst, const char **__restrict src,
68                      size_t nms, size_t len, mbstate_t *__restrict ps, locale_t loc )
69 {
70     __libcpp_locale_guard __current(loc);
71     return mbsnrtowcs( dst, src, nms, len, ps );
72 }
73 size_t wcsnrtombs_l( char *__restrict dst, const wchar_t **__restrict src,
74                      size_t nwc, size_t len, mbstate_t *__restrict ps, locale_t loc )
75 {
76     __libcpp_locale_guard __current(loc);
77     return wcsnrtombs( dst, src, nwc, len, ps );
78 }
79 wint_t btowc_l( int c, locale_t loc )
80 {
81     __libcpp_locale_guard __current(loc);
82     return btowc( c );
83 }
84 int wctob_l( wint_t c, locale_t loc )
85 {
86     __libcpp_locale_guard __current(loc);
87     return wctob( c );
88 }
89 
90 int snprintf_l(char *ret, size_t n, locale_t loc, const char *format, ...)
91 {
92     va_list ap;
93     va_start( ap, format );
94 #if defined(_LIBCPP_MSVCRT)
95     // FIXME: Remove usage of internal CRT function and globals.
96     int result = __stdio_common_vsprintf(
97         _CRT_INTERNAL_LOCAL_PRINTF_OPTIONS | _CRT_INTERNAL_PRINTF_STANDARD_SNPRINTF_BEHAVIOR,
98         ret, n, format, loc, ap);
99 #else
100     __libcpp_locale_guard __current(loc);
101     int result = vsnprintf( ret, n, format, ap );
102 #endif
103     va_end(ap);
104     return result;
105 }
106 
107 int asprintf_l( char **ret, locale_t loc, const char *format, ... )
108 {
109     va_list ap;
110     va_start( ap, format );
111     int result = vasprintf_l( ret, loc, format, ap );
112     va_end(ap);
113     return result;
114 }
115 int vasprintf_l( char **ret, locale_t loc, const char *format, va_list ap )
116 {
117     __libcpp_locale_guard __current(loc);
118     return __libcpp_vasprintf( ret, format, ap );
119 }
120 
121 #if !defined(_LIBCPP_MSVCRT)
122 float strtof_l(const char* nptr, char** endptr, locale_t loc) {
123   __libcpp_locale_guard __current(loc);
124   return strtof(nptr, endptr);
125 }
126 
127 long double strtold_l(const char* nptr, char** endptr, locale_t loc) {
128   __libcpp_locale_guard __current(loc);
129   return strtold(nptr, endptr);
130 }
131 #endif
132 
133 #if defined(__MINGW32__) && __MSVCRT_VERSION__ < 0x0800
134 size_t strftime_l(char *ret, size_t n, const char *format, const struct tm *tm,
135                   locale_t loc) {
136   __libcpp_locale_guard __current(loc);
137   return strftime(ret, n, format, tm);
138 }
139 #endif
140