platform_support.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // Define a bunch of macros that can be used in the tests instead of
  9. // implementation defined assumptions:
  10. // - locale names
  11. // - floating point number string output
  12. #ifndef PLATFORM_SUPPORT_H
  13. #define PLATFORM_SUPPORT_H
  14. // locale names
  15. #ifdef _WIN32
  16. // WARNING: Windows does not support UTF-8 codepages.
  17. // Locales are "converted" using https://docs.moodle.org/dev/Table_of_locales
  18. #define LOCALE_en_US "en-US"
  19. #define LOCALE_en_US_UTF_8 "en-US"
  20. #define LOCALE_cs_CZ_ISO8859_2 "cs-CZ"
  21. #define LOCALE_fr_FR_UTF_8 "fr-FR"
  22. #define LOCALE_fr_CA_ISO8859_1 "fr-CA"
  23. #define LOCALE_ru_RU_UTF_8 "ru-RU"
  24. #define LOCALE_zh_CN_UTF_8 "zh-CN"
  25. #elif defined(__CloudABI__)
  26. // Timezones are integrated into locales through LC_TIMEZONE_MASK on
  27. // CloudABI. LC_ALL_MASK can only be used if a timezone has also been
  28. // provided. UTC should be all right.
  29. #define LOCALE_en_US "en_US"
  30. #define LOCALE_en_US_UTF_8 "en_US.UTF-8@UTC"
  31. #define LOCALE_fr_FR_UTF_8 "fr_FR.UTF-8@UTC"
  32. #define LOCALE_fr_CA_ISO8859_1 "fr_CA.ISO-8859-1@UTC"
  33. #define LOCALE_cs_CZ_ISO8859_2 "cs_CZ.ISO-8859-2@UTC"
  34. #define LOCALE_ru_RU_UTF_8 "ru_RU.UTF-8@UTC"
  35. #define LOCALE_zh_CN_UTF_8 "zh_CN.UTF-8@UTC"
  36. #else
  37. #define LOCALE_en_US "en_US"
  38. #define LOCALE_en_US_UTF_8 "en_US.UTF-8"
  39. #define LOCALE_fr_FR_UTF_8 "fr_FR.UTF-8"
  40. #ifdef __linux__
  41. #define LOCALE_fr_CA_ISO8859_1 "fr_CA.ISO-8859-1"
  42. #define LOCALE_cs_CZ_ISO8859_2 "cs_CZ.ISO-8859-2"
  43. #else
  44. #define LOCALE_fr_CA_ISO8859_1 "fr_CA.ISO8859-1"
  45. #define LOCALE_cs_CZ_ISO8859_2 "cs_CZ.ISO8859-2"
  46. #endif
  47. #define LOCALE_ru_RU_UTF_8 "ru_RU.UTF-8"
  48. #define LOCALE_zh_CN_UTF_8 "zh_CN.UTF-8"
  49. #endif
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <codecvt>
  53. #include <locale>
  54. #include <string>
  55. #if defined(_WIN32) || defined(__MINGW32__)
  56. #include <io.h> // _mktemp_s
  57. #else
  58. #include <unistd.h> // close
  59. #endif
  60. #if defined(_NEWLIB_VERSION) && defined(__STRICT_ANSI__)
  61. // Newlib provides this, but in the header it's under __STRICT_ANSI__
  62. extern "C" {
  63. int mkstemp(char*);
  64. }
  65. #endif
  66. #ifndef __CloudABI__
  67. inline
  68. std::string get_temp_file_name()
  69. {
  70. #if defined(__MINGW32__)
  71. char Path[MAX_PATH + 1];
  72. char FN[MAX_PATH + 1];
  73. do { } while (0 == GetTempPath(MAX_PATH+1, Path));
  74. do { } while (0 == GetTempFileName(Path, "libcxx", 0, FN));
  75. return FN;
  76. #elif defined(_WIN32)
  77. char Name[] = "libcxx.XXXXXX";
  78. if (_mktemp_s(Name, sizeof(Name)) != 0) abort();
  79. return Name;
  80. #else
  81. std::string Name;
  82. int FD = -1;
  83. do {
  84. Name = "libcxx.XXXXXX";
  85. FD = mkstemp(&Name[0]);
  86. if (FD == -1 && errno == EINVAL) {
  87. perror("mkstemp");
  88. abort();
  89. }
  90. } while (FD == -1);
  91. close(FD);
  92. return Name;
  93. #endif
  94. }
  95. #ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
  96. inline
  97. std::wstring get_wide_temp_file_name()
  98. {
  99. return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t> >().from_bytes(
  100. get_temp_file_name());
  101. }
  102. #endif // _LIBCPP_HAS_OPEN_WITH_WCHAR
  103. #endif // __CloudABI__
  104. #endif // PLATFORM_SUPPORT_H