locale_win32.h 6.7 KB

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