Some bugs fixed
float support added float, double, QByteArray, QList<T> to MsgPackStream added float, double, QByteArrat tests for MsgPackStream added
This commit is contained in:
1 parent
1277a0c204
commit
7d5d72e814
8 files changed
+272
-39
No files matched your search
+10
-6
@@ -216,7 +216,7 @@ quint8 *MsgPackPrivate::pack_float(float f, quint8 *p, bool wr)
|
||||
if (wr) *p = 0xca;
|
||||
p++;
|
||||
if (wr) {
|
||||
quint8 *d = (quint8 *)&i;
|
||||
quint8 *d = (quint8 *)&f;
|
||||
#ifdef __LITTLE_ENDIAN__
|
||||
for (int i = 0; i < 4; ++i)
|
||||
*(p + 3 - i) = *(d + i);
|
||||
@@ -225,10 +225,9 @@ quint8 *MsgPackPrivate::pack_float(float f, quint8 *p, bool wr)
|
||||
*(p + i) = *(d + i);
|
||||
#endif
|
||||
}
|
||||
return p + 8;
|
||||
return p + 4;
|
||||
}
|
||||
|
||||
|
||||
quint8 *MsgPackPrivate::pack_double(double i, quint8 *p, bool wr)
|
||||
{
|
||||
if (wr) *p = 0xcb;
|
||||
@@ -246,9 +245,8 @@ quint8 *MsgPackPrivate::pack_double(double i, quint8 *p, bool wr)
|
||||
return p + 8;
|
||||
}
|
||||
|
||||
quint8 *MsgPackPrivate::pack_bin(const QByteArray &arr, quint8 *p, bool wr)
|
||||
quint8 *MsgPackPrivate::pack_bin_header(quint32 len, quint8 *p, bool wr)
|
||||
{
|
||||
int len = arr.length();
|
||||
if (len <= std::numeric_limits<quint8>::max()) {
|
||||
if (wr) *p = compatibilityMode ? 0xd9 : 0xc4;
|
||||
p++;
|
||||
@@ -265,9 +263,15 @@ quint8 *MsgPackPrivate::pack_bin(const QByteArray &arr, quint8 *p, bool wr)
|
||||
if (wr) _msgpack_store32(p, len);
|
||||
p += 4;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
quint8 *MsgPackPrivate::pack_bin(const QByteArray &arr, quint8 *p, bool wr)
|
||||
{
|
||||
quint32 len = arr.length();
|
||||
p = pack_bin_header(len, p, wr);
|
||||
if (wr) memcpy(p, arr.data(), len);
|
||||
p += len;
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
@@ -40,6 +40,7 @@ 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_float(float f, quint8 *p, bool wr);
|
||||
quint8 * pack_double(double i, quint8 *p, bool wr);
|
||||
quint8 * pack_bin_header(quint32 len, quint8 *p, bool wr);
|
||||
quint8 * pack_bin(const QByteArray &arr, quint8 *p, bool wr);
|
||||
quint8 * pack_map(const QVariantMap &map, quint8 *p, bool wr, QVector<QByteArray> &user_data);
|
||||
quint8 * pack_user(const QVariant &v, quint8 *p, bool wr, QVector<QByteArray> &user_data);
|
||||
|
||||
+56
-26
@@ -209,7 +209,7 @@ MsgPackStream &MsgPackStream::operator>>(float &f)
|
||||
CHECK_STREAM_PRECOND(*this);
|
||||
quint8 *fp = (quint8 *)&f;
|
||||
quint8 p[5];
|
||||
if (dev->read((char *)&p, 1) != 1) {
|
||||
if (dev->read((char *)p, 1) != 1) {
|
||||
setStatus(ReadPastEnd);
|
||||
return *this;
|
||||
}
|
||||
@@ -217,6 +217,10 @@ MsgPackStream &MsgPackStream::operator>>(float &f)
|
||||
setStatus(ReadCorruptData);
|
||||
return *this;
|
||||
}
|
||||
if (dev->read((char *)p + 1, 4) != 4) {
|
||||
setStatus(ReadPastEnd);
|
||||
return *this;
|
||||
}
|
||||
#ifdef __LITTLE_ENDIAN__
|
||||
for (int i = 0; i < 4; ++i)
|
||||
*(fp + 3 - i) = *(p + i + 1);
|
||||
@@ -230,9 +234,9 @@ MsgPackStream &MsgPackStream::operator>>(float &f)
|
||||
MsgPackStream &MsgPackStream::operator>>(double &d)
|
||||
{
|
||||
CHECK_STREAM_PRECOND(*this);
|
||||
quint8 *fp = (quint8 *)&f;
|
||||
quint8 *fp = (quint8 *)&d;
|
||||
quint8 p[9];
|
||||
if (dev->read((char *)&p, 1) != 1) {
|
||||
if (dev->read((char *)p, 1) != 1) {
|
||||
setStatus(ReadPastEnd);
|
||||
return *this;
|
||||
}
|
||||
@@ -240,6 +244,10 @@ MsgPackStream &MsgPackStream::operator>>(double &d)
|
||||
setStatus(ReadCorruptData);
|
||||
return *this;
|
||||
}
|
||||
if (dev->read((char *)p + 1, 8) != 8) {
|
||||
setStatus(ReadPastEnd);
|
||||
return *this;
|
||||
}
|
||||
#ifdef __LITTLE_ENDIAN__
|
||||
for (int i = 0; i < 8; ++i)
|
||||
*(fp + 7 - i) = *(p + i + 1);
|
||||
@@ -287,17 +295,39 @@ MsgPackStream &MsgPackStream::operator>>(QString &str)
|
||||
|
||||
MsgPackStream &MsgPackStream::operator>>(QByteArray &array)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
MsgPackStream &MsgPackStream::operator>>(QVariantList &list)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
MsgPackStream &MsgPackStream::operator>>(QVariantMap &map)
|
||||
{
|
||||
|
||||
CHECK_STREAM_PRECOND(*this);
|
||||
quint8 p[5];
|
||||
if (dev->read((char *)p, 1) != 1) {
|
||||
setStatus(ReadPastEnd);
|
||||
return *this;
|
||||
}
|
||||
quint32 len;
|
||||
if (p[0] == MsgPack::FirstByte::BIN8) {
|
||||
if (dev->read((char *)p + 1, 1) != 1) {
|
||||
setStatus(ReadPastEnd);
|
||||
return *this;
|
||||
}
|
||||
len = p[1];
|
||||
} else if (p[0] == MsgPack::FirstByte::BIN16) {
|
||||
if (dev->read((char *)p + 1, 2) != 2) {
|
||||
setStatus(ReadPastEnd);
|
||||
return *this;
|
||||
}
|
||||
len = _msgpack_load16(quint16, &p[1]);
|
||||
} else if (p[0] == MsgPack::FirstByte::BIN32) {
|
||||
if (dev->read((char *)p + 1, 4) != 4) {
|
||||
setStatus(ReadPastEnd);
|
||||
return *this;
|
||||
}
|
||||
len = _msgpack_load32(quint32, &p[1]);
|
||||
} else {
|
||||
setStatus(ReadCorruptData);
|
||||
return *this;
|
||||
}
|
||||
array.resize(len);
|
||||
if (dev->read(array.data(), len) != len)
|
||||
setStatus(ReadPastEnd);
|
||||
return *this;
|
||||
}
|
||||
|
||||
MsgPackStream &MsgPackStream::operator<<(bool b)
|
||||
@@ -364,7 +394,7 @@ MsgPackStream &MsgPackStream::operator<<(double d)
|
||||
{
|
||||
CHECK_STREAM_WRITE_PRECOND(*this);
|
||||
quint8 p[9];
|
||||
quint8 sz = MsgPackPrivate::pack_float(d, p, true) - p;
|
||||
quint8 sz = MsgPackPrivate::pack_double(d, p, true) - p;
|
||||
if (dev->write((char *)p, sz) != sz)
|
||||
setStatus(WriteFailed);
|
||||
return *this;
|
||||
@@ -399,17 +429,17 @@ MsgPackStream &MsgPackStream::operator<<(const char *str)
|
||||
|
||||
MsgPackStream &MsgPackStream::operator<<(QByteArray array)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
MsgPackStream &MsgPackStream::operator<<(QVariantList list)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
MsgPackStream &MsgPackStream::operator<<(QVariantMap map)
|
||||
{
|
||||
|
||||
CHECK_STREAM_WRITE_PRECOND(*this);
|
||||
quint8 p[5];
|
||||
quint32 len = array.length();
|
||||
quint8 header_len = MsgPackPrivate::pack_bin_header(len, p, true) - p;
|
||||
if (dev->write((char *)p, header_len) != header_len) {
|
||||
setStatus(WriteFailed);
|
||||
return *this;
|
||||
}
|
||||
if (dev->write(array.data(), len) != len)
|
||||
setStatus(WriteFailed);
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool MsgPackStream::unpack_upto_quint8(quint8 &u8, quint8 *p)
|
||||
|
||||
+25
-4
@@ -33,8 +33,6 @@ public:
|
||||
MsgPackStream &operator>>(double &d);
|
||||
MsgPackStream &operator>>(QString &str);
|
||||
MsgPackStream &operator>>(QByteArray &array);
|
||||
MsgPackStream &operator>>(QVariantList &list);
|
||||
MsgPackStream &operator>>(QVariantMap &map);
|
||||
|
||||
MsgPackStream &operator<<(bool b);
|
||||
MsgPackStream &operator<<(quint32 u32);
|
||||
@@ -46,8 +44,6 @@ public:
|
||||
MsgPackStream &operator<<(QString str);
|
||||
MsgPackStream &operator<<(const char *str);
|
||||
MsgPackStream &operator<<(QByteArray array);
|
||||
MsgPackStream &operator<<(QVariantList list);
|
||||
MsgPackStream &operator<<(QVariantMap map);
|
||||
|
||||
private:
|
||||
QIODevice *dev;
|
||||
@@ -64,4 +60,29 @@ private:
|
||||
bool unpack_upto_qint64(qint64 &i64, quint8 *p);
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
MsgPackStream& operator<<(MsgPackStream& s, const QList<T> &list)
|
||||
{
|
||||
s << (quint32)list.size();
|
||||
for (int i = 0; i < list.size(); ++i)
|
||||
s << list[i];
|
||||
return s;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
MsgPackStream& operator>>(MsgPackStream& s, QList<T> &list)
|
||||
{
|
||||
list.clear();
|
||||
quint32 size;
|
||||
s >> size;
|
||||
for (quint32 i = 0; i < size; ++i) {
|
||||
T t;
|
||||
s >> t;
|
||||
list.append(t);
|
||||
if (s.atEnd())
|
||||
break;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
#endif // STREAM_H
|
||||
Reference in new issue
Block a user