Browse Source

Add BinaryReader::readRemainingBytes()

charconv
Martchus 2 years ago
parent
commit
9a95db3773
  1. 4
      CMakeLists.txt
  2. 22
      io/binaryreader.cpp
  3. 1
      io/binaryreader.h
  4. 1
      tests/iotests.cpp

4
CMakeLists.txt

@ -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)

22
io/binaryreader.cpp

@ -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;

1
io/binaryreader.h

@ -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);

1
tests/iotests.cpp

@ -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);

Loading…
Cancel
Save