2
3
4
5
6
7
8
11#ifndef _CRT_SECURE_NO_WARNINGS
12# define _CRT_SECURE_NO_WARNINGS
24# ifndef WIN32_LEAN_AND_MEAN
25# define WIN32_LEAN_AND_MEAN
30# define O_CREAT _O_CREAT
31# define O_TRUNC _O_TRUNC
34# define S_IRUSR _S_IREAD
38# define S_IWUSR _S_IWRITE
42# define _SH_DENYNO 0x40
58inline unsigned convert_rwcount(
std::size_t count) {
59 return count <= UINT_MAX ?
static_cast<
unsigned>(count) : UINT_MAX;
71 fmt::report_system_error(errno,
"cannot close file");
75 fmt::CStringRef filename,
fmt::CStringRef mode) {
101 int mode = S_IRUSR | S_IWUSR;
102#if defined(_WIN32
) && !defined(__MINGW32__)
106 FMT_RETRY(fd_, FMT_POSIX_CALL(open(path.c_str(), oflag, mode)));
116 fmt::report_system_error(errno,
"cannot close file");
135 DWORD size_upper = 0;
136 HANDLE handle =
reinterpret_cast<HANDLE>(_get_osfhandle(
fd_));
137 DWORD size_lower =
FMT_SYSTEM(GetFileSize(handle, &size_upper));
138 if (size_lower == INVALID_FILE_SIZE) {
139 DWORD error = GetLastError();
140 if (error != NO_ERROR)
141 FMT_THROW(WindowsError(GetLastError(),
"cannot get file size"));
143 fmt::ULongLong long_size = size_upper;
144 return (long_size <<
sizeof(DWORD) * CHAR_BIT) | size_lower;
146 typedef struct stat Stat;
147 Stat file_stat = Stat();
148 if (FMT_POSIX_CALL(fstat(fd_, &file_stat)) == -1)
149 FMT_THROW(SystemError(errno,
"cannot get file attributes"));
150 FMT_STATIC_ASSERT(
sizeof(fmt::LongLong) >=
sizeof(file_stat.st_size),
151 "return type of File::size is not large enough");
152 return file_stat.st_size;
161 return internal::to_unsigned(result);
169 return internal::to_unsigned(result);
186 "cannot duplicate file descriptor {} to {}",
fd_, fd));
205 enum { DEFAULT_CAPACITY = 65536 };
206 int result =
FMT_POSIX_CALL(pipe(fds, DEFAULT_CAPACITY, _O_BINARY));
210 int result = FMT_POSIX_CALL(pipe(fds));
234 return si.dwPageSize;
236 long size = FMT_POSIX_CALL(sysconf(_SC_PAGESIZE));
238 FMT_THROW(SystemError(errno,
"cannot get memory page size"));
FMT_API ~BufferedFile() FMT_NOEXCEPT
FMT_API BufferedFile(CStringRef filename, CStringRef mode)
FMT_API int fileno() const
ErrorCode(int value=0) FMT_NOEXCEPT
FMT_API std::size_t write(const void *buffer, std::size_t count)
static FMT_API File dup(int fd)
FMT_API void dup2(int fd, ErrorCode &ec) FMT_NOEXCEPT
FMT_API void dup2(int fd)
static FMT_API void pipe(File &read_end, File &write_end)
FMT_API ~File() FMT_NOEXCEPT
FMT_API BufferedFile fdopen(const char *mode)
FMT_API std::size_t read(void *buffer, std::size_t count)
FMT_API LongLong size() const
FMT_API File(CStringRef path, int oflag)
#define FMT_RETRY_VAL(result, expression, error_result)
#define FMT_RETRY(result, expression)
#define FMT_POSIX_CALL(call)