ffead.server.doc
TemplateGenerator.cpp
1 /*
2  * TemplateGenerator.cpp
3  *
4  * Created on: 15-Feb-2013
5  * Author: sumeetc
6  */
7 
8 #include "TemplateGenerator.h"
9 
10 TemplateGenerator::TemplateGenerator() {
11  // TODO Auto-generated constructor stub
12 
13 }
14 
15 TemplateGenerator::~TemplateGenerator() {
16  // TODO Auto-generated destructor stub
17 }
18 
19 string TemplateGenerator::generateTempCd(string fileName,string &headersb,string &funcdefs)
20 {
21  ifstream infile;
22  string data;
23  vector<string> allcontent;
24  infile.open(fileName.c_str());
25  string file,dir;
26  int s,en;
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);
31  if(infile)
32  {
33  while(getline(infile, data))
34  {
35  allcontent.push_back(data+"\n");
36  }
37  }
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");
41  string tempo;
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)=='#')
50  {
51  StringUtil::replaceAll(temp,"#declare ","");
52  StringUtil::replaceAll(temp,"#","");
53  vector<string> tvec;
54  StringUtil::split(tvec, temp, (" "));
55  if(tvec.size()==2)
56  {
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)
61  {
62  declars.append(tvec.at(1)+" = *("+tvec.at(0)+"*)args[\""+tvec.at(1)+"\"];\n");
63  }
64  else
65  {
66  declars.append(tvec.at(1)+" = ("+tvec.at(0)+"*)args[\""+tvec.at(1)+"\"];\n");
67  }
68  declars.append("}\n");
69  }
70  }
71  else if(temp.find("#for(")==0 && temp.at(temp.length()-1)=='#')
72  {
73  StringUtil::replaceAll(temp,"#","");
74  tempo.append(temp + "\n{\n");
75  startedFor = true;
76  }
77  else if(temp.find("#rof")==0 && temp.at(temp.length()-1)=='#')
78  {
79  startedFor = false;
80  tempo.append("}\n");
81  }
82  else if(temp.find("#if(")==0 && temp.at(temp.length()-1)=='#')
83  {
84  StringUtil::replaceAll(temp,"#","");
85  tempo.append(temp + "\n{\n");
86  startedIf = true;
87  }
88  else if(temp.find("#elseif(")==0 && temp.at(temp.length()-1)=='#')
89  {
90  StringUtil::replaceAll(temp,"#","");
91  tempo.append("}\n" + temp + "\n{\n");
92  startedIf = true;
93  }
94  else if(temp.find("#else")==0 && temp.at(temp.length()-1)=='#')
95  {
96  StringUtil::replaceAll(temp,"#","");
97  tempo.append("}\n" + temp + "\n{\n");
98  startedIf = true;
99  }
100  else if(temp.find("#rof")==0 && temp.at(temp.length()-1)=='#')
101  {
102  startedFor = false;
103  tempo.append("}\n");
104  }
105  else if(temp.find("#fi")==0 && temp.at(temp.length()-1)=='#')
106  {
107  startedIf = false;
108  tempo.append("}\n");
109  }
110  else
111  {
112  for (int var1 = 0; var1 < (int)inplaceVarValues.size(); ++var1)
113  {
114  string rep = "${"+inplaceVarValues.at(var1)+"}";
115  string strrep = "$_S{"+inplaceVarValues.at(var1)+"}";
116  if(temp.find(rep)!=string::npos)
117  {
118  string repVal = inplaceVarValues.at(var1);
119  StringUtil::replaceAll(temp,rep,"\" + " + repVal + " + \"");
120  }
121  else if(temp.find(strrep)!=string::npos)
122  {
123  string repVal = inplaceVarValues.at(var1);
124  StringUtil::replaceAll(temp,strrep,"\" + CastUtil::lexical_cast<string>(" + repVal + ") + \"");
125  }
126  else if(temp.find("$_S{")!=string::npos && temp.find("}")!=string::npos
127  && temp.find("$_S{"+inplaceVarValues.at(var1))!=string::npos)
128  {
129  while(temp.find("$_S{")!=string::npos && temp.find("}")!=string::npos
130  && temp.find("$_S{"+inplaceVarValues.at(var1))!=string::npos)
131  {
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)
135  {
136  string repst = reps.substr(0, reps.find("."));
137  if(repst==inplaceVarValues.at(var1))
138  {
139  StringUtil::replaceAll(temp, oreps, "\" + CastUtil::lexical_cast<string>(" + reps + ") + \"");
140  }
141  }
142  }
143  }
144  else
145  {
146  while(temp.find("${")!=string::npos && temp.find("}")!=string::npos
147  && temp.find("${"+inplaceVarValues.at(var1))!=string::npos)
148  {
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)
152  {
153  string repst = reps.substr(0, reps.find("."));
154  if(repst==inplaceVarValues.at(var1))
155  {
156  StringUtil::replaceAll(temp, oreps, "\" + " + reps + " + \"");
157  }
158  }
159  }
160  }
161  }
162  while(temp.find("$_S{")!=string::npos && temp.find("}")!=string::npos)
163  {
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{")));
166  //if(reps.find(".")!=string::npos)
167  {
168  //string repst = reps.substr(0, reps.find("."));
169  StringUtil::replaceAll(temp, oreps, "\" + CastUtil::lexical_cast<string>(" + reps + ") + \"");
170  }
171  }
172  while(temp.find("${")!=string::npos && temp.find("}")!=string::npos)
173  {
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("${")));
176  //if(reps.find(".")!=string::npos)
177  {
178  //string repst = reps.substr(0, reps.find("."));
179  StringUtil::replaceAll(temp, oreps, "\" + " + reps + " + \"");
180  }
181  }
182  tempo.append("screen += \"" + temp + "\";\n");
183  }
184  }
185  bodies.append(tempo);
186  bodies.append("\nreturn screen;\n");
187  bodies.append("}\n");
188  declars.append(bodies);
189  return declars;
190 }
191 
192 string TemplateGenerator::generateTempCdAll(vector<string> fileNames)
193 {
194  string bodies,headersb="#include \"AfcInclude.h\"",funcdefs;
195  for (unsigned int var = 0; var < fileNames.size(); ++var)
196  {
197  bodies += generateTempCd(fileNames.at(var),headersb,funcdefs);
198  }
199  bodies = (headersb+"\nextern \"C\"\n{\n"+funcdefs+bodies+"}\n");
200  return bodies;
201 }
202 
203