Skip to content

Commit

Permalink
libdeng2|Reader|Write: Reading/writing fixed-size byte arrays
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
skyjake committed Mar 28, 2014
1 parent 2ed471f commit ff7f3f4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions doomsday/libdeng2/include/de/data/block.h
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions doomsday/libdeng2/include/de/data/reader.h
Expand Up @@ -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);

Expand Down
24 changes: 24 additions & 0 deletions doomsday/libdeng2/include/de/data/writer.h
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions doomsday/libdeng2/src/data/reader.cpp
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions doomsday/libdeng2/src/data/writer.cpp
Expand Up @@ -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.
Expand Down

0 comments on commit ff7f3f4

Please sign in to comment.