17# include <system_error>
19# if FMT_HAS_INCLUDE(<xlocale.h>)
26# if FMT_HAS_INCLUDE("winapifamily.h")
27# include <winapifamily.h>
29# if (FMT_HAS_INCLUDE(<fcntl.h>) || defined(__APPLE__) || \
30 defined(__linux__)) && \
31 (!defined(WINAPI_FAMILY) || \
32 (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP))
34# define FMT_USE_FCNTL 1
36# define FMT_USE_FCNTL 0
41# if defined(_WIN32) && !defined(__MINGW32__)
43# define FMT_POSIX(call) _##call
45# define FMT_POSIX(call) call
51# define FMT_HAS_SYSTEM
52# define FMT_POSIX_CALL(call) FMT_SYSTEM(call)
54# define FMT_SYSTEM(call) ::call
57# define FMT_POSIX_CALL(call) ::_##call
59# define FMT_POSIX_CALL(call) ::call
66# define FMT_RETRY_VAL(result, expression, error_result) \
68 (result) = (expression); \
69 } while ((result) == (error_result) && errno == EINTR)
71# define FMT_RETRY_VAL(result, expression, error_result) result = (expression)
74#define FMT_RETRY(result, expression) FMT_RETRY_VAL(result, expression, -1)
107 auto c_str() const -> const Char* {
return data_; }
114FMT_API
const std::error_category& system_category() noexcept;
117FMT_API
void format_windows_error(buffer<char>& out,
int error_code,
118 const char* message)
noexcept;
121FMT_API std::system_error vwindows_error(
int error_code, string_view format_str,
149template <
typename... Args>
150std::system_error windows_error(
int error_code, string_view message,
151 const Args&... args) {
152 return vwindows_error(error_code, message, fmt::make_format_args(args...));
157FMT_API
void report_windows_error(
int error_code,
const char* message)
noexcept;
159inline auto system_category() noexcept -> const std::error_category& {
160 return std::system_category();
166template <
typename S,
typename... Args,
typename Char = char_t<S>>
167void say(
const S& format_str, Args&&... args) {
168 std::system(format(
"say \"{}\"", format(format_str, args...)).c_str());
179 explicit buffered_file(FILE* f) : file_(f) {}
182 buffered_file(
const buffered_file&) =
delete;
183 void operator=(
const buffered_file&) =
delete;
186 buffered_file() noexcept : file_(
nullptr) {}
189 FMT_API ~buffered_file()
noexcept;
192 buffered_file(buffered_file&& other) noexcept : file_(other.file_) {
193 other.file_ =
nullptr;
196 auto operator=(buffered_file&& other) -> buffered_file& {
199 other.file_ =
nullptr;
204 FMT_API buffered_file(cstring_view filename, cstring_view mode);
207 FMT_API
void close();
210 auto get()
const noexcept -> FILE* {
return file_; }
212 FMT_API
auto descriptor()
const -> int;
214 template <
typename... T>
215 inline void print(string_view fmt,
const T&... args) {
216 const auto& vargs = fmt::make_format_args(args...);
217 detail::is_locking<T...>() ? fmt::vprint_buffered(file_, fmt, vargs)
218 : fmt::vprint(file_, fmt, vargs);
235 explicit file(
int fd) : fd_(fd) {}
242 RDONLY = FMT_POSIX(O_RDONLY),
243 WRONLY = FMT_POSIX(O_WRONLY),
244 RDWR = FMT_POSIX(O_RDWR),
245 CREATE = FMT_POSIX(O_CREAT),
246 APPEND = FMT_POSIX(O_APPEND),
247 TRUNC = FMT_POSIX(O_TRUNC)
251 file() noexcept : fd_(-1) {}
254 file(cstring_view path,
int oflag);
257 file(
const file&) =
delete;
258 void operator=(
const file&) =
delete;
260 file(file&& other) noexcept : fd_(other.fd_) { other.fd_ = -1; }
263 auto operator=(file&& other) -> file& {
274 auto descriptor() const noexcept ->
int {
return fd_; }
281 auto size() const ->
long long;
284 auto read(
void* buffer,
size_t count) ->
size_t;
287 auto write(const
void* buffer,
size_t count) ->
size_t;
291 static auto dup(
int fd) -> file;
299 void dup2(
int fd, std::error_code& ec) noexcept;
303 auto fdopen(const
char* mode) -> buffered_file;
305# if defined(_WIN32) && !defined(__MINGW32__)
308 static file open_windows_file(wcstring_view path,
int oflag);
322auto getpagesize() -> long;
327 buffer_size() =
default;
329 auto operator=(
size_t val)
const -> buffer_size {
330 auto bs = buffer_size();
336struct ostream_params {
337 int oflag = file::WRONLY | file::CREATE | file::TRUNC;
338 size_t buffer_size = BUFSIZ > 32768 ? BUFSIZ : 32768;
342 template <
typename... T>
343 ostream_params(T... params,
int new_oflag) : ostream_params(params...) {
347 template <
typename... T>
348 ostream_params(T... params, detail::buffer_size bs)
349 : ostream_params(params...) {
350 this->buffer_size = bs.value;
355# if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 2000
356 ostream_params(
int new_oflag) : oflag(new_oflag) {}
357 ostream_params(detail::buffer_size bs) : buffer_size(bs.value) {}
361class file_buffer final :
public buffer<char> {
365 FMT_API
static void grow(buffer<char>& buf,
size_t);
368 FMT_API file_buffer(cstring_view path,
const ostream_params& params);
369 FMT_API file_buffer(file_buffer&& other)
noexcept;
370 FMT_API ~file_buffer();
373 if (size() == 0)
return;
374 file_.write(data(), size() *
sizeof(data()[0]));
386constexpr auto buffer_size = detail::buffer_size();
390class FMT_API ostream {
392 FMT_MSC_WARNING(suppress : 4251)
393 detail::file_buffer buffer_;
395 ostream(cstring_view path,
const detail::ostream_params& params)
396 : buffer_(path, params) {}
399 ostream(ostream&& other) : buffer_(std::move(other.buffer_)) {}
403 void flush() { buffer_.flush(); }
405 template <
typename... T>
406 friend auto output_file(cstring_view path, T... params) -> ostream;
408 void close() { buffer_.close(); }
412 template <
typename... T>
void print(format_string<T...> fmt, T&&... args) {
413 vformat_to(appender(buffer_), fmt, fmt::make_format_args(args...));
430template <
typename... T>
431inline auto output_file(cstring_view path, T... params) -> ostream {
432 return {path, detail::ostream_params(params...)};
basic_cstring_view(const std::basic_string< Char > &s)
Constructs a string reference from an std::string object.
Definition os.h:104
basic_cstring_view(const Char *s)
Constructs a string reference object from a C string.
Definition os.h:101
auto c_str() const -> const char *
Definition os.h:107