1 // -*- C++ -*-
2 //===--------------------- support/win32/locale_win32.h -------------------===//
3 //
4 // The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef _LIBCPP_SUPPORT_WIN32_LOCALE_WIN32_H
12 #define _LIBCPP_SUPPORT_WIN32_LOCALE_WIN32_H
13
14 #include <__config>
15 #include <stdio.h>
16 #include <xlocinfo.h> // _locale_t
17 #include <__nullptr>
18
19 #define LC_COLLATE_MASK _M_COLLATE
20 #define LC_CTYPE_MASK _M_CTYPE
21 #define LC_MONETARY_MASK _M_MONETARY
22 #define LC_NUMERIC_MASK _M_NUMERIC
23 #define LC_TIME_MASK _M_TIME
24 #define LC_MESSAGES_MASK _M_MESSAGES
25 #define LC_ALL_MASK ( LC_COLLATE_MASK \
26 | LC_CTYPE_MASK \
27 | LC_MESSAGES_MASK \
28 | LC_MONETARY_MASK \
29 | LC_NUMERIC_MASK \
30 | LC_TIME_MASK )
31
32 class locale_t {
33 public:
locale_t()34 locale_t()
35 : __locale(nullptr), __locale_str(nullptr) {}
locale_t(std::nullptr_t)36 locale_t(std::nullptr_t)
37 : __locale(nullptr), __locale_str(nullptr) {}
locale_t(_locale_t __xlocale,const char * __xlocale_str)38 locale_t(_locale_t __xlocale, const char* __xlocale_str)
39 : __locale(__xlocale), __locale_str(__xlocale_str) {}
40
41 friend bool operator==(const locale_t& __left, const locale_t& __right) {
42 return __left.__locale == __right.__locale;
43 }
44
45 friend bool operator==(const locale_t& __left, int __right) {
46 return __left.__locale == nullptr && __right == 0;
47 }
48
49 friend bool operator==(const locale_t& __left, long long __right) {
50 return __left.__locale == nullptr && __right == 0;
51 }
52
53 friend bool operator==(const locale_t& __left, std::nullptr_t) {
54 return __left.__locale == nullptr;
55 }
56
57 friend bool operator==(int __left, const locale_t& __right) {
58 return __left == 0 && nullptr == __right.__locale;
59 }
60
61 friend bool operator==(std::nullptr_t, const locale_t& __right) {
62 return nullptr == __right.__locale;
63 }
64
65 friend bool operator!=(const locale_t& __left, const locale_t& __right) {
66 return !(__left == __right);
67 }
68
69 friend bool operator!=(const locale_t& __left, int __right) {
70 return !(__left == __right);
71 }
72
73 friend bool operator!=(const locale_t& __left, long long __right) {
74 return !(__left == __right);
75 }
76
77 friend bool operator!=(const locale_t& __left, std::nullptr_t __right) {
78 return !(__left == __right);
79 }
80
81 friend bool operator!=(int __left, const locale_t& __right) {
82 return !(__left == __right);
83 }
84
85 friend bool operator!=(std::nullptr_t __left, const locale_t& __right) {
86 return !(__left == __right);
87 }
88
89 operator bool() const {
90 return __locale != nullptr;
91 }
92
__get_locale()93 const char* __get_locale() const { return __locale_str; }
94
_locale_t()95 operator _locale_t() const {
96 return __locale;
97 }
98 private:
99 _locale_t __locale;
100 const char* __locale_str;
101 };
102
103 // Locale management functions
104 #define freelocale _free_locale
105 // FIXME: base currently unused. Needs manual work to construct the new locale
106 locale_t newlocale( int mask, const char * locale, locale_t base );
107 // uselocale can't be implemented on Windows because Windows allows partial modification
108 // of thread-local locale and so _get_current_locale() returns a copy while uselocale does
109 // not create any copies.
110 // We can still implement raii even without uselocale though.
111
112
113 lconv *localeconv_l( locale_t loc );
114 size_t mbrlen_l( const char *__restrict s, size_t n,
115 mbstate_t *__restrict ps, locale_t loc);
116 size_t mbsrtowcs_l( wchar_t *__restrict dst, const char **__restrict src,
117 size_t len, mbstate_t *__restrict ps, locale_t loc );
118 size_t wcrtomb_l( char *__restrict s, wchar_t wc, mbstate_t *__restrict ps,
119 locale_t loc);
120 size_t mbrtowc_l( wchar_t *__restrict pwc, const char *__restrict s,
121 size_t n, mbstate_t *__restrict ps, locale_t loc);
122 size_t mbsnrtowcs_l( wchar_t *__restrict dst, const char **__restrict src,
123 size_t nms, size_t len, mbstate_t *__restrict ps, locale_t loc);
124 size_t wcsnrtombs_l( char *__restrict dst, const wchar_t **__restrict src,
125 size_t nwc, size_t len, mbstate_t *__restrict ps, locale_t loc);
126 wint_t btowc_l( int c, locale_t loc );
127 int wctob_l( wint_t c, locale_t loc );
128
129 decltype(MB_CUR_MAX) MB_CUR_MAX_L( locale_t __l );
130
131 // the *_l functions are prefixed on Windows, only available for msvcr80+, VS2005+
132 #define mbtowc_l _mbtowc_l
133 #define strtoll_l _strtoi64_l
134 #define strtoull_l _strtoui64_l
135 #define strtod_l _strtod_l
136 #if defined(_LIBCPP_MSVCRT)
137 #define strtof_l _strtof_l
138 #define strtold_l _strtold_l
139 #else
140 float strtof_l(const char*, char**, locale_t);
141 long double strtold_l(const char*, char**, locale_t);
142 #endif
143 inline _LIBCPP_INLINE_VISIBILITY
144 int
islower_l(int c,_locale_t loc)145 islower_l(int c, _locale_t loc)
146 {
147 return _islower_l((int)c, loc);
148 }
149
150 inline _LIBCPP_INLINE_VISIBILITY
151 int
isupper_l(int c,_locale_t loc)152 isupper_l(int c, _locale_t loc)
153 {
154 return _isupper_l((int)c, loc);
155 }
156
157 #define isdigit_l _isdigit_l
158 #define isxdigit_l _isxdigit_l
159 #define strcoll_l _strcoll_l
160 #define strxfrm_l _strxfrm_l
161 #define wcscoll_l _wcscoll_l
162 #define wcsxfrm_l _wcsxfrm_l
163 #define toupper_l _toupper_l
164 #define tolower_l _tolower_l
165 #define iswspace_l _iswspace_l
166 #define iswprint_l _iswprint_l
167 #define iswcntrl_l _iswcntrl_l
168 #define iswupper_l _iswupper_l
169 #define iswlower_l _iswlower_l
170 #define iswalpha_l _iswalpha_l
171 #define iswdigit_l _iswdigit_l
172 #define iswpunct_l _iswpunct_l
173 #define iswxdigit_l _iswxdigit_l
174 #define towupper_l _towupper_l
175 #define towlower_l _towlower_l
176 #if defined(__MINGW32__) && __MSVCRT_VERSION__ < 0x0800
177 #define strftime_l( __s, __l, __f, __tm, __loc ) strftime( __s, __l, __f, __tm )
178 #else
179 #define strftime_l _strftime_l
180 #endif
181 #define sscanf_l( __s, __l, __f, ...) _sscanf_l( __s, __f, __l, __VA_ARGS__ )
182 #define sprintf_l( __s, __l, __f, ... ) _sprintf_l( __s, __f, __l, __VA_ARGS__ )
183 #define vsprintf_l( __s, __l, __f, ... ) _vsprintf_l( __s, __f, __l, __VA_ARGS__ )
184 #define vsnprintf_l( __s, __n, __l, __f, ... ) _vsnprintf_l( __s, __n, __f, __l, __VA_ARGS__ )
185 _LIBCPP_FUNC_VIS int snprintf_l(char *ret, size_t n, locale_t loc, const char *format, ...);
186 _LIBCPP_FUNC_VIS int asprintf_l( char **ret, locale_t loc, const char *format, ... );
187 _LIBCPP_FUNC_VIS int vasprintf_l( char **ret, locale_t loc, const char *format, va_list ap );
188
189 // not-so-pressing FIXME: use locale to determine blank characters
isblank_l(int c,locale_t)190 inline int isblank_l( int c, locale_t /*loc*/ )
191 {
192 return ( c == ' ' || c == '\t' );
193 }
iswblank_l(wint_t c,locale_t)194 inline int iswblank_l( wint_t c, locale_t /*loc*/ )
195 {
196 return ( c == L' ' || c == L'\t' );
197 }
198
199 #endif // _LIBCPP_SUPPORT_WIN32_LOCALE_WIN32_H
200