Browse Source

Added BinaryMemory reader

Thomas Buck 10 years ago
parent
commit
838fe35ad4
3 changed files with 158 additions and 3 deletions
  1. 1
    0
      ChangeLog.md
  2. 33
    3
      include/utils/binary.h
  3. 124
    0
      src/utils/binary.cpp

+ 1
- 0
ChangeLog.md View File

4
 
4
 
5
     [ 20141124 ]
5
     [ 20141124 ]
6
     * LoaderTR2 is starting to populate Room structures
6
     * LoaderTR2 is starting to populate Room structures
7
+    * Added BinaryMemory reader
7
 
8
 
8
     [ 20141123 ]
9
     [ 20141123 ]
9
     * Added texture viewer debug UI
10
     * Added texture viewer debug UI

+ 33
- 3
include/utils/binary.h View File

12
 
12
 
13
 class BinaryFile {
13
 class BinaryFile {
14
   public:
14
   public:
15
-
15
+    BinaryFile(const char* f = nullptr);
16
     ~BinaryFile();
16
     ~BinaryFile();
17
 
17
 
18
-    int open(const char* f);
18
+    int open(const char* f = nullptr);
19
 
19
 
20
     long long tell();
20
     long long tell();
21
-    void seek(long long pos);
21
+    void seek(long long pos = 0);
22
 
22
 
23
     int8_t read8();
23
     int8_t read8();
24
     uint8_t readU8();
24
     uint8_t readU8();
38
     std::ifstream file;
38
     std::ifstream file;
39
 };
39
 };
40
 
40
 
41
+class BinaryMemory {
42
+  public:
43
+    BinaryMemory(char* d = nullptr);
44
+
45
+    int open(char* d = nullptr);
46
+
47
+    long long tell();
48
+    void seek(long long pos = 0);
49
+
50
+    int8_t read8();
51
+    uint8_t readU8();
52
+
53
+    int16_t read16();
54
+    uint16_t readU16();
55
+
56
+    int32_t read32();
57
+    uint32_t readU32();
58
+
59
+    int64_t read64();
60
+    uint64_t readU64();
61
+
62
+    float readFloat();
63
+
64
+  private:
65
+    void read(char* d, int c);
66
+
67
+    char* data;
68
+    long long offset;
69
+};
70
+
41
 #endif
71
 #endif
42
 
72
 

+ 124
- 0
src/utils/binary.cpp View File

8
 #include "global.h"
8
 #include "global.h"
9
 #include "utils/binary.h"
9
 #include "utils/binary.h"
10
 
10
 
11
+BinaryFile::BinaryFile(const char* f) {
12
+    int r = open(f);
13
+    if (r != 0)
14
+        throw r;
15
+}
16
+
11
 BinaryFile::~BinaryFile() {
17
 BinaryFile::~BinaryFile() {
12
     if (file.is_open())
18
     if (file.is_open())
13
         file.close();
19
         file.close();
17
     if (file.is_open()) {
23
     if (file.is_open()) {
18
         return 1;
24
         return 1;
19
     } else {
25
     } else {
26
+        if (f == nullptr)
27
+            return 0;
20
         file.open(f, std::ios_base::in | std::ios_base::binary);
28
         file.open(f, std::ios_base::in | std::ios_base::binary);
21
         return (file ? 0 : 1);
29
         return (file ? 0 : 1);
22
     }
30
     }
139
     return ret;
147
     return ret;
140
 }
148
 }
141
 
149
 
150
+// ----------------------------------------------------------------------------
151
+
152
+BinaryMemory::BinaryMemory(char* d) : data(nullptr), offset(0) {
153
+    int r = open(d);
154
+    if (r != 0)
155
+        throw r;
156
+}
157
+
158
+int BinaryMemory::open(char* d) {
159
+    if (data != nullptr)
160
+        return 1;
161
+
162
+    if (d != nullptr) {
163
+        data = d;
164
+        offset = 0;
165
+    }
166
+
167
+    return 0;
168
+}
169
+
170
+long long BinaryMemory::tell() {
171
+    assert(offset >= 0);
172
+    return offset;
173
+}
174
+
175
+void BinaryMemory::seek(long long pos) {
176
+    assert(pos >= 0);
177
+    offset = pos;
178
+}
179
+
180
+void BinaryMemory::read(char* d, int c) {
181
+    for (int i = 0; i < c; i++) {
182
+        d[i] = data[offset + i];
183
+    }
184
+    offset += c;
185
+}
186
+
187
+uint8_t BinaryMemory::readU8() {
188
+    assert(offset >= 0);
189
+    uint8_t ret;
190
+    char* c = reinterpret_cast<char*>(&ret);
191
+    read(c, 1);
192
+    return ret;
193
+}
194
+
195
+uint16_t BinaryMemory::readU16() {
196
+    assert(offset >= 0);
197
+    uint8_t a = readU8();
198
+    uint8_t b = readU8();
199
+    return ((uint16_t)a | (uint16_t)(b << 8));
200
+}
201
+
202
+uint32_t BinaryMemory::readU32() {
203
+    assert(offset >= 0);
204
+    uint16_t a = readU16();
205
+    uint16_t b = readU16();
206
+    return ((uint32_t)a | (uint32_t)(b << 16));
207
+}
208
+
209
+uint64_t BinaryMemory::readU64() {
210
+    assert(offset >= 0);
211
+    uint32_t a = readU32();
212
+    uint32_t b = readU32();
213
+    return ((uint64_t)a | (uint64_t)(b << 32));
214
+}
215
+
216
+float BinaryMemory::readFloat() {
217
+    assert(offset >= 0);
218
+    uint32_t val = readU32();
219
+    char* a = reinterpret_cast<char*>(&val);
220
+
221
+    float ret;
222
+    char* b = reinterpret_cast<char*>(&ret);
223
+
224
+    for (int i = 0; i < 4; i++)
225
+        b[i] = a[i];
226
+
227
+    return ret;
228
+}
229
+
230
+int8_t BinaryMemory::read8() {
231
+    assert(offset >= 0);
232
+    int8_t ret;
233
+    char* p = reinterpret_cast<char*>(&ret);
234
+    read(p, sizeof(ret));
235
+    return ret;
236
+}
237
+
238
+int16_t BinaryMemory::read16() {
239
+    assert(offset >= 0);
240
+    int16_t ret;
241
+    char* p = reinterpret_cast<char*>(&ret);
242
+    read(p, sizeof(ret));
243
+    swapByteOrder(p, 2);
244
+    return ret;
245
+}
246
+
247
+int32_t BinaryMemory::read32() {
248
+    assert(offset >= 0);
249
+    int32_t ret;
250
+    char* p = reinterpret_cast<char*>(&ret);
251
+    read(p, sizeof(ret));
252
+    swapByteOrder(p, 4);
253
+    return ret;
254
+}
255
+
256
+int64_t BinaryMemory::read64() {
257
+    assert(offset >= 0);
258
+    assert(offset >= 0);
259
+    int64_t ret;
260
+    char* p = reinterpret_cast<char*>(&ret);
261
+    read(p, sizeof(ret));
262
+    swapByteOrder(p, 8);
263
+    return ret;
264
+}
265
+

Loading…
Cancel
Save