ffead.server.doc
DCPGenerator.cpp
1 /*
2  Copyright 2009-2012, Sumeet Chhetri
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 /*
17  * DCPGenerator.cpp
18  *
19  * Created on: Sep 5, 2009
20  * Author: sumeet
21  */
22 
23 #include "DCPGenerator.h"
24 
25 
26 DCPGenerator::DCPGenerator() {
27  // TODO Auto-generated constructor stub
28 
29 }
30 
31 DCPGenerator::~DCPGenerator() {
32  // TODO Auto-generated destructor stub
33 }
34 string DCPGenerator::generateDCPAll(strVec fileNames)
35 {
36  string bodies,headersb="#include \"AfcUtil.h\"",funcdefs;
37  for (unsigned int var = 0; var < fileNames.size(); ++var)
38  {
39  bodies += generateDCP(fileNames.at(var),headersb,funcdefs);
40  }
41  bodies = (headersb+"\nextern \"C\"\n{\n"+funcdefs+bodies+"}\n");
42  return bodies;
43 }
44 string DCPGenerator::generateDCP(string fileName,string &headersb,string &funcdefs)
45 {
46  ifstream infile;
47  string data,allcontent;
48  infile.open(fileName.c_str());
49  string file,dir;
50  int s,en;
51  s = fileName.find_last_of("/")+1;
52  dir = fileName.substr(0,s-1);
53  en = fileName.find_last_of(".");
54  file = fileName.substr(s,en-s);
55  if(infile)
56  {
57  while(getline(infile, data))
58  {
59  if(data.find("<import>")!=string::npos && data.find("</import>")!=string::npos)
60  {
61  int s = data.find("<import>")+8;
62  int e = data.find("</import>");
63  data=data.substr(s,e-s);
64  s = data.find_last_of("/")+1;
65  en = data.find_last_of(".");
66  string file1 = data.substr(s,en-s);
67  allcontent.append("<DCPB>screen << _"+file1+"emittHTML();\n</DCPB>");
68  /*ifstream inf(data.c_str());
69  if(inf)
70  {
71  while(getline(inf, data))
72  {
73  allcontent.append(data+"\n");
74  }
75  }*/
76  }
77  else
78  allcontent.append(data+"\n");
79  }
80  }
81  string header,bodies,funcs;
82  int b = allcontent.find("<DCPH>") + 6;
83  int e = allcontent.find("</DCPH>");
84  int len = e - b;
85  if(len>0)
86  {
87  header.append(allcontent.substr(b,len));
88  allcontent = allcontent.substr(e+7);
89  headersb.append(header+"\n");
90  }
91 
92  while(allcontent.find("<DCPF>")!=string::npos)
93  {
94  b = allcontent.find("<DCPF>");
95  e = allcontent.find("</DCPF>") + 7;
96  string temp1 = allcontent.substr(b,e-b);
97  StringUtil::replaceAll(allcontent,temp1,"");
98 
99  //string ter(allcontent.substr(0,b-6));
100  //StringUtil::replaceAll(ter,"\n","");
101  //StringUtil::replaceAll(ter,"\"","\\\"");
102  e = temp1.find("</DCPF>");
103  len = e - b;
104  funcs.append(temp1.substr(6,e-6));
105  //allcontent = allcontent.substr(e+7);
106  }
107  bodies.append(funcs);
108  //bodies.append();
109  funcdefs.append("string _"+file+"emittHTML();\n");
110  bodies.append("string _"+file+"emittHTML()\n{\n");
111  bodies.append("stringstream screen;\n");
112  while(allcontent.find("<DCPB>")!=string::npos)
113  {
114  b = allcontent.find("<DCPB>") + 6;
115  string ter(allcontent.substr(0,b-6));
116  StringUtil::replaceAll(ter,"\n","");
117  StringUtil::replaceAll(ter,"\"","\\\"");
118  bodies.append("screen << \""+ter+"\";");
119  e = allcontent.find("</DCPB>");
120  len = e - b;
121  bodies.append(allcontent.substr(b,len));
122  allcontent = allcontent.substr(e+7);
123  }
124  string ter(allcontent.substr(0));
125  StringUtil::replaceAll(ter,"\n","");
126  StringUtil::replaceAll(ter,"\"","\\\"");
127  bodies.append("screen << \""+ter+"\";\nstring scr;\nscr = screen.str();\n");
128  bodies.append("\nAfcUtil::writeTofile(\""+dir+"_"+file+".html\",scr,true);\n");
129  bodies.append("\nreturn scr;\n");
130  bodies.append("\n}\n");
131  //bodies.append("}\n");
132  return bodies;
133 }
134 
135