8 #include "TemplateGenerator.h"
10 TemplateGenerator::TemplateGenerator() {
15 TemplateGenerator::~TemplateGenerator() {
19 string TemplateGenerator::generateTempCd(
string fileName,
string &headersb,
string &funcdefs)
23 vector<string> allcontent;
24 infile.open(fileName.c_str());
27 s = fileName.find_last_of(
"/")+1;
28 dir = fileName.substr(0,s-1);
29 en = fileName.find_last_of(
".");
30 file = fileName.substr(s,en-s);
33 while(getline(infile, data))
35 allcontent.push_back(data+
"\n");
38 string header,bodies,funcs,declars;
39 funcdefs.append(
"string _"+file+
"emittTemplateHTML(map<string, void*> args);\n");
40 declars.append(
"string _"+file+
"emittTemplateHTML(map<string, void*> args)\n{\nstring screen;\n");
42 map<string,string> uselocVars;
43 vector<string> inplaceVarValues;
44 bool startedFor =
false, startedIf =
false;
45 for (
int var = 0; var < (int)allcontent.size(); ++var) {
46 string temp = allcontent.at(var);
47 StringUtil::trim(temp);
48 StringUtil::replaceAll(temp,
"\"",
"\\\"");
49 if(temp.find(
"#declare")==0 && temp.at(temp.length()-1)==
'#')
51 StringUtil::replaceAll(temp,
"#declare ",
"");
52 StringUtil::replaceAll(temp,
"#",
"");
54 StringUtil::split(tvec, temp, (
" "));
57 declars.append(temp+
";\n");
58 declars.append(
"if(args[\""+tvec.at(1)+
"\"]!=NULL)\n{\n");
59 inplaceVarValues.push_back(tvec.at(1));
60 if(tvec.at(0).find(
"*")==string::npos)
62 declars.append(tvec.at(1)+
" = *("+tvec.at(0)+
"*)args[\""+tvec.at(1)+
"\"];\n");
66 declars.append(tvec.at(1)+
" = ("+tvec.at(0)+
"*)args[\""+tvec.at(1)+
"\"];\n");
68 declars.append(
"}\n");
71 else if(temp.find(
"#for(")==0 && temp.at(temp.length()-1)==
'#')
73 StringUtil::replaceAll(temp,
"#",
"");
74 tempo.append(temp +
"\n{\n");
77 else if(temp.find(
"#rof")==0 && temp.at(temp.length()-1)==
'#')
82 else if(temp.find(
"#if(")==0 && temp.at(temp.length()-1)==
'#')
84 StringUtil::replaceAll(temp,
"#",
"");
85 tempo.append(temp +
"\n{\n");
88 else if(temp.find(
"#elseif(")==0 && temp.at(temp.length()-1)==
'#')
90 StringUtil::replaceAll(temp,
"#",
"");
91 tempo.append(
"}\n" + temp +
"\n{\n");
94 else if(temp.find(
"#else")==0 && temp.at(temp.length()-1)==
'#')
96 StringUtil::replaceAll(temp,
"#",
"");
97 tempo.append(
"}\n" + temp +
"\n{\n");
100 else if(temp.find(
"#rof")==0 && temp.at(temp.length()-1)==
'#')
105 else if(temp.find(
"#fi")==0 && temp.at(temp.length()-1)==
'#')
112 for (
int var1 = 0; var1 < (int)inplaceVarValues.size(); ++var1)
114 string rep =
"${"+inplaceVarValues.at(var1)+
"}";
115 string strrep =
"$_S{"+inplaceVarValues.at(var1)+
"}";
116 if(temp.find(rep)!=string::npos)
118 string repVal = inplaceVarValues.at(var1);
119 StringUtil::replaceAll(temp,rep,
"\" + " + repVal +
" + \"");
121 else if(temp.find(strrep)!=string::npos)
123 string repVal = inplaceVarValues.at(var1);
124 StringUtil::replaceAll(temp,strrep,
"\" + CastUtil::lexical_cast<string>(" + repVal +
") + \"");
126 else if(temp.find(
"$_S{")!=string::npos && temp.find(
"}")!=string::npos
127 && temp.find(
"$_S{"+inplaceVarValues.at(var1))!=string::npos)
129 while(temp.find(
"$_S{")!=string::npos && temp.find(
"}")!=string::npos
130 && temp.find(
"$_S{"+inplaceVarValues.at(var1))!=string::npos)
132 string reps = temp.substr(temp.find(
"$_S{")+4, temp.find(
"}")-(temp.find(
"$_S{")+4));
133 string oreps = temp.substr(temp.find(
"$_S{"), temp.find(
"}")+1-(temp.find(
"$_S{")));
134 if(reps.find(
".")!=string::npos)
136 string repst = reps.substr(0, reps.find(
"."));
137 if(repst==inplaceVarValues.at(var1))
139 StringUtil::replaceAll(temp, oreps,
"\" + CastUtil::lexical_cast<string>(" + reps +
") + \"");
146 while(temp.find(
"${")!=string::npos && temp.find(
"}")!=string::npos
147 && temp.find(
"${"+inplaceVarValues.at(var1))!=string::npos)
149 string reps = temp.substr(temp.find(
"${")+2, temp.find(
"}")-(temp.find(
"${")+2));
150 string oreps = temp.substr(temp.find(
"${"), temp.find(
"}")+1-(temp.find(
"${")));
151 if(reps.find(
".")!=string::npos)
153 string repst = reps.substr(0, reps.find(
"."));
154 if(repst==inplaceVarValues.at(var1))
156 StringUtil::replaceAll(temp, oreps,
"\" + " + reps +
" + \"");
162 while(temp.find(
"$_S{")!=string::npos && temp.find(
"}")!=string::npos)
164 string reps = temp.substr(temp.find(
"$_S{")+4, temp.find(
"}")-(temp.find(
"$_S{")+4));
165 string oreps = temp.substr(temp.find(
"$_S{"), temp.find(
"}")+1-(temp.find(
"$_S{")));
169 StringUtil::replaceAll(temp, oreps,
"\" + CastUtil::lexical_cast<string>(" + reps +
") + \"");
172 while(temp.find(
"${")!=string::npos && temp.find(
"}")!=string::npos)
174 string reps = temp.substr(temp.find(
"${")+2, temp.find(
"}")-(temp.find(
"${")+2));
175 string oreps = temp.substr(temp.find(
"${"), temp.find(
"}")+1-(temp.find(
"${")));
179 StringUtil::replaceAll(temp, oreps,
"\" + " + reps +
" + \"");
182 tempo.append(
"screen += \"" + temp +
"\";\n");
185 bodies.append(tempo);
186 bodies.append(
"\nreturn screen;\n");
187 bodies.append(
"}\n");
188 declars.append(bodies);
192 string TemplateGenerator::generateTempCdAll(vector<string> fileNames)
194 string bodies,headersb=
"#include \"AfcInclude.h\"",funcdefs;
195 for (
unsigned int var = 0; var < fileNames.size(); ++var)
197 bodies += generateTempCd(fileNames.at(var),headersb,funcdefs);
199 bodies = (headersb+
"\nextern \"C\"\n{\n"+funcdefs+bodies+
"}\n");