-
Notifications
You must be signed in to change notification settings - Fork 224
/
filehandle.h
335 lines (278 loc) · 8.33 KB
/
filehandle.h
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//////////////////////////////////////////////////////////////////////
// This file is part of Remere's Map Editor
//////////////////////////////////////////////////////////////////////
// Remere's Map Editor is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Remere's Map Editor is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//////////////////////////////////////////////////////////////////////
#ifndef RME_FILEHANDLE_H_
#define RME_FILEHANDLE_H_
#include "definitions.h"
#include <stdexcept>
#include <string>
#include <stack>
#include <stdio.h>
#ifndef FORCEINLINE
# ifdef _MSV_VER
# define FORCEINLINE __forceinline
# else
# define FORCEINLINE inline
# endif
#endif
enum FileHandleError {
FILE_NO_ERROR,
FILE_COULD_NOT_OPEN,
FILE_INVALID_IDENTIFIER,
FILE_STRING_TOO_LONG,
FILE_READ_ERROR,
FILE_WRITE_ERROR,
FILE_SYNTAX_ERROR,
FILE_PREMATURE_END,
};
enum NodeType {
NODE_START = 0xfe,
NODE_END = 0xff,
ESCAPE_CHAR = 0xfd,
};
class FileHandle
{
public:
FileHandle() : error_code(FILE_NO_ERROR), file(nullptr) {}
virtual ~FileHandle() { close(); }
bool seek(size_t offset, int origin = SEEK_SET);
size_t tell();
FORCEINLINE void skip(size_t offset) { seek(offset, SEEK_CUR); }
// Ensures we don't accidentally copy it.
FileHandle(const FileHandle &) = delete;
FileHandle &operator=(const FileHandle &) = delete;
virtual void close();
virtual bool isOpen() { return file != nullptr; }
virtual bool isOk() { return isOpen() && error_code == FILE_NO_ERROR && ferror(file) == 0; }
std::string getErrorMessage();
public:
FileHandleError error_code;
FILE* file;
};
class FileReadHandle : public FileHandle
{
public:
explicit FileReadHandle(const std::string& name);
virtual ~FileReadHandle();
FORCEINLINE bool getU8(uint8_t& u8) { return getType(u8); }
FORCEINLINE bool getByte(uint8_t& u8) { return getType(u8); }
FORCEINLINE bool getSByte(int8_t& i8) { return getType(i8); }
FORCEINLINE bool getU16(uint16_t& u16) { return getType(u16); }
FORCEINLINE bool getU32(uint32_t& u32) { return getType(u32); }
FORCEINLINE bool get32(int32_t& i32) { return getType(i32); }
bool getRAW(uint8_t* ptr, size_t sz);
bool getRAW(std::string& str, size_t sz);
bool getString(std::string& str);
bool getLongString(std::string& str);
virtual void close();
size_t size() { return file_size; }
protected:
size_t file_size;
template<class T>
bool getType(T& ref) {
fread(&ref, sizeof(ref), 1, file);
return ferror(file) == 0;
}
};
class NodeFileReadHandle;
class DiskNodeFileReadHandle;
class MemoryNodeFileReadHandle;
class BinaryNode
{
public:
BinaryNode(NodeFileReadHandle* file, BinaryNode* parent);
~BinaryNode();
FORCEINLINE bool getU8(uint8_t& u8) { return getType(u8); }
FORCEINLINE bool getByte(uint8_t& u8) { return getType(u8); }
FORCEINLINE bool getU16(uint16_t& u16) { return getType(u16); }
FORCEINLINE bool getU32(uint32_t& u32) { return getType(u32); }
FORCEINLINE bool getU64(uint64_t& u64) { return getType(u64); }
FORCEINLINE bool skip(size_t sz) {
if(read_offset + sz > data.size()) {
read_offset = data.size();
return false;
}
read_offset += sz;
return true;
}
bool getRAW(uint8_t* ptr, size_t sz);
bool getRAW(std::string& str, size_t sz);
bool getString(std::string& str);
bool getLongString(std::string& str);
BinaryNode* getChild();
// Returns this on success, nullptr on failure
BinaryNode* advance();
protected:
template<class T>
bool getType(T& ref) {
if(read_offset + sizeof(ref) > data.size()) {
read_offset = data.size();
return false;
}
ref = *(T*)(data.data()+read_offset);
read_offset += sizeof(ref);
return true;
}
void load();
std::string data;
size_t read_offset;
NodeFileReadHandle* file;
BinaryNode* parent;
BinaryNode* child;
friend class DiskNodeFileReadHandle;
friend class MemoryNodeFileReadHandle;
};
class NodeFileReadHandle : public FileHandle
{
public:
NodeFileReadHandle();
virtual ~NodeFileReadHandle();
virtual BinaryNode* getRootNode() = 0;
virtual size_t size() = 0;
virtual size_t tell() = 0;
protected:
BinaryNode* getNode(BinaryNode* parent);
void freeNode(BinaryNode* node);
// Returns false when end-of-file is reached
virtual bool renewCache() = 0;
bool last_was_start;
uint8_t* cache;
size_t cache_size;
size_t cache_length;
size_t local_read_index;
BinaryNode* root_node;
std::stack<void*> unused;
friend class BinaryNode;
};
class DiskNodeFileReadHandle : public NodeFileReadHandle
{
public:
DiskNodeFileReadHandle(const std::string& name, const std::vector<std::string>& acceptable_identifiers);
virtual ~DiskNodeFileReadHandle();
virtual void close();
virtual BinaryNode* getRootNode();
virtual size_t size() { return file_size; }
virtual size_t tell() {if(file) return ftell(file); return 0; }
protected:
virtual bool renewCache();
size_t file_size;
};
class MemoryNodeFileReadHandle : public NodeFileReadHandle
{
public:
// Does NOT claim ownership of the memory it is given.
MemoryNodeFileReadHandle(const uint8_t* data, size_t size);
virtual ~MemoryNodeFileReadHandle();
void assign(const uint8_t* data, size_t size);
virtual void close();
virtual BinaryNode* getRootNode();
virtual size_t size() { return cache_size; }
virtual size_t tell() { return local_read_index; }
virtual bool isOk() { return true; }
protected:
virtual bool renewCache();
uint8_t* index;
};
class FileWriteHandle : public FileHandle
{
public:
explicit FileWriteHandle(const std::string& name);
virtual ~FileWriteHandle();
FORCEINLINE bool addU8(uint8_t u8) { return addType(u8); }
FORCEINLINE bool addByte(uint8_t u8) { return addType(u8); }
FORCEINLINE bool addU16(uint16_t u16) { return addType(u16); }
FORCEINLINE bool addU32(uint32_t u32) { return addType(u32); }
FORCEINLINE bool addU64(uint64_t u64) { return addType(u64); }
bool addString(const std::string& str);
bool addString(const char* str);
bool addLongString(const std::string& str);
bool addRAW(const std::string& str);
bool addRAW(const uint8_t* ptr, size_t sz);
bool addRAW(const char* c) { return addRAW(reinterpret_cast<const uint8_t*>(c), strlen(c)); }
void flush();
protected:
template<class T>
bool addType(T ref) {
fwrite(&ref, sizeof(ref), 1, file);
return ferror(file) == 0;
}
};
class NodeFileWriteHandle : public FileHandle
{
public:
NodeFileWriteHandle();
virtual ~NodeFileWriteHandle();
bool addNode(uint8_t nodetype);
bool endNode();
bool addU8(uint8_t u8);
bool addByte(uint8_t u8);
bool addU16(uint16_t u16);
bool addU32(uint32_t u32);
bool addU64(uint64_t u64);
bool addString(const std::string& str);
bool addLongString(const std::string& str);
bool addRAW(std::string& str);
bool addRAW(const uint8_t* ptr, size_t sz);
bool addRAW(const char* c) { return addRAW(reinterpret_cast<const uint8_t*>(c), strlen(c)); }
protected:
virtual void renewCache() = 0;
static uint8_t NODE_START;
static uint8_t NODE_END;
static uint8_t ESCAPE_CHAR;
uint8_t* cache;
size_t cache_size;
size_t local_write_index;
FORCEINLINE void writeBytes(const uint8_t* ptr, size_t sz) {
if(sz) {
do {
if(*ptr == NODE_START || *ptr == NODE_END || *ptr == ESCAPE_CHAR) {
cache[local_write_index++] = ESCAPE_CHAR;
if(local_write_index >= cache_size) {
renewCache();
}
}
cache[local_write_index++] = *ptr;
if(local_write_index >= cache_size) {
renewCache();
}
++ptr;
--sz;
} while(sz != 0);
}
}
};
class DiskNodeFileWriteHandle : public NodeFileWriteHandle
{
public:
DiskNodeFileWriteHandle(const std::string& name, const std::string& identifier);
virtual ~DiskNodeFileWriteHandle();
virtual void close();
protected:
virtual void renewCache();
};
class MemoryNodeFileWriteHandle : public NodeFileWriteHandle
{
public:
MemoryNodeFileWriteHandle();
virtual ~MemoryNodeFileWriteHandle();
void reset();
virtual void close();
uint8_t* getMemory();
size_t getSize();
protected:
virtual void renewCache();
};
#endif