12 : m_file{file}, m_xor{
std::move(data_xor)}
15 auto pos{std::ftell(
m_file)};
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());
106 return ::FileCommit(
m_file);
116 return ::TruncateFile(
m_file, size);
void ignore(size_t nSize)
std::vector< std::byte > m_xor
void seek(int64_t offset, int origin)
void read(Span< std::byte > dst)
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
bool Truncate(unsigned size)
std::size_t detail_fread(Span< std::byte > dst)
Implementation detail, only used internally.
std::optional< int64_t > m_position
AutoFile(std::FILE *file, std::vector< std::byte > data_xor={})
void write(Span< const std::byte > src)
A Span is an object that can refer to a contiguous sequence of objects.
constexpr std::size_t size() const noexcept
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset) const noexcept
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
constexpr C * data() const noexcept
constexpr C * begin() const noexcept
void Xor(Span< std::byte > write, Span< const std::byte > key, size_t key_offset=0)