| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
1 |
// Formatting library for C++ - optional OS-specific functionality |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
2 |
// |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
3 |
// Copyright (c) 2012 - 2016, Victor Zverovich |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
4 |
// All rights reserved. |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
5 |
// |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
6 |
// For the license information refer to format.h. |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
7 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
8 |
// Disable bogus MSVC warnings. |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
9 |
#if !defined(_CRT_SECURE_NO_WARNINGS) && defined(_MSC_VER) |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
10 |
# define _CRT_SECURE_NO_WARNINGS |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
11 |
#endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
12 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
13 |
#include "fmt/os.h" |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
14 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
15 |
#ifndef FMT_MODULE |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
16 |
# include <climits> |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
17 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
18 |
# if FMT_USE_FCNTL |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
19 |
# include <sys/stat.h> |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
20 |
# include <sys/types.h> |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
21 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
22 |
# ifdef _WRS_KERNEL // VxWorks7 kernel |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
23 |
# include <ioLib.h> // getpagesize |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
24 |
# endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
25 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
26 |
# ifndef _WIN32 |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
27 |
# include <unistd.h> |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
28 |
# else |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
29 |
# ifndef WIN32_LEAN_AND_MEAN |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
30 |
# define WIN32_LEAN_AND_MEAN |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
31 |
# endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
32 |
# include <io.h> |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
33 |
# endif // _WIN32 |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
34 |
# endif // FMT_USE_FCNTL |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
35 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
36 |
# ifdef _WIN32 |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
37 |
# include <windows.h> |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
38 |
# endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
39 |
#endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
40 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
41 |
#ifdef _WIN32 |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
42 |
# ifndef S_IRUSR |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
43 |
# define S_IRUSR _S_IREAD |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
44 |
# endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
45 |
# ifndef S_IWUSR |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
46 |
# define S_IWUSR _S_IWRITE |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
47 |
# endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
48 |
# ifndef S_IRGRP |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
49 |
# define S_IRGRP 0 |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
50 |
# endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
51 |
# ifndef S_IWGRP |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
52 |
# define S_IWGRP 0 |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
53 |
# endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
54 |
# ifndef S_IROTH |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
55 |
# define S_IROTH 0 |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
56 |
# endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
57 |
# ifndef S_IWOTH |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
58 |
# define S_IWOTH 0 |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
59 |
# endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
60 |
#endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
61 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
62 |
namespace { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
63 |
#ifdef _WIN32 |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
64 |
// Return type of read and write functions. |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
65 |
using rwresult = int; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
66 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
67 |
// On Windows the count argument to read and write is unsigned, so convert |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
68 |
// it from size_t preventing integer overflow. |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
69 |
inline unsigned convert_rwcount(std::size_t count) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
70 |
return count <= UINT_MAX ? static_cast<unsigned>(count) : UINT_MAX; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
71 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
72 |
#elif FMT_USE_FCNTL |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
73 |
// Return type of read and write functions. |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
74 |
using rwresult = ssize_t; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
75 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
76 |
inline std::size_t convert_rwcount(std::size_t count) { return count; } |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
77 |
#endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
78 |
} // namespace |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
79 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
80 |
FMT_BEGIN_NAMESPACE |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
81 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
82 |
#ifdef _WIN32 |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
83 |
namespace detail { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
84 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
85 |
class system_message { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
86 |
system_message(const system_message&) = delete; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
87 |
void operator=(const system_message&) = delete; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
88 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
89 |
unsigned long result_; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
90 |
wchar_t* message_; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
91 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
92 |
static bool is_whitespace(wchar_t c) noexcept { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
93 |
return c == L' ' || c == L'\n' || c == L'\r' || c == L'\t' || c == L'\0'; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
94 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
95 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
96 |
public: |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
97 |
explicit system_message(unsigned long error_code) |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
98 |
: result_(0), message_(nullptr) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
99 |
result_ = FormatMessageW( |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
100 |
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
101 |
FORMAT_MESSAGE_IGNORE_INSERTS, |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
102 |
nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
103 |
reinterpret_cast<wchar_t*>(&message_), 0, nullptr); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
104 |
if (result_ != 0) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
105 |
while (result_ != 0 && is_whitespace(message_[result_ - 1])) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
106 |
--result_; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
107 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
108 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
109 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
110 |
~system_message() { LocalFree(message_); } |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
111 |
explicit operator bool() const noexcept { return result_ != 0; } |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
112 |
operator basic_string_view<wchar_t>() const noexcept { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
113 |
return basic_string_view<wchar_t>(message_, result_); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
114 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
115 |
}; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
116 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
117 |
class utf8_system_category final : public std::error_category { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
118 |
public: |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
119 |
const char* name() const noexcept override { return "system"; } |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
120 |
std::string message(int error_code) const override { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
121 |
auto&& msg = system_message(error_code); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
122 |
if (msg) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
123 |
auto utf8_message = to_utf8<wchar_t>(); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
124 |
if (utf8_message.convert(msg)) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
125 |
return utf8_message.str(); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
126 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
127 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
128 |
return "unknown error"; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
129 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
130 |
}; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
131 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
132 |
} // namespace detail |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
133 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
134 |
FMT_API const std::error_category& system_category() noexcept { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
135 |
static const detail::utf8_system_category category; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
136 |
return category; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
137 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
138 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
139 |
std::system_error vwindows_error(int err_code, string_view format_str, |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
140 |
format_args args) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
141 |
auto ec = std::error_code(err_code, system_category()); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
142 |
return std::system_error(ec, vformat(format_str, args)); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
143 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
144 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
145 |
void detail::format_windows_error(detail::buffer<char>& out, int error_code, |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
146 |
const char* message) noexcept { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
147 |
FMT_TRY { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
148 |
auto&& msg = system_message(error_code); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
149 |
if (msg) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
150 |
auto utf8_message = to_utf8<wchar_t>(); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
151 |
if (utf8_message.convert(msg)) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
152 |
fmt::format_to(appender(out), FMT_STRING("{}: {}"), message, |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
153 |
string_view(utf8_message)); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
154 |
return; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
155 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
156 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
157 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
158 |
FMT_CATCH(...) {} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
159 |
format_error_code(out, error_code, message); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
160 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
161 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
162 |
void report_windows_error(int error_code, const char* message) noexcept { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
163 |
report_error(detail::format_windows_error, error_code, message); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
164 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
165 |
#endif // _WIN32 |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
166 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
167 |
buffered_file::~buffered_file() noexcept { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
168 |
if (file_ && FMT_SYSTEM(fclose(file_)) != 0) |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
169 |
report_system_error(errno, "cannot close file"); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
170 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
171 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
172 |
buffered_file::buffered_file(cstring_view filename, cstring_view mode) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
173 |
FMT_RETRY_VAL(file_, FMT_SYSTEM(fopen(filename.c_str(), mode.c_str())), |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
174 |
nullptr); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
175 |
if (!file_) |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
176 |
FMT_THROW(system_error(errno, FMT_STRING("cannot open file {}"), |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
177 |
filename.c_str())); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
178 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
179 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
180 |
void buffered_file::close() { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
181 |
if (!file_) return; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
182 |
int result = FMT_SYSTEM(fclose(file_)); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
183 |
file_ = nullptr; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
184 |
if (result != 0) |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
185 |
FMT_THROW(system_error(errno, FMT_STRING("cannot close file"))); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
186 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
187 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
188 |
int buffered_file::descriptor() const { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
189 |
#ifdef FMT_HAS_SYSTEM |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
190 |
// fileno is a macro on OpenBSD. |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
191 |
# ifdef fileno |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
192 |
# undef fileno |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
193 |
# endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
194 |
int fd = FMT_POSIX_CALL(fileno(file_)); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
195 |
#elif defined(_WIN32) |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
196 |
int fd = _fileno(file_); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
197 |
#else |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
198 |
int fd = fileno(file_); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
199 |
#endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
200 |
if (fd == -1) |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
201 |
FMT_THROW(system_error(errno, FMT_STRING("cannot get file descriptor"))); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
202 |
return fd; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
203 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
204 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
205 |
#if FMT_USE_FCNTL |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
206 |
# ifdef _WIN32 |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
207 |
using mode_t = int; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
208 |
# endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
209 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
210 |
constexpr mode_t default_open_mode = |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
211 |
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
212 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
213 |
file::file(cstring_view path, int oflag) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
214 |
# if defined(_WIN32) && !defined(__MINGW32__) |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
215 |
fd_ = -1; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
216 |
auto converted = detail::utf8_to_utf16(string_view(path.c_str())); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
217 |
*this = file::open_windows_file(converted.c_str(), oflag); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
218 |
# else |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
219 |
FMT_RETRY(fd_, FMT_POSIX_CALL(open(path.c_str(), oflag, default_open_mode))); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
220 |
if (fd_ == -1) |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
221 |
FMT_THROW( |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
222 |
system_error(errno, FMT_STRING("cannot open file {}"), path.c_str())); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
223 |
# endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
224 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
225 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
226 |
file::~file() noexcept { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
227 |
// Don't retry close in case of EINTR! |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
228 |
// See http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
229 |
if (fd_ != -1 && FMT_POSIX_CALL(close(fd_)) != 0) |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
230 |
report_system_error(errno, "cannot close file"); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
231 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
232 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
233 |
void file::close() { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
234 |
if (fd_ == -1) return; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
235 |
// Don't retry close in case of EINTR! |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
236 |
// See http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
237 |
int result = FMT_POSIX_CALL(close(fd_)); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
238 |
fd_ = -1; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
239 |
if (result != 0) |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
240 |
FMT_THROW(system_error(errno, FMT_STRING("cannot close file"))); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
241 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
242 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
243 |
long long file::size() const { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
244 |
# ifdef _WIN32 |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
245 |
// Use GetFileSize instead of GetFileSizeEx for the case when _WIN32_WINNT |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
246 |
// is less than 0x0500 as is the case with some default MinGW builds. |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
247 |
// Both functions support large file sizes. |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
248 |
DWORD size_upper = 0; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
249 |
HANDLE handle = reinterpret_cast<HANDLE>(_get_osfhandle(fd_)); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
250 |
DWORD size_lower = FMT_SYSTEM(GetFileSize(handle, &size_upper)); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
251 |
if (size_lower == INVALID_FILE_SIZE) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
252 |
DWORD error = GetLastError(); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
253 |
if (error != NO_ERROR) |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
254 |
FMT_THROW(windows_error(GetLastError(), "cannot get file size")); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
255 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
256 |
unsigned long long long_size = size_upper; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
257 |
return (long_size << sizeof(DWORD) * CHAR_BIT) | size_lower; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
258 |
# else |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
259 |
using Stat = struct stat; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
260 |
Stat file_stat = Stat(); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
261 |
if (FMT_POSIX_CALL(fstat(fd_, &file_stat)) == -1) |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
262 |
FMT_THROW(system_error(errno, FMT_STRING("cannot get file attributes"))); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
263 |
static_assert(sizeof(long long) >= sizeof(file_stat.st_size), |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
264 |
"return type of file::size is not large enough"); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
265 |
return file_stat.st_size; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
266 |
# endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
267 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
268 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
269 |
std::size_t file::read(void* buffer, std::size_t count) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
270 |
rwresult result = 0; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
271 |
FMT_RETRY(result, FMT_POSIX_CALL(read(fd_, buffer, convert_rwcount(count)))); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
272 |
if (result < 0) |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
273 |
FMT_THROW(system_error(errno, FMT_STRING("cannot read from file"))); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
274 |
return detail::to_unsigned(result); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
275 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
276 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
277 |
std::size_t file::write(const void* buffer, std::size_t count) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
278 |
rwresult result = 0; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
279 |
FMT_RETRY(result, FMT_POSIX_CALL(write(fd_, buffer, convert_rwcount(count)))); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
280 |
if (result < 0) |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
281 |
FMT_THROW(system_error(errno, FMT_STRING("cannot write to file"))); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
282 |
return detail::to_unsigned(result); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
283 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
284 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
285 |
file file::dup(int fd) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
286 |
// Don't retry as dup doesn't return EINTR. |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
287 |
// http://pubs.opengroup.org/onlinepubs/009695399/functions/dup.html |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
288 |
int new_fd = FMT_POSIX_CALL(dup(fd)); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
289 |
if (new_fd == -1) |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
290 |
FMT_THROW(system_error( |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
291 |
errno, FMT_STRING("cannot duplicate file descriptor {}"), fd)); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
292 |
return file(new_fd); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
293 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
294 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
295 |
void file::dup2(int fd) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
296 |
int result = 0; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
297 |
FMT_RETRY(result, FMT_POSIX_CALL(dup2(fd_, fd))); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
298 |
if (result == -1) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
299 |
FMT_THROW(system_error( |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
300 |
errno, FMT_STRING("cannot duplicate file descriptor {} to {}"), fd_, |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
301 |
fd)); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
302 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
303 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
304 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
305 |
void file::dup2(int fd, std::error_code& ec) noexcept { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
306 |
int result = 0; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
307 |
FMT_RETRY(result, FMT_POSIX_CALL(dup2(fd_, fd))); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
308 |
if (result == -1) ec = std::error_code(errno, std::generic_category()); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
309 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
310 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
311 |
buffered_file file::fdopen(const char* mode) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
312 |
// Don't retry as fdopen doesn't return EINTR. |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
313 |
# if defined(__MINGW32__) && defined(_POSIX_) |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
314 |
FILE* f = ::fdopen(fd_, mode); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
315 |
# else |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
316 |
FILE* f = FMT_POSIX_CALL(fdopen(fd_, mode)); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
317 |
# endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
318 |
if (!f) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
319 |
FMT_THROW(system_error( |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
320 |
errno, FMT_STRING("cannot associate stream with file descriptor"))); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
321 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
322 |
buffered_file bf(f); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
323 |
fd_ = -1; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
324 |
return bf; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
325 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
326 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
327 |
# if defined(_WIN32) && !defined(__MINGW32__) |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
328 |
file file::open_windows_file(wcstring_view path, int oflag) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
329 |
int fd = -1; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
330 |
auto err = _wsopen_s(&fd, path.c_str(), oflag, _SH_DENYNO, default_open_mode); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
331 |
if (fd == -1) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
332 |
FMT_THROW(system_error(err, FMT_STRING("cannot open file {}"), |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
333 |
detail::to_utf8<wchar_t>(path.c_str()).c_str())); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
334 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
335 |
return file(fd); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
336 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
337 |
# endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
338 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
339 |
pipe::pipe() { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
340 |
int fds[2] = {}; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
341 |
# ifdef _WIN32 |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
342 |
// Make the default pipe capacity same as on Linux 2.6.11+. |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
343 |
enum { DEFAULT_CAPACITY = 65536 }; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
344 |
int result = FMT_POSIX_CALL(pipe(fds, DEFAULT_CAPACITY, _O_BINARY)); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
345 |
# else |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
346 |
// Don't retry as the pipe function doesn't return EINTR. |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
347 |
// http://pubs.opengroup.org/onlinepubs/009696799/functions/pipe.html |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
348 |
int result = FMT_POSIX_CALL(pipe(fds)); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
349 |
# endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
350 |
if (result != 0) |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
351 |
FMT_THROW(system_error(errno, FMT_STRING("cannot create pipe"))); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
352 |
// The following assignments don't throw. |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
353 |
read_end = file(fds[0]); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
354 |
write_end = file(fds[1]); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
355 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
356 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
357 |
# if !defined(__MSDOS__) |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
358 |
long getpagesize() { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
359 |
# ifdef _WIN32 |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
360 |
SYSTEM_INFO si; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
361 |
GetSystemInfo(&si); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
362 |
return si.dwPageSize; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
363 |
# else |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
364 |
# ifdef _WRS_KERNEL |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
365 |
long size = FMT_POSIX_CALL(getpagesize()); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
366 |
# else |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
367 |
long size = FMT_POSIX_CALL(sysconf(_SC_PAGESIZE)); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
368 |
# endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
369 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
370 |
if (size < 0) |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
371 |
FMT_THROW(system_error(errno, FMT_STRING("cannot get memory page size"))); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
372 |
return size; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
373 |
# endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
374 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
375 |
# endif |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
376 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
377 |
namespace detail { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
378 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
379 |
void file_buffer::grow(buffer<char>& buf, size_t) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
380 |
if (buf.size() == buf.capacity()) static_cast<file_buffer&>(buf).flush(); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
381 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
382 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
383 |
file_buffer::file_buffer(cstring_view path, const ostream_params& params) |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
384 |
: buffer<char>(grow), file_(path, params.oflag) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
385 |
set(new char[params.buffer_size], params.buffer_size); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
386 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
387 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
388 |
file_buffer::file_buffer(file_buffer&& other) noexcept |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
389 |
: buffer<char>(grow, other.data(), other.size(), other.capacity()), |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
390 |
file_(std::move(other.file_)) { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
391 |
other.clear(); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
392 |
other.set(nullptr, 0); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
393 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
394 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
395 |
file_buffer::~file_buffer() { |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
396 |
flush(); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
397 |
delete[] data(); |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
398 |
} |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
399 |
} // namespace detail |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
400 |
|
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
401 |
ostream::~ostream() = default; |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
402 |
#endif // FMT_USE_FCNTL |
| 47fd5c32 |
Poul Sander |
2024-10-05 08:44 |
403 |
FMT_END_NAMESPACE |
|
|
1970-01-01 00:00 |
404 |
|