]> git.proxmox.com Git - ceph.git/blame - ceph/src/fmt/test/util.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / fmt / test / util.h
CommitLineData
11fdf7f2
TL
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>
20effc67 10#include <locale>
11fdf7f2
TL
11#include <string>
12
1e59de90
TL
13#ifdef FMT_MODULE_TEST
14import fmt;
15#else
16# include "fmt/os.h"
17#endif // FMT_MODULE_TEST
11fdf7f2 18
11fdf7f2 19#ifdef _MSC_VER
9f95a23c 20# define FMT_VSNPRINTF vsprintf_s
11fdf7f2 21#else
9f95a23c 22# define FMT_VSNPRINTF vsnprintf
11fdf7f2
TL
23#endif
24
f67539c2 25template <size_t SIZE>
9f95a23c 26void safe_sprintf(char (&buffer)[SIZE], const char* format, ...) {
11fdf7f2
TL
27 std::va_list args;
28 va_start(args, format);
29 FMT_VSNPRINTF(buffer, SIZE, format, args);
30 va_end(args);
31}
32
20effc67 33extern const char* const file_content;
11fdf7f2
TL
34
35// Opens a buffered file for reading.
f67539c2 36fmt::buffered_file open_buffered_file(FILE** fp = nullptr);
11fdf7f2 37
9f95a23c 38inline FILE* safe_fopen(const char* filename, const char* mode) {
11fdf7f2
TL
39#if defined(_WIN32) && !defined(__MINGW32__)
40 // Fix MSVC warning about "unsafe" fopen.
1e59de90 41 FILE* f = nullptr;
11fdf7f2
TL
42 errno = fopen_s(&f, filename, mode);
43 return f;
44#else
45 return std::fopen(filename, mode);
46#endif
47}
48
20effc67 49template <typename Char> class basic_test_string {
11fdf7f2
TL
50 private:
51 std::basic_string<Char> value_;
52
20effc67 53 static const Char empty[];
11fdf7f2
TL
54
55 public:
20effc67 56 explicit basic_test_string(const Char* value = empty) : value_(value) {}
11fdf7f2 57
9f95a23c 58 const std::basic_string<Char>& value() const { return value_; }
11fdf7f2
TL
59};
60
20effc67 61template <typename Char> const Char basic_test_string<Char>::empty[] = {0};
11fdf7f2 62
20effc67
TL
63typedef basic_test_string<char> test_string;
64typedef basic_test_string<wchar_t> test_wstring;
11fdf7f2
TL
65
66template <typename Char>
9f95a23c 67std::basic_ostream<Char>& operator<<(std::basic_ostream<Char>& os,
20effc67 68 const basic_test_string<Char>& s) {
11fdf7f2
TL
69 os << s.value();
70 return os;
71}
72
20effc67 73class date {
11fdf7f2 74 int year_, month_, day_;
9f95a23c 75
11fdf7f2 76 public:
20effc67 77 date(int year, int month, int day) : year_(year), month_(month), day_(day) {}
11fdf7f2
TL
78
79 int year() const { return year_; }
80 int month() const { return month_; }
81 int day() const { return day_; }
82};
20effc67
TL
83
84// Returns a locale with the given name if available or classic locale othewise.
85std::locale get_locale(const char* name, const char* alt_name = nullptr);