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