-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryReader.h
More file actions
56 lines (43 loc) · 1.2 KB
/
BinaryReader.h
File metadata and controls
56 lines (43 loc) · 1.2 KB
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
#ifndef BINARYREADER_H
#define BINARYREADER_H
#include <QtCore>
class BinaryReader
{
public:
BinaryReader(QByteArray file, QDataStream::ByteOrder order = QDataStream::LittleEndian) : m_buffer(file), m_stream(&m_buffer, QIODevice::ReadOnly)
{
m_stream.setByteOrder(order);
}
int remaning() { return m_stream.device()->bytesAvailable(); }
template <class T>
BinaryReader& operator>>(T& value)
{
m_stream >> value;
return *this;
}
template <typename T>
T read()
{
T v;
*this >> v;
return v;
}
bool readBool() { return read<bool>(); }
qint8 readByte() { return read<qint8>(); }
qint16 readShort() { return read<qint16>(); }
qint32 readInt(){ return read<qint32>(); }
qint64 readLong() { return read<qint64>(); }
QString readString(quint16 length)
{
QByteArray bytes;
bytes.resize(length);
for (quint16 i = 0; i < length; ++i)
bytes[i] = readByte();
QString string = QString(bytes);
return string;
}
private:
QByteArray m_buffer;
QDataStream m_stream;
};
#endif // BINARYREADER_H