]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/fmt/test/util.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / seastar / fmt / test / util.h
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 <string>
11
12 #include "fmt/posix.h"
13
14 enum {BUFFER_SIZE = 256};
15
16 #ifdef _MSC_VER
17 # define FMT_VSNPRINTF vsprintf_s
18 #else
19 # define FMT_VSNPRINTF vsnprintf
20 #endif
21
22 template <std::size_t SIZE>
23 void safe_sprintf(char (&buffer)[SIZE], const char *format, ...) {
24 std::va_list args;
25 va_start(args, format);
26 FMT_VSNPRINTF(buffer, SIZE, format, args);
27 va_end(args);
28 }
29
30 // Increment a number in a string.
31 void increment(char *s);
32
33 std::string get_system_error(int error_code);
34
35 extern const char *const FILE_CONTENT;
36
37 // Opens a buffered file for reading.
38 fmt::buffered_file open_buffered_file(FILE **fp = nullptr);
39
40 inline FILE *safe_fopen(const char *filename, const char *mode) {
41 #if defined(_WIN32) && !defined(__MINGW32__)
42 // Fix MSVC warning about "unsafe" fopen.
43 FILE *f = 0;
44 errno = fopen_s(&f, filename, mode);
45 return f;
46 #else
47 return std::fopen(filename, mode);
48 #endif
49 }
50
51 template <typename Char>
52 class BasicTestString {
53 private:
54 std::basic_string<Char> value_;
55
56 static const Char EMPTY[];
57
58 public:
59 explicit BasicTestString(const Char *value = EMPTY) : value_(value) {}
60
61 const std::basic_string<Char> &value() const { return value_; }
62 };
63
64 template <typename Char>
65 const Char BasicTestString<Char>::EMPTY[] = {0};
66
67 typedef BasicTestString<char> TestString;
68 typedef BasicTestString<wchar_t> TestWString;
69
70 template <typename Char>
71 std::basic_ostream<Char> &operator<<(
72 std::basic_ostream<Char> &os, const BasicTestString<Char> &s) {
73 os << s.value();
74 return os;
75 }
76
77 class Date {
78 int year_, month_, day_;
79 public:
80 Date(int year, int month, int day) : year_(year), month_(month), day_(day) {}
81
82 int year() const { return year_; }
83 int month() const { return month_; }
84 int day() const { return day_; }
85 };