| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#ifndef FMT_OS_H_ |
| 9 |
#define FMT_OS_H_ |
| 10 |
|
| 11 |
#include <cerrno> |
| 12 |
#include <cstddef> |
| 13 |
#include <cstdio> |
| 14 |
#include <system_error> |
| 15 |
|
| 16 |
#if defined __APPLE__ || defined(__FreeBSD__) |
| 17 |
# include <xlocale.h> |
| 18 |
#endif |
| 19 |
|
| 20 |
#include "format.h" |
| 21 |
|
| 22 |
#ifndef FMT_USE_FCNTL |
| 23 |
|
| 24 |
# if FMT_HAS_INCLUDE("winapifamily.h") |
| 25 |
# include <winapifamily.h> |
| 26 |
# endif |
| 27 |
# if (FMT_HAS_INCLUDE(<fcntl.h>) || defined(__APPLE__) || \ |
| 28 |
defined(__linux__)) && \ |
| 29 |
(!defined(WINAPI_FAMILY) || \ |
| 30 |
(WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)) |
| 31 |
# include <fcntl.h> |
| 32 |
# define FMT_USE_FCNTL 1 |
| 33 |
# else |
| 34 |
# define FMT_USE_FCNTL 0 |
| 35 |
# endif |
| 36 |
#endif |
| 37 |
|
| 38 |
#ifndef FMT_POSIX |
| 39 |
# if defined(_WIN32) && !defined(__MINGW32__) |
| 40 |
|
| 41 |
# define FMT_POSIX(call) _##call |
| 42 |
# else |
| 43 |
# define FMT_POSIX(call) call |
| 44 |
# endif |
| 45 |
#endif |
| 46 |
|
| 47 |
|
| 48 |
#ifdef FMT_SYSTEM |
| 49 |
# define FMT_POSIX_CALL(call) FMT_SYSTEM(call) |
| 50 |
#else |
| 51 |
# define FMT_SYSTEM(call) ::call |
| 52 |
# ifdef _WIN32 |
| 53 |
|
| 54 |
# define FMT_POSIX_CALL(call) ::_##call |
| 55 |
# else |
| 56 |
# define FMT_POSIX_CALL(call) ::call |
| 57 |
# endif |
| 58 |
#endif |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
#ifndef _WIN32 |
| 63 |
# define FMT_RETRY_VAL(result, expression, error_result) \ |
| 64 |
do { \ |
| 65 |
(result) = (expression); \ |
| 66 |
} while ((result) == (error_result) && errno == EINTR) |
| 67 |
#else |
| 68 |
# define FMT_RETRY_VAL(result, expression, error_result) result = (expression) |
| 69 |
#endif |
| 70 |
|
| 71 |
#define FMT_RETRY(result, expression) FMT_RETRY_VAL(result, expression, -1) |
| 72 |
|
| 73 |
FMT_BEGIN_NAMESPACE |
| 74 |
FMT_BEGIN_EXPORT |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
template <typename Char> class basic_cstring_view { |
| 102 |
private: |
| 103 |
const Char* data_; |
| 104 |
|
| 105 |
public: |
| 106 |
|
| 107 |
basic_cstring_view(const Char* s) : data_(s) {} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
basic_cstring_view(const std::basic_string<Char>& s) : data_(s.c_str()) {} |
| 115 |
|
| 116 |
|
| 117 |
const Char* c_str() const { return data_; } |
| 118 |
}; |
| 119 |
|
| 120 |
using cstring_view = basic_cstring_view<char>; |
| 121 |
using wcstring_view = basic_cstring_view<wchar_t>; |
| 122 |
|
| 123 |
#ifdef _WIN32 |
| 124 |
FMT_API const std::error_category& system_category() noexcept; |
| 125 |
|
| 126 |
namespace detail { |
| 127 |
FMT_API void format_windows_error(buffer<char>& out, int error_code, |
| 128 |
const char* message) noexcept; |
| 129 |
} |
| 130 |
|
| 131 |
FMT_API std::system_error vwindows_error(int error_code, string_view format_str, |
| 132 |
format_args args); |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
template <typename... Args> |
| 163 |
std::system_error windows_error(int error_code, string_view message, |
| 164 |
const Args&... args) { |
| 165 |
return vwindows_error(error_code, message, fmt::make_format_args(args...)); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
FMT_API void report_windows_error(int error_code, const char* message) noexcept; |
| 171 |
#else |
| 172 |
inline const std::error_category& system_category() noexcept { |
| 173 |
return std::system_category(); |
| 174 |
} |
| 175 |
#endif |
| 176 |
|
| 177 |
|
| 178 |
#ifdef __OSX__ |
| 179 |
template <typename S, typename... Args, typename Char = char_t<S>> |
| 180 |
void say(const S& format_str, Args&&... args) { |
| 181 |
std::system(format("say \"{}\"", format(format_str, args...)).c_str()); |
| 182 |
} |
| 183 |
#endif |
| 184 |
|
| 185 |
|
| 186 |
class buffered_file { |
| 187 |
private: |
| 188 |
FILE* file_; |
| 189 |
|
| 190 |
friend class file; |
| 191 |
|
| 192 |
explicit buffered_file(FILE* f) : file_(f) {} |
| 193 |
|
| 194 |
public: |
| 195 |
buffered_file(const buffered_file&) = delete; |
| 196 |
void operator=(const buffered_file&) = delete; |
| 197 |
|
| 198 |
|
| 199 |
buffered_file() noexcept : file_(nullptr) {} |
| 200 |
|
| 201 |
|
| 202 |
FMT_API ~buffered_file() noexcept; |
| 203 |
|
| 204 |
public: |
| 205 |
buffered_file(buffered_file&& other) noexcept : file_(other.file_) { |
| 206 |
other.file_ = nullptr; |
| 207 |
} |
| 208 |
|
| 209 |
buffered_file& operator=(buffered_file&& other) { |
| 210 |
close(); |
| 211 |
file_ = other.file_; |
| 212 |
other.file_ = nullptr; |
| 213 |
return *this; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
FMT_API buffered_file(cstring_view filename, cstring_view mode); |
| 218 |
|
| 219 |
|
| 220 |
FMT_API void close(); |
| 221 |
|
| 222 |
|
| 223 |
FILE* get() const noexcept { return file_; } |
| 224 |
|
| 225 |
FMT_API int descriptor() const; |
| 226 |
|
| 227 |
void vprint(string_view format_str, format_args args) { |
| 228 |
fmt::vprint(file_, format_str, args); |
| 229 |
} |
| 230 |
|
| 231 |
template <typename... Args> |
| 232 |
inline void print(string_view format_str, const Args&... args) { |
| 233 |
vprint(format_str, fmt::make_format_args(args...)); |
| 234 |
} |
| 235 |
}; |
| 236 |
|
| 237 |
#if FMT_USE_FCNTL |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
class FMT_API file { |
| 245 |
private: |
| 246 |
int fd_; |
| 247 |
|
| 248 |
|
| 249 |
explicit file(int fd) : fd_(fd) {} |
| 250 |
|
| 251 |
public: |
| 252 |
|
| 253 |
enum { |
| 254 |
RDONLY = FMT_POSIX(O_RDONLY), |
| 255 |
WRONLY = FMT_POSIX(O_WRONLY), |
| 256 |
RDWR = FMT_POSIX(O_RDWR), |
| 257 |
CREATE = FMT_POSIX(O_CREAT), |
| 258 |
APPEND = FMT_POSIX(O_APPEND), |
| 259 |
TRUNC = FMT_POSIX(O_TRUNC) |
| 260 |
}; |
| 261 |
|
| 262 |
|
| 263 |
file() noexcept : fd_(-1) {} |
| 264 |
|
| 265 |
|
| 266 |
file(cstring_view path, int oflag); |
| 267 |
|
| 268 |
public: |
| 269 |
file(const file&) = delete; |
| 270 |
void operator=(const file&) = delete; |
| 271 |
|
| 272 |
file(file&& other) noexcept : fd_(other.fd_) { other.fd_ = -1; } |
| 273 |
|
| 274 |
|
| 275 |
file& operator=(file&& other) { |
| 276 |
close(); |
| 277 |
fd_ = other.fd_; |
| 278 |
other.fd_ = -1; |
| 279 |
return *this; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
~file() noexcept; |
| 284 |
|
| 285 |
|
| 286 |
int descriptor() const noexcept { return fd_; } |
| 287 |
|
| 288 |
|
| 289 |
void close(); |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
long long size() const; |
| 294 |
|
| 295 |
|
| 296 |
size_t read(void* buffer, size_t count); |
| 297 |
|
| 298 |
|
| 299 |
size_t write(const void* buffer, size_t count); |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
static file dup(int fd); |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
void dup2(int fd); |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
void dup2(int fd, std::error_code& ec) noexcept; |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
static void pipe(file& read_end, file& write_end); |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
buffered_file fdopen(const char* mode); |
| 320 |
|
| 321 |
# if defined(_WIN32) && !defined(__MINGW32__) |
| 322 |
|
| 323 |
|
| 324 |
static file open_windows_file(wcstring_view path, int oflag); |
| 325 |
# endif |
| 326 |
}; |
| 327 |
|
| 328 |
|
| 329 |
long getpagesize(); |
| 330 |
|
| 331 |
namespace detail { |
| 332 |
|
| 333 |
struct buffer_size { |
| 334 |
buffer_size() = default; |
| 335 |
size_t value = 0; |
| 336 |
buffer_size operator=(size_t val) const { |
| 337 |
auto bs = buffer_size(); |
| 338 |
bs.value = val; |
| 339 |
return bs; |
| 340 |
} |
| 341 |
}; |
| 342 |
|
| 343 |
struct ostream_params { |
| 344 |
int oflag = file::WRONLY | file::CREATE | file::TRUNC; |
| 345 |
size_t buffer_size = BUFSIZ > 32768 ? BUFSIZ : 32768; |
| 346 |
|
| 347 |
ostream_params() {} |
| 348 |
|
| 349 |
template <typename... T> |
| 350 |
ostream_params(T... params, int new_oflag) : ostream_params(params...) { |
| 351 |
oflag = new_oflag; |
| 352 |
} |
| 353 |
|
| 354 |
template <typename... T> |
| 355 |
ostream_params(T... params, detail::buffer_size bs) |
| 356 |
: ostream_params(params...) { |
| 357 |
this->buffer_size = bs.value; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
# if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 2000 |
| 363 |
ostream_params(int new_oflag) : oflag(new_oflag) {} |
| 364 |
ostream_params(detail::buffer_size bs) : buffer_size(bs.value) {} |
| 365 |
# endif |
| 366 |
}; |
| 367 |
|
| 368 |
class file_buffer final : public buffer<char> { |
| 369 |
file file_; |
| 370 |
|
| 371 |
FMT_API void grow(size_t) override; |
| 372 |
|
| 373 |
public: |
| 374 |
FMT_API file_buffer(cstring_view path, const ostream_params& params); |
| 375 |
FMT_API file_buffer(file_buffer&& other); |
| 376 |
FMT_API ~file_buffer(); |
| 377 |
|
| 378 |
void flush() { |
| 379 |
if (size() == 0) return; |
| 380 |
file_.write(data(), size() * sizeof(data()[0])); |
| 381 |
clear(); |
| 382 |
} |
| 383 |
|
| 384 |
void close() { |
| 385 |
flush(); |
| 386 |
file_.close(); |
| 387 |
} |
| 388 |
}; |
| 389 |
|
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
constexpr detail::buffer_size buffer_size{}; |
| 395 |
|
| 396 |
|
| 397 |
class FMT_API ostream { |
| 398 |
private: |
| 399 |
FMT_MSC_WARNING(suppress : 4251) |
| 400 |
detail::file_buffer buffer_; |
| 401 |
|
| 402 |
ostream(cstring_view path, const detail::ostream_params& params) |
| 403 |
: buffer_(path, params) {} |
| 404 |
|
| 405 |
public: |
| 406 |
ostream(ostream&& other) : buffer_(std::move(other.buffer_)) {} |
| 407 |
|
| 408 |
~ostream(); |
| 409 |
|
| 410 |
void flush() { buffer_.flush(); } |
| 411 |
|
| 412 |
template <typename... T> |
| 413 |
friend ostream output_file(cstring_view path, T... params); |
| 414 |
|
| 415 |
void close() { buffer_.close(); } |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
template <typename... T> void print(format_string<T...> fmt, T&&... args) { |
| 422 |
vformat_to(detail::buffer_appender<char>(buffer_), fmt, |
| 423 |
fmt::make_format_args(args...)); |
| 424 |
} |
| 425 |
}; |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
template <typename... T> |
| 443 |
inline ostream output_file(cstring_view path, T... params) { |
| 444 |
return {path, detail::ostream_params(params...)}; |
| 445 |
} |
| 446 |
#endif |
| 447 |
|
| 448 |
FMT_END_EXPORT |
| 449 |
FMT_END_NAMESPACE |
| 450 |
|
| 451 |
#endif |