-
Notifications
You must be signed in to change notification settings - Fork 29
/
ModParser.cpp
389 lines (360 loc) · 8.94 KB
/
ModParser.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
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
#include "ModParser.h"
#include <iostream>
#include <sstream>
#include <cstring>
#include <cctype>
std::string Trim(std::string str)
{
std::string ret = str, wspc(" \t\f\v\n\r");
size_t pos = ret.find_first_not_of(wspc);
if (pos != std::string::npos)
{
ret = ret.substr(pos);
}
else
{
return "";
}
pos = ret.find_last_not_of(wspc);
ret = ret.substr(0, pos + 1);
return ret;
}
size_t SplitAt(char ch, std::string in, std::string& out1, std::string& out2)
{
size_t pos = in.find(ch);
if (pos != std::string::npos)
{
out1 = Trim(in.substr(0, pos));
out2 = Trim(in.substr(pos + 1));
}
else
{
out1 = Trim(in);
out2 = "";
}
return pos;
}
const char chLookup[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
std::string MakeTextBlock(char *data, size_t dataSize)
{
std::string out = "";
for (unsigned i = 0; i < dataSize; ++i)
{
if ((i%16 == 0) && (i != 0))
out += '\n';
uint8_t ch = data[i];
uint8_t up = ((ch & 0xF0) >> 4);
uint8_t lw = (ch & 0x0F);
out += chLookup[up];
out += chLookup[lw];
out += ' ';
}
out += '\n';
return out;
}
std::string GetFilename(std::string str)
{
unsigned found = str.find_last_of("/\\");
return str.substr(found + 1);
}
std::string GetStringValue(const std::string& TextBuffer)
{
std::string str = TextBuffer;
if (str.length() < 1)
return "";
str = str.substr(0, str.find_first_of("\n")); /// get first line
str = Trim(str); /// remove leading and trailing white-spaces
if (str.find('\"') != std::string::npos) /// remove ""
{
str = str.substr(str.find_first_not_of("\""));
str = str.substr(0, str.find_last_not_of("\"") + 1);
}
return str;
}
std::vector<char> GetDataChunk(const std::string& TextBuffer)
{
std::vector<char> data;
if (TextBuffer.length() < 1)
return data;
std::istringstream ss(TextBuffer);
while (ss.good())
{
int byte;
ss >> std::hex >> byte;
if (!ss.fail() && !ss.bad())
data.push_back(byte);
}
return data;
}
int GetIntValue(const std::string& TextBuffer)
{
int val = 0;
std::string str = ::GetStringValue(TextBuffer);
if (str.length() < 1)
return 0;
//val = std::stoi(str, nullptr, 0);
std::istringstream ss(str);
/*if (str.find("0x") != std::string::npos)
ss >> std::hex >> val;
else
ss >> std::dec >> val;*/
ss >> val;
return val;
}
unsigned GetUnsignedValue(const std::string& TextBuffer)
{
unsigned val = 0;
std::string str = ::GetStringValue(TextBuffer);
if (str.length() < 1)
return 0;
std::istringstream ss(str);
if (str.find("0x") != std::string::npos)
ss >> std::hex >> val;
else
ss >> std::dec >> val;
return val;
}
float GetFloatValue(const std::string& TextBuffer)
{
float val = 0;
std::string str = ::GetStringValue(TextBuffer);
if (str.length() < 1)
return 0;
std::istringstream ss(str);
ss >> val;
return val;
}
std::string ModParser::GetText()
{
std::string str = "";
std::string line = "";
size_t savedPos = modFile.tellg();
line = GetLine(); /// read key/section line
if (isKey)
{
str += line.substr(line.find("=") + 1);
while ( modFile.good() )
{
savedPos = modFile.tellg();
line = GetLine();
if (FindKey(line) != -1 || FindSection(line) != -1)
{
modFile.clear(); /// to parse last line correctly
break;
}
str += std::string("\n") + line;
}
}
else if (isSection)
{
while ( modFile.good() )
{
savedPos = modFile.tellg();
line = GetLine();
if (FindKey(line) != -1 || FindSection(line) != -1)
{
modFile.clear(); /// to parse last line correctly
break;
}
if (str != "") str += "\n";
str += line;
}
}
modFile.seekg(savedPos, std::ios::beg);
return str;
}
std::string ModParser::GetTextValue()
{
return Value;
}
std::vector<char> ModParser::GetDataChunk()
{
return ::GetDataChunk(Value);
}
std::string ModParser::GetStringValue()
{
return ::GetStringValue(Value);
}
int ModParser::GetIntValue()
{
return ::GetIntValue(Value);
}
float ModParser::GetFloatValue()
{
return ::GetFloatValue(Value);
}
std::string ModParser::GetLine()
{
std::string line = "";
int ch = 0;
while(ch != 0x0D && ch != 0x0A && modFile.good())
{
ch = modFile.get();
if (modFile.fail() || modFile.bad())
break;
if (CStyleComments && ch == '/' && modFile.peek() == '/')
{
while (ch != 0x0D && ch != 0x0A && modFile.good())
ch = modFile.get();
}
else if (CStyleComments && ch == '/' && modFile.peek() == '*')
{
while (modFile.good())
{
ch = modFile.get();
if (ch == '*' && modFile.peek() == '/')
{
ch = modFile.get();
break;
}
}
}
else if (ch == commentLine)
{
while (ch != 0x0D && ch != 0x0A && modFile.good())
ch = modFile.get();
}
else if (ch == commentBegin)
{
while (ch != commentEnd && modFile.good())
ch = modFile.get();
}
else if (ch != 0x0D && ch != 0x0A && modFile.good())
{
line += ch;
}
}
if (modFile.peek() == 0x0A || modFile.peek() == 0x0D)
modFile.get();
return line;
}
int ModParser::FindNext()
{
if (!modFile.good())
return -1;
Name = "";
Value = "";
Index = -1;
int idx = -1, keyIdx = -1, sectionIdx = -1;
std::string line = "";
size_t savedPos = modFile.tellg();
while ( keyIdx == -1 && sectionIdx == -1 && modFile.good() )
{
savedPos = modFile.tellg();
line = GetLine();
keyIdx = FindKey(line);
sectionIdx = FindSection(line);
}
modFile.clear(); /// to get last line value correctly
modFile.seekg(savedPos, std::ios::beg);
isKey = (keyIdx != -1);
isSection = (sectionIdx != -1);
if (keyIdx != -1)
{
idx = keyIdx;
Name = keyNames[idx];
}
if (sectionIdx != -1)
{
idx = sectionIdx;
Name = sectionNames[idx];
}
if (idx != -1)
{
Value = GetText();
}
Index = idx;
return idx;
}
int ModParser::FindKey(std::string str)
{
size_t pos = str.find("=");
if (pos == std::string::npos)
return -1;
std::string name = Trim(str.substr(0, pos));
/*std::string name = str.substr(0, pos);
name = name.substr(name.find_first_not_of(" "));
name = name.substr(0, name.find_last_not_of(" ") + 1);*/
int idx = FindKeyNameIdx(name);
return idx;
}
int ModParser::FindSection(std::string str)
{
if (str.find("[") == std::string::npos || str.find("]") == std::string::npos)
return -1;
std::string name = Trim(str);
/*std::string name = str;
name = name.substr(name.find_first_not_of(" "));
name = name.substr(0, name.find_last_not_of(" ") + 1);*/
int idx = FindSectionNameIdx(name);
return idx;
}
int ModParser::FindKeyNameIdx(std::string name)
{
int idx = -1;
for (unsigned int i = 0; i < keyNames.size(); ++i)
if (keyNames[i] == name)
idx = i;
return idx;
}
int ModParser::FindSectionNameIdx(std::string name)
{
int idx = -1;
for (unsigned int i = 0; i < sectionNames.size(); ++i)
if (sectionNames[i] == name)
idx = i;
return idx;
}
bool ModParser::OpenModFile(const char* name)
{
if (modFile.is_open())
{
modFile.close();
modFile.clear();
}
modFile.open(name, std::ios::binary);
if (!modFile.good())
return false;
/// check if file is text
while (modFile.good())
{
char ch = modFile.get();
if (modFile.eof())
break;
if (ch < 1 || ch > 127)
{
modFile.close();
return false;
}
}
modFile.clear();
modFile.seekg(0);
isKey = false;
isSection = false;
Name = "";
Value = "";
Index = -1;
return true;
}
void ModParser::AddKeyName(std::string name)
{
keyNames.push_back(name);
}
void ModParser::AddSectionName(std::string name)
{
sectionNames.push_back(name);
}
void ModParser::SetKeyNames(std::vector<std::string> names)
{
keyNames = names;
}
void ModParser::SetSectionNames(std::vector<std::string> names)
{
sectionNames = names;
}
void ModParser::SetCommentMarkers(char begMarker, char endMarker, char lineMarker)
{
commentBegin = begMarker;
commentEnd = endMarker;
commentLine = lineMarker;
}