forked from mrkite/TerraFirma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle.cpp
117 lines (99 loc) · 1.8 KB
/
handle.cpp
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
113
114
115
116
117
/**
* @Copyright 2015 seancode
*
* Binary reader class, reads files or qbytearrays
*/
#include "./handle.h"
#include <QFile>
Handle::Handle(QString filename) {
QFile f(filename);
if (f.exists() && f.open(QIODevice::ReadOnly)) {
pos = 0;
len = f.size();
bytearray = f.read(len);
data = (const quint8 *)bytearray.constData();
} else {
data = NULL;
}
}
Handle::Handle(QByteArray array) {
bytearray = array;
data = (const quint8 *)bytearray.constData();
pos = 0;
len = bytearray.length();
}
bool Handle::exists() const {
return data != NULL;
}
bool Handle::eof() const {
return pos >= len;
}
quint8 Handle::r8() {
return data[pos++];
}
quint16 Handle::r16() {
quint16 r = data[pos++];
r |= data[pos++] << 8;
return r;
}
quint32 Handle::r32() {
quint32 r = data[pos++];
r |= data[pos++] << 8;
r |= data[pos++] << 16;
r |= data[pos++] << 24;
return r;
}
quint64 Handle::r64() {
quint64 r = r32();
r |= (quint64)r32() << 32;
return r;
}
float Handle::rf() {
union {
float f;
quint32 l;
} fl;
fl.l = r32();
return fl.f;
}
double Handle::rd() {
union {
double d;
quint64 l;
} dl;
dl.l = r64();
return dl.d;
}
QString Handle::rs() {
uint len = 0;
int shift = 0;
quint8 u7;
do {
u7 = data[pos++];
len |= (u7 & 0x7f) << shift;
shift += 7;
} while (u7 & 0x80);
return read(len);
}
QString Handle::read(int len) {
int oldPos = pos;
pos += len;
return QString::fromUtf8((const char *)data + oldPos, len);
}
const char *Handle::readBytes(int len) {
int oldPos = pos;
pos += len;
return (const char *)data + oldPos;
}
void Handle::skip(int len) {
pos += len;
}
void Handle::seek(int offset) {
pos = offset;
}
qint64 Handle::tell() const {
return pos;
}
qint64 Handle::length() const {
return len;
}