Add BinaryReader::readRemainingBytes()

This commit is contained in:
Martchus 2019-10-30 20:25:21 +01:00
parent 2cdde81077
commit 9a95db3773
4 changed files with 22 additions and 6 deletions

View File

@ -112,8 +112,8 @@ set(META_APP_URL "https://github.com/${META_APP_AUTHOR}/${META_PROJECT_NAME}")
set(META_APP_DESCRIPTION "Useful C++ classes and routines such as argument parser, IO and conversion utilities")
set(META_FEATURES_FOR_COMPILER_DETECTION_HEADER cxx_thread_local)
set(META_VERSION_MAJOR 5)
set(META_VERSION_MINOR 0)
set(META_VERSION_PATCH 2)
set(META_VERSION_MINOR 1)
set(META_VERSION_PATCH 0)
# find required 3rd party libraries
include(3rdParty)

View File

@ -44,10 +44,9 @@ void BinaryReader::setStream(istream *stream, bool giveOwnership)
/*!
* \brief Returns the size of the assigned stream.
*
* The size is determined by seeking to the end of the stream and returning this offset.
*
* \remarks The method will seek back to the previous offset before returning.
* \remarks
* - The size is determined by seeking to the end of the stream and returning this offset.
* - The method will seek back to the previous offset before returning.
*/
istream::pos_type BinaryReader::readStreamsize()
{
@ -58,6 +57,21 @@ istream::pos_type BinaryReader::readStreamsize()
return streamsize;
}
/*!
* \brief Returns the number of remaining bytes in the stream from the current offset.
* \remarks
* - This is achieved by seeking to the end of the stream.
* - The method will seek back to the previous offset before returning.
*/
istream::pos_type BinaryReader::readRemainingBytes()
{
istream::pos_type cp = m_stream->tellg();
m_stream->seekg(0, ios_base::end);
const auto streamsize = m_stream->tellg();
m_stream->seekg(cp);
return streamsize - cp;
}
void BinaryReader::bufferVariableLengthInteger()
{
static constexpr int maxPrefixLength = 8;

View File

@ -26,6 +26,7 @@ public:
bool eof() const;
bool canRead() const;
std::istream::pos_type readStreamsize();
std::istream::pos_type readRemainingBytes();
void read(char *buffer, std::streamsize length);
void read(std::uint8_t *buffer, std::streamsize length);
void read(std::vector<char> &buffer, std::streamsize length);

View File

@ -92,6 +92,7 @@ void IoTests::testBinaryReader()
BinaryReader reader(&testFile);
CPPUNIT_ASSERT_EQUAL(reader.readStreamsize(), static_cast<istream::pos_type>(398));
CPPUNIT_ASSERT(reader.readUInt16LE() == 0x0102u);
CPPUNIT_ASSERT_EQUAL(reader.readRemainingBytes(), static_cast<istream::pos_type>(396));
CPPUNIT_ASSERT(reader.readUInt16BE() == 0x0102u);
CPPUNIT_ASSERT(reader.readUInt24LE() == 0x010203u);
CPPUNIT_ASSERT(reader.readUInt24BE() == 0x010203u);