xref: /freebsd/contrib/llvm-project/libcxx/include/__locale_dir/locale_base_api/win32.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 // -*- C++ -*-
2 //===-----------------------------------------------------------------------===//
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___LOCALE_LOCALE_BASE_API_WIN32_H
11 #define _LIBCPP___LOCALE_LOCALE_BASE_API_WIN32_H
12 
13 #include <__config>
14 #include <cstddef>
15 #include <locale.h> // _locale_t
16 #include <stdio.h>
17 #include <string>
18 
19 #define _X_ALL LC_ALL
20 #define _X_COLLATE LC_COLLATE
21 #define _X_CTYPE LC_CTYPE
22 #define _X_MONETARY LC_MONETARY
23 #define _X_NUMERIC LC_NUMERIC
24 #define _X_TIME LC_TIME
25 #define _X_MAX LC_MAX
26 #define _X_MESSAGES 6
27 #define _NCAT (_X_MESSAGES + 1)
28 
29 #define _CATMASK(n) ((1 << (n)) >> 1)
30 #define _M_COLLATE _CATMASK(_X_COLLATE)
31 #define _M_CTYPE _CATMASK(_X_CTYPE)
32 #define _M_MONETARY _CATMASK(_X_MONETARY)
33 #define _M_NUMERIC _CATMASK(_X_NUMERIC)
34 #define _M_TIME _CATMASK(_X_TIME)
35 #define _M_MESSAGES _CATMASK(_X_MESSAGES)
36 #define _M_ALL (_CATMASK(_NCAT) - 1)
37 
38 #define LC_COLLATE_MASK _M_COLLATE
39 #define LC_CTYPE_MASK _M_CTYPE
40 #define LC_MONETARY_MASK _M_MONETARY
41 #define LC_NUMERIC_MASK _M_NUMERIC
42 #define LC_TIME_MASK _M_TIME
43 #define LC_MESSAGES_MASK _M_MESSAGES
44 #define LC_ALL_MASK                                                                                                    \
45   (LC_COLLATE_MASK | LC_CTYPE_MASK | LC_MESSAGES_MASK | LC_MONETARY_MASK | LC_NUMERIC_MASK | LC_TIME_MASK)
46 
47 class __lconv_storage {
48 public:
__lconv_storage(const lconv * __lc_input)49   __lconv_storage(const lconv* __lc_input) {
50     __lc_ = *__lc_input;
51 
52     __decimal_point_     = __lc_input->decimal_point;
53     __thousands_sep_     = __lc_input->thousands_sep;
54     __grouping_          = __lc_input->grouping;
55     __int_curr_symbol_   = __lc_input->int_curr_symbol;
56     __currency_symbol_   = __lc_input->currency_symbol;
57     __mon_decimal_point_ = __lc_input->mon_decimal_point;
58     __mon_thousands_sep_ = __lc_input->mon_thousands_sep;
59     __mon_grouping_      = __lc_input->mon_grouping;
60     __positive_sign_     = __lc_input->positive_sign;
61     __negative_sign_     = __lc_input->negative_sign;
62 
63     __lc_.decimal_point     = const_cast<char*>(__decimal_point_.c_str());
64     __lc_.thousands_sep     = const_cast<char*>(__thousands_sep_.c_str());
65     __lc_.grouping          = const_cast<char*>(__grouping_.c_str());
66     __lc_.int_curr_symbol   = const_cast<char*>(__int_curr_symbol_.c_str());
67     __lc_.currency_symbol   = const_cast<char*>(__currency_symbol_.c_str());
68     __lc_.mon_decimal_point = const_cast<char*>(__mon_decimal_point_.c_str());
69     __lc_.mon_thousands_sep = const_cast<char*>(__mon_thousands_sep_.c_str());
70     __lc_.mon_grouping      = const_cast<char*>(__mon_grouping_.c_str());
71     __lc_.positive_sign     = const_cast<char*>(__positive_sign_.c_str());
72     __lc_.negative_sign     = const_cast<char*>(__negative_sign_.c_str());
73   }
74 
__get()75   lconv* __get() { return &__lc_; }
76 
77 private:
78   lconv __lc_;
79   std::string __decimal_point_;
80   std::string __thousands_sep_;
81   std::string __grouping_;
82   std::string __int_curr_symbol_;
83   std::string __currency_symbol_;
84   std::string __mon_decimal_point_;
85   std::string __mon_thousands_sep_;
86   std::string __mon_grouping_;
87   std::string __positive_sign_;
88   std::string __negative_sign_;
89 };
90 
91 class locale_t {
92 public:
locale_t()93   locale_t() : __locale_(nullptr), __locale_str_(nullptr), __lc_(nullptr) {}
locale_t(std::nullptr_t)94   locale_t(std::nullptr_t) : __locale_(nullptr), __locale_str_(nullptr), __lc_(nullptr) {}
locale_t(_locale_t __xlocale,const char * __xlocale_str)95   locale_t(_locale_t __xlocale, const char* __xlocale_str)
96       : __locale_(__xlocale), __locale_str_(__xlocale_str), __lc_(nullptr) {}
locale_t(const locale_t & __l)97   locale_t(const locale_t& __l) : __locale_(__l.__locale_), __locale_str_(__l.__locale_str_), __lc_(nullptr) {}
98 
~locale_t()99   ~locale_t() { delete __lc_; }
100 
101   locale_t& operator=(const locale_t& __l) {
102     __locale_     = __l.__locale_;
103     __locale_str_ = __l.__locale_str_;
104     // __lc_ not copied
105     return *this;
106   }
107 
108   friend bool operator==(const locale_t& __left, const locale_t& __right) {
109     return __left.__locale_ == __right.__locale_;
110   }
111 
112   friend bool operator==(const locale_t& __left, int __right) { return __left.__locale_ == nullptr && __right == 0; }
113 
114   friend bool operator==(const locale_t& __left, long long __right) {
115     return __left.__locale_ == nullptr && __right == 0;
116   }
117 
118   friend bool operator==(const locale_t& __left, std::nullptr_t) { return __left.__locale_ == nullptr; }
119 
120   friend bool operator==(int __left, const locale_t& __right) { return __left == 0 && nullptr == __right.__locale_; }
121 
122   friend bool operator==(std::nullptr_t, const locale_t& __right) { return nullptr == __right.__locale_; }
123 
124   friend bool operator!=(const locale_t& __left, const locale_t& __right) { return !(__left == __right); }
125 
126   friend bool operator!=(const locale_t& __left, int __right) { return !(__left == __right); }
127 
128   friend bool operator!=(const locale_t& __left, long long __right) { return !(__left == __right); }
129 
130   friend bool operator!=(const locale_t& __left, std::nullptr_t __right) { return !(__left == __right); }
131 
132   friend bool operator!=(int __left, const locale_t& __right) { return !(__left == __right); }
133 
134   friend bool operator!=(std::nullptr_t __left, const locale_t& __right) { return !(__left == __right); }
135 
136   operator bool() const { return __locale_ != nullptr; }
137 
__get_locale()138   const char* __get_locale() const { return __locale_str_; }
139 
_locale_t()140   operator _locale_t() const { return __locale_; }
141 
__store_lconv(const lconv * __input_lc)142   lconv* __store_lconv(const lconv* __input_lc) {
143     delete __lc_;
144     __lc_ = new __lconv_storage(__input_lc);
145     return __lc_->__get();
146   }
147 
148 private:
149   _locale_t __locale_;
150   const char* __locale_str_;
151   __lconv_storage* __lc_ = nullptr;
152 };
153 
154 // Locale management functions
155 #define freelocale _free_locale
156 // FIXME: base currently unused. Needs manual work to construct the new locale
157 locale_t newlocale(int __mask, const char* __locale, locale_t __base);
158 // uselocale can't be implemented on Windows because Windows allows partial modification
159 // of thread-local locale and so _get_current_locale() returns a copy while uselocale does
160 // not create any copies.
161 // We can still implement raii even without uselocale though.
162 
163 lconv* localeconv_l(locale_t& __loc);
164 size_t mbrlen_l(const char* __restrict __s, size_t __n, mbstate_t* __restrict __ps, locale_t __loc);
165 size_t mbsrtowcs_l(
166     wchar_t* __restrict __dst, const char** __restrict __src, size_t __len, mbstate_t* __restrict __ps, locale_t __loc);
167 size_t wcrtomb_l(char* __restrict __s, wchar_t __wc, mbstate_t* __restrict __ps, locale_t __loc);
168 size_t mbrtowc_l(
169     wchar_t* __restrict __pwc, const char* __restrict __s, size_t __n, mbstate_t* __restrict __ps, locale_t __loc);
170 size_t mbsnrtowcs_l(wchar_t* __restrict __dst,
171                     const char** __restrict __src,
172                     size_t __nms,
173                     size_t __len,
174                     mbstate_t* __restrict __ps,
175                     locale_t __loc);
176 size_t wcsnrtombs_l(char* __restrict __dst,
177                     const wchar_t** __restrict __src,
178                     size_t __nwc,
179                     size_t __len,
180                     mbstate_t* __restrict __ps,
181                     locale_t __loc);
182 wint_t btowc_l(int __c, locale_t __loc);
183 int wctob_l(wint_t __c, locale_t __loc);
184 
185 decltype(MB_CUR_MAX) MB_CUR_MAX_L(locale_t __l);
186 
187 // the *_l functions are prefixed on Windows, only available for msvcr80+, VS2005+
188 #define mbtowc_l _mbtowc_l
189 #define strtoll_l _strtoi64_l
190 #define strtoull_l _strtoui64_l
191 #define strtod_l _strtod_l
192 #if defined(_LIBCPP_MSVCRT)
193 #  define strtof_l _strtof_l
194 #  define strtold_l _strtold_l
195 #else
196 _LIBCPP_EXPORTED_FROM_ABI float strtof_l(const char*, char**, locale_t);
197 _LIBCPP_EXPORTED_FROM_ABI long double strtold_l(const char*, char**, locale_t);
198 #endif
islower_l(int __c,_locale_t __loc)199 inline _LIBCPP_HIDE_FROM_ABI int islower_l(int __c, _locale_t __loc) { return _islower_l((int)__c, __loc); }
200 
isupper_l(int __c,_locale_t __loc)201 inline _LIBCPP_HIDE_FROM_ABI int isupper_l(int __c, _locale_t __loc) { return _isupper_l((int)__c, __loc); }
202 
203 #define isdigit_l _isdigit_l
204 #define isxdigit_l _isxdigit_l
205 #define strcoll_l _strcoll_l
206 #define strxfrm_l _strxfrm_l
207 #define wcscoll_l _wcscoll_l
208 #define wcsxfrm_l _wcsxfrm_l
209 #define toupper_l _toupper_l
210 #define tolower_l _tolower_l
211 #define iswspace_l _iswspace_l
212 #define iswprint_l _iswprint_l
213 #define iswcntrl_l _iswcntrl_l
214 #define iswupper_l _iswupper_l
215 #define iswlower_l _iswlower_l
216 #define iswalpha_l _iswalpha_l
217 #define iswdigit_l _iswdigit_l
218 #define iswpunct_l _iswpunct_l
219 #define iswxdigit_l _iswxdigit_l
220 #define towupper_l _towupper_l
221 #define towlower_l _towlower_l
222 #if defined(__MINGW32__) && __MSVCRT_VERSION__ < 0x0800
223 _LIBCPP_EXPORTED_FROM_ABI size_t strftime_l(char* ret, size_t n, const char* format, const struct tm* tm, locale_t loc);
224 #else
225 #  define strftime_l _strftime_l
226 #endif
227 #define sscanf_l(__s, __l, __f, ...) _sscanf_l(__s, __f, __l, __VA_ARGS__)
228 _LIBCPP_EXPORTED_FROM_ABI int snprintf_l(char* __ret, size_t __n, locale_t __loc, const char* __format, ...);
229 _LIBCPP_EXPORTED_FROM_ABI int asprintf_l(char** __ret, locale_t __loc, const char* __format, ...);
230 _LIBCPP_EXPORTED_FROM_ABI int vasprintf_l(char** __ret, locale_t __loc, const char* __format, va_list __ap);
231 
232 // not-so-pressing FIXME: use locale to determine blank characters
iswblank_l(wint_t __c,locale_t)233 inline int iswblank_l(wint_t __c, locale_t /*loc*/) { return (__c == L' ' || __c == L'\t'); }
234 
235 #endif // _LIBCPP___LOCALE_LOCALE_BASE_API_WIN32_H
236