From 9e61498ca2daa06e2fec11390e927f785542ed0a Mon Sep 17 00:00:00 2001 From: Isaikin Roman Date: Fri, 29 May 2015 20:15:26 +0300 Subject: [PATCH] MsgPackStream: writeBytes() added --- .gitignore | 3 ++- src/stream.cpp | 8 ++++++++ src/stream.h | 20 +++++++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index c5ec2ea..3e5bad7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *.pro.user* build lib -Makefile +Makefile* *.autosave bin/ +*.o diff --git a/src/stream.cpp b/src/stream.cpp index 81a683d..a4a68a6 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -442,6 +442,14 @@ MsgPackStream &MsgPackStream::operator<<(QByteArray array) return *this; } +bool MsgPackStream::writeBytes(const char *data, uint len) +{ + CHECK_STREAM_WRITE_PRECOND(*this); + if (dev->write(data, len) != len) + setStatus(WriteFailed); + return *this; +} + bool MsgPackStream::unpack_upto_quint8(quint8 &u8, quint8 *p) { if (*p <= MsgPack::FirstByte::POSITIVE_FIXINT) { diff --git a/src/stream.h b/src/stream.h index d84f874..0b733d2 100644 --- a/src/stream.h +++ b/src/stream.h @@ -1,6 +1,7 @@ #ifndef STREAM_H #define STREAM_H #include +#include class MsgPackStream { @@ -44,6 +45,8 @@ public: MsgPackStream &operator<<(QString str); MsgPackStream &operator<<(const char *str); MsgPackStream &operator<<(QByteArray array); + bool writeBytes(const char *data, uint len); + private: QIODevice *dev; @@ -63,7 +66,22 @@ private: template MsgPackStream& operator<<(MsgPackStream& s, const QList &list) { - s << (quint32)list.size(); + quint32 len = list.size(); + quint8 p[5]; + if (len <= 15) { + p[0] = 0x90 | len; + s.writeBytes(p, 1); + } else if (len <= std::numeric_limits::max()) { + p[0] = 0xdc; + _msgpack_store16(p + 1, len); + s.writeBytes(p, 3); + } else { + p[0] = 0xdd; + _msgpack_store32(p + 1, len); + s.writeBytes(p, 5); + } + if (s.status() != MsgPackStream::Ok) + return *this; for (int i = 0; i < list.size(); ++i) s << list[i]; return s;