MsgPackStream work

This commit is contained in:
Isaikin Roman
2015-05-16 12:07:10 +03:00
parent 87bd3dedb1
commit 32b04745ee
3 changed files with 90 additions and 1 deletions

View File

@ -183,7 +183,7 @@ quint8 * MsgPackPrivate::unpack_float64(QVariant &v, quint8 *p)
for (int i = 0; i < 8; ++i) for (int i = 0; i < 8; ++i)
*(fd + 7 - i) = *(p + i); *(fd + 7 - i) = *(p + i);
#else #else
for (int i = 0; i < 4; ++i) for (int i = 0; i < 8; ++i)
*(fp + i) = *(p + i); *(fp + i) = *(p + i);
#endif #endif
v = d; v = d;

View File

@ -1,6 +1,7 @@
#include "stream.h" #include "stream.h"
#include <QBuffer> #include <QBuffer>
#include "private/sysdep.h" #include "private/sysdep.h"
#include "private/pack_p.h"
#include "msgpack_common.h" #include "msgpack_common.h"
#include <QDebug> #include <QDebug>
@ -136,3 +137,77 @@ MsgPackStream &MsgPackStream::operator>>(quint32 &u32)
} }
MsgPackStream &MsgPackStream::operator>>(QString &str)
{
CHECK_STREAM_PRECOND(*this);
quint8 p[5];
if (dev->read((char *)p, 1) != 1) {
setStatus(ReadPastEnd);
return *this;
}
int len = 0;
if (*p >= 0xa0 && *p <= 0xbf) { // fixstr
len = (*p) & 0x1f; // 0b00011111
} else if (*p == MsgPack::FirstByte::STR8) {
if (dev->read((char *)p + 1, 1) == 1)
len = p[1];
} else if (*p == MsgPack::FirstByte::STR16) {
if (dev->read((char *)p + 1, 2) == 2)
len = _msgpack_load16(int, &p[1]);
} else if (*p == MsgPack::FirstByte::STR32) {
if (dev->read((char *)p + 1, 4) == 4)
len = _msgpack_load32(int, &p[1]);
} else {
setStatus(ReadCorruptData);
return *this;
}
}
MsgPackStream &MsgPackStream::operator<<(bool b)
{
CHECK_STREAM_WRITE_PRECOND(*this);
quint8 m = b == true ?
MsgPack::FirstByte::TRUE : MsgPack::FirstByte::FALSE;
if (dev->write((char *)&m, 1) != 1)
setStatus(WriteFailed);
return *this;
}
MsgPackStream &MsgPackStream::operator<<(quint32 u32)
{
CHECK_STREAM_WRITE_PRECOND(*this);
quint8 p[5];
quint8 sz = MsgPackPrivate::pack_uint(u32, p, true) - p;
if (!dev->write((char *)p, sz) != sz)
setStatus(WriteFailed);
return *this;
}
MsgPackStream &MsgPackStream::operator<<(qint32 i32)
{
CHECK_STREAM_WRITE_PRECOND(*this);
quint8 p[5];
quint8 sz = MsgPackPrivate::pack_uint(i32, p, true) - p;
if (!dev->write((char *)p, sz) != sz)
setStatus(WriteFailed);
return *this;
}
MsgPackStream &MsgPackStream::operator<<(QString str)
{
CHECK_STREAM_WRITE_PRECOND(*this);
quint8 *p = (quint8 *)0;
quint32 sz = MsgPackPrivate::pack_string(str, p, false) - p;
quint8 *data = new quint8[sz];
MsgPackPrivate::pack(str, data, true);
if (dev->write((char *)data, sz) != sz)
setStatus(WriteFailed);
delete[] data;
return *this;
}
MsgPackStream &MsgPackStream::operator<<(const char *str)
{
}

View File

@ -38,12 +38,26 @@ public:
MsgPackStream &operator>>(QVariantList &list); MsgPackStream &operator>>(QVariantList &list);
MsgPackStream &operator>>(QVariantMap &map); MsgPackStream &operator>>(QVariantMap &map);
MsgPackStream &operator<<(bool b);
MsgPackStream &operator<<(quint32 u32);
MsgPackStream &operator<<(quint64 u64);
MsgPackStream &operator<<(qint32 i32);
MsgPackStream &operator<<(qint64 i64);
MsgPackStream &operator<<(float f);
MsgPackStream &operator<<(double d);
MsgPackStream &operator<<(QString str);
MsgPackStream &operator<<(QByteArray array);
MsgPackStream &operator<<(QVariantList list);
MsgPackStream &operator<<(QVariantMap map);
private: private:
QIODevice *dev; QIODevice *dev;
bool compatibility; bool compatibility;
bool owndev; bool owndev;
Status q_status; Status q_status;
MsgPackStream &operator<<(const char *str); // use QStringLiteral instead
}; };
#endif // STREAM_H #endif // STREAM_H