-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTable.cpp
456 lines (370 loc) · 11.1 KB
/
Table.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
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
454
455
456
//-----------------------------------------------------------------------------
// Table - A table library by Klarth
// email - stevemonaco@hotmail.com
// Open source and free to use
//-----------------------------------------------------------------------------
#include "stdafx.h"
#include <string>
#include <vector>
#include <fstream>
#include <cstdlib>
#include <map>
#include <utility>
#include <list>
#include "Table.h"
using namespace std;
const char* HexAlphaNum = "ABCDEFabcdef0123456789";
Table::Table()
{
TblEntries = 0;
memset(LongestText, 0, 256 * 4);
LongestText[(int)'<'] = 5; // Length of <$XX>
LongestText[(int)'('] = 5; // Length of ($XX)
DefEndLine = "<LINE>";
DefEndString = "<END>";
LongestHex = 1;
StringCount = 0;
bAddEndToken = true;
}
Table::~Table()
{
// Clear Errors
if (!Errors.empty())
Errors.clear();
// Clear the map
if (!LookupHex.empty())
LookupHex.clear();
}
//-----------------------------------------------------------------------------
// EncodeStream() - Encodes text in a vector to the string tables
//-----------------------------------------------------------------------------
unsigned int Table::EncodeStream(string& scriptbuf, unsigned int& BadCharOffset)
{
TBL_STRING TblString;
TXT_STRING TxtString;
string hexstr;
string subtextstr;
unsigned char i;
unsigned int EncodedSize = 0;
bool bIsEndToken = false;
std::map<string, string>::iterator mapit;
unsigned int BufOffset = 0;
hexstr.reserve(LongestHex * 2);
if (scriptbuf.empty())
return 0;
if (!StringTable.empty())
{
TBL_STRING RestoreStr = StringTable.back();
if (RestoreStr.EndToken.empty()) // No end string...restore and keep adding
{
StringTable.pop_back();
TblString.Text = RestoreStr.Text;
TxtString = TxtStringTable.back();
TxtStringTable.pop_back();
}
}
while (BufOffset < scriptbuf.size()) // Translate the whole buffer
{
bIsEndToken = false;
i = LongestText[(unsigned char)scriptbuf[BufOffset]]; // Use LUT
while (i)
{
subtextstr = scriptbuf.substr(BufOffset, i);
mapit = LookupHex.find(subtextstr);
if (mapit == LookupHex.end()) // if the entry isn't found
{
i--;
continue;
}
hexstr = mapit->second;
TxtString.Text += subtextstr;
// Search to see if it's an end token, if it is, add to the string table
for (unsigned int j = 0; j < EndTokens.size(); j++)
if (EndTokens[j] == subtextstr)
{
bIsEndToken = true;
if (bAddEndToken)
AddToTable(hexstr, &TblString);
TxtString.EndToken = subtextstr;
TblString.EndToken = subtextstr;
EncodedSize += (unsigned int)TblString.Text.size();
TxtStringTable.push_back(TxtString);
StringTable.push_back(TblString);
TxtString.EndToken = "";
TxtString.Text.clear();
TblString.EndToken = "";
TblString.Text.clear();
break; // Only once
}
if (!bIsEndToken)
AddToTable(hexstr, &TblString);
BufOffset += i;
break; // Entry is finished
}
if (i == 0) // no entries found
{
BadCharOffset = BufOffset;
return -1;
}
}
// Encode any extra data that doesn't have an EndToken
if (!TblString.Text.empty())
StringTable.push_back(TblString);
if (!TxtString.Text.empty())
TxtStringTable.push_back(TxtString);
EncodedSize += (unsigned int)TblString.Text.size();
scriptbuf.clear();
return EncodedSize;
}
inline void Table::InitHexTable()
{
char textbuf[16];
char hexbuf[16];
for (unsigned int i = 0; i < 0x100; i++)
{
sprintf(textbuf, "<$%02X>", i);
sprintf(hexbuf, "%02X", i);
LookupHex.insert(map<string, string>::value_type(string(textbuf), string(hexbuf)));
// WindHex style hex codes
sprintf(textbuf, "($%02X)", i);
LookupHex.insert(map<string, string>::value_type(string(textbuf), string(hexbuf)));
}
for (unsigned int i = 0x0A; i < 0x100; i += 0x10)
{
for (unsigned int j = 0; j < 6; j++)
{
sprintf(textbuf, "<$%02x>", i + j);
sprintf(hexbuf, "%02X", i + j);
LookupHex.insert(map<string, string>::value_type(string(textbuf), string(hexbuf)));
// WindHex style hex codes (shouldn't be necessary for lowercase, though)
sprintf(textbuf, "($%02x)", i);
LookupHex.insert(map<string, string>::value_type(string(textbuf), string(hexbuf)));
}
}
}
//-----------------------------------------------------------------------------
// GetHexValue() - Returns a Hex value from a Text string from the table
//-----------------------------------------------------------------------------
inline string& Table::GetHexValue(string& Textstring)
{
return (LookupHex.find(Textstring))->second;
}
//-----------------------------------------------------------------------------
// OpenTable() - Opens, Parses, and Loads a file to memory
//-----------------------------------------------------------------------------
int Table::OpenTable(const char* TableFilename)
{
string HexVal;
char testchar;
string TextString;
list<string> EntryList;
string Entry;
LineNumber = 0;
LookupHex.clear();
InitHexTable();
ifstream TblFile(TableFilename);
if (!TblFile.is_open()) // File can't be opened
return TBL_OPEN_ERROR;
unsigned char utfheader[4];
// Detect UTF-8 header
if (TblFile.peek() == 0xEF)
{
TblFile.read((char*)utfheader, 3);
if (utfheader[0] != 0xEF || utfheader[1] != 0xBB || utfheader[2] != 0xBF)
TblFile.seekg(ios::beg); // Seek beginning, not a UTF-8 header
}
// Read the file
while (!TblFile.eof())
{
getline(TblFile, Entry);
EntryList.push_back(Entry);
}
TblFile.close();
// Parse each line
for (list<string>::iterator i = EntryList.begin(); i != EntryList.end(); i++)
{
LineNumber++;
if (i->length() == 0) // Blank line
continue;
testchar = (*i)[0];
switch (testchar)
{
// Various bookmark and handakuten/dakuten/linked entries are skipped
case '(': case '[': case '{': case '$': case '!': case '@':
break;
case '*': // End line
if (!parseendline(*i))
return TBL_PARSE_ERROR;
break;
case '/': // End string
if (!parseendstring(*i))
return TBL_PARSE_ERROR;
break;
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'A': case 'B': case 'C': case 'D':
case 'E': case 'F': // Normal entry value
if (!parseentry(*i))
return TBL_PARSE_ERROR;
break;
default:
return TBL_PARSE_ERROR;
}
}
return TBL_OK;
}
//-----------------------------------------------------------------------------
// parseendline() - parses a break line table value: ex, *FE
// You can also define messages like *FE=<End Text>
//-----------------------------------------------------------------------------
inline bool Table::parseendline(string line)
{
line.erase(0, 1);
size_t pos = line.find_first_not_of(HexAlphaNum, 0);
string hexstr;
string textstr;
if (pos == string::npos) // Non-alphanum characters not found, *FE type entry?
{
textstr = DefEndLine;
hexstr = line;
LookupHex.insert(std::map<string, string>::value_type(textstr, hexstr));
return true;
}
else
hexstr = line.substr(0, pos);
if ((hexstr.length() % 2) != 0) // Hex token length is not a multiple of 2
{
return false;
}
pos = line.find_first_of("=", 0);
if (pos == string::npos) // No equal sign means it's a default entry
textstr = DefEndLine;
else
{
line.erase(0, pos + 1);
textstr = line;
}
if (textstr.length() > LongestText[(unsigned char)textstr[0]])
LongestText[(unsigned char)textstr[0]] = (int)textstr.length();
LookupHex.insert(std::map<string, string>::value_type(textstr, hexstr));
return true;
}
//-----------------------------------------------------------------------------
// parseendstring() - parses a string break table value
// Only ones like /FF=<end>
// and /<end> (gives a blank string value)
//-----------------------------------------------------------------------------
inline bool Table::parseendstring(string line)
{
line.erase(0, 1);
size_t pos = line.find_first_of(HexAlphaNum, 0);
string hexstr = "";
string textstr = "";
if (pos != 0) // /<end> type entry
{
EndTokens.push_back(line);
LookupHex.insert(std::map<string, string>::value_type(line, hexstr));
return true;
}
pos = line.find_first_not_of(HexAlphaNum, 0);
hexstr = line.substr(0, pos);
if ((hexstr.length() % 2) != 0) // Hex token length is not a multiple of 2
{
return false;
}
pos = line.find_first_of("=", 0);
if (pos == string::npos) // No "=" found, a /FF type entry
textstr = DefEndString;
else
{
line.erase(0, pos + 1);
textstr = line;
if (textstr.length() == 0) // Blank RHS
{
return false;
}
}
if (textstr.length() > LongestText[(unsigned char)textstr[0]])
LongestText[(unsigned char)textstr[0]] = (int)textstr.length();
EndTokens.push_back(textstr);
LookupHex.insert(std::map<string, string>::value_type(textstr, hexstr));
return true;
}
//-----------------------------------------------------------------------------
// parseentry() - parses a hex = text line
//-----------------------------------------------------------------------------
inline bool Table::parseentry(string line)
{
// Get location after hex string
size_t pos = line.find_first_not_of(HexAlphaNum, 0);
if (pos == string::npos) // String is all hex characters
{
return false;
}
string hexstr = line.substr(0, pos);
if ((hexstr.length() % 2) != 0) // Hex string not a multiple of 2 length
{
return false;
}
string textstr;
pos = line.find_first_of("=", 0);
if (pos == line.length() - 1) // End of the line, blank entry means it's an error
{
return false;
}
else
{
line.erase(0, pos + 1);
textstr = line;
}
if (textstr.length() > LongestText[(unsigned char)textstr[0]])
LongestText[(unsigned char)textstr[0]] = (int)textstr.length();
LookupHex.insert(std::map<string, string>::value_type(textstr, hexstr));
return true;
}
//-----------------------------------------------------------------------------
// parsews() - Eats all blanks and eoln's until a valid character or eof
//-----------------------------------------------------------------------------
inline void Table::parsews(ifstream& file)
{
char testch;
do {
file.get(testch);
if (testch == '\n')
LineNumber++;
} while (((testch == SPACE) || (testch == '\n')) && (!file.eof()));
if ((!file.eof()) || (testch != '\n'))
file.seekg(-1, ios::cur);
}
//-----------------------------------------------------------------------------
// HexToDec(...) - Converts a hex character to its dec equiv
//-----------------------------------------------------------------------------
inline unsigned short HexToDec(char HexChar)
{
switch (HexChar)
{
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'a': case 'A': return 10;
case 'b': case 'B': return 11;
case 'c': case 'C': return 12;
case 'd': case 'D': return 13;
case 'e': case 'E': return 14;
case 'f': case 'F': return 15;
}
// Never gets here
printf("badstr");
return 15;
}
inline void Table::AddToTable(string& Hexstring, TBL_STRING* TblStr)
{
for (unsigned int k = 0; k < Hexstring.length(); k += 2)
TblStr->Text += (HexToDec(Hexstring[k + 1]) | (HexToDec(Hexstring[k]) << 4));
}