16 auto pos{std::ftell(
m_file)};
23 if (!
m_file)
throw std::ios_base::failure(
"AutoFile::read: file handle is nullptr");
26 if (!
m_position)
throw std::ios_base::failure(
"AutoFile::read: position unknown");
36 throw std::ios_base::failure(
"AutoFile::seek: file handle is nullptr");
38 if (std::fseek(
m_file, offset, origin) != 0) {
39 throw std::ios_base::failure(
feof() ?
"AutoFile::seek: end of file" :
"AutoFile::seek: fseek failed");
48 throw std::ios_base::failure(
"AutoFile::seek: ftell failed");
56 if (!
m_position.has_value())
throw std::ios_base::failure(
"AutoFile::tell: position unknown");
63 throw std::ios_base::failure(
"AutoFile::size: file handle is nullptr");
77 throw std::ios_base::failure(
feof() ?
"AutoFile::read: end of file" :
"AutoFile::read: fread failed");
83 if (!
m_file)
throw std::ios_base::failure(
"AutoFile::ignore: file handle is nullptr");
84 unsigned char data[4096];
86 size_t nNow = std::min<size_t>(nSize,
sizeof(data));
88 throw std::ios_base::failure(
feof() ?
"AutoFile::ignore: end of file" :
"AutoFile::ignore: fread failed");
97 if (!
m_file)
throw std::ios_base::failure(
"AutoFile::write: file handle is nullptr");
100 throw std::ios_base::failure(
"AutoFile::write: write failed");
105 std::array<std::byte, 4096> buf;
107 auto buf_now{std::span{buf}.first(std::min<size_t>(
src.size(), buf.size()))};
117 if (!
m_file)
throw std::ios_base::failure(
"AutoFile::write_buffer: file handle is nullptr");
119 if (!
m_position)
throw std::ios_base::failure(
"AutoFile::write_buffer: obfuscation position unknown");
123 throw std::ios_base::failure(
"AutoFile::write_buffer: write failed");
131 return ::FileCommit(
m_file);
void ignore(size_t nSize)
void write_buffer(std::span< std::byte > src)
Write a mutable buffer more efficiently than write(), obfuscating the buffer in-place.
void seek(int64_t offset, int origin)
Wrapper around fseek().
int64_t size()
Return the size of the file.
std::size_t detail_fread(std::span< std::byte > dst)
Implementation detail, only used internally.
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
AutoFile(std::FILE *file, const Obfuscation &obfuscation={})
bool Truncate(unsigned size)
Wrapper around TruncateFile().
void write(std::span< const std::byte > src)
std::optional< int64_t > m_position
Obfuscation m_obfuscation
int64_t tell()
Find position within the file.
bool Commit()
Wrapper around FileCommit().
void read(std::span< std::byte > dst)
size_t GetMemoryUsage() const noexcept
Compute total memory usage of this object (own memory + any dynamic memory).
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.