-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseFile.cpp
More file actions
109 lines (90 loc) · 2.84 KB
/
ParseFile.cpp
File metadata and controls
109 lines (90 loc) · 2.84 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
#include "ParseFile.h"
ParseFile::ParseFile(){
}
Build ParseFile::getBuild(string fileName){
// reset variables for new build
labelLocations.clear();
displayLines.clear();
lineInformation.clear();
// read labels first
readLabels(fileName);
// analyse entire file after
readFile(fileName);
// return build
return Build(lineInformation);
}
// trim code to correct format
string trim(
const std::string& str,
const std::string& whitespace = " \t")
{
const auto strBegin = str.find_first_not_of(whitespace);
if (strBegin == std::string::npos)
return ""; // no content
const auto strEnd = str.find_last_not_of(whitespace);
const auto strRange = strEnd - strBegin + 1;
return str.substr(strBegin, strRange);
}
string reduce(
const std::string& str,
const std::string& fill = " ",
const std::string& whitespace = " \t")
{
// trim first
auto result = trim(str, whitespace);
// replace sub ranges
auto beginSpace = result.find_first_of(whitespace);
while (beginSpace != std::string::npos)
{
const auto endSpace = result.find_first_not_of(whitespace, beginSpace);
const auto range = endSpace - beginSpace;
result.replace(beginSpace, range, fill);
const auto newStart = beginSpace + fill.length();
beginSpace = result.find_first_of(whitespace, newStart);
}
return result;
}
void ParseFile::readLabels(string fileName){
ifstream infile(fileName);
string line;
int pc = 0;
while(getline(infile, line)){
// clean lines
line = reduce(line); // from whitespace
line = line.substr(0, line.find('#')); // from comments
if(line.size() == 0) continue;
if(line[line.length()-1] == ':'){
labelLocations[line.substr(0,line.size()-1)] = pc;
}else{
pc++;
}
}
}
void ParseFile::readFile(string fileName){
ifstream infile(fileName);
int i = 0;
string line;
while(getline(infile, line)){
// clean line
line = reduce(line); // from whitespace
line = line.substr(0, line.find('#')); // from comments
if(line.size() == 0) continue;
line = reduce(line, " ", ","); // from commas
line = reduce(line); // after comma reduction
// check as label or executable line
string res = "";
if(line[line.size()-1] == ':'){
res = "label : " + line + " (" + to_string(labelLocations[line.substr(0,line.size()-1)]) + ")";
}else{
res = "exec : " + line + " (" + to_string(i) + ")";
i++;
lineInformation.push_back(ExecLine(line,labelLocations));
}
displayLines.push_back(res);
}
}
void ParseFile::printLines(){
for(int i = 0; i < displayLines.size(); i++){
cout << displayLines[i] << endl;
}
};