16 #include <system_error> 18 #if defined __APPLE__ || defined(__FreeBSD__) 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_POSIX_CALL(call) FMT_SYSTEM(call) 53 # define FMT_SYSTEM(call) ::call 56 # define FMT_POSIX_CALL(call) ::_##call 58 # define FMT_POSIX_CALL(call) ::call 65 # define FMT_RETRY_VAL(result, expression, error_result) \ 67 (result) = (expression); \ 68 } while ((result) == (error_result) && errno == EINTR) 70 # define FMT_RETRY_VAL(result, expression, error_result) result = (expression) 73 #define FMT_RETRY(result, expression) FMT_RETRY_VAL(result, expression, -1) 76 FMT_MODULE_EXPORT_BEGIN
119 const Char*
c_str()
const {
return data_; }
126 template <
typename ParseContext>
127 FMT_CONSTEXPR
auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
131 template <
typename FormatContext>
132 FMT_CONSTEXPR
auto format(
const std::error_code& ec, FormatContext& ctx)
const 133 -> decltype(ctx.out()) {
134 auto out = ctx.out();
135 out = detail::write_bytes(out, ec.category().name(),
137 out = detail::write<Char>(out, Char(
':'));
138 out = detail::write<Char>(out, ec.value());
144 FMT_API
const std::error_category& system_category() FMT_NOEXCEPT;
146 FMT_BEGIN_DETAIL_NAMESPACE
149 class utf16_to_utf8 {
157 size_t size()
const {
return buffer_.size() - 1; }
158 const char*
c_str()
const {
return &buffer_[0]; }
159 std::string str()
const {
return std::string(&buffer_[0], size()); }
167 FMT_API
void format_windows_error(
buffer<char>& out,
int error_code,
168 const char* message) FMT_NOEXCEPT;
169 FMT_END_DETAIL_NAMESPACE
171 FMT_API std::system_error vwindows_error(
int error_code,
string_view format_str,
202 template <
typename... Args>
203 std::system_error windows_error(
int error_code,
string_view message,
204 const Args&... args) {
205 return vwindows_error(error_code, message, fmt::make_format_args(args...));
210 FMT_API
void report_windows_error(
int error_code,
211 const char* message) FMT_NOEXCEPT;
213 inline const std::error_category& system_category() FMT_NOEXCEPT {
214 return std::system_category();
220 template <
typename S,
typename... Args,
typename Char = char_t<S>>
221 void say(
const S& format_str, Args&&... args) {
222 std::system(format(
"say \"{}\"", format(format_str, args...)).
c_str());
247 other.file_ =
nullptr;
253 other.file_ =
nullptr;
261 FMT_API
void close();
264 FILE*
get()
const FMT_NOEXCEPT {
return file_; }
268 FMT_API int(fileno)()
const;
271 fmt::vprint(file_, format_str, args);
274 template <
typename... Args>
275 inline void print(
string_view format_str,
const Args&... args) {
276 vprint(format_str, fmt::make_format_args(args...));
292 explicit file(
int fd) : fd_(fd) {}
297 RDONLY = FMT_POSIX(O_RDONLY),
298 WRONLY = FMT_POSIX(O_WRONLY),
299 RDWR = FMT_POSIX(O_RDWR),
300 CREATE = FMT_POSIX(O_CREAT),
301 APPEND = FMT_POSIX(O_APPEND),
302 TRUNC = FMT_POSIX(O_TRUNC)
306 file() FMT_NOEXCEPT : fd_(-1) {}
312 file(
const file&) =
delete;
313 void operator=(
const file&) =
delete;
315 file(file&& other) FMT_NOEXCEPT : fd_(other.fd_) { other.fd_ = -1; }
318 file& operator=(file&& other) {
326 FMT_API ~file() FMT_NOEXCEPT;
329 int descriptor()
const FMT_NOEXCEPT {
return fd_; }
332 FMT_API
void close();
336 FMT_API
long long size()
const;
339 FMT_API
size_t read(
void*
buffer,
size_t count);
342 FMT_API
size_t write(
const void* buffer,
size_t count);
346 FMT_API
static file dup(
int fd);
350 FMT_API
void dup2(
int fd);
354 FMT_API
void dup2(
int fd, std::error_code& ec) FMT_NOEXCEPT;
358 FMT_API
static void pipe(file& read_end, file& write_end);
368 FMT_BEGIN_DETAIL_NAMESPACE
371 buffer_size() =
default;
373 buffer_size operator=(
size_t val)
const {
374 auto bs = buffer_size();
380 struct ostream_params {
381 int oflag = file::WRONLY | file::CREATE | file::TRUNC;
382 size_t buffer_size = BUFSIZ > 32768 ? BUFSIZ : 32768;
386 template <
typename... T>
387 ostream_params(T... params,
int new_oflag) : ostream_params(params...) {
391 template <
typename... T>
392 ostream_params(T... params, detail::buffer_size bs)
393 : ostream_params(params...) {
394 this->buffer_size = bs.value;
399 # if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 2000 400 ostream_params(
int new_oflag) : oflag(new_oflag) {}
401 ostream_params(detail::buffer_size bs) : buffer_size(bs.value) {}
405 FMT_END_DETAIL_NAMESPACE
409 constexpr detail::buffer_size buffer_size{};
412 class FMT_API ostream final :
private detail::buffer<char> {
416 void grow(
size_t)
override;
418 ostream(
cstring_view path,
const detail::ostream_params& params)
419 : file_(path, params.oflag) {
420 set(
new char[params.buffer_size], params.buffer_size);
424 ostream(ostream&& other)
425 : detail::buffer<char>(other.data(), other.size(), other.capacity()),
426 file_(std::move(other.file_)) {
428 other.set(
nullptr, 0);
436 if (size() == 0)
return;
437 file_.write(data(), size());
441 template <
typename... T>
442 friend ostream output_file(
cstring_view path, T... params);
454 vformat_to(detail::buffer_appender<char>(*
this), fmt,
455 fmt::make_format_args(args...));
474 template <
typename... T>
475 inline ostream output_file(
cstring_view path, T... params) {
476 return {path, detail::ostream_params(params...)};
478 #endif // FMT_USE_FCNTL 485 using locale_t = _locale_t;
487 static void freelocale(locale_t loc) { _free_locale(loc); }
489 static double strtod_l(
const char* nptr,
char** endptr, _locale_t loc) {
490 return _strtod_l(nptr, endptr, loc);
497 using type = locale_t;
498 locale(
const locale&) =
delete;
499 void operator=(
const locale&) =
delete;
503 locale_ = FMT_SYSTEM(newlocale(LC_NUMERIC_MASK,
"C",
nullptr));
505 locale_ = _create_locale(LC_NUMERIC,
"C");
507 if (!locale_) FMT_THROW(system_error(errno,
"cannot create locale"));
509 ~locale() { freelocale(locale_); }
511 type
get()
const {
return locale_; }
515 FMT_DEPRECATED
double strtod(
const char*& str)
const {
517 double result = strtod_l(str, &end, locale_);
524 FMT_MODULE_EXPORT_END
basic_cstring_view(const Char *s)
Constructs a string reference object from a C string.
Definition: os.h:109
A dynamically growing memory buffer for trivially copyable/constructible types with the first SIZE el...
Definition: format.h:677
A contiguous memory buffer with an optional growing ability.
Definition: core.h:778
Definition: format.h:2772
An implementation of std::basic_string_view for pre-C++17.
Definition: core.h:448
A reference to a null-terminated string.
Definition: os.h:103
basic_cstring_view(const std::basic_string< Char > &s)
Constructs a string reference from an std::string object.
Definition: os.h:116
Definition: color.hpp:198
const Char * c_str() const
Returns the pointer to a C string.
Definition: os.h:119