summaryrefslogtreecommitdiffstats
path: root/include/util/simpleserializer.h
blob: 1388aa549c88389c7e77bbc2fc890ab94de5f163 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifndef INCLUDE_SIMPLESERIALIZER_H
#define INCLUDE_SIMPLESERIALIZER_H

#include <QString>
#include <QMap>
#include "dsp/dsptypes.h"
#include "util/export.h"

class SDRANGELOVE_API SimpleSerializer {
public:
	SimpleSerializer(quint32 version);

	void writeS32(quint32 id, qint32 value);
	void writeU32(quint32 id, quint32 value);
	void writeS64(quint32 id, qint64 value);
	void writeU64(quint32 id, quint64 value);
	void writeFloat(quint32 id, float value);
	void writeDouble(quint32 id, double value);
	void writeReal(quint32 id, Real value)
	{
		if(sizeof(Real) == 4)
			writeFloat(id, value);
		else writeDouble(id, value);
	}
	void writeBool(quint32 id, bool value);
	void writeString(quint32 id, const QString& value);
	void writeBlob(quint32 id, const QByteArray& value);

	const QByteArray& final();

protected:
	enum Type {
		TSigned32 = 0,
		TUnsigned32 = 1,
		TSigned64 = 2,
		TUnsigned64 = 3,
		TFloat = 4,
		TDouble = 5,
		TBool = 6,
		TString = 7,
		TBlob = 8,
		TVersion = 9
	};

	QByteArray m_data;
	bool m_finalized;

	bool writeTag(Type type, quint32 id, quint32 length);
};

class SDRANGELOVE_API SimpleDeserializer {
public:
	SimpleDeserializer(const QByteArray& data);

	bool readS32(quint32 id, qint32* result, qint32 def = 0) const;
	bool readU32(quint32 id, quint32* result, quint32 def = 0) const;
	bool readS64(quint32 id, qint64* result, qint64 def = 0) const;
	bool readU64(quint32 id, quint64* result, quint64 def = 0) const;
	bool readFloat(quint32 id, float* result, float def = 0) const;
	bool readDouble(quint32 id, double* result, double def = 0) const;
	bool readReal(quint32 id, Real* result, Real def = 0) const;
	bool readBool(quint32 id, bool* result, bool def = false) const;
	bool readString(quint32 id, QString* result, const QString& def = QString::null) const;
	bool readBlob(quint32 id, QByteArray* result, const QByteArray& def = QByteArray()) const;

	bool isValid() const { return m_valid; }
	quint32 getVersion() const { return m_version; }
	void dump() const;

private:
	enum Type {
		TSigned32 = 0,
		TUnsigned32 = 1,
		TSigned64 = 2,
		TUnsigned64 = 3,
		TFloat = 4,
		TDouble = 5,
		TBool = 6,
		TString = 7,
		TBlob = 8,
		TVersion = 9
	};

	struct Element {
		Type type;
		quint32 ofs;
		quint32 length;

		Element(Type _type, quint32 _ofs, quint32 _length) :
			type(_type),
			ofs(_ofs),
			length(_length)
		{ }
	};
	typedef QMap<quint32, Element> Elements;

	QByteArray m_data;
	bool m_valid;
	Elements m_elements;
	quint32 m_version;

	bool parseAll();
	bool readTag(uint* readOfs, uint readEnd, Type* type, quint32* id, quint32* length) const;
	quint8 readByte(uint* readOfs) const
	{
		quint8 res = m_data[*readOfs];
		(*readOfs)++;
		return res;
	}
};

#endif // INCLUDE_SIMPLESERIALIZER_H