streams: Base BufferedFile on AutoFile instead of CAutoFile

This commit is contained in:
Anthony Towns 2023-11-15 13:07:14 +10:00
parent 98b0acda0f
commit e63f643079
3 changed files with 4 additions and 11 deletions

View File

@ -529,7 +529,7 @@ public:
} }
}; };
/** Wrapper around a CAutoFile& that implements a ring buffer to /** Wrapper around an AutoFile& that implements a ring buffer to
* deserialize from. It guarantees the ability to rewind a given number of bytes. * 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. * Will automatically close the file when it goes out of scope if not null.
@ -538,7 +538,7 @@ public:
class BufferedFile class BufferedFile
{ {
private: private:
CAutoFile& m_src; AutoFile& m_src;
uint64_t nSrcPos{0}; //!< how many bytes have been read from source 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 m_read_pos{0}; //!< how many bytes have been read from this
uint64_t nReadLimit; //!< up to which position we're allowed to read uint64_t nReadLimit; //!< up to which position we're allowed to read
@ -585,15 +585,13 @@ private:
} }
public: public:
BufferedFile(CAutoFile& file, uint64_t nBufSize, uint64_t nRewindIn) BufferedFile(AutoFile& file, uint64_t nBufSize, uint64_t nRewindIn)
: m_src{file}, nReadLimit{std::numeric_limits<uint64_t>::max()}, nRewind{nRewindIn}, vchBuf(nBufSize, std::byte{0}) : m_src{file}, nReadLimit{std::numeric_limits<uint64_t>::max()}, nRewind{nRewindIn}, vchBuf(nBufSize, std::byte{0})
{ {
if (nRewindIn >= nBufSize) if (nRewindIn >= nBufSize)
throw std::ios_base::failure("Rewind limit must be less than buffer size"); throw std::ios_base::failure("Rewind limit must be less than buffer size");
} }
int GetVersion() const { return m_src.GetVersion(); }
//! check whether we're at the end of the source file //! check whether we're at the end of the source file
bool eof() const { bool eof() const {
return m_read_pos == nSrcPos && m_src.feof(); return m_read_pos == nSrcPos && m_src.feof();

View File

@ -20,9 +20,8 @@ FUZZ_TARGET(buffered_file)
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider}; FuzzedFileProvider fuzzed_file_provider{fuzzed_data_provider};
std::optional<BufferedFile> opt_buffered_file; std::optional<BufferedFile> opt_buffered_file;
CAutoFile fuzzed_file{ AutoFile fuzzed_file{
fuzzed_file_provider.open(), fuzzed_file_provider.open(),
0,
ConsumeRandomLengthByteVector<std::byte>(fuzzed_data_provider), ConsumeRandomLengthByteVector<std::byte>(fuzzed_data_provider),
}; };
try { try {
@ -65,6 +64,5 @@ FUZZ_TARGET(buffered_file)
}); });
} }
opt_buffered_file->GetPos(); opt_buffered_file->GetPos();
opt_buffered_file->GetVersion();
} }
} }

View File

@ -271,9 +271,6 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file)
BufferedFile bf{file, 25, 10}; BufferedFile bf{file, 25, 10};
BOOST_CHECK(!bf.eof()); BOOST_CHECK(!bf.eof());
// This member has no functional effect.
BOOST_CHECK_EQUAL(bf.GetVersion(), 333);
uint8_t i; uint8_t i;
bf >> i; bf >> i;
BOOST_CHECK_EQUAL(i, 0); BOOST_CHECK_EQUAL(i, 0);