From ff7f3f4bbce1a3151fee4f4ea4f8083560084726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Ker=C3=A4nen?= Date: Fri, 28 Mar 2014 08:53:03 +0200 Subject: [PATCH] libdeng2|Reader|Write: Reading/writing fixed-size byte arrays Added more convenient methods for reading and writing byte arrays of preset size, which means the size itself won't be read/written, only the bytes. --- doomsday/libdeng2/include/de/data/block.h | 5 +++++ doomsday/libdeng2/include/de/data/reader.h | 11 ++++++++++ doomsday/libdeng2/include/de/data/writer.h | 24 ++++++++++++++++++++++ doomsday/libdeng2/src/data/reader.cpp | 6 ++++++ doomsday/libdeng2/src/data/writer.cpp | 10 +++++++++ 5 files changed, 56 insertions(+) diff --git a/doomsday/libdeng2/include/de/data/block.h b/doomsday/libdeng2/include/de/data/block.h index 865aab7594..7643c94d82 100644 --- a/doomsday/libdeng2/include/de/data/block.h +++ b/doomsday/libdeng2/include/de/data/block.h @@ -32,6 +32,11 @@ class IIStream; /** * Data buffer that implements the byte array interface. * + * Note that Block is based on QByteArray, and thus automatically always ensures + * that the data is followed by a terminating \0 character (even if one is not + * part of the actual Block contents). Therefore it is safe to use it in functions + * that assume zero-terminated strings. + * * @ingroup data */ class DENG2_PUBLIC Block : public QByteArray, public IByteArray, public IBlock diff --git a/doomsday/libdeng2/include/de/data/reader.h b/doomsday/libdeng2/include/de/data/reader.h index 1653bf4649..b3014eb48f 100644 --- a/doomsday/libdeng2/include/de/data/reader.h +++ b/doomsday/libdeng2/include/de/data/reader.h @@ -168,6 +168,17 @@ class DENG2_PUBLIC Reader */ Reader &readBytes(dsize count, IByteArray &destination); + /** + * Reads a fixed number of bytes and puts them into a destination + * byte array. The complete @a destination is filled with new bytes; + * its size won't change. + * + * @param destination Destination array. The size of this array + * determines how many bytes to read. + * @return Reference to the Reader. + */ + Reader &readPresetSize(IByteArray &destination); + /// Reads a Block from the source buffer. Reader &operator >> (Block &block); diff --git a/doomsday/libdeng2/include/de/data/writer.h b/doomsday/libdeng2/include/de/data/writer.h index 3bc91e815f..a108dd60c9 100644 --- a/doomsday/libdeng2/include/de/data/writer.h +++ b/doomsday/libdeng2/include/de/data/writer.h @@ -157,6 +157,30 @@ class DENG2_PUBLIC Writer */ Writer &operator << (FixedByteArray const &fixedByteArray); + /** + * Writes a fixed-size sequence of bytes to the destination buffer. + * The size of the sequence is @em NOT included in the written data. + * When reading, the reader must know the size beforehand. + * + * @param count Number of bytes to write. + * @param array Data to write. + * + * @return Reference to the Writer. + */ + Writer &writeBytes(dsize count, IByteArray const &array); + + /** + * Writes a fixed-size sequence of bytes to the destination buffer. + * The size of the sequence is @em NOT included in the written data. + * When reading, the reader must know the size beforehand + * (Reader::readPresetSize()). + * + * @param array Array to write. + * + * @return Reference to the Writer. + */ + Writer &writePresetSize(IByteArray const &array); + /** * Writes @a block into the destination buffer. Writes the size of the * block in addition to its contents, so a Reader will not need to know diff --git a/doomsday/libdeng2/src/data/reader.cpp b/doomsday/libdeng2/src/data/reader.cpp index 0de305edbc..bcd00f4e1f 100644 --- a/doomsday/libdeng2/src/data/reader.cpp +++ b/doomsday/libdeng2/src/data/reader.cpp @@ -353,6 +353,12 @@ Reader &Reader::readBytes(dsize count, IByteArray &destination) return *this >> dest; } +Reader &Reader::readPresetSize(IByteArray &destination) +{ + FixedByteArray dest(destination); + return *this >> dest; +} + Reader &Reader::operator >> (Block &block) { duint size = 0; diff --git a/doomsday/libdeng2/src/data/writer.cpp b/doomsday/libdeng2/src/data/writer.cpp index a41f8e436c..077539e29b 100644 --- a/doomsday/libdeng2/src/data/writer.cpp +++ b/doomsday/libdeng2/src/data/writer.cpp @@ -210,6 +210,16 @@ Writer &Writer::operator << (FixedByteArray const &fixedByteArray) return *this; } +Writer &Writer::writeBytes(dsize count, IByteArray const &array) +{ + return *this << FixedByteArray(array, 0, count); +} + +Writer &Writer::writePresetSize(IByteArray const &array) +{ + return *this << FixedByteArray(array); +} + Writer &Writer::operator << (Block const &block) { // First write the length of the block.