forked from UTAP/APHTTP
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtemplate_parser.cpp
More file actions
206 lines (175 loc) · 6.65 KB
/
template_parser.cpp
File metadata and controls
206 lines (175 loc) · 6.65 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "template_parser.hpp"
#include <map>
using namespace std;
static std::string mkdirNoErrors(const std::string &dirName) {
// do not error if dir already exists (-p flag on linux)
#ifdef _WIN32
return ("(if not exist \"" + dirName + "\" " + SysCmd::mkdir + "\"" +
dirName + "\")");
#else
return (SysCmd::mkdir + "-p \"" + dirName + "\"");
#endif
}
int TemplateParser::lastParserNum = 0;
const std::string localTemplate(const int parserNum) {
return "local" + std::to_string(parserNum) + ".html";
}
TemplateParser::TemplateParser(string _filePath) {
filePath = _filePath;
variableCount = 0;
programName =
to_string(TemplateParser::lastParserNum) + SysCmd::fileExtention;
parserNum = TemplateParser::lastParserNum++;
code = "";
parseTemplate();
makeExecutableTemplate();
}
string TemplateParser::getHtml(map<string, string> _context) {
TemplateUtils::writeMapToFile(outputFolder + "/" + mapFile, &_context);
return runGeneratedCode();
}
void TemplateParser::parseTemplate() {
string unparsedTemplate = readFile(filePath);
int parsePointer = 0;
while (parsePointer < (signed int)unparsedTemplate.size()) {
int begin = findBeginOfCodeBlock(parsePointer, unparsedTemplate);
int end = findEndOfCodeBlock(parsePointer, unparsedTemplate);
if (begin < 0)
break;
appendHTMLToCode(parsePointer, begin, unparsedTemplate);
appendCodeBlockToCode(begin, end, unparsedTemplate);
parsePointer = end + endCodeBlockTag.size();
}
appendHTMLToCode(parsePointer, unparsedTemplate.size(), unparsedTemplate);
}
int TemplateParser::findBeginOfCodeBlock(int startPosition,
string &unparsedTemplate) {
return findSubStrPosition(unparsedTemplate, beginCodeBlockTag, startPosition);
}
int TemplateParser::findEndOfCodeBlock(int startPosition,
string &unparsedTemplate) {
return findSubStrPosition(unparsedTemplate, endCodeBlockTag, startPosition);
}
void TemplateParser::appendHTMLToCode(int begin, int end,
string const &unparsedTemplate) {
code += "\nstring __variable" + to_string(variableCount) + ";";
code += "\n__variable" + to_string(variableCount) +
" = __unparsedTemplate__.substr(";
code += to_string(begin) + ", " + to_string(end - begin) + ");";
code += "\ncout << __variable" + to_string(variableCount) + ";";
variableCount++;
}
void TemplateParser::appendCodeBlockToCode(int begin, int end,
string &unparsedTemplate) {
if (end <= begin || begin < 0)
throw Server::Exception("Can not parse template " + filePath);
int codeBlockSize = end - begin - beginCodeBlockTag.size();
code +=
unparsedTemplate.substr(begin + beginCodeBlockTag.size(), codeBlockSize);
}
void TemplateParser::makeExecutableTemplate() {
generateCode();
compileCode();
makeLocalTemplate();
}
void TemplateParser::makeLocalTemplate() {
string templateContent = readFile(filePath);
if (writeToFile(templateContent,
outputFolder + "/" + localTemplate(parserNum)) < 0)
throw Server::Exception("Can not write template to local " + outputFolder +
"folder");
}
void TemplateParser::generateCode() {
addReadFromTemplateToCode();
addContextMapToCode();
addIncludesToCode();
addReturnToCode();
}
void TemplateParser::compileCode() {
if (writeToFile(code, toCompileFile) < 0)
throw Server::Exception("Can not write generated template code!");
string cmd = mkdirNoErrors(outputFolder) + " && " + cc + " " + toCompileFile +
" " + utilitiesPath + " -o " + outputFolder + SysCmd::slash +
programName + "&& " + SysCmd::rm + toCompileFile;
string error = "Can not compile template " + filePath;
TemplateUtils::runSystemCommand(cmd, error);
}
string TemplateParser::runGeneratedCode() {
string cmd = SysCmd::programStart + outputFolder + SysCmd::slash +
programName + " " + " > " + staticTemplate;
string error = "Error in running template " + filePath;
TemplateUtils::runSystemCommand(cmd, error);
string html = readFile(staticTemplate);
cmd = SysCmd::rm + staticTemplate;
error = "Error in deleting static template for " + filePath;
TemplateUtils::runSystemCommand(cmd, error);
return html;
}
void TemplateParser::addIncludesToCode() {
string include = "#include <iostream>\n";
include += "#include <string>\n";
include += "#include <map>\n";
include += "#include <cstring>\n";
include += "#include \"" + utilitiesHeaderPath + "\"\n";
include += "using namespace std;\n";
code = include + "int main(int argc, char const *argv[])\n{\n" + code + "\n";
}
void TemplateParser::addReadFromTemplateToCode() {
code = "string __unparsedTemplate__ = readFile(\"" + outputFolder + "/" +
localTemplate(parserNum) + "\");\n" + code;
}
void TemplateParser::addReturnToCode() { code += "return 0;\n}\n"; }
void TemplateParser::addContextMapToCode() {
string mapCode = "std::map<std::string, std::string> context;\n";
// `mapFile` should be changed if we want to handle requests
// in a multi-thread non-blocking way
mapCode +=
"readMapFromFile(\"" + outputFolder + "/" + mapFile + "\", &context);\n";
code = mapCode + code;
}
TemplateParser::~TemplateParser() {
deleteExecutable();
deleteLocalTemplate();
}
void TemplateParser::deleteExecutable() {
string cmd = SysCmd::rm + outputFolder + SysCmd::slash + programName;
string error = "Error in deleting executable file at " + outputFolder + "/" +
programName;
TemplateUtils::runSystemCommand(cmd, error);
}
void TemplateParser::deleteLocalTemplate() {
string cmd =
SysCmd::rm + outputFolder + SysCmd::slash + localTemplate(parserNum);
string error = "Error in deleting local template at " + outputFolder + "/" +
localTemplate(parserNum);
TemplateUtils::runSystemCommand(cmd, error);
}
void TemplateParser::TemplateUtils::runSystemCommand(string command,
string error) {
int ret = system(command.c_str());
#ifdef _WIN32
if (ret != 0) {
throw Server::Exception(error);
}
#else
if (WEXITSTATUS(ret) != EXIT_SUCCESS) {
throw Server::Exception(error);
}
#endif
}
int TemplateParser::TemplateUtils::writeMapToFile(
std::string fname, std::map<std::string, std::string> *m) {
int count = 0;
if (m->empty())
return 0;
FILE *fp = fopen(fname.c_str(), "w");
if (!fp)
return -errno;
for (std::map<std::string, std::string>::iterator it = m->begin();
it != m->end(); it++) {
fprintf(fp, "%s=%s\n", it->first.c_str(), it->second.c_str());
count++;
}
fclose(fp);
return count;
}