xref: /aosp_15_r20/external/fmtlib/test/util.cc (revision 5c90c05cd622c0a81b57953a4d343e0e489f2e08)
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 
8 #include "util.h"
9 
10 #include <cstring>
11 
12 const char* const file_content = "Don't panic!";
13 
open_buffered_file(FILE ** fp)14 fmt::buffered_file open_buffered_file(FILE** fp) {
15 #if FMT_USE_FCNTL
16   auto pipe = fmt::pipe();
17   pipe.write_end.write(file_content, std::strlen(file_content));
18   pipe.write_end.close();
19   fmt::buffered_file f = pipe.read_end.fdopen("r");
20   if (fp) *fp = f.get();
21 #else
22   fmt::buffered_file f("test-file", "w");
23   fputs(file_content, f.get());
24   if (fp) *fp = f.get();
25 #endif
26   return f;
27 }
28 
do_get_locale(const char * name)29 std::locale do_get_locale(const char* name) {
30   try {
31     return std::locale(name);
32   } catch (const std::runtime_error&) {
33   }
34   return std::locale::classic();
35 }
36 
get_locale(const char * name,const char * alt_name)37 std::locale get_locale(const char* name, const char* alt_name) {
38   auto loc = do_get_locale(name);
39   if (loc == std::locale::classic() && alt_name) loc = do_get_locale(alt_name);
40 #ifdef __OpenBSD__
41   // Locales are not working in OpenBSD:
42   // https://github.com/fmtlib/fmt/issues/3670.
43   loc = std::locale::classic();
44 #endif
45   if (loc == std::locale::classic())
46     fmt::print(stderr, "{} locale is missing.\n", name);
47   return loc;
48 }
49