-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIniArchive.cpp
More file actions
444 lines (342 loc) · 9.3 KB
/
Copy pathIniArchive.cpp
File metadata and controls
444 lines (342 loc) · 9.3 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// copyright DearGPU
// precompiled header with comment line afterwards to not get reordering from clang-format
#include <assert.h>
#include <vector>
#include "ASCIIFile.h" // IO_GetFileSize()
#include "IniArchive.h"
#include "math.h"
// 32bit max file size
// todo: better error handling (bool IsValid() so not every operation needs to be checked)
class CGeneralArchive {
public:
CGeneralArchive() : bIsLoading(false), outHandle(0), LoadPos(0) {}
~CGeneralArchive() {
if (IsSaving()) {
uint32 FileSize = (uint32)Data.size();
if (fwrite(&Data[0], FileSize, 1, outHandle) != 1) {
assert(0);
}
fclose(outHandle);
}
}
// @ param bZeroTermination allows to a byte of 0 termination, could be made working for unicode
// as well
// bool OpenForLoading(const TCHAR* pFileName, bool bZeroTermination = false) {
bool OpenForLoading(const char* pFileName, bool bZeroTermination = false)
{
assert(pFileName);
assert(!IsLoading());
assert(!IsSaving());
assert(!Data.size());
FILE* in = 0;
// if (_wfopen_s(&in, pFileName, L"rb") != 0) {
if (fopen_s(&in, pFileName, "rb") != 0) {
return false;
}
size_t FileSize = IO_GetFileSize(pFileName);
if (FileSize == -1) {
// internal error
return false;
}
Data.resize(FileSize + bZeroTermination);
if (FileSize) {
if (fread(&Data[0], FileSize, 1, in) != 1) {
assert(0);
}
}
if (bZeroTermination) {
Data[FileSize] = 0;
}
bIsLoading = true;
fclose(in);
return true;
}
// bool OpenForSaving(const TCHAR* pFileName) {
bool OpenForSaving(const char* pFileName) {
assert(pFileName);
assert(!IsLoading());
assert(!IsSaving());
assert(!Data.size());
// 1MB reserve to avoid reallocations
Data.reserve(1024 * 1024);
// if (_wfopen_s(&outHandle, pFileName, L"wb") != 0) {
if (fopen_s(&outHandle, pFileName, "wb") != 0) {
return false;
}
assert(IsSaving());
return true;
}
bool IsLoading() const {
return bIsLoading;
}
bool IsSuccessfulLoading() const {
return IsLoading() && LoadPos > 0 && LoadPos == Data.size();
}
bool IsSaving() const {
return outHandle != 0;
}
// @param InChunkId e.g. 'Chk8' or 'BVH_'
bool Chunk(const uint32 InChunkId) {
assert(InChunkId);
uint32 BackupPos = LoadPos;
uint32 ChunkId = InChunkId;
Serialize(&ChunkId, sizeof(ChunkId));
if (IsSaving()) {
return true;
}
if (ChunkId == InChunkId) {
return true;
}
LoadPos = BackupPos;
return false;
}
void Serialize(void* InData, uint32 InSize) {
assert(InData);
assert(InSize);
if (IsLoading()) {
uint32 MaxSize = (uint32)Data.size();
assert(LoadPos + InSize <= MaxSize);
memcpy(InData, &Data[LoadPos], InSize);
LoadPos += InSize;
} else {
uint32 OldSize = (uint32)Data.size();
Data.resize(OldSize + InSize);
memcpy(&Data[OldSize], InData, InSize);
}
}
size_t GetSize() const {
return Data.size();
}
const uint8* GetDataPtr() const {
return &Data[0];
}
public:
bool bIsLoading;
FILE* outHandle;
std::vector<uint8> Data;
uint32 LoadPos;
};
void ParseWhiteSpace(const uint8*& p) {
while (*p != 0 && (uint8)*p <= ' ') {
++p;
}
}
bool ParseStartsWith(const uint8*& _p, const char* Token) {
const uint8* p = _p;
const uint8* t = (const uint8*)Token;
while (*t) {
if (*p != *t) {
return false;
}
++p;
++t;
}
_p = p;
return true;
}
void ParseLine(const uint8*& p, std::string& Out) {
Out.clear();
// can be optimized, does a lot of resize
while (*p) {
if (*p == 13) // CR
{
++p;
if (*p == 10) // CR+LF
++p;
break;
}
if (*p == 10) // LF
{
++p;
break;
}
Out += *p++;
}
}
// without Numbers
bool IsNameCharacter(uint8 Value) {
return (Value >= 'a' && Value <= 'z') || (Value >= 'A' && Value <= 'Z') || Value == '_';
}
bool IsDigitCharacter(uint8 Value) {
return Value >= '0' && Value <= '9';
}
bool isValidKey(const char* key)
{
if (!key)
return false;
for(const char* p = key; *p; ++p)
{
if (!IsNameCharacter(*p) && !IsDigitCharacter(*p) && *p != '_')
return false;
}
return true;
}
bool ParseName(const uint8*& p, std::string& Out) {
bool Ret = false;
Out.clear();
// can be optimized, does a lot of resize
if (IsNameCharacter(*p)) {
Out += *p++;
Ret = true;
}
while (IsNameCharacter(*p) || IsDigitCharacter(*p)) {
Out += *p++;
}
return Ret;
}
//bool CIniArchive::Load(const TCHAR* pFileName)
bool CIniArchive::Load(const char* pFileName)
{
assert(pFileName);
assert(!bIsLoading);
// includes 0 termination
CGeneralArchive LoadedArchive;
if (!LoadedArchive.OpenForLoading(pFileName, true)) {
return false;
}
const uint8* pFile = LoadedArchive.GetDataPtr();
std::string Key, Value;
std::string Line;
// lines separated by "\r\n"
std::string PendingCommentBlock;
while (*pFile) {
ParseLine(pFile, Line);
// parse line
{
const uint8* p = (const uint8*)Line.c_str();
ParseWhiteSpace(p);
if (!ParseStartsWith(p, ";")) {
if (ParseName(p, Key)) {
ParseWhiteSpace(p);
if (ParseStartsWith(p, "=")) {
ParseWhiteSpace(p);
ParseLine(p, Value);
SValueEx ValueEx;
ValueEx.Value = Value;
ValueEx.CommentBlock = PendingCommentBlock;
PendingCommentBlock.clear();
// ValueEx.bLoadedFromFile = true;
// duplicates override the former settings
KeyValuePairs[Key] = ValueEx;
continue;
} else {
// bad syntax in ini, ignoring line
assert(0);
}
}
} else {
if (!PendingCommentBlock.empty()) {
PendingCommentBlock += "\r\n";
}
PendingCommentBlock += Line;
}
}
}
bIsLoading = true;
return true;
}
bool StartsWithSeparatorLine(const char* CommentBlock) {
// e.g. "; -------------------------\r\n"
const uint8* p = (const uint8*)CommentBlock;
ParseWhiteSpace(p);
if (*p == ';') {
++p;
ParseWhiteSpace(p);
if (*p == '-') {
return true;
}
}
return false;
}
//bool CIniArchive::Save(const TCHAR* pFileName) {
bool CIniArchive::Save(const char* pFileName) {
assert(pFileName);
assert(!bIsLoading); // This means once instance for saving and one for loading? How can we maintain comments this way? Change?
FILE* outHandle = 0;
// if (_wfopen_s(&outHandle, pFileName, L"wb") != 0) {
if (fopen_s(&outHandle, pFileName, "wb") != 0) {
return false;
}
bool bFirst = true;
// append data that was missing
for (auto it = KeyValuePairs.begin(), end = KeyValuePairs.end(); it != end; ++it) {
if (!bFirst) {
bool bStartsWithSeparatorLine = StartsWithSeparatorLine(it->second.CommentBlock.c_str());
if (!bStartsWithSeparatorLine) {
// fprintf(outHandle, "; -------------------------\r\n");
}
}
bFirst = false;
if (!it->second.CommentBlock.empty()) {
fprintf(outHandle, "%s\r\n", it->second.CommentBlock.c_str());
}
fprintf(outHandle, "%s = %s\r\n", it->first.c_str(), it->second.Value.c_str());
}
fclose(outHandle);
return true;
}
CIniArchive::SValueEx* CIniArchive::GetByKey(std::string Key) {
auto it = KeyValuePairs.find(Key);
if (it != KeyValuePairs.end()) {
return &(it->second);
}
return nullptr;
}
void CIniArchive::AddKeyValue(
const char* Key,
const std::string& Value,
const char* InCommentBlock) {
SValueEx ValueEx;
assert(!Value.empty());
ValueEx.Value = Value;
// ValueEx.bLoadedFromFile = false;
if (InCommentBlock) {
ValueEx.CommentBlock = InCommentBlock;
}
auto dataPair = std::pair<std::string, SValueEx>(Key, ValueEx);
// duplicate key, use the last one but warn in debug
// assert(KeyValuePairs.find(dataPair) == KeyValuePairs.end());
KeyValuePairs.insert(dataPair);
}
void CIniArchive::AddKeyValue(const char* Key, float Value, const char* OptionalHelp) {
assert(isValidKey(Key));
char str[80];
sprintf_s(str, sizeof(str) / sizeof(str[0]), "%g", Value);
AddKeyValue(Key, str, OptionalHelp);
}
void CIniArchive::AddKeyValue(const char* Key, int Value, const char* OptionalHelp) {
assert(isValidKey(Key));
char str[80];
sprintf_s(str, sizeof(str) / sizeof(str[0]), "%i", Value);
AddKeyValue(Key, str, OptionalHelp);
}
void Reflection(CIniArchive& archive, const char* Key, float& inoutValue) {
assert(isValidKey(Key));
if (archive.IsSaving())
archive.AddKeyValue(Key, inoutValue);
else {
if (CIniArchive::SValueEx* p = archive.GetByKey(Key)) {
inoutValue = p->GetFloat(inoutValue);
}
}
}
void Reflection(CIniArchive& archive, const char* Key, int& inoutValue) {
assert(isValidKey(Key));
if (archive.IsSaving())
archive.AddKeyValue(Key, inoutValue);
else {
if (CIniArchive::SValueEx* p = archive.GetByKey(Key)) {
inoutValue = p->GetInt(inoutValue);
}
}
}
void Reflection(CIniArchive& archive, const char* Key, bool& inoutValue) {
assert(isValidKey(Key));
if (archive.IsSaving())
archive.AddKeyValue(Key, inoutValue);
else {
if (CIniArchive::SValueEx* p = archive.GetByKey(Key)) {
inoutValue = p->GetInt(inoutValue) != 0;
}
}
}