__bsd_locale_fallbacks.h 4.1 KB

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