Mercurial > minori
comparison dep/fmt/test/util.h @ 343:1faa72660932
*: transfer back to cmake from autotools
autotools just made lots of things more complicated than
they should have and many things broke (i.e. translations)
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Thu, 20 Jun 2024 05:56:06 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
342:adb79bdde329 | 343:1faa72660932 |
---|---|
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 <cstdarg> | |
9 #include <cstdio> | |
10 #include <locale> | |
11 #include <string> | |
12 | |
13 #include "fmt/os.h" | |
14 | |
15 #ifdef _MSC_VER | |
16 # define FMT_VSNPRINTF vsprintf_s | |
17 #else | |
18 # define FMT_VSNPRINTF vsnprintf | |
19 #endif | |
20 | |
21 template <size_t SIZE> | |
22 void safe_sprintf(char (&buffer)[SIZE], const char* format, ...) { | |
23 std::va_list args; | |
24 va_start(args, format); | |
25 FMT_VSNPRINTF(buffer, SIZE, format, args); | |
26 va_end(args); | |
27 } | |
28 | |
29 extern const char* const file_content; | |
30 | |
31 // Opens a buffered file for reading. | |
32 auto open_buffered_file(FILE** fp = nullptr) -> fmt::buffered_file; | |
33 | |
34 inline auto safe_fopen(const char* filename, const char* mode) -> FILE* { | |
35 #if defined(_WIN32) && !defined(__MINGW32__) | |
36 // Fix MSVC warning about "unsafe" fopen. | |
37 FILE* f = nullptr; | |
38 errno = fopen_s(&f, filename, mode); | |
39 return f; | |
40 #else | |
41 return std::fopen(filename, mode); | |
42 #endif | |
43 } | |
44 | |
45 template <typename Char> class basic_test_string { | |
46 private: | |
47 std::basic_string<Char> value_; | |
48 | |
49 static const Char empty[]; | |
50 | |
51 public: | |
52 explicit basic_test_string(const Char* value = empty) : value_(value) {} | |
53 | |
54 auto value() const -> const std::basic_string<Char>& { return value_; } | |
55 }; | |
56 | |
57 template <typename Char> const Char basic_test_string<Char>::empty[] = {0}; | |
58 | |
59 using test_string = basic_test_string<char>; | |
60 using test_wstring = basic_test_string<wchar_t>; | |
61 | |
62 template <typename Char> | |
63 auto operator<<(std::basic_ostream<Char>& os, const basic_test_string<Char>& s) | |
64 -> std::basic_ostream<Char>& { | |
65 os << s.value(); | |
66 return os; | |
67 } | |
68 | |
69 class date { | |
70 int year_, month_, day_; | |
71 | |
72 public: | |
73 date(int year, int month, int day) : year_(year), month_(month), day_(day) {} | |
74 | |
75 auto year() const -> int { return year_; } | |
76 auto month() const -> int { return month_; } | |
77 auto day() const -> int { return day_; } | |
78 }; | |
79 | |
80 // Returns a locale with the given name if available or classic locale | |
81 // otherwise. | |
82 auto get_locale(const char* name, const char* alt_name = nullptr) | |
83 -> std::locale; |