util.cc 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. fmt::file read_end, write_end;
  13. fmt::file::pipe(read_end, write_end);
  14. write_end.write(file_content, std::strlen(file_content));
  15. write_end.close();
  16. fmt::buffered_file f = read_end.fdopen("r");
  17. if (fp) *fp = f.get();
  18. #else
  19. fmt::buffered_file f("test-file", "w");
  20. fputs(file_content, f.get());
  21. if (fp) *fp = f.get();
  22. #endif
  23. return f;
  24. }
  25. std::locale do_get_locale(const char* name) {
  26. try {
  27. return std::locale(name);
  28. } catch (const std::runtime_error&) {
  29. }
  30. return std::locale::classic();
  31. }
  32. std::locale get_locale(const char* name, const char* alt_name) {
  33. auto loc = do_get_locale(name);
  34. if (loc == std::locale::classic() && alt_name) {
  35. loc = do_get_locale(alt_name);
  36. }
  37. if (loc == std::locale::classic())
  38. fmt::print(stderr, "{} locale is missing.\n", name);
  39. return loc;
  40. }