xlocale.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #ifndef _LIBCPP___SUPPORT_IBM_XLOCALE_H
  10. #define _LIBCPP___SUPPORT_IBM_XLOCALE_H
  11. #if defined(__MVS__)
  12. #include <__support/ibm/locale_mgmt_zos.h>
  13. #endif // defined(__MVS__)
  14. #include <stdarg.h>
  15. #include "cstdlib"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #if defined(__MVS__)
  20. #include <wctype.h>
  21. // POSIX routines
  22. #include <__support/xlocale/__posix_l_fallback.h>
  23. #endif // defined(__MVS__)
  24. namespace {
  25. struct __setAndRestore {
  26. explicit __setAndRestore(locale_t locale) {
  27. if (locale == (locale_t)0) {
  28. __cloc = newlocale(LC_ALL_MASK, "C", /* base */ (locale_t)0);
  29. __stored = uselocale(__cloc);
  30. } else {
  31. __stored = uselocale(locale);
  32. }
  33. }
  34. ~__setAndRestore() {
  35. uselocale(__stored);
  36. if (__cloc)
  37. freelocale(__cloc);
  38. }
  39. private:
  40. locale_t __stored = (locale_t)0;
  41. locale_t __cloc = (locale_t)0;
  42. };
  43. } // namespace
  44. // The following are not POSIX routines. These are quick-and-dirty hacks
  45. // to make things pretend to work
  46. inline _LIBCPP_HIDE_FROM_ABI long long
  47. strtoll_l(const char *__nptr, char **__endptr, int __base, locale_t locale) {
  48. __setAndRestore __newloc(locale);
  49. return ::strtoll(__nptr, __endptr, __base);
  50. }
  51. inline _LIBCPP_HIDE_FROM_ABI long
  52. strtol_l(const char *__nptr, char **__endptr, int __base, locale_t locale) {
  53. __setAndRestore __newloc(locale);
  54. return ::strtol(__nptr, __endptr, __base);
  55. }
  56. inline _LIBCPP_HIDE_FROM_ABI double
  57. strtod_l(const char *__nptr, char **__endptr, locale_t locale) {
  58. __setAndRestore __newloc(locale);
  59. return ::strtod(__nptr, __endptr);
  60. }
  61. inline _LIBCPP_HIDE_FROM_ABI float
  62. strtof_l(const char *__nptr, char **__endptr, locale_t locale) {
  63. __setAndRestore __newloc(locale);
  64. return ::strtof(__nptr, __endptr);
  65. }
  66. inline _LIBCPP_HIDE_FROM_ABI long double
  67. strtold_l(const char *__nptr, char **__endptr, locale_t locale) {
  68. __setAndRestore __newloc(locale);
  69. return ::strtold(__nptr, __endptr);
  70. }
  71. inline _LIBCPP_HIDE_FROM_ABI unsigned long long
  72. strtoull_l(const char *__nptr, char **__endptr, int __base, locale_t locale) {
  73. __setAndRestore __newloc(locale);
  74. return ::strtoull(__nptr, __endptr, __base);
  75. }
  76. inline _LIBCPP_HIDE_FROM_ABI unsigned long
  77. strtoul_l(const char *__nptr, char **__endptr, int __base, locale_t locale) {
  78. __setAndRestore __newloc(locale);
  79. return ::strtoul(__nptr, __endptr, __base);
  80. }
  81. inline _LIBCPP_HIDE_FROM_ABI int
  82. vasprintf(char **strp, const char *fmt, va_list ap) {
  83. const size_t buff_size = 256;
  84. if ((*strp = (char *)malloc(buff_size)) == NULL) {
  85. return -1;
  86. }
  87. va_list ap_copy;
  88. // va_copy may not be provided by the C library in C++ 03 mode.
  89. #if defined(_LIBCPP_CXX03_LANG) && __has_builtin(__builtin_va_copy)
  90. __builtin_va_copy(ap_copy, ap);
  91. #else
  92. va_copy(ap_copy, ap);
  93. #endif
  94. int str_size = vsnprintf(*strp, buff_size, fmt, ap_copy);
  95. va_end(ap_copy);
  96. if ((size_t) str_size >= buff_size) {
  97. if ((*strp = (char *)realloc(*strp, str_size + 1)) == NULL) {
  98. return -1;
  99. }
  100. str_size = vsnprintf(*strp, str_size + 1, fmt, ap);
  101. }
  102. return str_size;
  103. }
  104. #ifdef __cplusplus
  105. }
  106. #endif
  107. #endif // _LIBCPP___SUPPORT_IBM_XLOCALE_H