-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefinitions.cpp
More file actions
102 lines (93 loc) · 2.5 KB
/
Definitions.cpp
File metadata and controls
102 lines (93 loc) · 2.5 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
#include "Global.h"
#include "Hash.h"
#include "Definitions.h"
#include "StringHelper.h"
#include "Console.h"
#include <fstream>
#include <sstream>
#ifdef WIN32
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#include <unistd.h>
#define GetCurrentDir getcwd
#endif
// Normal definitions from funk code like "q = hl" or "q = 50"
unordered_map<string, string> Definitions = unordered_map<string, string>();
// Definitions from z80 include files like "gbuf = 1234h"
unordered_map<const char *, char *, char_ptr_hasher, char_ptr_equals> DefinitionsHashed = unordered_map<const char *, char *, char_ptr_hasher, char_ptr_equals>();
vector<char *>buffers;
int ParseBuffer(char *file_begin, char *file_end)
{
int found = 0;
char *ptr = file_begin,
*line_start, *line_end,
*key_start, *key_end,
*equ_start, *equ_end,
*value_start, *value_end;
while(ptr < file_end)
{
line_start = ptr;
line_end = SkipLine(line_start);
ptr = line_end + 1;
key_start = SkipWhiteSpace(line_start);
key_end = SkipWord(key_start);
if (key_end >= line_end) continue;
equ_start = SkipWhiteSpace(key_end);
equ_end = SkipEqu(equ_start);
if (equ_end >= line_end) continue;
if (equ_start == equ_end) continue;
if (!(*equ_start == '=' || stricmp(equ_start, "equ"))) continue;
value_start = SkipWhiteSpace(equ_end);
value_end = SkipLineTillComment(value_start);
if (value_end > line_end) continue;
// Terminate strings
*key_end = *value_end = 0;
DefinitionsHashed[key_start] = value_start;
found++;
}
Trace("Found %d definitions", found, DefinitionsHashed.size());
return found;
}
char *LoadDefinitionsFile(string filePath)
{
ifstream file(filePath);
if(!file.is_open()) {
Error("Cannot open file <%s>", filePath.c_str());
return NULL;
}
if (!file.good()) {
Error("Cannot read file <%s>", filePath.c_str());
return NULL;
}
// Read file into buffer
int length = file.seekg(0, file.end).tellg();
char *buffer = new char[length+1];
file.seekg(0, file.beg);
file.read(buffer, length);
if (!file.eof()) Error("File <%s> could not be read entirely", filePath.c_str());
length = file.gcount();
file.close();
// Terminate buffer
buffer[length] = 0;
// Parse contents
ParseBuffer(buffer, buffer + length -1);
buffers.push_back(buffer);
return buffer;
}
int ParseNumber(char *ptr)
{
int result = -1;
std::stringstream ss;
if (*ptr == '0' && *(ptr+1) == 'x')
{
ss << std::hex << ptr+2;
ss >> result;
}
else if(*(SkipWord(ptr)-1) == 'h')
{
ss << std::hex << ptr;
ss >> result;
}
return result;
}