MsgPackStream QString and const char * added

Qt types: QPoint, QSize, QRect implemented using MsgPackStream
MsgPackStream QString tests added
This commit is contained in:
Roman
2015-05-19 22:41:22 +03:00
parent b7fbf6413e
commit f978fb0d78
11 changed files with 190 additions and 141 deletions

2
.gitignore vendored
View File

@@ -1,4 +1,4 @@
*.pro.user *.pro.user*
build build
lib lib
Makefile Makefile

View File

@@ -10,14 +10,14 @@
namespace MsgPack { namespace MsgPack {
/** /**
* @brief pack user type to byte array data * pack some variant to byte array data
* @arg variant user type, must be registered first * when write == false only calculates and returns size
* @return array with user data only, all other fields will be added automatically * when write == true writes bytes to data, and returns the same size
* return type size
*/ */
typedef QByteArray (*pack_user_f)(const QVariant &variant); typedef QByteArray (*pack_user_f)(const QVariant &variant);
/** /**
* @brief unpack user type to QVariant * unpack user type to QVariant
* @arg data only user data, without size and messagepack type
*/ */
typedef QVariant (*unpack_user_f)(const QByteArray &data); typedef QVariant (*unpack_user_f)(const QByteArray &data);
/** /**

View File

@@ -10,14 +10,14 @@
namespace MsgPack { namespace MsgPack {
/** /**
* pack some variant to byte array data * @brief pack user type to byte array data
* when write == false only calculates and returns size * @arg variant user type, must be registered first
* when write == true writes bytes to data, and returns the same size * @return array with user data only, all other fields will be added automatically
* return type size
*/ */
typedef quint32 (*pack_user_f)(const QVariant &variant, QByteArray &data, bool write); typedef QByteArray (*pack_user_f)(const QVariant &variant);
/** /**
* unpack user type to QVariant * @brief unpack user type to QVariant
* @arg data only user data, without size and messagepack type
*/ */
typedef QVariant (*unpack_user_f)(const QByteArray &data); typedef QVariant (*unpack_user_f)(const QByteArray &data);
/** /**

View File

@@ -175,9 +175,8 @@ quint8 *MsgPackPrivate::pack_stringlist(const QStringList &list, quint8 *p, bool
return p; return p;
} }
quint8 *MsgPackPrivate::pack_string(const QString &str, quint8 *p, bool wr) quint8 *MsgPackPrivate::pack_string_raw(const char *str, quint32 len, quint8 *p, bool wr)
{ {
int len = str.length();
if (len <= 31) { if (len <= 31) {
if (wr) *p = 0xa0 | len; if (wr) *p = 0xa0 | len;
p++; p++;
@@ -198,11 +197,18 @@ quint8 *MsgPackPrivate::pack_string(const QString &str, quint8 *p, bool wr)
if (wr) _msgpack_store32(p, len); if (wr) _msgpack_store32(p, len);
p += 4; p += 4;
} }
if (wr) memcpy(p, str.toUtf8().data(), len); if (wr) memcpy(p, str, len);
return p + len; return p + len;
} }
quint8 *MsgPackPrivate::pack_string(const QString &str, quint8 *p, bool wr)
{
QByteArray str_data = str.toUtf8();
quint32 str_len = str_data.length();
return pack_string_raw(str_data.data(), str_len, p, wr);
}
quint8 *MsgPackPrivate::pack_double(double i, quint8 *p, bool wr) quint8 *MsgPackPrivate::pack_double(double i, quint8 *p, bool wr)
{ {
if (wr) *p = 0xcb; if (wr) *p = 0xcb;

View File

@@ -36,6 +36,7 @@ quint8 * pack_arraylen(quint32 len, quint8 *p, bool wr);
quint8 * pack_array(const QVariantList &list, quint8 *p, bool wr, QVector<QByteArray> &user_data); quint8 * pack_array(const QVariantList &list, quint8 *p, bool wr, QVector<QByteArray> &user_data);
quint8 * pack_stringlist(const QStringList &list, quint8 *p, bool wr); quint8 * pack_stringlist(const QStringList &list, quint8 *p, bool wr);
quint8 * pack_string_raw(const char *str, quint32 len, quint8 *p, bool wr);
quint8 * pack_string(const QString &str, quint8 *p, bool wr); quint8 * pack_string(const QString &str, quint8 *p, bool wr);
quint8 * pack_double(double i, quint8 *p, bool wr); quint8 * pack_double(double i, quint8 *p, bool wr);
quint8 * pack_bin(const QByteArray &arr, quint8 *p, bool wr); quint8 * pack_bin(const QByteArray &arr, quint8 *p, bool wr);

View File

@@ -1,6 +1,7 @@
#include "qt_types_p.h" #include "qt_types_p.h"
#include "pack_p.h" #include "pack_p.h"
#include "unpack_p.h" #include "unpack_p.h"
#include "stream.h"
#include "sysdep.h" #include "sysdep.h"
#include <QDebug> #include <QDebug>
@@ -169,90 +170,59 @@ QVariant MsgPackPrivate::unpack_qdatetime(const QByteArray &data)
// Points and Vectors // Points and Vectors
QByteArray MsgPackPrivate::pack_qpoint(const QVariant &variant) QByteArray MsgPackPrivate::pack_qpoint(const QVariant &variant)
{ {
// QPoint pt = variant.toPoint(); QByteArray packed;
// quint8 size = pack_two_integers(pt.x(), pt.y(), 0, false); MsgPackStream stream(&packed, QIODevice::WriteOnly);
// if (write) { QPoint pt = variant.toPoint();
// data.resize(size); stream << pt.x() << pt.y();
// pack_two_integers(pt.x(), pt.y(), (quint8 *)data.data(), true); return packed;
// }
// return size;
} }
QVariant MsgPackPrivate::unpack_qpoint(const QByteArray &data) QVariant MsgPackPrivate::unpack_qpoint(const QByteArray &data)
{ {
// quint8 *p = (quint8 *)data.data(); MsgPackStream stream(data);
// qint32 x; qint32 x, y;
// bool ok; stream >> x >> y;
// p = MsgPack::Ext::unpack_upto_qint32(&x, p, &ok); qDebug() << "unpack qpoint stream" << (stream.status() == MsgPackStream::Ok);
// QPoint pt; return QPoint(x, y);
// pt.setX(x);
// MsgPack::Ext::unpack_upto_qint32(&x, p, &ok);
// pt.setY(x);
// return pt;
} }
QByteArray MsgPackPrivate::pack_qsize(const QVariant &variant) QByteArray MsgPackPrivate::pack_qsize(const QVariant &variant)
{ {
// QSize sz = variant.toSize(); QByteArray packed;
// quint8 size = pack_two_integers(sz.width(), sz.height(), 0, false); MsgPackStream stream(&packed, QIODevice::WriteOnly);
// if (write) { QSize sz = variant.toSize();
// data.resize(size); stream << sz.width() << sz.height();
// pack_two_integers(sz.width(), sz.height(), (quint8 *)data.data(), true); return packed;
// }
// return size;
} }
QVariant MsgPackPrivate::unpack_qsize(const QByteArray &data) QVariant MsgPackPrivate::unpack_qsize(const QByteArray &data)
{ {
// quint8 *p = (quint8 *)data.data(); MsgPackStream stream(data);
// qint32 x; qint32 width, height;
// bool ok; stream >> width >> height;
// p = MsgPack::Ext::unpack_upto_qint32(&x, p, &ok); return QSize(width, height);
// QSize sz;
// sz.setWidth(x);
// MsgPack::Ext::unpack_upto_qint32(&x, p, &ok);
// sz.setHeight(x);
// return sz;
} }
QByteArray MsgPackPrivate::pack_qrect(const QVariant &variant) QByteArray MsgPackPrivate::pack_qrect(const QVariant &variant)
{ {
// QRect rect = variant.toRect(); QRect rect = variant.toRect();
// QPoint pt1 = rect.topLeft(); QPoint pt1 = rect.topLeft();
// QPoint pt2 = rect.bottomRight(); QPoint pt2 = rect.bottomRight();
// quint8 size = pack_two_integers(pt1.x(), pt1.y(), 0, false); QByteArray packed;
// size += pack_two_integers(pt2.x(), pt2.y(), 0, false); MsgPackStream stream(&packed, QIODevice::WriteOnly);
// if (write) { stream << pt1.x() << pt1.y() << pt2.x() << pt2.y();
// data.resize(size); return packed;
// quint8 *p = (quint8 *)data.data();
// p += pack_two_integers(pt1.x(), pt1.y(), p, true);
// pack_two_integers(pt2.x(), pt2.y(), p, true);
// }
// return size;
} }
QVariant MsgPackPrivate::unpack_qrect(const QByteArray &data) QVariant MsgPackPrivate::unpack_qrect(const QByteArray &data)
{ {
// quint8 *p = (quint8 *)data.data(); MsgPackStream stream(data);
// qint32 x; qint32 x, y;
// bool ok; stream >> x >> y;
QRect rect;
// p = MsgPack::Ext::unpack_upto_qint32(&x, p, &ok); rect.setTopLeft(QPoint(x, y));
// QPoint pt; stream >> x >> y;
// pt.setX(x); rect.setBottomRight(QPoint(x, y));
// p = MsgPack::Ext::unpack_upto_qint32(&x, p, &ok); return rect;
// pt.setY(x);
// QRect rect;
// rect.setTopLeft(pt);
// p = MsgPack::Ext::unpack_upto_qint32(&x, p, &ok);
// pt.setX(x);
// p = MsgPack::Ext::unpack_upto_qint32(&x, p, &ok);
// pt.setY(x);
// rect.setBottomRight(pt);
// return rect;
} }

View File

@@ -26,15 +26,15 @@
return retVal; return retVal;
MsgPackStream::MsgPackStream() : MsgPackStream::MsgPackStream() :
dev(0), compatibility(false), owndev(false), q_status(Ok) dev(0), owndev(false), q_status(Ok)
{ } { }
MsgPackStream::MsgPackStream(QIODevice *d) : MsgPackStream::MsgPackStream(QIODevice *d) :
dev(d), compatibility(false), owndev(false) dev(d), owndev(false)
{ } { }
MsgPackStream::MsgPackStream(QByteArray *a, QIODevice::OpenMode mode) : MsgPackStream::MsgPackStream(QByteArray *a, QIODevice::OpenMode mode) :
compatibility(false), owndev(true), q_status(Ok) owndev(true), q_status(Ok)
{ {
QBuffer *buf = new QBuffer(a); QBuffer *buf = new QBuffer(a);
buf->open(mode); buf->open(mode);
@@ -42,7 +42,7 @@ MsgPackStream::MsgPackStream(QByteArray *a, QIODevice::OpenMode mode) :
} }
MsgPackStream::MsgPackStream(const QByteArray &a) : MsgPackStream::MsgPackStream(const QByteArray &a) :
compatibility(false), owndev(true), q_status(Ok) owndev(true), q_status(Ok)
{ {
QBuffer *buf = new QBuffer(); QBuffer *buf = new QBuffer();
buf->setData(a); buf->setData(a);
@@ -69,11 +69,6 @@ bool MsgPackStream::atEnd() const
return dev ? dev->atEnd() : true; return dev ? dev->atEnd() : true;
} }
void MsgPackStream::setCompatibility(bool isEnabled)
{
compatibility = isEnabled;
}
MsgPackStream::Status MsgPackStream::status() const MsgPackStream::Status MsgPackStream::status() const
{ {
return q_status; return q_status;
@@ -113,7 +108,8 @@ MsgPackStream &MsgPackStream::operator >>(quint8 &u8)
setStatus(ReadPastEnd); setStatus(ReadPastEnd);
return *this; return *this;
} }
unpack_upto_quint8(u8, &p); if (!unpack_upto_quint8(u8, &p))
setStatus(ReadCorruptData);
return *this; return *this;
} }
@@ -125,7 +121,8 @@ MsgPackStream &MsgPackStream::operator>>(quint16 &u16)
setStatus(ReadPastEnd); setStatus(ReadPastEnd);
return *this; return *this;
} }
unpack_upto_quint16(u16, &p); if (!unpack_upto_quint16(u16, &p {
setStatus(ReadCorruptData);
return *this; return *this;
} }
@@ -137,7 +134,8 @@ MsgPackStream &MsgPackStream::operator>>(quint32 &u32)
setStatus(ReadPastEnd); setStatus(ReadPastEnd);
return *this; return *this;
} }
unpack_upto_quint32(u32, &p); if (!unpack_upto_quint32(u32, &p {
setStatus(ReadCorruptData);
return *this; return *this;
} }
@@ -149,7 +147,8 @@ MsgPackStream &MsgPackStream::operator>>(quint64 &u64)
setStatus(ReadPastEnd); setStatus(ReadPastEnd);
return *this; return *this;
} }
unpack_upto_quint64(u64, &p); if (!unpack_upto_quint64(u64, &p {
setStatus(ReadCorruptData);
return *this; return *this;
} }
@@ -161,7 +160,7 @@ MsgPackStream &MsgPackStream::operator>>(qint8 &i8)
setStatus(ReadPastEnd); setStatus(ReadPastEnd);
return *this; return *this;
} }
unpack_upto_qint8(i8, &p); if (!unpack_upto_qint8(i8, &p)) setStatus(ReadCorruptData);
return *this; return *this;
} }
@@ -173,7 +172,8 @@ MsgPackStream &MsgPackStream::operator>>(qint16 &i16)
setStatus(ReadPastEnd); setStatus(ReadPastEnd);
return *this; return *this;
} }
unpack_upto_qint16(i16, &p); if (!unpack_upto_qint16(i16, &p){
setStatus(ReadCorruptData);
return *this; return *this;
} }
@@ -185,7 +185,8 @@ MsgPackStream &MsgPackStream::operator>>(qint32 &i32)
setStatus(ReadPastEnd); setStatus(ReadPastEnd);
return *this; return *this;
} }
unpack_upto_qint32(i32, &p); if (!unpack_upto_qint32(i32, &p){
setStatus(ReadCorruptData);
return *this; return *this;
} }
@@ -197,7 +198,8 @@ MsgPackStream &MsgPackStream::operator>>(qint64 &i64)
setStatus(ReadPastEnd); setStatus(ReadPastEnd);
return *this; return *this;
} }
unpack_upto_qint64(i64, &p); if (!unpack_upto_qint64(i64, &p){
setStatus(ReadCorruptData);
return *this; return *this;
} }
@@ -226,6 +228,14 @@ MsgPackStream &MsgPackStream::operator>>(QString &str)
setStatus(ReadCorruptData); setStatus(ReadCorruptData);
return *this; return *this;
} }
quint8 *data = new quint8[len];
if (dev->read((char *)data, len) != len) {
setStatus(ReadPastEnd);
delete[] data;
return *this;
}
str = QString::fromUtf8((const char*) data, len);
delete[] data;
} }
MsgPackStream &MsgPackStream::operator<<(bool b) MsgPackStream &MsgPackStream::operator<<(bool b)
@@ -284,8 +294,7 @@ MsgPackStream &MsgPackStream::operator<<(QString str)
quint8 *p = (quint8 *)0; quint8 *p = (quint8 *)0;
quint32 sz = MsgPackPrivate::pack_string(str, p, false) - p; quint32 sz = MsgPackPrivate::pack_string(str, p, false) - p;
quint8 *data = new quint8[sz]; quint8 *data = new quint8[sz];
QVector<QByteArray> user_data; MsgPackPrivate::pack_string(str, data, true);
MsgPackPrivate::pack(str, data, true, user_data);
if (dev->write((char *)data, sz) != sz) if (dev->write((char *)data, sz) != sz)
setStatus(WriteFailed); setStatus(WriteFailed);
delete[] data; delete[] data;
@@ -294,21 +303,26 @@ MsgPackStream &MsgPackStream::operator<<(QString str)
MsgPackStream &MsgPackStream::operator<<(const char *str) MsgPackStream &MsgPackStream::operator<<(const char *str)
{ {
CHECK_STREAM_WRITE_PRECOND(*this);
quint8 *p = (quint8 *)0;
quint32 str_len = strlen(str);
quint32 sz = MsgPackPrivate::pack_string_raw(str, str_len, p, false) - p;
quint8 *data = new quint8[sz];
MsgPackPrivate::pack_string_raw(str, str_len, data, true);
if (dev->write((char *)data, sz) != sz)
setStatus(WriteFailed);
delete[] data;
return *this;
} }
bool MsgPackStream::unpack_upto_quint8(quint8 &u8, quint8 *p) bool MsgPackStream::unpack_upto_quint8(quint8 &u8, quint8 *p)
{ {
if (*p <= MsgPack::FirstByte::POSITIVE_FIXINT) { if (*p <= MsgPack::FirstByte::POSITIVE_FIXINT) {
u8 = *p; u8 = *p;
} else if (*p == MsgPack::FirstByte::UINT8) { } else if (*p == MsgPack::FirstByte::UINT8) {
if (dev->read((char* )&u8, 1) != 1) { if (dev->read((char* )&u8, 1) != 1)
setStatus(ReadPastEnd);
return false; return false;
}
} else { } else {
setStatus(ReadCorruptData);
return false; return false;
} }
return true; return true;
@@ -318,10 +332,8 @@ bool MsgPackStream::unpack_upto_quint16(quint16 &u16, quint8 *p)
{ {
if (*p == MsgPack::FirstByte::UINT16) { if (*p == MsgPack::FirstByte::UINT16) {
quint8 d[2]; quint8 d[2];
if (dev->read((char *)d, 2) != 2) { if (dev->read((char *)d, 2) != 2)
setStatus(ReadPastEnd);
return false; return false;
}
u16 = _msgpack_load16(quint16, d); u16 = _msgpack_load16(quint16, d);
} else { } else {
quint8 u8; quint8 u8;
@@ -329,16 +341,15 @@ bool MsgPackStream::unpack_upto_quint16(quint16 &u16, quint8 *p)
u16 = u8; u16 = u8;
return ok; return ok;
} }
return true;
} }
bool MsgPackStream::unpack_upto_quint32(quint32 &u32, quint8 *p) bool MsgPackStream::unpack_upto_quint32(quint32 &u32, quint8 *p)
{ {
if (*p == MsgPack::FirstByte::UINT32) { if (*p == MsgPack::FirstByte::UINT32) {
quint8 d[4]; quint8 d[4];
if (dev->read((char *)d, 4) != 4) { if (dev->read((char *)d, 4) != 4)
setStatus(ReadPastEnd);
return false; return false;
}
u32 = _msgpack_load32(quint32, d); u32 = _msgpack_load32(quint32, d);
return true; return true;
} else { } else {
@@ -347,6 +358,7 @@ bool MsgPackStream::unpack_upto_quint32(quint32 &u32, quint8 *p)
u32 = u16; u32 = u16;
return ok; return ok;
} }
return true;
} }
bool MsgPackStream::unpack_upto_quint64(quint64 &u64, quint8 *p) bool MsgPackStream::unpack_upto_quint64(quint64 &u64, quint8 *p)
@@ -354,7 +366,6 @@ bool MsgPackStream::unpack_upto_quint64(quint64 &u64, quint8 *p)
if (*p == MsgPack::FirstByte::UINT64) { if (*p == MsgPack::FirstByte::UINT64) {
quint8 d[8]; quint8 d[8];
if (dev->read((char *)d, 8) != 8) { if (dev->read((char *)d, 8) != 8) {
setStatus(ReadPastEnd);
return false; return false;
} }
u64 = _msgpack_load64(quint64, d); u64 = _msgpack_load64(quint64, d);
@@ -373,14 +384,13 @@ bool MsgPackStream::unpack_upto_qint8(qint8 &i8, quint8 *p)
i8 = *p; i8 = *p;
} else if (*p == MsgPack::FirstByte::INT8) { } else if (*p == MsgPack::FirstByte::INT8) {
if (dev->read((char *)&i8, 1) != 1) { if (dev->read((char *)&i8, 1) != 1) {
setStatus(ReadPastEnd);
return false; return false;
} }
} else { } else {
quint8 u8; quint8 u8;
bool ok = unpack_upto_quint8(u8, p); bool ok = unpack_upto_quint8(u8, p);
if (u8 > std::numeric_limits<qint8>::max()) if (u8 > std::numeric_limits<qint8>::max())
setStatus(ReadCorruptData); return false;
else else
i8 = u8; i8 = u8;
return ok; return ok;
@@ -393,11 +403,9 @@ bool MsgPackStream::unpack_upto_qint16(qint16 &i16, quint8 *p)
if (*p == MsgPack::FirstByte::INT16) { if (*p == MsgPack::FirstByte::INT16) {
quint8 d[2]; quint8 d[2];
if (dev->read((char *)d, 2) != 2) { if (dev->read((char *)d, 2) != 2) {
setStatus(ReadPastEnd);
return false; return false;
} }
i16 = _msgpack_load16(qint16, d); i16 = _msgpack_load16(qint16, d);
return true;
} else { } else {
qint8 i8; qint8 i8;
bool ok = unpack_upto_qint8(i8, p); bool ok = unpack_upto_qint8(i8, p);
@@ -406,13 +414,14 @@ bool MsgPackStream::unpack_upto_qint16(qint16 &i16, quint8 *p)
} else { } else {
quint16 u16; quint16 u16;
bool ok = unpack_upto_quint16(u16, p); bool ok = unpack_upto_quint16(u16, p);
if (u16 <= std::numeric_limits<qint16>::max()) if (u16 > std::numeric_limits<qint16>::max())
i16 = u16; return false;
else else
setStatus(ReadCorruptData); i16 = u16;
return ok; return ok;
} }
} }
return true;
} }
bool MsgPackStream::unpack_upto_qint32(qint32 &i32, quint8 *p) bool MsgPackStream::unpack_upto_qint32(qint32 &i32, quint8 *p)
@@ -420,11 +429,9 @@ bool MsgPackStream::unpack_upto_qint32(qint32 &i32, quint8 *p)
if(*p == MsgPack::FirstByte::INT32) { if(*p == MsgPack::FirstByte::INT32) {
quint8 d[4]; quint8 d[4];
if (dev->read((char *)d, 4) != 4) { if (dev->read((char *)d, 4) != 4) {
setStatus(ReadPastEnd);
return false; return false;
} }
i32 = _msgpack_load32(qint32, d); i32 = _msgpack_load32(qint32, d);
return true;
} else { } else {
qint16 i16; qint16 i16;
bool ok = unpack_upto_qint16(i16, p); bool ok = unpack_upto_qint16(i16, p);
@@ -433,23 +440,22 @@ bool MsgPackStream::unpack_upto_qint32(qint32 &i32, quint8 *p)
} else { } else {
quint32 u32; quint32 u32;
bool ok = unpack_upto_quint32(u32, p); bool ok = unpack_upto_quint32(u32, p);
if (u32 <= std::numeric_limits<qint32>::max()) if (u32 > std::numeric_limits<qint32>::max())
i32 = u32; return false;
else else
setStatus(ReadCorruptData); i32 = u32;
return ok; return ok;
} }
} }
return true;
} }
bool MsgPackStream::unpack_upto_qint64(qint64 &i64, quint8 *p) bool MsgPackStream::unpack_upto_qint64(qint64 &i64, quint8 *p)
{ {
if(*p == MsgPack::FirstByte::INT64) { if(*p == MsgPack::FirstByte::INT64) {
quint8 d[8]; quint8 d[8];
if (dev->read((char *)d, 8) != 8) { if (dev->read((char *)d, 8) != 8)
setStatus(ReadPastEnd);
return false; return false;
}
i64 = _msgpack_load64(qint64, d); i64 = _msgpack_load64(qint64, d);
return true; return true;
} else { } else {
@@ -460,11 +466,12 @@ bool MsgPackStream::unpack_upto_qint64(qint64 &i64, quint8 *p)
} else { } else {
quint64 u64; quint64 u64;
bool ok = unpack_upto_quint64(u64, p); bool ok = unpack_upto_quint64(u64, p);
if (u64 <= std::numeric_limits<qint64>::max()) if (u64 > std::numeric_limits<qint64>::max())
i64 = u64; return false;
else else
setStatus(ReadCorruptData); i64 = u64;
return ok; return ok;
} }
} }
return true;
} }

View File

@@ -15,8 +15,6 @@ public:
QIODevice *device() const; QIODevice *device() const;
bool atEnd() const; bool atEnd() const;
void setCompatibility(bool isEnabled);
enum Status {Ok, ReadPastEnd, ReadCorruptData, WriteFailed}; enum Status {Ok, ReadPastEnd, ReadCorruptData, WriteFailed};
Status status() const; Status status() const;
void resetStatus(); void resetStatus();
@@ -46,19 +44,16 @@ public:
MsgPackStream &operator<<(float f); MsgPackStream &operator<<(float f);
MsgPackStream &operator<<(double d); MsgPackStream &operator<<(double d);
MsgPackStream &operator<<(QString str); MsgPackStream &operator<<(QString str);
MsgPackStream &operator<<(const char *str);
MsgPackStream &operator<<(QByteArray array); MsgPackStream &operator<<(QByteArray array);
MsgPackStream &operator<<(QVariantList list); MsgPackStream &operator<<(QVariantList list);
MsgPackStream &operator<<(QVariantMap map); MsgPackStream &operator<<(QVariantMap map);
private: private:
QIODevice *dev; QIODevice *dev;
bool compatibility;
bool owndev; bool owndev;
Status q_status; Status q_status;
MsgPackStream &operator<<(const char *str); // use QStringLiteral instead
bool unpack_upto_quint8(quint8 &u8, quint8 *p); bool unpack_upto_quint8(quint8 &u8, quint8 *p);
bool unpack_upto_quint16(quint16 &u16, quint8 *p); bool unpack_upto_quint16(quint16 &u16, quint8 *p);
bool unpack_upto_quint32(quint32 &u32, quint8 *p); bool unpack_upto_quint32(quint32 &u32, quint8 *p);

View File

@@ -216,14 +216,14 @@ void PackTest::test_float()
void PackTest::test_str() void PackTest::test_str()
{ {
QString str = QString::fromUtf8("msgpack rocks"); QString str = QStringLiteral("msgpack rocks");
QByteArray arr = MsgPack::pack(str); QByteArray arr = MsgPack::pack(str);
QVERIFY(arr.size() == 14); QVERIFY(arr.size() == 14);
quint8 *p = (quint8 *)arr.data(); quint8 *p = (quint8 *)arr.data();
QVERIFY(p[0] == (0xa0 | str.length())); QVERIFY(p[0] == (0xa0 | str.length()));
QVERIFY(memcmp(p + 1, str.toUtf8().data(), str.size()) == 0); QVERIFY(memcmp(p + 1, str.toUtf8().data(), str.size()) == 0);
str = QString::fromUtf8(QByteArray(32, 'm')); str = QString(QByteArray(32, 'm'));
arr = MsgPack::pack(str); arr = MsgPack::pack(str);
QVERIFY(arr.size() == 32 + 2); QVERIFY(arr.size() == 32 + 2);
p = (quint8 *)arr.data(); p = (quint8 *)arr.data();
@@ -231,7 +231,7 @@ void PackTest::test_str()
QVERIFY(p[1] == 32); QVERIFY(p[1] == 32);
QVERIFY(memcmp(p + 2, str.toUtf8().data(), str.size()) == 0); QVERIFY(memcmp(p + 2, str.toUtf8().data(), str.size()) == 0);
str = QString::fromUtf8(QByteArray(256, 's')); str = QString(QByteArray(256, 's'));
arr = MsgPack::pack(str); arr = MsgPack::pack(str);
QVERIFY(arr.size() == 256 + 3); QVERIFY(arr.size() == 256 + 3);
p = (quint8 *)arr.data(); p = (quint8 *)arr.data();
@@ -240,7 +240,7 @@ void PackTest::test_str()
QVERIFY(p[2] == 0x00); QVERIFY(p[2] == 0x00);
QVERIFY(memcmp(p + 3, str.toUtf8().data(), str.size()) == 0); QVERIFY(memcmp(p + 3, str.toUtf8().data(), str.size()) == 0);
str = QString::fromUtf8(QByteArray(65536, 'g')); str = QString(QByteArray(65536, 'g'));
arr = MsgPack::pack(str); arr = MsgPack::pack(str);
QVERIFY(arr.size() == 65536 + 5); QVERIFY(arr.size() == 65536 + 5);
p = (quint8 *)arr.data(); p = (quint8 *)arr.data();

View File

@@ -77,6 +77,8 @@ void QtTypesTest::test_qpoint()
packed = MsgPack::pack(pt); packed = MsgPack::pack(pt);
QVERIFY(packed.size() == 9); QVERIFY(packed.size() == 9);
pt2 = MsgPack::unpack(packed).toPoint(); pt2 = MsgPack::unpack(packed).toPoint();
qDebug() << pt << pt2;
qDebug() << packed.toHex() << packed.size();
QVERIFY(pt == pt2); QVERIFY(pt == pt2);
pt = QPoint(std::numeric_limits<qint32>::max(), std::numeric_limits<qint32>::max()); pt = QPoint(std::numeric_limits<qint32>::max(), std::numeric_limits<qint32>::max());

View File

@@ -12,6 +12,8 @@ class StreamTest : public QObject
private Q_SLOTS: private Q_SLOTS:
void test_unpack_integers(); void test_unpack_integers();
void test_pack_integers(); void test_pack_integers();
void test_unpack_string();
void test_pack_string();
}; };
@@ -135,6 +137,72 @@ void StreamTest::test_pack_integers()
QVERIFY(l[19].toLongLong() == std::numeric_limits<qint64>::min()); QVERIFY(l[19].toLongLong() == std::numeric_limits<qint64>::min());
} }
void StreamTest::test_unpack_string()
{
QString str = QStringLiteral("msgpack rocks");
QByteArray packed = MsgPack::pack(str);
QString str2;
{
MsgPackStream stream(packed);
stream >> str2;
QVERIFY(str == str2);
}
{
str = QString(QByteArray(32, 'm'));
packed = MsgPack::pack(str);
MsgPackStream stream(packed);
stream >> str2;
QVERIFY(str == str2);
}
{
str = QString::fromUtf8(QByteArray(256, 's'));
packed = MsgPack::pack(str);
MsgPackStream stream(packed);
stream >> str2;
QVERIFY(str == str2);
}
{
str = QString(QByteArray(65536, 'g'));
packed = MsgPack::pack(str);
MsgPackStream stream(packed);
stream >> str2;
QVERIFY(str == str2);
}
}
void StreamTest::test_pack_string()
{
QByteArray packed;
MsgPackStream stream(&packed, QIODevice::WriteOnly);
QStringList strs;
stream << "abc";
strs << "abc";
QString s;
for (int i = 0; i < 8; ++i)
s += "xy";
stream << s;
strs << s;
s = QString();
for (int i = 0; i < 64; ++i)
s += "msgp";
stream << s;
strs << s;
s = QString();
for (int i = 0; i < 4096; ++i)
s += "messagepack test";
stream << s;
strs << s;
stream << "";
QStringList l = MsgPack::unpack(packed).toStringList();
QVERIFY(l[0] == strs[0]);
QVERIFY(l[1] == strs[1]);
QVERIFY(l[2] == strs[2]);
QVERIFY(l[3] == strs[3]);
QVERIFY(l[4].isEmpty());
}
QTEST_APPLESS_MAIN(StreamTest) QTEST_APPLESS_MAIN(StreamTest)