-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
194 lines (164 loc) · 4.86 KB
/
main.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
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
/*
* CP Template Generator
*
* author: calgagi
* Calvin Gagliano
*/
#include <fstream>
#include <iostream>
#include <unistd.h>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
using namespace std;
// file to be printed to target file after libraries are printed
string CP_GEN_AFTER = "./user.cpp";
// Dependency graph for libraries that require other libraries
// Note: this should be a DAG
// Syntax: {lib, {list of libs that lib is dependent on}}
map<string, vector<string>> dep_graph = {
{"lca", {"graph", "rlamb"}}
};
// ============= Helper functions ==============
void error(string msg) {
cout << "cp_gen: " << msg << endl;
exit(1);
}
string noAfterDot(const string& filename) {
int where = filename.find(".");
if (where == string::npos) {
error("Could not find task in filename");
}
return filename.substr(0, where);
}
void shavePath(string& path) {
while (!path.empty() && path.back() != '/') {
path.pop_back();
}
return;
}
string pathToGen() {
char buff[512];
// Linux syscall that gets the linked path to the current executable, "gen"
ssize_t r = ::readlink("/proc/self/exe", buff, 511);
if (r == -1) {
error("Could not find path to current executable.");
}
else {
buff[r] = '\0';
}
// this assumes that gen is in the root directory of cp_gen (where lib is)
string gen_path = buff;
shavePath(gen_path);
return gen_path;
}
// ============= Output functions ==============
void output_file(fstream& new_file, const string& file_path) {
fstream to_output;
to_output.open(file_path);
if (!to_output.is_open()) {
error("Could not find " + file_path + " file");
}
string line;
while (getline(to_output, line)) {
// Skip "#pragma once" lines, they are used for dependencies in testing
if (line != "#pragma once") {
new_file << line << endl;
}
}
return;
}
void output_templates(fstream& new_file, const vector<string>& templates) {
// this assumes that gen is in the root directory of cp_gen (where lib is)
string gen_path = pathToGen();
gen_path += "lib/";
set<string> already_outputted;
for (const string& ds : templates) {
vector<string> to_output;
queue<string> q;
q.push(ds);
// perform BFS on dependency graph
while (!q.empty()) {
string cur_lib = q.front();
q.pop();
if (already_outputted.find(cur_lib) != already_outputted.end()) {
continue;
}
already_outputted.insert(cur_lib);
to_output.push_back(cur_lib);
for (const string& dep : dep_graph[cur_lib]) {
q.push(dep);
}
}
// output files in reverse visited order
for (int i = to_output.size() - 1; ~i; i--) {
output_file(new_file, gen_path + to_output[i] + ".hpp");
new_file << endl;
}
}
return;
}
void output(fstream& new_file, const string& filename, const vector<string>& templates) {
// Static Header
time_t now = time(0);
new_file << "/*\n"
" * generated by github.com/calgagi/cp_gen\n"
" * file: " << filename << "\n"
" * time: " << ctime(&now) << ""
" */\n"
"\n"
"#include <bits/stdc++.h>\n"
"using namespace std;\n"
"typedef long long ll;\n"
"typedef long double ld;\n\n"
"namespace cp {\n\n";
string gen_path = pathToGen();
// Output templates
output_templates(new_file, templates);
new_file << "}\n\n"
"/* ===== BEGIN USER CODE ===== */\n\n";
// User-defined after cp namespace file
output_file(new_file, gen_path + CP_GEN_AFTER);
return;
}
// =============== main ================
int main(int argc, char** argv) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
char cwdBuf[10000];
string cwd = "";
if (getcwd(cwdBuf, sizeof(cwdBuf)) == NULL) {
error("Could not get current working directory");
}
else {
cwd = cwdBuf;
}
// Parse CLI arguments
vector<string> cliArgs;
string filename = "";
for (int i = 1; i < argc; i++) {
cliArgs.push_back(argv[i]);
if (cliArgs.back()[0] != '-') {
// we've found our filename
filename = cliArgs.back();
cliArgs.pop_back();
}
else {
// remove leading '-'
cliArgs.back().erase(cliArgs.back().begin());
}
}
if (filename == "") {
error("Could not find filename");
}
string path = cwd + "/" + filename;
fstream new_file;
new_file.open(path, ios_base::out);
if (!new_file.is_open()) {
error("Could not open file for output");
}
output(new_file, filename, cliArgs);
return 0;
}