From 64ae92b16f6ea8d067ccc8016d8b4b7e5c9d0894 Mon Sep 17 00:00:00 2001 From: David Given Date: Sun, 14 Apr 2019 14:34:59 +0200 Subject: [PATCH] Add a few more useful functions. --- lib/bytes.cc | 10 ++++++++++ lib/bytes.h | 15 ++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/bytes.cc b/lib/bytes.cc index 3a926e67..fa651430 100644 --- a/lib/bytes.cc +++ b/lib/bytes.cc @@ -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; diff --git a/lib/bytes.h b/lib/bytes.h index 6408bd0c..0dc5ebb9 100644 --- a/lib/bytes.h +++ b/lib/bytes.h @@ -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; };