__bsd_locale_fallbacks.h 4.2 KB

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