15# include <system_error>
20#if defined __APPLE__ || defined(__FreeBSD__)
21# if FMT_HAS_INCLUDE(<xlocale.h>)
28# if FMT_HAS_INCLUDE("winapifamily.h")
29# include <winapifamily.h>
31# if (FMT_HAS_INCLUDE(<fcntl.h>) || defined(__APPLE__) || \
32 defined(__linux__)) && \
33 (!defined(WINAPI_FAMILY) || \
34 (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP))
36# define FMT_USE_FCNTL 1
38# define FMT_USE_FCNTL 0
43# if defined(_WIN32) && !defined(__MINGW32__)
45# define FMT_POSIX(call) _##call
47# define FMT_POSIX(call) call
53# define FMT_HAS_SYSTEM
54# define FMT_POSIX_CALL(call) FMT_SYSTEM(call)
56# define FMT_SYSTEM(call) ::call
59# define FMT_POSIX_CALL(call) ::_##call
61# define FMT_POSIX_CALL(call) ::call
68# define FMT_RETRY_VAL(result, expression, error_result) \
70 (result) = (expression); \
71 } while ((result) == (error_result) && errno == EINTR)
73# define FMT_RETRY_VAL(result, expression, error_result) result = (expression)
76#define FMT_RETRY(result, expression) FMT_RETRY_VAL(result, expression, -1)
122 auto c_str() const -> const Char* {
return data_; }
129FMT_API
const std::error_category& system_category() noexcept;
132FMT_API
void format_windows_error(buffer<char>& out,
int error_code,
133 const char* message)
noexcept;
136FMT_API std::system_error vwindows_error(
int error_code,
string_view format_str,
167template <
typename... Args>
168std::system_error windows_error(
int error_code,
string_view message,
169 const Args&... args) {
170 return vwindows_error(error_code, message, fmt::make_format_args(args...));
175FMT_API
void report_windows_error(
int error_code,
const char* message)
noexcept;
177inline auto system_category() noexcept -> const std::error_category& {
178 return std::system_category();
184template <
typename S,
typename... Args,
typename Char = char_t<S>>
185void say(
const S& format_str, Args&&... args) {
186 std::system(format(
"say \"{}\"", format(format_str, args...)).c_str());
211 other.file_ =
nullptr;
217 other.file_ =
nullptr;
225 FMT_API
void close();
228 auto get()
const noexcept -> FILE* {
return file_; }
230 FMT_API
auto descriptor()
const -> int;
232 template <
typename... T>
233 inline void print(
string_view fmt,
const T&... args) {
234 const auto& vargs = fmt::make_format_args(args...);
235 detail::is_locking<T...>() ? fmt::vprint_buffered(file_, fmt, vargs)
236 : fmt::vprint(file_, fmt, vargs);
253 explicit file(
int fd) : fd_(fd) {}
260 RDONLY = FMT_POSIX(O_RDONLY),
261 WRONLY = FMT_POSIX(O_WRONLY),
262 RDWR = FMT_POSIX(O_RDWR),
263 CREATE = FMT_POSIX(O_CREAT),
264 APPEND = FMT_POSIX(O_APPEND),
265 TRUNC = FMT_POSIX(O_TRUNC)
269 file() noexcept : fd_(-1) {}
275 file(
const file&) =
delete;
276 void operator=(
const file&) =
delete;
278 file(file&& other) noexcept : fd_(other.fd_) { other.fd_ = -1; }
281 auto operator=(file&& other) -> file& {
292 auto descriptor() const noexcept ->
int {
return fd_; }
299 auto size() const ->
long long;
302 auto read(
void* buffer,
size_t count) ->
size_t;
305 auto write(const
void* buffer,
size_t count) ->
size_t;
309 static auto dup(
int fd) -> file;
317 void dup2(
int fd, std::error_code& ec) noexcept;
323# if defined(_WIN32) && !defined(__MINGW32__)
326 static file open_windows_file(
wcstring_view path,
int oflag);
340auto getpagesize() -> long;
345 buffer_size() =
default;
347 auto operator=(
size_t val)
const -> buffer_size {
348 auto bs = buffer_size();
354struct ostream_params {
355 int oflag = file::WRONLY | file::CREATE | file::TRUNC;
356 size_t buffer_size = BUFSIZ > 32768 ? BUFSIZ : 32768;
360 template <
typename... T>
361 ostream_params(T... params,
int new_oflag) : ostream_params(params...) {
365 template <
typename... T>
366 ostream_params(T... params, detail::buffer_size bs)
367 : ostream_params(params...) {
368 this->buffer_size = bs.value;
373# if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 2000
374 ostream_params(
int new_oflag) : oflag(new_oflag) {}
375 ostream_params(detail::buffer_size bs) : buffer_size(bs.value) {}
379class file_buffer final :
public buffer<char> {
383 FMT_API
static void grow(buffer<char>& buf,
size_t);
386 FMT_API file_buffer(
cstring_view path,
const ostream_params& params);
387 FMT_API file_buffer(file_buffer&& other)
noexcept;
388 FMT_API ~file_buffer();
391 if (size() == 0)
return;
392 file_.write(data(), size() *
sizeof(data()[0]));
406constexpr detail::buffer_size buffer_size{};
409class FMT_API ostream {
411 FMT_MSC_WARNING(suppress : 4251)
412 detail::file_buffer buffer_;
414 ostream(
cstring_view path,
const detail::ostream_params& params)
415 : buffer_(path, params) {}
418 ostream(ostream&& other) : buffer_(std::move(other.buffer_)) {}
422 void flush() { buffer_.flush(); }
424 template <
typename... T>
425 friend auto output_file(
cstring_view path, T... params) -> ostream;
427 void close() { buffer_.close(); }
434 vformat_to(
appender(buffer_), fmt, fmt::make_format_args(args...));
453template <
typename... T>
454inline auto output_file(
cstring_view path, T... params) -> ostream {
455 return {path, detail::ostream_params(params...)};
basic_cstring_view(const std::basic_string< Char > &s)
Definition os.h:119
basic_cstring_view(const Char *s)
Definition os.h:112
auto c_str() const -> const Char *
Definition os.h:122