-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reader.cpp
120 lines (113 loc) · 3.36 KB
/
Reader.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
#include "Reader.h"
//------------------------------------------------------------------------------
Reader::Reader(FilesToProcessProtected &files,
ProcessedFilesProtected &results) :
files(files), results(results) {}
Reader::~Reader(){}
void Reader::run() {
std::string fileName;
while (this->files.getFileNameIfEmpty(fileName)) {
int V = findLabels(fileName);
Graph G(V);
int result = parseText(G, fileName);
results.addResult(result, fileName);
}
}
int Reader::findLabels(std::string& fileName) {
std::fstream file;
int current_node = 0;
file.open(fileName);
if (file.is_open()){
while (!file.eof()){
current_node = fillDictionnary(file);
}
}
file.close();
return current_node;
}
int Reader::fillDictionnary(std::fstream& file) {
std::string line;
int current_node = 0;
while (std::getline(file, line)) {
std::string delimiter = ":";
std::size_t label_found = line.find(delimiter);
if (line == "") {
continue;
} else if (label_found != std::string::npos) {
std::string label = line.substr(0, label_found);
this->dic.insert(std::pair<std::string,int>(label, current_node));
}
current_node++;
}
return current_node;
}
int Reader::parseText(Graph& G, std::string& file_name) {
std::fstream file;
file.open(file_name);
if (file.is_open()){
while (!file.eof()){
findRetAndJumpLabels(G, file);
}
}
file.close();
this->dic.clear();
if(G.isCyclic(0) || (G.isCyclic(0) && G.unusedInstruction())) {
return CYCLIC;
} else if (G.unusedInstruction()) {
return INSTR;
}
return GOOD;
}
void Reader::findRetAndJumpLabels(Graph& G, std::fstream& file) {
std::string line;
int current_node = 0;
while (std::getline(file, line)) {
std::size_t ret_found = line.find("ret");
int array_element_found = 0;
std::array<std::string,10> array = {"jmp","ja","jeq","jneq","jne",
"jlt","jle","jgt","jge","jset"};
std::size_t jump_found = 0;
for (int i = 0; i<10 && array_element_found == 0; i++) {
jump_found = line.find(array[i]);
if (jump_found != std::string::npos) {
array_element_found = 1;
}
}
if (line == "") {
continue;
} else if (ret_found != std::string::npos) {
} else if (jump_found != std::string::npos) {
findJumpLabels(G, line, current_node);
} else {
G.addEdge(current_node, (current_node + 1));
}
current_node++;
}
}
void Reader::findJumpLabels(Graph& G, std::string line, int current_node) {
std::string::iterator begin = line.begin();
std::string::iterator end = line.end();
int n = std::count(begin, end, ',');
if (n == 1) {
std::map<std::string,int>::iterator it;
std::string jmp_arg = line.substr(line.find_last_of(" ")+1);
it = this->dic.find(jmp_arg);
G.addEdge(current_node, it->second);
G.addEdge(current_node, (current_node + 1));
} else if (n == 2) {
std::map<std::string,int>::iterator it1;
std::map<std::string,int>::iterator it2;
std::string jmp_label0 = line.substr(line.find(",")+2);
std::string jmp_label1 = jmp_label0.substr(0, jmp_label0.find(","));
std::string jmp_label2 = line.substr(line.find_last_of(",")+2);
it1 = this->dic.find(jmp_label1);
it2 = this->dic.find(jmp_label2);
G.addEdge(current_node, it1->second);
G.addEdge(current_node, it2->second);
} else {
std::map<std::string,int>::iterator it;
std::string jmp_label = line.substr(line.find_last_of(" ")+1);
it = this->dic.find(jmp_label);
G.addEdge(current_node, it->second);
}
}