5 #define PRINTF_FORMAT_S32 "%d" 6 #define PRINTF_FORMAT_U32 "%u" 7 #define PRINTF_FORMAT_S64 "%d" 8 #define PRINTF_FORMAT_U64 "%u" 10 #define PRINTF_FORMAT_S32 "%d" 11 #define PRINTF_FORMAT_U32 "%u" 12 #define PRINTF_FORMAT_S64 "%lld" 13 #define PRINTF_FORMAT_U64 "%llu" 28 if(version >= (1 << 24))
30 else if(version >= (1 << 16))
32 else if(version >= (1 << 8))
40 for(
int i = length;
i >= 0;
i--)
41 m_data.push_back((
char)((version >> (
i * 8)) & 0xff));
49 qCritical(
"SimpleSerializer: ID 0 is not allowed");
53 if((value < -(1 << 23)) || (value >= (1 << 23)))
55 else if((value < -(1 << 15)) || (value >= (1 << 15)))
57 else if((value < -(1 << 7)) || (value >= (1 << 7)))
67 for(
int i = length;
i >= 0;
i--)
68 m_data.push_back((
char)((value >> (
i * 8)) & 0xff));
76 qCritical(
"SimpleSerializer: ID 0 is not allowed");
80 if(value >= (1 << 24))
82 else if(value >= (1 << 16))
84 else if(value >= (1 << 8))
94 for(
int i = length;
i >= 0;
i--)
95 m_data.push_back((
char)((value >> (
i * 8)) & 0xff));
103 qCritical(
"SimpleSerializer: ID 0 is not allowed");
107 if((value < -(1ll << 55)) || (value >= (1ll << 55)))
109 else if((value < -(1ll << 47)) || (value >= (1ll << 47)))
111 else if((value < -(1ll << 39)) || (value >= (1ll << 39)))
113 else if((value < -(1ll << 31)) || (value >= (1ll << 31)))
115 else if((value < -(1ll << 23)) || (value >= (1ll << 23)))
117 else if((value < -(1ll << 15)) || (value >= (1ll << 15)))
119 else if((value < -(1ll << 7)) || (value >= (1ll << 7)))
129 for(
int i = length;
i >= 0;
i--)
130 m_data.push_back((
char)((value >> (
i * 8)) & 0xff));
138 qCritical(
"SimpleSerializer: ID 0 is not allowed");
142 if(value >= (1ll << 56))
144 else if(value >= (1ll << 48))
146 else if(value >= (1ll << 40))
148 else if(value >= (1ll << 32))
150 else if(value >= (1ll << 24))
152 else if(value >= (1ll << 16))
154 else if(value >= (1ll << 8))
164 for(
int i = length;
i >= 0;
i--)
165 m_data.push_back((
char)((value >> (
i * 8)) & 0xff));
177 qCritical(
"SimpleSerializer: ID 0 is not allowed");
185 m_data.push_back((
char)((tmp.
u >> 24) & 0xff));
186 m_data.push_back((
char)((tmp.
u >> 16) & 0xff));
187 m_data.push_back((
char)((tmp.
u >> 8) & 0xff));
188 m_data.push_back((
char)(tmp.
u & 0xff));
200 qCritical(
"SimpleSerializer: ID 0 is not allowed");
208 m_data.push_back((
char)((tmp.
u >> 56) & 0xff));
209 m_data.push_back((
char)((tmp.
u >> 48) & 0xff));
210 m_data.push_back((
char)((tmp.
u >> 40) & 0xff));
211 m_data.push_back((
char)((tmp.
u >> 32) & 0xff));
212 m_data.push_back((
char)((tmp.
u >> 24) & 0xff));
213 m_data.push_back((
char)((tmp.
u >> 16) & 0xff));
214 m_data.push_back((
char)((tmp.
u >> 8) & 0xff));
215 m_data.push_back((
char)(tmp.
u & 0xff));
221 qCritical(
"SimpleSerializer: ID 0 is not allowed");
228 m_data.push_back((
char)0x01);
229 else m_data.push_back((
char)0x00);
235 qCritical(
"SimpleSerializer: ID 0 is not allowed");
239 QByteArray utf8 = value.toUtf8();
248 qCritical(
"SimpleSerializer: ID 0 is not allowed");
266 qCritical(
"SimpleSerializer: config has already been finalized (id %u)",
id);
275 else if(
id < (1 << 16))
277 else if(
id < (1 << 24))
281 if(length < (1 << 8))
283 else if(length < (1 << 16))
285 else if(length < (1 << 24))
289 m_data.push_back((
char)((type << 4) | (idLen << 2) | lengthLen));
290 for(
int i = idLen;
i >= 0;
i--)
291 m_data.push_back((
char)((
id >> (
i * 8)) & 0xff));
292 for(
int i = lengthLen;
i >= 0;
i--)
293 m_data.push_back((
char)((length >> (
i * 8)) & 0xff));
304 Elements::const_iterator it =
m_elements.constFind(0);
314 for(uint
i = 0;
i < it->length;
i++)
326 Elements::const_iterator it =
m_elements.constFind(
id);
336 for(uint
i = 0;
i < it->length;
i++) {
338 if((
i == 0) && (byte & 0x80))
340 tmp = (tmp << 8) | byte;
354 Elements::const_iterator it =
m_elements.constFind(
id);
364 for(uint
i = 0;
i < it->length;
i++)
365 tmp = (tmp << 8) |
readByte(&readOfs);
378 Elements::const_iterator it =
m_elements.constFind(
id);
388 for(uint
i = 0;
i < it->length;
i++) {
390 if((
i == 0) && (byte & 0x80))
392 tmp = (tmp << 8) | byte;
406 Elements::const_iterator it =
m_elements.constFind(
id);
416 for(uint
i = 0;
i < it->length;
i++)
417 tmp = (tmp << 8) |
readByte(&readOfs);
430 Elements::const_iterator it =
m_elements.constFind(
id);
440 for(
int i = 0;
i < 4;
i++)
454 Elements::const_iterator it =
m_elements.constFind(
id);
464 for(
int i = 0;
i < 8;
i++)
486 if(
sizeof(
Real) == 4) {
489 Elements::const_iterator it =
m_elements.constFind(
id);
491 goto returnDefault32;
493 goto returnDefault32;
495 goto returnDefault32;
499 for(
int i = 0;
i < 4;
i++)
510 Elements::const_iterator it =
m_elements.constFind(
id);
512 goto returnDefault64;
514 goto returnDefault64;
516 goto returnDefault64;
520 for(
int i = 0;
i < 8;
i++)
535 Elements::const_iterator it =
m_elements.constFind(
id);
538 if(it->type !=
TBool)
557 Elements::const_iterator it =
m_elements.constFind(
id);
563 *result = QString::fromUtf8(
m_data.data() + it->ofs, it->length);
573 Elements::const_iterator it =
m_elements.constFind(
id);
576 if(it->type !=
TBlob)
579 *result = QByteArray(
m_data.data() + it->ofs, it->length);
590 qDebug(
"SimpleDeserializer dump: data invalid");
593 qDebug(
"SimpleDeserializer dump: version %u",
m_version);
625 qDebug(
"id %d, FLOAT, len %d: %f", it.key(), it->length, tmp);
631 qDebug(
"id %d, DOUBLE, len %d: %f", it.key(), it->length, tmp);
637 qDebug(
"id %d, BOOL, len %d: %s", it.key(), it->length, tmp ?
"true" :
"false");
643 qDebug(
"id %d, STRING, len %d: \"%s\"", it.key(), it->length, qPrintable(tmp));
649 qDebug(
"id %d, BLOB, len %d", it.key(), it->length);
653 qDebug(
"id %d, VERSION, len %d", it.key(), it->length);
657 qDebug(
"id %d, UNKNOWN TYPE 0x%02x, len %d", it.key(), it->type, it->length);
679 while(readOfs < (uint)
m_data.size()) {
686 qDebug(
"SimpleDeserializer: same ID found twice (id %u)",
id);
694 if(readOfs == (uint)
m_data.size())
704 *type = (
Type)(tag >> 4);
705 int idLen = ((tag >> 2) & 0x03) + 1;
706 int lengthLen = (tag & 0x03) + 1;
709 if(((*readOfs) + idLen + lengthLen) > readEnd)
713 for(
int i = 0;
i < idLen;
i++)
714 tmp = (tmp << 8) |
readByte(readOfs);
717 for(
int i = 0;
i < lengthLen;
i++)
718 tmp = (tmp << 8) |
readByte(readOfs);
722 if(((*readOfs) + (*length)) > readEnd)
Assert<(bool(sizeof(double)==8))> double_must_be_64_bits[bool(sizeof(double)==8) ? 1 :-1]
quint8 readByte(uint *readOfs) const
void writeDouble(quint32 id, double value)
bool readFloat(quint32 id, float *result, float def=0) const
bool readS64(quint32 id, qint64 *result, qint64 def=0) const
void writeFloat(quint32 id, float value)
#define PRINTF_FORMAT_U32
void writeBlob(quint32 id, const QByteArray &value)
Assert<(bool(sizeof(float)==4))> float_must_be_32_bits[bool(sizeof(float)==4) ? 1 :-1]
bool readU32(quint32 id, quint32 *result, quint32 def=0) const
bool readDouble(quint32 id, double *result, double def=0) const
bool readString(quint32 id, QString *result, const QString &def=QString::null) const
bool readBool(quint32 id, bool *result, bool def=false) const
SimpleSerializer(quint32 version)
bool writeTag(Type type, quint32 id, quint32 length)
bool readS32(quint32 id, qint32 *result, qint32 def=0) const
bool readBlob(quint32 id, QByteArray *result, const QByteArray &def=QByteArray()) const
void writeS32(quint32 id, qint32 value)
bool readU64(quint32 id, quint64 *result, quint64 def=0) const
void writeU32(quint32 id, quint32 value)
bool readTag(uint *readOfs, uint readEnd, Type *type, quint32 *id, quint32 *length) const
#define PRINTF_FORMAT_S32
bool readReal(quint32 id, Real *result, Real def=0) const
SimpleDeserializer(const QByteArray &data)
void writeBool(quint32 id, bool value)
void writeS64(quint32 id, qint64 value)
void writeString(quint32 id, const QString &value)
const QByteArray & final()
void writeU64(quint32 id, quint64 value)