mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-10 14:08:40 +01:00
Make BufferedFile to be a CAutoFile wrapper
This refactor allows to forward some calls to the underlying CAutoFile, instead of re-implementing the logic in the buffered file.
This commit is contained in:
@@ -571,7 +571,7 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
/** Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to
|
||||
/** Wrapper around a CAutoFile& that implements a ring buffer to
|
||||
* deserialize from. It guarantees the ability to rewind a given number of bytes.
|
||||
*
|
||||
* Will automatically close the file when it goes out of scope if not null.
|
||||
@@ -580,9 +580,7 @@ public:
|
||||
class BufferedFile
|
||||
{
|
||||
private:
|
||||
const int nVersion;
|
||||
|
||||
FILE *src; //!< source file
|
||||
CAutoFile& m_src;
|
||||
uint64_t nSrcPos{0}; //!< how many bytes have been read from source
|
||||
uint64_t m_read_pos{0}; //!< how many bytes have been read from this
|
||||
uint64_t nReadLimit; //!< up to which position we're allowed to read
|
||||
@@ -598,9 +596,9 @@ private:
|
||||
readNow = nAvail;
|
||||
if (readNow == 0)
|
||||
return false;
|
||||
size_t nBytes = fread((void*)&vchBuf[pos], 1, readNow, src);
|
||||
size_t nBytes{m_src.detail_fread(Span{vchBuf}.subspan(pos, readNow))};
|
||||
if (nBytes == 0) {
|
||||
throw std::ios_base::failure(feof(src) ? "BufferedFile::Fill: end of file" : "BufferedFile::Fill: fread failed");
|
||||
throw std::ios_base::failure{m_src.feof() ? "BufferedFile::Fill: end of file" : "BufferedFile::Fill: fread failed"};
|
||||
}
|
||||
nSrcPos += nBytes;
|
||||
return true;
|
||||
@@ -629,19 +627,18 @@ private:
|
||||
}
|
||||
|
||||
public:
|
||||
BufferedFile(FILE* fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nVersionIn)
|
||||
: nVersion{nVersionIn}, nReadLimit{std::numeric_limits<uint64_t>::max()}, nRewind{nRewindIn}, vchBuf(nBufSize, std::byte{0})
|
||||
BufferedFile(CAutoFile& file, uint64_t nBufSize, uint64_t nRewindIn)
|
||||
: m_src{file}, nReadLimit{std::numeric_limits<uint64_t>::max()}, nRewind{nRewindIn}, vchBuf(nBufSize, std::byte{0})
|
||||
{
|
||||
if (nRewindIn >= nBufSize)
|
||||
throw std::ios_base::failure("Rewind limit must be less than buffer size");
|
||||
src = fileIn;
|
||||
}
|
||||
|
||||
int GetVersion() const { return nVersion; }
|
||||
int GetVersion() const { return m_src.GetVersion(); }
|
||||
|
||||
//! check whether we're at the end of the source file
|
||||
bool eof() const {
|
||||
return m_read_pos == nSrcPos && feof(src);
|
||||
return m_read_pos == nSrcPos && m_src.feof();
|
||||
}
|
||||
|
||||
//! read a number of bytes
|
||||
|
||||
Reference in New Issue
Block a user