mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-18 22:35:39 +01:00
Add AutoFile::seek and tell
It's useful to be able to seek to a specific position in a file. Allow AutoFile to seek by using fseek. It's also useful to be able to get the current position in a file. Allow AutoFile to tell by using ftell.
This commit is contained in:
@@ -21,6 +21,28 @@ std::size_t AutoFile::detail_fread(Span<std::byte> dst)
|
||||
}
|
||||
}
|
||||
|
||||
void AutoFile::seek(int64_t offset, int origin)
|
||||
{
|
||||
if (IsNull()) {
|
||||
throw std::ios_base::failure("AutoFile::seek: file handle is nullptr");
|
||||
}
|
||||
if (std::fseek(m_file, offset, origin) != 0) {
|
||||
throw std::ios_base::failure(feof() ? "AutoFile::seek: end of file" : "AutoFile::seek: fseek failed");
|
||||
}
|
||||
}
|
||||
|
||||
int64_t AutoFile::tell()
|
||||
{
|
||||
if (IsNull()) {
|
||||
throw std::ios_base::failure("AutoFile::tell: file handle is nullptr");
|
||||
}
|
||||
int64_t r{std::ftell(m_file)};
|
||||
if (r < 0) {
|
||||
throw std::ios_base::failure("AutoFile::tell: ftell failed");
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
void AutoFile::read(Span<std::byte> dst)
|
||||
{
|
||||
if (detail_fread(dst) != dst.size()) {
|
||||
|
||||
Reference in New Issue
Block a user