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