diff --git a/src/msgpack-qt.pro b/src/msgpack-qt.pro index 2e187fd..d69ffaf 100644 --- a/src/msgpack-qt.pro +++ b/src/msgpack-qt.pro @@ -28,7 +28,6 @@ SOURCES += msgpack.cpp \ private/pack_p.cpp \ private/unpack_p.cpp \ private/qt_types_p.cpp \ - msgpack_ext.cpp \ stream.cpp HEADERS += \ @@ -39,5 +38,4 @@ HEADERS += \ msgpack_common.h \ msgpack_export.h \ private/qt_types_p.h \ - msgpack_ext.h \ stream.h diff --git a/src/private/qt_types_p.cpp b/src/private/qt_types_p.cpp index 8491ab1..da3d260 100644 --- a/src/private/qt_types_p.cpp +++ b/src/private/qt_types_p.cpp @@ -2,7 +2,6 @@ #include "pack_p.h" #include "unpack_p.h" #include "sysdep.h" -#include "../msgpack_ext.h" #include @@ -187,90 +186,90 @@ quint8 MsgPackPrivate::pack_two_integers(qint32 a, qint32 b, quint8 *to, bool wr quint32 MsgPackPrivate::pack_qpoint(const QVariant &variant, QByteArray &data, bool write) { - QPoint pt = variant.toPoint(); - quint8 size = pack_two_integers(pt.x(), pt.y(), 0, false); - if (write) { - data.resize(size); - pack_two_integers(pt.x(), pt.y(), (quint8 *)data.data(), true); - } - return size; + // QPoint pt = variant.toPoint(); + // quint8 size = pack_two_integers(pt.x(), pt.y(), 0, false); + // if (write) { + // data.resize(size); + // pack_two_integers(pt.x(), pt.y(), (quint8 *)data.data(), true); + // } + // return size; } QVariant MsgPackPrivate::unpack_qpoint(const QByteArray &data) { - quint8 *p = (quint8 *)data.data(); - qint32 x; - bool ok; - p = MsgPack::Ext::unpack_upto_qint32(&x, p, &ok); - QPoint pt; - pt.setX(x); - MsgPack::Ext::unpack_upto_qint32(&x, p, &ok); - pt.setY(x); - return pt; + // quint8 *p = (quint8 *)data.data(); + // qint32 x; + // bool ok; + // p = MsgPack::Ext::unpack_upto_qint32(&x, p, &ok); + // QPoint pt; + // pt.setX(x); + // MsgPack::Ext::unpack_upto_qint32(&x, p, &ok); + // pt.setY(x); + // return pt; } quint32 MsgPackPrivate::pack_qsize(const QVariant &variant, QByteArray &data, bool write) { - QSize sz = variant.toSize(); - quint8 size = pack_two_integers(sz.width(), sz.height(), 0, false); - if (write) { - data.resize(size); - pack_two_integers(sz.width(), sz.height(), (quint8 *)data.data(), true); - } - return size; + // QSize sz = variant.toSize(); + // quint8 size = pack_two_integers(sz.width(), sz.height(), 0, false); + // if (write) { + // data.resize(size); + // pack_two_integers(sz.width(), sz.height(), (quint8 *)data.data(), true); + // } + // return size; } QVariant MsgPackPrivate::unpack_qsize(const QByteArray &data) { - quint8 *p = (quint8 *)data.data(); - qint32 x; - bool ok; - p = MsgPack::Ext::unpack_upto_qint32(&x, p, &ok); - QSize sz; - sz.setWidth(x); - MsgPack::Ext::unpack_upto_qint32(&x, p, &ok); - sz.setHeight(x); - return sz; + // quint8 *p = (quint8 *)data.data(); + // qint32 x; + // bool ok; + // p = MsgPack::Ext::unpack_upto_qint32(&x, p, &ok); + // QSize sz; + // sz.setWidth(x); + // MsgPack::Ext::unpack_upto_qint32(&x, p, &ok); + // sz.setHeight(x); + // return sz; } quint32 MsgPackPrivate::pack_qrect(const QVariant &variant, QByteArray &data, bool write) { - QRect rect = variant.toRect(); - QPoint pt1 = rect.topLeft(); - QPoint pt2 = rect.bottomRight(); - quint8 size = pack_two_integers(pt1.x(), pt1.y(), 0, false); - size += pack_two_integers(pt2.x(), pt2.y(), 0, false); - if (write) { - data.resize(size); - 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; + // QRect rect = variant.toRect(); + // QPoint pt1 = rect.topLeft(); + // QPoint pt2 = rect.bottomRight(); + // quint8 size = pack_two_integers(pt1.x(), pt1.y(), 0, false); + // size += pack_two_integers(pt2.x(), pt2.y(), 0, false); + // if (write) { + // data.resize(size); + // 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) { - quint8 *p = (quint8 *)data.data(); - qint32 x; - bool ok; + // quint8 *p = (quint8 *)data.data(); + // qint32 x; + // bool ok; - p = MsgPack::Ext::unpack_upto_qint32(&x, p, &ok); - QPoint pt; - pt.setX(x); - p = MsgPack::Ext::unpack_upto_qint32(&x, p, &ok); - pt.setY(x); + // p = MsgPack::Ext::unpack_upto_qint32(&x, p, &ok); + // QPoint pt; + // pt.setX(x); + // p = MsgPack::Ext::unpack_upto_qint32(&x, p, &ok); + // pt.setY(x); - QRect rect; - rect.setTopLeft(pt); + // 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); + // 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); + // rect.setBottomRight(pt); - return rect; + // return rect; } diff --git a/src/stream.cpp b/src/stream.cpp index d90bbe6..8e9ce3e 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -1,5 +1,8 @@ #include "stream.h" #include +#include "private/sysdep.h" +#include "msgpack_common.h" +#include #undef CHECK_STREAM_PRECOND #ifndef QT_NO_DEBUG @@ -84,15 +87,52 @@ void MsgPackStream::setStatus(Status status) q_status = status; } -MsgPackStream &MsgPackStream::operator >>(quint8 &u8) +MsgPackStream &MsgPackStream::operator>>(bool &b) { - u8 = 0; CHECK_STREAM_PRECOND(*this) - char c; - if (!dev->getChar(&c)) + quint8 p[1]; + if (!dev->getChar((char *)p)) { + b = false; setStatus(ReadPastEnd); - else - u8 = quint8(c); + } else { + if (p[0] != MsgPack::FirstByte::TRUE || + p[0] != MsgPack::FirstByte::FALSE) + setStatus(ReadCorruptData); + b = (p[0] == MsgPack::FirstByte::TRUE); + } return *this; } +MsgPackStream &MsgPackStream::operator >>(quint8 &u8) +{ + CHECK_STREAM_PRECOND(*this) + char c; + if (!dev->getChar(&c)) { + u8 = 0; + setStatus(ReadPastEnd); + } else { + u8 = quint8(c); + } + return *this; +} + +MsgPackStream &MsgPackStream::operator>>(quint16 &u16) +{ + CHECK_STREAM_PRECOND(*this); + quint8 p[3]; + if (dev->read((char *)p, 3) != 3) { + u16 = 0; + setStatus(ReadPastEnd); + } else { + if (p[0] != MsgPack::FirstByte::UINT16) + setStatus(ReadCorruptData); + u16 = _msgpack_load16(quint16, (p + 1)); + } + return *this; +} + +MsgPackStream &MsgPackStream::operator>>(quint32 &u32) +{ + +} +