forked from romixlab/qmsgpack
some bugs fixed, test written, version() added
This commit is contained in:
+6
-1
@@ -1,4 +1,4 @@
|
||||
set(qmsgpack_srcs msgpack.cpp private/pack_p.cpp private/unpack_p.cpp)
|
||||
set(qmsgpack_srcs msgpack.cpp msgpack_common.cpp private/pack_p.cpp private/unpack_p.cpp)
|
||||
set(qmsgpack_headers msgpack.h msgpack_common.h msgpack_export.h)
|
||||
|
||||
add_library(qmsgpack SHARED ${qmsgpack_srcs} ${qmsgpack_headers})
|
||||
@@ -9,6 +9,11 @@ else ()
|
||||
target_link_libraries(qmsgpack ${QT_LIBRARIES})
|
||||
endif ()
|
||||
|
||||
configure_file(
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/msgpack_common.h.in"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/msgpack_common.h"
|
||||
IMMEDIATE @ONLY)
|
||||
|
||||
if (NOT android)
|
||||
set_target_properties(qmsgpack PROPERTIES
|
||||
VERSION ${QMSGPACK_MAJOR}.${QMSGPACK_MINOR}.${QMSGPACK_VERSION}
|
||||
|
||||
+18
-8
@@ -30,18 +30,28 @@ int main(int argc, char *argv[])
|
||||
Q_UNUSED(argv)
|
||||
//QCoreApplication a(argc, argv);
|
||||
|
||||
MsgPack::registerPacker(QMetaType::QPoint, 7, packQPoint);
|
||||
MsgPack::registerUnpacker(7, unpackQPoint);
|
||||
qDebug() << "msgpack v" << MsgPack::version();
|
||||
|
||||
QVariantList l;
|
||||
QVariantMap map;
|
||||
map["one"] = 1;
|
||||
map["map"] = QVariantMap();
|
||||
// map["booleans"] = QVariantList() << false << true;
|
||||
// map["list2"] = QVariantList() << (QVariantList() << "abc" << "def")
|
||||
// << (QVariantList() << "qwe" << "rty");
|
||||
map["integers"] = QVariantList() << 0 << 127 << -31 << 128 << 777;
|
||||
// map["bytearray"] = QByteArray("bytes");
|
||||
|
||||
// QVariantMap map2;
|
||||
// map2["one"] = 1;
|
||||
// map2["two"] = QVariantMap();
|
||||
// map["map2"] = map2;
|
||||
|
||||
QByteArray arr = MsgPack::pack(map);
|
||||
|
||||
QVariantMap um = MsgPack::unpack(arr).toMap();
|
||||
|
||||
l << QPoint(12, 13);
|
||||
QByteArray arr = MsgPack::pack(l);
|
||||
qDebug() << arr.toBase64();
|
||||
|
||||
qDebug() << MsgPack::unpack(arr);
|
||||
|
||||
|
||||
return 0;
|
||||
//return a.exec();
|
||||
}
|
||||
|
||||
+3
-2
@@ -11,8 +11,8 @@ QT -= gui
|
||||
TARGET = qmsgpack
|
||||
CONFIG -= app_bundle
|
||||
|
||||
TEMPLATE = lib
|
||||
DEFINES += MSGPACK_MAKE_LIB
|
||||
TEMPLATE = app
|
||||
#DEFINES += MSGPACK_MAKE_LIB
|
||||
target.path = ../build
|
||||
INSTALLS += target
|
||||
QMAKE_CXXFLAGS += -fPIC
|
||||
@@ -20,6 +20,7 @@ QMAKE_CXXFLAGS += -fPIC
|
||||
|
||||
SOURCES += main.cpp \
|
||||
msgpack.cpp \
|
||||
msgpack_common.cpp \
|
||||
private/pack_p.cpp \
|
||||
private/unpack_p.cpp
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
#include "msgpack_common.h"
|
||||
|
||||
QString MsgPack::version()
|
||||
{
|
||||
return QString("%1.%2.%3")
|
||||
.arg(MSGPACK_MAJOR)
|
||||
.arg(MSGPACK_MINOR)
|
||||
.arg(MSGPACK_VERSION);
|
||||
}
|
||||
@@ -3,6 +3,10 @@
|
||||
|
||||
#include <QVariant>
|
||||
|
||||
#define MSGPACK_MAJOR 0
|
||||
#define MSGPACK_MINOR 0
|
||||
#define MSGPACK_VERSION 0
|
||||
|
||||
namespace MsgPack {
|
||||
/**
|
||||
* pack some variant to byte array data
|
||||
@@ -12,7 +16,9 @@ namespace MsgPack {
|
||||
*/
|
||||
typedef quint32 (*pack_user_f)(const QVariant &variant, QByteArray &data, bool write);
|
||||
|
||||
typedef QVariant (*unpack_user_f)(QByteArray &data);
|
||||
typedef QVariant (*unpack_user_f)(const QByteArray &data);
|
||||
|
||||
QString version();
|
||||
}
|
||||
|
||||
#endif // COMMON_H
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef COMMON_H
|
||||
#define COMMON_H
|
||||
|
||||
#include <QVariant>
|
||||
|
||||
#define MSGPACK_MAJOR @QMSGPACK_MAJOR@
|
||||
#define MSGPACK_MINOR @QMSGPACK_MINOR@
|
||||
#define MSGPACK_VERSION @QMSGPACK_VERSION@
|
||||
|
||||
namespace MsgPack {
|
||||
/**
|
||||
* pack some variant to byte array data
|
||||
* when write == false only calculates and returns size
|
||||
* when write == true writes bytes to data, and returns the same size
|
||||
* return type size
|
||||
*/
|
||||
typedef quint32 (*pack_user_f)(const QVariant &variant, QByteArray &data, bool write);
|
||||
|
||||
typedef QVariant (*unpack_user_f)(const QByteArray &data);
|
||||
|
||||
QString version();
|
||||
}
|
||||
|
||||
#endif // COMMON_H
|
||||
+20
-2
@@ -18,6 +18,8 @@ quint8 *MsgPackPrivate::pack(const QVariant &v, quint8 *p, bool wr)
|
||||
p = pack_string(v.toString(), p, wr);
|
||||
else if (t == QMetaType::QVariantList)
|
||||
p = pack_list(v.toList(), p, wr);
|
||||
else if (t == QMetaType::QStringList)
|
||||
p = pack_stringlist(v.toStringList(), p, wr);
|
||||
else if (t == QMetaType::LongLong)
|
||||
p = pack_longlong(v.toLongLong(), p, wr);
|
||||
else if (t == QMetaType::ULongLong)
|
||||
@@ -120,9 +122,8 @@ quint8 *MsgPackPrivate::pack_bool(const QVariant &v, quint8 *p, bool wr)
|
||||
return p + 1;
|
||||
}
|
||||
|
||||
quint8 *MsgPackPrivate::pack_list(const QVariantList &list, quint8 *p, bool wr)
|
||||
quint8 *MsgPackPrivate::pack_listlen(quint32 len, quint8 *p, bool wr)
|
||||
{
|
||||
int len = list.length();
|
||||
if (len <= 15) {
|
||||
if (wr) *p = 0x90 | len;
|
||||
p++;
|
||||
@@ -137,11 +138,27 @@ quint8 *MsgPackPrivate::pack_list(const QVariantList &list, quint8 *p, bool wr)
|
||||
if (wr) _msgpack_store32(p, len);
|
||||
p += 4;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
quint8 *MsgPackPrivate::pack_list(const QVariantList &list, quint8 *p, bool wr)
|
||||
{
|
||||
int len = list.length();
|
||||
p = pack_listlen(len, p, wr);
|
||||
foreach (QVariant item, list)
|
||||
p = pack(item, p, wr);
|
||||
return p;
|
||||
}
|
||||
|
||||
quint8 *MsgPackPrivate::pack_stringlist(const QStringList &list, quint8 *p, bool wr)
|
||||
{
|
||||
int len = list.length();
|
||||
p = pack_listlen(len, p, wr);
|
||||
foreach (QString item, list)
|
||||
p = pack_string(item, p, wr);
|
||||
return p;
|
||||
}
|
||||
|
||||
quint8 *MsgPackPrivate::pack_string(const QString &str, quint8 *p, bool wr)
|
||||
{
|
||||
int len = str.length();
|
||||
@@ -306,3 +323,4 @@ quint8 *MsgPackPrivate::pack_user(const QVariant &v, quint8 *p, bool wr)
|
||||
memcpy(p, data.data(), len);
|
||||
return p += len;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,15 +22,16 @@ quint8 * pack_longlong(qint64 i, quint8 *p, bool wr);
|
||||
quint8 * pack_ulonglong(quint64 i, quint8 *p, bool wr);
|
||||
|
||||
quint8 * pack_bool(const QVariant &v, quint8 *p, bool wr);
|
||||
|
||||
quint8 * pack_listlen(quint32 len, quint8 *p, bool wr);
|
||||
quint8 * pack_list(const QVariantList &list, quint8 *p, bool wr);
|
||||
quint8 * pack_stringlist(const QStringList &list, 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_array(const QByteArray &arr, quint8 *p, bool wr);
|
||||
quint8 * pack_map(const QVariantMap &map, quint8 *p, bool wr);
|
||||
quint8 * pack_user(const QVariant &v, quint8 *p, bool wr);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif // PACK_P_H
|
||||
|
||||
@@ -352,24 +352,28 @@ quint8 * MsgPackPrivate::unpack_fixext16(QVariant &v, quint8 *p)
|
||||
|
||||
quint8 * MsgPackPrivate::unpack_ext8(QVariant &v, quint8 *p)
|
||||
{
|
||||
qint8 type = *(++p);
|
||||
quint32 len = *(p++);
|
||||
p++;
|
||||
quint32 len = *(p);
|
||||
p += 1;
|
||||
qint8 type = *(p);
|
||||
return unpack_ext(v, p + 1, type, len);
|
||||
}
|
||||
|
||||
quint8 * MsgPackPrivate::unpack_ext16(QVariant &v, quint8 *p)
|
||||
{
|
||||
qint8 type = *(++p);
|
||||
p++;
|
||||
quint32 len = _msgpack_load16(quint32, p);
|
||||
p += 2;
|
||||
qint8 type = *(p);
|
||||
return unpack_ext(v, p + 1, type, len);
|
||||
}
|
||||
|
||||
quint8 * MsgPackPrivate::unpack_ext32(QVariant &v, quint8 *p)
|
||||
{
|
||||
qint8 type = *(++p);
|
||||
p++;
|
||||
quint32 len = _msgpack_load32(quint32, p);
|
||||
p += 4;
|
||||
qint8 type = *(p);
|
||||
return unpack_ext(v, p + 1, type, len);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user