__bsd_locale_fallbacks.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. // The BSDs have lots of *_l functions. This file provides reimplementations
  10. // of those functions for non-BSD platforms.
  11. //===----------------------------------------------------------------------===//
  12. #ifndef _LIBCPP___BSD_LOCALE_FALLBACKS_H
  13. #define _LIBCPP___BSD_LOCALE_FALLBACKS_H
  14. #include <stdarg.h>
  15. #include <stdlib.h>
  16. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  17. # pragma GCC system_header
  18. #endif
  19. _LIBCPP_BEGIN_NAMESPACE_STD
  20. inline _LIBCPP_INLINE_VISIBILITY
  21. decltype(MB_CUR_MAX) __libcpp_mb_cur_max_l(locale_t __l)
  22. {
  23. __libcpp_locale_guard __current(__l);
  24. return MB_CUR_MAX;
  25. }
  26. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  27. inline _LIBCPP_INLINE_VISIBILITY
  28. wint_t __libcpp_btowc_l(int __c, locale_t __l)
  29. {
  30. __libcpp_locale_guard __current(__l);
  31. return btowc(__c);
  32. }
  33. inline _LIBCPP_INLINE_VISIBILITY
  34. int __libcpp_wctob_l(wint_t __c, locale_t __l)
  35. {
  36. __libcpp_locale_guard __current(__l);
  37. return wctob(__c);
  38. }
  39. inline _LIBCPP_INLINE_VISIBILITY
  40. size_t __libcpp_wcsnrtombs_l(char *__dest, const wchar_t **__src, size_t __nwc,
  41. size_t __len, mbstate_t *__ps, locale_t __l)
  42. {
  43. __libcpp_locale_guard __current(__l);
  44. return wcsnrtombs(__dest, __src, __nwc, __len, __ps);
  45. }
  46. inline _LIBCPP_INLINE_VISIBILITY
  47. size_t __libcpp_wcrtomb_l(char *__s, wchar_t __wc, mbstate_t *__ps, locale_t __l)
  48. {
  49. __libcpp_locale_guard __current(__l);
  50. return wcrtomb(__s, __wc, __ps);
  51. }
  52. inline _LIBCPP_INLINE_VISIBILITY
  53. size_t __libcpp_mbsnrtowcs_l(wchar_t * __dest, const char **__src, size_t __nms,
  54. size_t __len, mbstate_t *__ps, locale_t __l)
  55. {
  56. __libcpp_locale_guard __current(__l);
  57. return mbsnrtowcs(__dest, __src, __nms, __len, __ps);
  58. }
  59. inline _LIBCPP_INLINE_VISIBILITY
  60. size_t __libcpp_mbrtowc_l(wchar_t *__pwc, const char *__s, size_t __n,
  61. mbstate_t *__ps, locale_t __l)
  62. {
  63. __libcpp_locale_guard __current(__l);
  64. return mbrtowc(__pwc, __s, __n, __ps);
  65. }
  66. inline _LIBCPP_INLINE_VISIBILITY
  67. int __libcpp_mbtowc_l(wchar_t *__pwc, const char *__pmb, size_t __max, locale_t __l)
  68. {
  69. __libcpp_locale_guard __current(__l);
  70. return mbtowc(__pwc, __pmb, __max);
  71. }
  72. inline _LIBCPP_INLINE_VISIBILITY
  73. size_t __libcpp_mbrlen_l(const char *__s, size_t __n, mbstate_t *__ps, locale_t __l)
  74. {
  75. __libcpp_locale_guard __current(__l);
  76. return mbrlen(__s, __n, __ps);
  77. }
  78. #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
  79. inline _LIBCPP_INLINE_VISIBILITY
  80. lconv *__libcpp_localeconv_l(locale_t __l)
  81. {
  82. __libcpp_locale_guard __current(__l);
  83. return localeconv();
  84. }
  85. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  86. inline _LIBCPP_INLINE_VISIBILITY
  87. size_t __libcpp_mbsrtowcs_l(wchar_t *__dest, const char **__src, size_t __len,
  88. mbstate_t *__ps, locale_t __l)
  89. {
  90. __libcpp_locale_guard __current(__l);
  91. return mbsrtowcs(__dest, __src, __len, __ps);
  92. }
  93. #endif
  94. inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 4, 5)
  95. int __libcpp_snprintf_l(char *__s, size_t __n, locale_t __l, const char *__format, ...) {
  96. va_list __va;
  97. va_start(__va, __format);
  98. __libcpp_locale_guard __current(__l);
  99. int __res = vsnprintf(__s, __n, __format, __va);
  100. va_end(__va);
  101. return __res;
  102. }
  103. inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 3, 4)
  104. int __libcpp_asprintf_l(char **__s, locale_t __l, const char *__format, ...) {
  105. va_list __va;
  106. va_start(__va, __format);
  107. __libcpp_locale_guard __current(__l);
  108. int __res = vasprintf(__s, __format, __va);
  109. va_end(__va);
  110. return __res;
  111. }
  112. inline _LIBCPP_ATTRIBUTE_FORMAT(__scanf__, 3, 4)
  113. int __libcpp_sscanf_l(const char *__s, locale_t __l, const char *__format, ...) {
  114. va_list __va;
  115. va_start(__va, __format);
  116. __libcpp_locale_guard __current(__l);
  117. int __res = vsscanf(__s, __format, __va);
  118. va_end(__va);
  119. return __res;
  120. }
  121. _LIBCPP_END_NAMESPACE_STD
  122. #endif // _LIBCPP___BSD_LOCALE_FALLBACKS_H