MsgPackStream: writeBytes() added

This commit is contained in:
Isaikin Roman
2015-05-29 20:15:26 +03:00
parent 7d5d72e814
commit 9e61498ca2
3 changed files with 29 additions and 2 deletions

3
.gitignore vendored
View File

@ -1,6 +1,7 @@
*.pro.user*
build
lib
Makefile
Makefile*
*.autosave
bin/
*.o

View File

@ -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) {

View File

@ -1,6 +1,7 @@
#ifndef STREAM_H
#define STREAM_H
#include <QIODevice>
#include <limits.h>
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 <typename T>
MsgPackStream& operator<<(MsgPackStream& s, const QList<T> &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<quint16>::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;