Repository navigation
Expand file tree
/
Copy pathTable.cpp
More file actions
646 lines (534 loc) · 14.1 KB
/
Copy pathTable.cpp
File metadata and controls
646 lines (534 loc) · 14.1 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//-----------------------------------------------------------------------------
// Table - A table library by Klarth, http://rpgd.emulationworld.com/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 "Table.h"
using namespace std;
Table::Table()
{
TblEntries = 0;
memset(LongestText, 0, 256*4);
LongestText[(int)'<'] = 5; // Length of <$XX>
LongestText[(int)'('] = 5; // Length of ($XX)
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;
LineNumber = 1;
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 Table File until eof
TblFile.seekg(0, ios::end);
unsigned int TblFileSize = TblFile.tellg();
TblFile.seekg(0, ios::beg);
//BUG, switched to comparing pos to size
//while(!TblFile.eof())
while (TblFile.tellg() < TblFileSize)
{
HexVal.clear();
TextString.clear();
// Read the hex number, skip whitespace, skip equal sign
parsews(TblFile);
TblFile.get(testchar);
if(TblFile.eof())
break;
TblFile.seekg(-1, ios::cur);
switch(testchar)
{
case '(':
if(parsebookmark(TblFile))
break;
else
return TBL_PARSE_ERROR;
case '[':
parsescriptdump(TblFile);
break;
case '{':
parsescriptinsert(TblFile);
break;
case '/':
if(parseendstring(TblFile))
{
TblEntries++;
break;
}
else
return TBL_PARSE_ERROR;
case '*':
if(parseendline(TblFile))
{
TblEntries++;
break;
}
else
return TBL_PARSE_ERROR;
case '$': case '!': case '@': // Skip line, linked/dakuten/handakuten entries not supported
while(TblFile.get() != '\n' && !TblFile.eof());
break;
default:
if(parseentry(TblFile))
{
break;
TblEntries++;
}
else
return TBL_PARSE_ERROR;
}
} // End table reading loop
return TBL_OK;
}
//-----------------------------------------------------------------------------
// parsebookmark() - Parses a bookmark like (8000h)Text1
//-----------------------------------------------------------------------------
inline bool Table::parsebookmark(ifstream& file)
{
char testch;
string bookname;
string hexaddress;
unsigned int address;
file.get(testch); // should be '('
while(true)
{
file.get(testch);
if((file.eof()) || (testch == 'h') || (testch == 'H') || (testch == '\n'))
break;
hexaddress += testch;
}
// Convert a hex string to an unsigned long
address = strtoul(hexaddress.c_str(), NULL, 16);
parsews(file);
file.get(testch); // should be ')'
if(testch != ')')
return false;
parsews(file);
// Get the name
while(true)
{
file.get(testch);
if((testch == '\n') || file.eof())
break;
bookname += testch;
}
TBL_BOOKMARK bookmark;
bookmark.address = address;
bookmark.description = bookname;
Bookmarks.push_back(bookmark);
return true;
}
//-----------------------------------------------------------------------------
// parseendline() - parses a break line table value: ex, *FE
// You can also define messages like *FE=<End Text>
//-----------------------------------------------------------------------------
inline bool Table::parseendline(ifstream& file)
{
char testch;
string hexstr, textstr;
file.get(testch); // the *
parsews(file);
// Get the hex
while(true)
{
file.get(testch);
if((testch == '\n') || file.eof() || (testch == '='))
break;
hexstr += testch;
}
if(testch != '=') // normal entry
{
// Add to the map
LookupHex[DefEndString] = hexstr;
}
else
{
// Get what the string is
while(true)
{
file.get(testch);
if((testch == '\n') || file.eof())
break;
textstr += testch;
}
// Add custom message to the map
LookupHex[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(ifstream& file)
{
char testch;
string hexstr, textstr;
file.get(testch); // the /
parsews(file);
// Get the first part
while(true)
{
file.get(testch);
if((testch == '\n') || file.eof() || (testch == '='))
break;
hexstr += testch;
}
if(testch == '\n' || file.eof()) // Must be a blank value string (/<end>)
{
size_t Pos = hexstr.find_first_of("0123456789ABCDEF");
if(Pos == 0)
return false;
textstr = hexstr;
hexstr.clear();
}
else if(testch == '=') // Must be a /FF=<end> type string
{
while(true)
{
file.get(testch);
if((testch == '\n') || file.eof())
break;
textstr += testch;
}
}
else
return false;
// Add custom string to the map
LookupHex[textstr] = hexstr;
EndTokens.push_back(textstr);
return true;
}
//-----------------------------------------------------------------------------
// parseentry() - parses a hex = text line
//-----------------------------------------------------------------------------
inline bool Table::parseentry(ifstream& file)
{
char testch;
string Hex, Text;
// get the hex
while(true)
{
file.get(testch);
if((testch == SPACE) || (testch == '='))
break;
else
Hex += testch;
}
// get the equal sign
if(testch != '=')
{
parsews(file);
file.get(testch);
if(testch != '=')
return false; // bad formatting
}
// get the value
file.get(testch);
while(!file.eof() && testch != '\n')
{
Text += testch;
file.get(testch);
}
// Hex entries are strings, so divide the length by two to get the bytes
if(Hex.length() & 1) // Not a 8n bit hex number
Hex.insert(0, "0");
if((Hex.length() >> 1) > LongestHex)
LongestHex = ((unsigned int)Hex.length() / 2);
// Get the longest text string
if(Text.length() > LongestText[(unsigned char)Text[0]])
LongestText[(unsigned char)Text[0]] = (int)Text.length();
LookupHex.insert(std::map<string,string>::value_type(Text, Hex));
return true;
}
//-----------------------------------------------------------------------------
// parsescriptdump() - Parses a script dump entry, like [8000h-8450h]Block 1
//-----------------------------------------------------------------------------
inline bool Table::parsescriptdump(ifstream& file)
{
char testch;
unsigned int HexAddr1, HexAddr2;
string HexOff1, HexOff2, Description;
TBL_DUMPMARK dumpmark;
file.get(testch); // the '['
// The first hex entry
while(true)
{
file.get(testch);
if((file.eof()) || (testch == '-') || (testch == '\n'))
break;
HexOff1 += testch;
}
HexAddr1 = strtoul(HexOff1.c_str(), NULL, 16);
parsews(file);
// The second hex entry
while(true)
{
file.get(testch);
if((file.eof()) || (testch == ']') || (testch == '\n'))
break;
HexOff2 += testch;
}
HexAddr2 = strtoul(HexOff2.c_str(), NULL, 16);
parsews(file);
// The name of the scriptdump
while(true)
{
file.get(testch);
if((testch == '\n') || file.eof())
break;
Description += testch;
}
if(HexAddr1 <= HexAddr2)
{
dumpmark.StartAddress = HexAddr1;
dumpmark.EndAddress = HexAddr2;
}
else
{
dumpmark.StartAddress = HexAddr2;
dumpmark.EndAddress = HexAddr1;
}
dumpmark.description = Description;
Dumpmarks.push_back(dumpmark);
return 1;
}
//-----------------------------------------------------------------------------
// parsescriptinsert() - Parses script insert bookmarks
// ex - {8000h-TextDump.txt}Block-e 1
//-----------------------------------------------------------------------------
inline bool Table::parsescriptinsert(ifstream& file)
{
char testch;
int HexAddress;
string HexOff, FileName, Description;
TBL_INSMARK insertmark;
file.get(testch); // {
// Get the hex offset
while(true)
{
file.get(testch);
if((file.eof()) || (testch == '-') || (testch == '\n'))
break;
HexOff += testch;
}
HexAddress = strtoul(HexOff.c_str(), NULL, 16);
parsews(file);
// Get the filename
while(true)
{
file.get(testch);
if((file.eof()) || (testch == ')') || (testch == '\n'))
break;
HexOff += testch;
}
parsews(file);
// Get the description
while(true)
{
file.get(testch);
if((testch == '\n') || file.eof())
break;
Description += testch;
}
insertmark.address = HexAddress;
insertmark.filename = FileName;
insertmark.description = Description;
Insertmarks.push_back(insertmark);
return 1;
}
//-----------------------------------------------------------------------------
// 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));
}