1 // -*- C++ -*-
2 //===--------------------- support/win32/locale_win32.h -------------------===//
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_SUPPORT_WIN32_LOCALE_WIN32_H
11 #define _LIBCPP_SUPPORT_WIN32_LOCALE_WIN32_H
12 
13 #include <__config>
14 #include <stdio.h>
15 #include <xlocinfo.h> // _locale_t
16 #include <__nullptr>
17 
18 #define LC_COLLATE_MASK _M_COLLATE
19 #define LC_CTYPE_MASK _M_CTYPE
20 #define LC_MONETARY_MASK _M_MONETARY
21 #define LC_NUMERIC_MASK _M_NUMERIC
22 #define LC_TIME_MASK _M_TIME
23 #define LC_MESSAGES_MASK _M_MESSAGES
24 #define LC_ALL_MASK (  LC_COLLATE_MASK \
25                      | LC_CTYPE_MASK \
26                      | LC_MESSAGES_MASK \
27                      | LC_MONETARY_MASK \
28                      | LC_NUMERIC_MASK \
29                      | LC_TIME_MASK )
30 
31 class __lconv_storage {
32 public:
__lconv_storage(const lconv * __lc_input)33     __lconv_storage(const lconv *__lc_input) {
34         __lc = *__lc_input;
35 
36         __decimal_point = __lc_input->decimal_point;
37         __thousands_sep = __lc_input->thousands_sep;
38         __grouping = __lc_input->grouping;
39         __int_curr_symbol = __lc_input->int_curr_symbol;
40         __currency_symbol = __lc_input->currency_symbol;
41         __mon_decimal_point = __lc_input->mon_decimal_point;
42         __mon_thousands_sep = __lc_input->mon_thousands_sep;
43         __mon_grouping = __lc_input->mon_grouping;
44         __positive_sign = __lc_input->positive_sign;
45         __negative_sign = __lc_input->negative_sign;
46 
47         __lc.decimal_point = const_cast<char *>(__decimal_point.c_str());
48         __lc.thousands_sep = const_cast<char *>(__thousands_sep.c_str());
49         __lc.grouping = const_cast<char *>(__grouping.c_str());
50         __lc.int_curr_symbol = const_cast<char *>(__int_curr_symbol.c_str());
51         __lc.currency_symbol = const_cast<char *>(__currency_symbol.c_str());
52         __lc.mon_decimal_point = const_cast<char *>(__mon_decimal_point.c_str());
53         __lc.mon_thousands_sep = const_cast<char *>(__mon_thousands_sep.c_str());
54         __lc.mon_grouping = const_cast<char *>(__mon_grouping.c_str());
55         __lc.positive_sign = const_cast<char *>(__positive_sign.c_str());
56         __lc.negative_sign = const_cast<char *>(__negative_sign.c_str());
57     }
58 
__get()59     lconv *__get() {
60         return &__lc;
61     }
62 private:
63     lconv __lc;
64     std::string __decimal_point;
65     std::string __thousands_sep;
66     std::string __grouping;
67     std::string __int_curr_symbol;
68     std::string __currency_symbol;
69     std::string __mon_decimal_point;
70     std::string __mon_thousands_sep;
71     std::string __mon_grouping;
72     std::string __positive_sign;
73     std::string __negative_sign;
74 };
75 
76 class locale_t {
77 public:
locale_t()78     locale_t()
79         : __locale(nullptr), __locale_str(nullptr), __lc(nullptr) {}
locale_t(std::nullptr_t)80     locale_t(std::nullptr_t)
81         : __locale(nullptr), __locale_str(nullptr), __lc(nullptr) {}
locale_t(_locale_t __xlocale,const char * __xlocale_str)82     locale_t(_locale_t __xlocale, const char* __xlocale_str)
83         : __locale(__xlocale), __locale_str(__xlocale_str), __lc(nullptr) {}
locale_t(const locale_t & __l)84     locale_t(const locale_t &__l)
85         : __locale(__l.__locale), __locale_str(__l.__locale_str), __lc(nullptr) {}
86 
~locale_t()87     ~locale_t() {
88         delete __lc;
89     }
90 
91     locale_t &operator =(const locale_t &__l) {
92         __locale = __l.__locale;
93         __locale_str = __l.__locale_str;
94         // __lc not copied
95         return *this;
96     }
97 
98     friend bool operator==(const locale_t& __left, const locale_t& __right) {
99         return __left.__locale == __right.__locale;
100     }
101 
102     friend bool operator==(const locale_t& __left, int __right) {
103         return __left.__locale == nullptr && __right == 0;
104     }
105 
106     friend bool operator==(const locale_t& __left, long long __right) {
107         return __left.__locale == nullptr && __right == 0;
108     }
109 
110     friend bool operator==(const locale_t& __left, std::nullptr_t) {
111         return __left.__locale == nullptr;
112     }
113 
114     friend bool operator==(int __left, const locale_t& __right) {
115         return __left == 0 && nullptr == __right.__locale;
116     }
117 
118     friend bool operator==(std::nullptr_t, const locale_t& __right) {
119         return nullptr == __right.__locale;
120     }
121 
122     friend bool operator!=(const locale_t& __left, const locale_t& __right) {
123         return !(__left == __right);
124     }
125 
126     friend bool operator!=(const locale_t& __left, int __right) {
127         return !(__left == __right);
128     }
129 
130     friend bool operator!=(const locale_t& __left, long long __right) {
131         return !(__left == __right);
132     }
133 
134     friend bool operator!=(const locale_t& __left, std::nullptr_t __right) {
135         return !(__left == __right);
136     }
137 
138     friend bool operator!=(int __left, const locale_t& __right) {
139         return !(__left == __right);
140     }
141 
142     friend bool operator!=(std::nullptr_t __left, const locale_t& __right) {
143         return !(__left == __right);
144     }
145 
146     operator bool() const {
147         return __locale != nullptr;
148     }
149 
__get_locale()150     const char* __get_locale() const { return __locale_str; }
151 
_locale_t()152     operator _locale_t() const {
153         return __locale;
154     }
155 
__store_lconv(const lconv * __input_lc)156     lconv *__store_lconv(const lconv *__input_lc) {
157         delete __lc;
158         __lc = new __lconv_storage(__input_lc);
159         return __lc->__get();
160     }
161 private:
162     _locale_t __locale;
163     const char* __locale_str;
164     __lconv_storage *__lc = nullptr;
165 };
166 
167 // Locale management functions
168 #define freelocale _free_locale
169 // FIXME: base currently unused. Needs manual work to construct the new locale
170 locale_t newlocale( int mask, const char * locale, locale_t base );
171 // uselocale can't be implemented on Windows because Windows allows partial modification
172 // of thread-local locale and so _get_current_locale() returns a copy while uselocale does
173 // not create any copies.
174 // We can still implement raii even without uselocale though.
175 
176 
177 lconv *localeconv_l( locale_t &loc );
178 size_t mbrlen_l( const char *__restrict s, size_t n,
179                  mbstate_t *__restrict ps, locale_t loc);
180 size_t mbsrtowcs_l( wchar_t *__restrict dst, const char **__restrict src,
181                     size_t len, mbstate_t *__restrict ps, locale_t loc );
182 size_t wcrtomb_l( char *__restrict s, wchar_t wc, mbstate_t *__restrict ps,
183                   locale_t loc);
184 size_t mbrtowc_l( wchar_t *__restrict pwc, const char *__restrict s,
185                   size_t n, mbstate_t *__restrict ps, locale_t loc);
186 size_t mbsnrtowcs_l( wchar_t *__restrict dst, const char **__restrict src,
187                      size_t nms, size_t len, mbstate_t *__restrict ps, locale_t loc);
188 size_t wcsnrtombs_l( char *__restrict dst, const wchar_t **__restrict src,
189                      size_t nwc, size_t len, mbstate_t *__restrict ps, locale_t loc);
190 wint_t btowc_l( int c, locale_t loc );
191 int wctob_l( wint_t c, locale_t loc );
192 
193 decltype(MB_CUR_MAX) MB_CUR_MAX_L( locale_t __l );
194 
195 // the *_l functions are prefixed on Windows, only available for msvcr80+, VS2005+
196 #define mbtowc_l _mbtowc_l
197 #define strtoll_l _strtoi64_l
198 #define strtoull_l _strtoui64_l
199 #define strtod_l _strtod_l
200 #if defined(_LIBCPP_MSVCRT)
201 #define strtof_l _strtof_l
202 #define strtold_l _strtold_l
203 #else
204 float strtof_l(const char*, char**, locale_t);
205 long double strtold_l(const char*, char**, locale_t);
206 #endif
207 inline _LIBCPP_INLINE_VISIBILITY
208 int
islower_l(int c,_locale_t loc)209 islower_l(int c, _locale_t loc)
210 {
211  return _islower_l((int)c, loc);
212 }
213 
214 inline _LIBCPP_INLINE_VISIBILITY
215 int
isupper_l(int c,_locale_t loc)216 isupper_l(int c, _locale_t loc)
217 {
218  return _isupper_l((int)c, loc);
219 }
220 
221 #define isdigit_l _isdigit_l
222 #define isxdigit_l _isxdigit_l
223 #define strcoll_l _strcoll_l
224 #define strxfrm_l _strxfrm_l
225 #define wcscoll_l _wcscoll_l
226 #define wcsxfrm_l _wcsxfrm_l
227 #define toupper_l _toupper_l
228 #define tolower_l _tolower_l
229 #define iswspace_l _iswspace_l
230 #define iswprint_l _iswprint_l
231 #define iswcntrl_l _iswcntrl_l
232 #define iswupper_l _iswupper_l
233 #define iswlower_l _iswlower_l
234 #define iswalpha_l _iswalpha_l
235 #define iswdigit_l _iswdigit_l
236 #define iswpunct_l _iswpunct_l
237 #define iswxdigit_l _iswxdigit_l
238 #define towupper_l _towupper_l
239 #define towlower_l _towlower_l
240 #if defined(__MINGW32__) && __MSVCRT_VERSION__ < 0x0800
241 _LIBCPP_FUNC_VIS size_t strftime_l(char *ret, size_t n, const char *format,
242                                    const struct tm *tm, locale_t loc);
243 #else
244 #define strftime_l _strftime_l
245 #endif
246 #define sscanf_l( __s, __l, __f, ...) _sscanf_l( __s, __f, __l, __VA_ARGS__ )
247 #define sprintf_l( __s, __l, __f, ... ) _sprintf_l( __s, __f, __l, __VA_ARGS__ )
248 #define vsprintf_l( __s, __l, __f, ... ) _vsprintf_l( __s, __f, __l, __VA_ARGS__ )
249 #define vsnprintf_l( __s, __n, __l, __f, ... ) _vsnprintf_l( __s, __n, __f, __l, __VA_ARGS__ )
250 _LIBCPP_FUNC_VIS int snprintf_l(char *ret, size_t n, locale_t loc, const char *format, ...);
251 _LIBCPP_FUNC_VIS int asprintf_l( char **ret, locale_t loc, const char *format, ... );
252 _LIBCPP_FUNC_VIS int vasprintf_l( char **ret, locale_t loc, const char *format, va_list ap );
253 
254 // not-so-pressing FIXME: use locale to determine blank characters
isblank_l(int c,locale_t)255 inline int isblank_l( int c, locale_t /*loc*/ )
256 {
257     return ( c == ' ' || c == '\t' );
258 }
iswblank_l(wint_t c,locale_t)259 inline int iswblank_l( wint_t c, locale_t /*loc*/ )
260 {
261     return ( c == L' ' || c == L'\t' );
262 }
263 
264 #endif // _LIBCPP_SUPPORT_WIN32_LOCALE_WIN32_H
265