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)) && \ 35 # define FMT_USE_FCNTL 1 37 # define FMT_USE_FCNTL 0 42 # if defined(_WIN32) && !defined(__MINGW32__) 44 # define FMT_POSIX(call) _##call 46 # define FMT_POSIX(call) call 52 # define FMT_HAS_SYSTEM 53 # define FMT_POSIX_CALL(call) FMT_SYSTEM(call) 55 # define FMT_SYSTEM(call) ::call 58 # define FMT_POSIX_CALL(call) ::_##call 60 # define FMT_POSIX_CALL(call) ::call 67 # define FMT_RETRY_VAL(result, expression, error_result) \ 69 (result) = (expression); \ 70 } while ((result) == (error_result) && errno == EINTR) 72 # define FMT_RETRY_VAL(result, expression, error_result) result = (expression) 75 #define FMT_RETRY(result, expression) FMT_RETRY_VAL(result, expression, -1) 108 auto c_str() const -> const Char* {
return data_; }
115 FMT_API
const std::error_category& system_category() noexcept;
118 FMT_API
void format_windows_error(buffer<char>& out,
int error_code,
119 const char* message) noexcept;
122 FMT_API std::system_error vwindows_error(
int error_code,
string_view fmt,
150 template <
typename... T>
151 auto windows_error(
int error_code,
string_view message,
const T&... args)
152 -> std::system_error {
153 return vwindows_error(error_code, message, vargs<T...>{{args...}});
158 FMT_API
void report_windows_error(
int error_code,
const char* message) noexcept;
160 inline auto system_category() noexcept ->
const std::error_category& {
161 return std::system_category();
167 template <
typename S,
typename... Args,
typename Char = char_t<S>>
168 void say(
const S& fmt, Args&&... args) {
169 std::system(format(
"say \"{}\"", format(fmt, args...)).
c_str());
174 class buffered_file {
180 inline explicit buffered_file(FILE* f) : file_(f) {}
183 buffered_file(
const buffered_file&) =
delete;
184 void operator=(
const buffered_file&) =
delete;
187 inline buffered_file() noexcept : file_(
nullptr) {}
190 FMT_API ~buffered_file() noexcept;
193 inline buffered_file(buffered_file&& other) noexcept : file_(other.file_) {
194 other.file_ =
nullptr;
197 inline auto operator=(buffered_file&& other) -> buffered_file& {
200 other.file_ =
nullptr;
208 FMT_API
void close();
211 inline auto get()
const noexcept -> FILE* {
return file_; }
213 FMT_API
auto descriptor()
const -> int;
215 template <
typename... T>
216 inline void print(
string_view fmt,
const T&... args) {
217 fmt::vargs<T...> vargs = {{args...}};
218 detail::is_locking<T...>() ? fmt::vprint_buffered(file_, fmt, vargs)
219 : fmt::vprint(file_, fmt, vargs);
236 explicit file(
int fd) : fd_(fd) {}
243 RDONLY = FMT_POSIX(O_RDONLY),
244 WRONLY = FMT_POSIX(O_WRONLY),
245 RDWR = FMT_POSIX(O_RDWR),
246 CREATE = FMT_POSIX(O_CREAT),
247 APPEND = FMT_POSIX(O_APPEND),
248 TRUNC = FMT_POSIX(O_TRUNC)
252 inline file() noexcept : fd_(-1) {}
258 file(
const file&) =
delete;
259 void operator=(
const file&) =
delete;
261 inline file(file&& other) noexcept : fd_(other.fd_) { other.fd_ = -1; }
264 inline auto operator=(file&& other) -> file& {
275 inline auto descriptor()
const noexcept ->
int {
return fd_; }
282 auto size()
const ->
long long;
285 auto read(
void* buffer,
size_t count) -> size_t;
288 auto write(
const void* buffer,
size_t count) -> size_t;
292 static auto dup(
int fd) -> file;
300 void dup2(
int fd, std::error_code& ec) noexcept;
304 auto fdopen(
const char* mode) -> buffered_file;
306 # if defined(_WIN32) && !defined(__MINGW32__) 309 static file open_windows_file(
wcstring_view path,
int oflag);
313 struct FMT_API pipe {
323 auto getpagesize() -> long;
328 constexpr buffer_size() =
default;
330 FMT_CONSTEXPR
auto operator=(
size_t val)
const -> buffer_size {
331 auto bs = buffer_size();
337 struct ostream_params {
338 int oflag = file::WRONLY | file::CREATE | file::TRUNC;
339 size_t buffer_size = BUFSIZ > 32768 ? BUFSIZ : 32768;
341 constexpr ostream_params() {}
343 template <
typename... T>
344 ostream_params(T... params,
int new_oflag) : ostream_params(params...) {
348 template <
typename... T>
349 ostream_params(T... params, detail::buffer_size bs)
350 : ostream_params(params...) {
351 this->buffer_size = bs.value;
356 # if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 2000 357 ostream_params(
int new_oflag) : oflag(new_oflag) {}
358 ostream_params(detail::buffer_size bs) : buffer_size(bs.value) {}
364 FMT_INLINE_VARIABLE constexpr
auto buffer_size = detail::buffer_size();
372 ostream(
cstring_view path,
const detail::ostream_params& params);
374 static void grow(buffer<char>& buf,
size_t);
377 ostream(ostream&& other) noexcept;
385 inline void flush() {
386 if (size() == 0)
return;
387 file_.write(data(), size() *
sizeof(data()[0]));
391 template <
typename... T>
392 friend auto output_file(
cstring_view path, T... params) -> ostream;
394 inline void close() {
401 template <
typename... T>
void print(format_string<T...> fmt, T&&... args) {
402 vformat_to(appender(*
this), fmt.str, vargs<T...>{{args...}});
419 template <
typename... T>
420 inline auto output_file(
cstring_view path, T... params) -> ostream {
421 return {path, detail::ostream_params(params...)};
423 #endif // FMT_USE_FCNTL basic_cstring_view(const Char *s)
Constructs a string reference object from a C string.
Definition: os.h:102
Converts a string literal into a format string that will be parsed at compile time and converted into...
Definition: args.h:20
A reference to a null-terminated string.
Definition: os.h:96
basic_cstring_view(const std::basic_string< Char > &s)
Constructs a string reference from an std::string object.
Definition: os.h:105
auto c_str() const -> const Char *
Returns the pointer to a C string.
Definition: os.h:108
A contiguous memory buffer with an optional growing ability. It is an internal class and shouldn't be...
Definition: base.h:1763