-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphMaker.cpp
More file actions
184 lines (165 loc) · 4.96 KB
/
graphMaker.cpp
File metadata and controls
184 lines (165 loc) · 4.96 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
#include "graphMaker.h"
#include "utils.h"
bool getInclude(const std::string& line, Include& include)
{
std::string clean_line = utils::removeWhiteSpaces(line);
bool had_include = false;
// Check if #include is on the start
if (clean_line.size() > 0 && clean_line[0] == '#')
{
bool has_include = false;
size_t filename_start = -1;
bool quotation = false;
std::string word = "#include";
size_t word_pos = 0;
for (size_t i = 0; i < clean_line.size(); i++)
{
// Check for #include at the start
if (!has_include)
{
if (clean_line[i] == word[word_pos])
{
word_pos++;
}
else if (clean_line[i] == ' ' || clean_line[i] == '\t')
{
continue;
}
if (word_pos == word.size())
{
has_include = true;
}
continue;
}
if (clean_line[i] == ' ' || clean_line[i] == '\t')
{
continue;
}
// Get the file name
if (clean_line[i] == '<')
{
include.quotation_marks = false;
filename_start = i;
}
else if (clean_line[i] == '"' && !quotation)
{
quotation = true;
include.quotation_marks = true;
filename_start = i;
}
else if (clean_line[i] == '>' && include.quotation_marks == false)
{
include.include_file = clean_line.substr(filename_start + 1, i - filename_start - 1);
had_include = true;
break;
}
else if (clean_line[i] == '"' && include.quotation_marks == true)
{
include.include_file = clean_line.substr(filename_start + 1, i - filename_start - 1);
had_include = true;
break;
}
}
}
return had_include;
}
inline int randomInt(int low, int high) { return low + rand() % (low - high); }
std::string getRandomColor()
{
int color_dec = randomInt(0, 4095);
std::stringstream stream;
stream << '#' << std::setfill('0') << std::setw(3) << std::hex << color_dec;
std::string color(stream.str());
return color;
}
std::string getRandomFromList(std::vector<std::string>& list)
{
int random = rand() % list.size();
return list[random];
}
std::string getNextFromList(std::vector<std::string>& list, bool get_last = false)
{
static int pos = 0;
if (pos >= list.size())
{
pos = 0;
}
if (get_last)
{
if (pos - 1 < 0)
{
return list[list.size() - 1];
}
return list[pos - 1];
}
return list[pos++];
}
void printLineColor(std::ostream& out, int start, int end)
{
if (start >= end)
{
return;
}
out << "\tlinkStyle ";
for (int i = start; i < end; i++)
{
out << i;
if (!(i + 1 == end))
{
out << ',';
}
}
out << " stroke-width:2px,fill:none,stroke:" << getNextFromList(colors) << ";\n";
}
inline bool ends_with(std::string const& value, std::string const& ending)
{
if (ending.size() > value.size())
{
return false;
}
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}
void printNodeColor(std::ostream& out, const std::string& node_name)
{
if (node_name.empty())
{
return;
}
bool c_type = ends_with(node_name, ".c") || ends_with(node_name, ".cpp");
out << "\tstyle " << node_name << " stroke-width:2px,fill:" << (c_type ? "lightgreen" : "bisque") << ",stroke:" << getNextFromList(colors, true)
<< ";\n";
}
void printIncludeGraph(std::ostream& out, const std::vector<std::filesystem::directory_entry>& files)
{
// Add the graph mermaid title
out << "graph TD\n";
int start = 0;
int end = 0;
for (size_t i = 0; i < files.size(); i++)
{
out << "\n " << files[i].path().filename().c_str() << '(' << files[i].path().filename().c_str() << ')' << '\n';
std::ifstream in_file{files[i].path()};
std::string line;
while (std::getline(in_file, line))
{
Include tmp;
if (getInclude(line, tmp))
{
// From
out << " " << files[i].path().filename().c_str() << " --> ";
// Label
// out << (tmp.quotation_marks ? "|local|" : "|system|") << ' ';
// To
out << tmp.include_file << '\n';
end++;
}
}
printLineColor(out, start, end);
if (start != end)
{
printNodeColor(out, files[i].path().filename().string());
}
start = end;
in_file.close();
}
}