util.cc 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Formatting library for C++ - test utilities
  2. //
  3. // Copyright (c) 2012 - present, Victor Zverovich
  4. // All rights reserved.
  5. //
  6. // For the license information refer to format.h.
  7. #include "util.h"
  8. #include <cstring>
  9. const char* const file_content = "Don't panic!";
  10. fmt::buffered_file open_buffered_file(FILE** fp) {
  11. #if FMT_USE_FCNTL
  12. auto pipe = fmt::pipe();
  13. pipe.write_end.write(file_content, std::strlen(file_content));
  14. pipe.write_end.close();
  15. fmt::buffered_file f = pipe.read_end.fdopen("r");
  16. if (fp) *fp = f.get();
  17. #else
  18. fmt::buffered_file f("test-file", "w");
  19. fputs(file_content, f.get());
  20. if (fp) *fp = f.get();
  21. #endif
  22. return f;
  23. }
  24. std::locale do_get_locale(const char* name) {
  25. try {
  26. return std::locale(name);
  27. } catch (const std::runtime_error&) {
  28. }
  29. return std::locale::classic();
  30. }
  31. std::locale get_locale(const char* name, const char* alt_name) {
  32. auto loc = do_get_locale(name);
  33. if (loc == std::locale::classic() && alt_name) loc = do_get_locale(alt_name);
  34. #ifdef __OpenBSD__
  35. // Locales are not working in OpenBSD:
  36. // https://github.com/fmtlib/fmt/issues/3670.
  37. loc = std::locale::classic();
  38. #endif
  39. if (loc == std::locale::classic())
  40. fmt::print(stderr, "{} locale is missing.\n", name);
  41. return loc;
  42. }