mirror of
https://github.com/romixlab/qmsgpack.git
synced 2025-06-25 01:21:33 +02:00
MsgPackStream: writeBytes() added
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,6 +1,7 @@
|
|||||||
*.pro.user*
|
*.pro.user*
|
||||||
build
|
build
|
||||||
lib
|
lib
|
||||||
Makefile
|
Makefile*
|
||||||
*.autosave
|
*.autosave
|
||||||
bin/
|
bin/
|
||||||
|
*.o
|
||||||
|
@ -442,6 +442,14 @@ MsgPackStream &MsgPackStream::operator<<(QByteArray array)
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool MsgPackStream::writeBytes(const char *data, uint len)
|
||||||
|
{
|
||||||
|
CHECK_STREAM_WRITE_PRECOND(*this);
|
||||||
|
if (dev->write(data, len) != len)
|
||||||
|
setStatus(WriteFailed);
|
||||||
|
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) {
|
||||||
|
20
src/stream.h
20
src/stream.h
@ -1,6 +1,7 @@
|
|||||||
#ifndef STREAM_H
|
#ifndef STREAM_H
|
||||||
#define STREAM_H
|
#define STREAM_H
|
||||||
#include <QIODevice>
|
#include <QIODevice>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
class MsgPackStream
|
class MsgPackStream
|
||||||
{
|
{
|
||||||
@ -44,6 +45,8 @@ public:
|
|||||||
MsgPackStream &operator<<(QString str);
|
MsgPackStream &operator<<(QString str);
|
||||||
MsgPackStream &operator<<(const char *str);
|
MsgPackStream &operator<<(const char *str);
|
||||||
MsgPackStream &operator<<(QByteArray array);
|
MsgPackStream &operator<<(QByteArray array);
|
||||||
|
bool writeBytes(const char *data, uint len);
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QIODevice *dev;
|
QIODevice *dev;
|
||||||
@ -63,7 +66,22 @@ private:
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
MsgPackStream& operator<<(MsgPackStream& s, const QList<T> &list)
|
MsgPackStream& operator<<(MsgPackStream& s, const QList<T> &list)
|
||||||
{
|
{
|
||||||
s << (quint32)list.size();
|
quint32 len = list.size();
|
||||||
|
quint8 p[5];
|
||||||
|
if (len <= 15) {
|
||||||
|
p[0] = 0x90 | len;
|
||||||
|
s.writeBytes(p, 1);
|
||||||
|
} else if (len <= std::numeric_limits<quint16>::max()) {
|
||||||
|
p[0] = 0xdc;
|
||||||
|
_msgpack_store16(p + 1, len);
|
||||||
|
s.writeBytes(p, 3);
|
||||||
|
} else {
|
||||||
|
p[0] = 0xdd;
|
||||||
|
_msgpack_store32(p + 1, len);
|
||||||
|
s.writeBytes(p, 5);
|
||||||
|
}
|
||||||
|
if (s.status() != MsgPackStream::Ok)
|
||||||
|
return *this;
|
||||||
for (int i = 0; i < list.size(); ++i)
|
for (int i = 0; i < list.size(); ++i)
|
||||||
s << list[i];
|
s << list[i];
|
||||||
return s;
|
return s;
|
||||||
|
Reference in New Issue
Block a user