Add a few more useful functions.

This commit is contained in:
David Given
2019-04-14 14:34:59 +02:00
parent 1747ef1f74
commit 64ae92b16f
2 changed files with 24 additions and 1 deletions

View File

@@ -273,6 +273,16 @@ ByteWriter Bytes::writer()
return ByteWriter(*this);
}
ByteWriter& ByteWriter::operator +=(std::istream& stream)
{
Bytes buffer(4096);
while (stream.read((char*) buffer.begin(), buffer.size()))
this->append(buffer);
this->append(buffer.slice(0, stream.gcount()));
return *this;
}
void BitWriter::push(uint32_t bits, size_t size)
{
bits <<= 32-size;

View File

@@ -69,7 +69,7 @@ public:
unsigned pos = 0;
bool eof() const
{ return pos == _bytes.size(); }
{ return pos >= _bytes.size(); }
ByteReader& seek(unsigned pos)
{
@@ -77,6 +77,12 @@ public:
return *this;
}
ByteReader& skip(int delta)
{
this->pos += delta;
return *this;
}
const Bytes read(unsigned len)
{
const Bytes bytes = _bytes.slice(pos, len);
@@ -256,11 +262,18 @@ public:
return *this;
}
ByteWriter& operator += (std::istream& stream);
ByteWriter& append(const Bytes data)
{
return *this += data;
}
ByteWriter& append(std::istream& stream)
{
return *this += stream;
}
private:
Bytes& _bytes;
};