12 : m_file{file}, m_xor{std::move(data_xor)}
15 auto pos{std::ftell(m_file)};
16 if (pos >= 0) m_position = pos;
22 if (!
m_file)
throw std::ios_base::failure(
"AutoFile::read: file handle is nullptr");
25 if (!
m_position.has_value())
throw std::ios_base::failure(
"AutoFile::read: position unknown");
35 throw std::ios_base::failure(
"AutoFile::seek: file handle is nullptr");
37 if (std::fseek(
m_file, offset, origin) != 0) {
38 throw std::ios_base::failure(
feof() ?
"AutoFile::seek: end of file" :
"AutoFile::seek: fseek failed");
40 if (origin == SEEK_SET) {
42 }
else if (origin == SEEK_CUR &&
m_position.has_value()) {
45 int64_t r{std::ftell(
m_file)};
47 throw std::ios_base::failure(
"AutoFile::seek: ftell failed");
55 if (!
m_position.has_value())
throw std::ios_base::failure(
"AutoFile::tell: position unknown");
62 throw std::ios_base::failure(
feof() ?
"AutoFile::read: end of file" :
"AutoFile::read: fread failed");
68 if (!
m_file)
throw std::ios_base::failure(
"AutoFile::ignore: file handle is nullptr");
69 unsigned char data[4096];
71 size_t nNow = std::min<size_t>(nSize,
sizeof(
data));
72 if (std::fread(
data, 1, nNow,
m_file) != nNow) {
73 throw std::ios_base::failure(
feof() ?
"AutoFile::ignore: end of file" :
"AutoFile::ignore: fread failed");
82 if (!
m_file)
throw std::ios_base::failure(
"AutoFile::write: file handle is nullptr");
85 throw std::ios_base::failure(
"AutoFile::write: write failed");
89 if (!
m_position.has_value())
throw std::ios_base::failure(
"AutoFile::write: position unknown");
90 std::array<std::byte, 4096> buf;
91 while (src.
size() > 0) {
92 auto buf_now{
Span{buf}.
first(std::min<size_t>(src.
size(), buf.size()))};
93 std::copy(src.
begin(), src.
begin() + buf_now.size(), buf_now.begin());
95 if (std::fwrite(buf_now.data(), 1, buf_now.size(),
m_file) != buf_now.size()) {
96 throw std::ios_base::failure{
"XorFile::write: failed"};
98 src = src.
subspan(buf_now.size());
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset) const noexcept
constexpr std::size_t size() const noexcept
std::optional< int64_t > m_position
void Xor(Span< std::byte > write, Span< const std::byte > key, size_t key_offset=0)
std::vector< std::byte > m_xor
void read(Span< std::byte > dst)
constexpr C * begin() const noexcept
bool Truncate(unsigned size)
bool TruncateFile(FILE *file, unsigned int length)
constexpr C * data() const noexcept
AutoFile(std::FILE *file, std::vector< std::byte > data_xor={})
void seek(int64_t offset, int origin)
A Span is an object that can refer to a contiguous sequence of objects.
std::size_t detail_fread(Span< std::byte > dst)
Implementation detail, only used internally.
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
void write(Span< const std::byte > src)
void ignore(size_t nSize)
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.