-
Notifications
You must be signed in to change notification settings - Fork 0
/
StructParser.h
453 lines (343 loc) · 9.63 KB
/
StructParser.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
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
445
446
447
448
449
450
451
452
453
#pragma once
/* ------------------------------------------------------
Struct Parser
Parses an INI file into a struct.
Usage:
- Create a new object of the type of struct you want to parse data into. This is not the object the data goes into, it's just used to calculate offsets.
- Create a new instance of the StructParser class using the template for the desired struct.
- Set the base object to the instance you created earlier, either via the constructor or SetBase() member.
- Call Link for every value you want to parse into the target object later on.
To actually parse the data:
- Call Parse/ParseFile on the object you want to parse the data into. The source can be a file or data in memory.
IMPORTANT:
The object used as base is not the target of the data being parsed. It is only used to calculate offsets for the values.
Link() must be called with the members of the instance that you previously used as base.
The base object can go out of scope or free'd after setting up all links.
You can use SetBase(nullptr) to make sure no links are being created for an invalid struct once you are done setting it up (although there are sanity checks, the addresses can collide by coincidence).
*/// ----------------------------------------------------
#include <ctype.h>
#include <vector>
// ------------------------------------------------------
namespace LinkType
{
enum
{
SIGNED = 0, // Signed value of any size
UNSIGNED, // Unsigned value of any size
BOOL = UNSIGNED, // Included for clarity
FLOAT, // Floating point value, ie float or double
STRING, // Zero terminated char array
MAX
};
}
// ------------------------------------------------------
template<typename T>
class StructParser
{
private:
struct StructLink
{
int iType = -1;
size_t iElementSize = 0;
size_t iIndexes = 0;
size_t iOffset = 0;
char *pSection = nullptr;
char *pKey = nullptr;
};
T* m_pBase;
std::vector<StructLink*>
m_vLinks;
bool _CmpStr(const char* szText1, const char* szText2, bool bIgnoreCase)
{
size_t
iLen1 = strlen(szText1),
iLen2 = strlen(szText2);
if (iLen1 != iLen2)
return false;
if (bIgnoreCase)
{
for (size_t i = 0; i < iLen1; ++i)
if (tolower((int)szText1[i]) != tolower((int)szText2[i]))
return false;
}
else
{
for (size_t i = 0; i < iLen1; ++i)
if (szText1[i] != szText2[i])
return false;
}
return true;
}
size_t _RemovePadding(char* szText)
{
size_t
iLen = strlen(szText),
iLenNew,
iFirstChar = iLen,
iLastChar = iLen;
for (size_t i = 0; i < iLen; ++i)
{
if (szText[i] != ' ')
{
if (iFirstChar == iLen)
iFirstChar = i;
iLastChar = i;
}
}
if (iFirstChar == iLen)
{
szText[0] = 0;
return 0;
}
else if (iFirstChar == iLastChar)
{
szText[0] = szText[iFirstChar];
szText[1] = 0;
return 1;
}
iLenNew = iLastChar - iFirstChar + 1;
memmove(szText, szText + iFirstChar, iLenNew);
szText[iLenNew] = 0;
return iLenNew;
}
public:
StructParser(T* pBase = nullptr) :
m_pBase(pBase)
{
}
~StructParser()
{
ResetLinks();
}
void SetBase(T* pBase = nullptr)
{
m_pBase = pBase;
}
// iType: See LinkType::
// iElementSize: Size of one element in bytes ie. sizeof(type). Supported sizes: 1, 2, 4, 8
// iIndexes: The amount of indexes. If the array size is unknown you can use 0, however this is only safe for properly
// terminated char strings that are guaranteed to fit.
// pTarget: Target variable within the base struct.
// szSection: Name of the INI section.
// szKey: Name of the INI key.
bool Link(int iType, void* pTarget, size_t iElementSize, size_t iIndexes, const char* szSection, const char* szKey)
{
StructLink
*pLink;
size_t
iLen,
iOffset;
if (iType < 0 || iType >= LinkType::MAX || !m_pBase || (size_t)(void*)pTarget < (size_t)(void*)m_pBase)
return false;
iOffset = (size_t)(void*)pTarget - (size_t)(void*)m_pBase;
if (iOffset + iElementSize * (iIndexes == 0 ? 1 : iIndexes) >= sizeof(T))
return false;
pLink = new StructLink;
pLink->iType = iType;
pLink->iElementSize = iElementSize;
pLink->iIndexes = iIndexes;
pLink->iOffset = iOffset;
iLen = strlen(szSection) + 1;
pLink->pSection = new char[iLen];
memcpy(pLink->pSection, szSection, iLen);
iLen = strlen(szKey) + 1;
pLink->pKey = new char[iLen];
memcpy(pLink->pKey, szKey, iLen);
m_vLinks.push_back(pLink);
return true;
}
void ResetLinks()
{
for (auto &p : m_vLinks)
{
delete[] p->pSection;
delete[] p->pKey;
delete p;
}
m_vLinks.clear();
}
int ParseFile(const char* szFileName, T* pTarget, bool bIgnoreCase = true)
{
FILE
*pFile;
char
*pData;
size_t
iLen;
int
iParsedValues;
if (fopen_s(&pFile, szFileName, "r"))
return -1;
#if _WIN64
_fseeki64(pFile, 0, SEEK_END);
iLen = (size_t)_ftelli64(pFile);
_fseeki64(pFile, 0, SEEK_SET);
#else
fseek(pFile, 0, SEEK_END);
iLen = (size_t)ftell(pFile);
fseek(pFile, 0, SEEK_SET);
#endif
pData = new char[iLen + 1U];
pData[iLen] = 0;
fread(pData, sizeof(char), iLen, pFile);
fclose(pFile);
iParsedValues = Parse(pData, pTarget, bIgnoreCase, iLen);
delete[] pData;
return iParsedValues;
}
int Parse(char* pSource, T* pTarget, bool bIgnoreCase = true, size_t iSourceLen = 0)
{
char
*pLine = nullptr,
*pSection = nullptr;
int
iParsedValues = 0;
size_t
iLen = (iSourceLen == 0 ? strlen(pSource) : iSourceLen),
iLineStart = 0,
iLineLen = 0,
iTmpLen;
if (!iLen)
return -1;
for (size_t i = 0; i <= iLen; ++i)
{
if (i == iLen || pSource[i] == '\r' || pSource[i] == '\n' || pSource[i] == 0)
{
if (iLineLen < 2)
{
iLineStart = i + 1;
iLineLen = 0;
continue;
}
// Free previous line
if (pLine)
{
delete[] pLine;
pLine = nullptr;
}
// Allocate a new line and fill it
pLine = new char[iLineLen + 1];
memcpy(pLine, pSource + iLineStart, iLineLen);
pLine[iLineLen] = 0;
// Reset line counter
iLineStart = i + 1;
iLineLen = 0;
// Remove padding and check remaining length
iTmpLen = _RemovePadding(pLine);
if (iTmpLen < 2)
continue;
if (pLine[0] == '[' && pLine[iTmpLen - 1] == ']')
{
// If this is a section copy the pointer to the section pointer and delete the old one (if any)
pLine[0] = ' ';
pLine[iTmpLen - 1] = ' ';
if (pSection)
delete[] pSection;
// After removing [ and ] make sure there is text left, otherwise reset the section
if (_RemovePadding(pLine))
{
pSection = pLine;
pLine = nullptr;
}
else
{
pSection = nullptr;
}
}
else
{
// If this is a value, parse it
if (ParseValue(pSection, pLine, pTarget, bIgnoreCase))
++iParsedValues;
}
}
else
{
++iLineLen;
}
}
// Clean up
if (pSection)
delete[] pSection;
if(pLine)
delete[] pLine;
return iParsedValues;
}
bool ParseValue(char* pSection, char* pLine, T* pTarget, bool bIgnoreCase)
{
char
*pKey,
*pValue,
*pContext;
size_t
iSize;
size_t
iPointer;
pKey = strtok_s(pLine, "=", &pContext);
if (!pKey || !_RemovePadding(pKey))
return false;
pValue = strtok_s(nullptr, "=", &pContext);
if (!pValue || !_RemovePadding(pValue))
return false;
if (_CmpStr(pValue, "true", true))
{
pValue[0] = 0;
strcat_s(pValue, 2, "1");
}
else if (_CmpStr(pValue, "false", true))
{
pValue[0] = 0;
strcat_s(pValue, 2, "0");
}
for (auto &pLink : m_vLinks)
{
// Check if the current value and link have either no section, or the same section
// and if the keys match.
if (((!pSection && !pLink->pSection) || (pSection && _CmpStr(pSection, pLink->pSection, bIgnoreCase))) &&
_CmpStr(pKey, pLink->pKey, bIgnoreCase)
)
{
iPointer = (size_t)pTarget + pLink->iOffset;
switch (pLink->iType)
{
case LinkType::SIGNED:
if (pLink->iElementSize == 1)
sscanf_s(pValue, "%hhi", (__int8*)iPointer);
else if (pLink->iElementSize == 2)
sscanf_s(pValue, "%hi", (__int16*)iPointer);
else if (pLink->iElementSize == 4)
sscanf_s(pValue, "%i", (__int32*)iPointer);
else if (pLink->iElementSize == 8)
sscanf_s(pValue, "%lli", (__int64*)iPointer);
break;
case LinkType::UNSIGNED:
if (pLink->iElementSize == 1)
sscanf_s(pValue, "%hhu", (unsigned __int8*)iPointer);
else if (pLink->iElementSize == 2)
sscanf_s(pValue, "%hu", (unsigned __int16*)iPointer);
else if (pLink->iElementSize == 4)
sscanf_s(pValue, "%u", (unsigned __int32*)iPointer);
else if (pLink->iElementSize == 8)
sscanf_s(pValue, "%llu", (unsigned __int64*)iPointer);
break;
case LinkType::FLOAT:
if (pLink->iElementSize == 4)
sscanf_s(pValue, "%f", (float*)iPointer);
else if (pLink->iElementSize == 8)
sscanf_s(pValue, "%lf", (double*)iPointer);
break;
case LinkType::STRING:
if (pLink->iIndexes == 0)
iSize = strlen(pValue);
else
iSize = pLink->iIndexes;
if (pLink->iElementSize == 1)
sscanf_s(pValue, "%[^\t\n]", (char*)iPointer, (unsigned int)iSize);
break;
}
}
}
return true;
}
};
// ------------------------------------------------------