Skip to content

Commit

Permalink
libcore|Writer: Added method for writing text
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Mar 20, 2016
1 parent 647e661 commit 4eef6bb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
21 changes: 19 additions & 2 deletions doomsday/sdk/libcore/include/de/data/writer.h
Expand Up @@ -14,7 +14,7 @@
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
* http://www.gnu.org/licenses</small>
*/

#ifndef LIBDENG2_WRITER_H
Expand Down Expand Up @@ -134,9 +134,26 @@ class DENG2_PUBLIC Writer
Writer &operator << (ddouble const &value);
//@}

/// Write a string to the destination buffer.
/**
* Writes a string and its size to the destination buffer.
*
* The text is written as UTF-8 and is prefixed by the 32-bit size of the text. This
* is intended for use when writing binary content.
*
* @param text Text string.
*/
Writer &operator << (String const &text);

/**
* Writes a string to the destination buffer.
*
* The text is written as UTF-8 and is @em NOT prefixed by the size. This is intended
* for writing text content.
*
* @param text Text string.
*/
Writer &writeText(String const &text);

/**
* Writes a sequence bytes to the destination buffer. The size of the byte
* array is included in the written data.
Expand Down
20 changes: 14 additions & 6 deletions doomsday/sdk/libcore/src/data/writer.cpp
Expand Up @@ -14,7 +14,7 @@
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
* http://www.gnu.org/licenses</small>
*/

#include "de/Writer"
Expand All @@ -26,6 +26,7 @@
#include "de/ByteRefArray"
#include "de/ByteArrayFile"
#include "de/data/byteorder.h"

#include <QScopedPointer>

namespace de {
Expand Down Expand Up @@ -132,7 +133,7 @@ Writer &Writer::operator << (duchar const &byte)
Writer &Writer::operator << (dint16 const &word)
{
return *this << static_cast<duint16>(word);
}
}

Writer &Writer::operator << (duint16 const &word)
{
Expand All @@ -145,7 +146,7 @@ Writer &Writer::operator << (duint16 const &word)
Writer &Writer::operator << (dint32 const &dword)
{
return *this << static_cast<duint32>(dword);
}
}

Writer &Writer::operator << (duint32 const &dword)
{
Expand All @@ -158,7 +159,7 @@ Writer &Writer::operator << (duint32 const &dword)
Writer &Writer::operator << (dint64 const &qword)
{
return *this << static_cast<duint64>(qword);
}
}

Writer &Writer::operator << (duint64 const &qword)
{
Expand All @@ -183,13 +184,20 @@ Writer &Writer::operator << (String const &text)
Block bytes = text.toUtf8();

// First write the length of the text.
duint size = bytes.size();
duint32 size = bytes.size();
*this << size;

d->write(bytes.data(), size);
return *this;
}

Writer &Writer::writeText(String const &text)
{
Block const utf8 = text.toUtf8();
d->write(utf8.dataConst(), utf8.size());
return *this;
}

Writer &Writer::operator << (IByteArray const &byteArray)
{
// First write the length of the array.
Expand All @@ -203,7 +211,7 @@ Writer &Writer::operator << (FixedByteArray const &fixedByteArray)
* that the source data actually exists anywhere. The object implementing
* IByteArray could be generating it on the fly.
*/

// Read the entire contents of the array.
Block copy(fixedByteArray);
d->write(copy.data(), copy.size());
Expand Down

0 comments on commit 4eef6bb

Please sign in to comment.