CMS 3D CMS Logo

L1GtVhdlTemplateFile.cc
Go to the documentation of this file.
1 
15 // this class header
17 
18 // system include files
19 #include <iostream>
20 #include <fstream>
21 #include <sstream>
22 #include <map>
23 #include <string>
24 #include <vector>
25 
26 // constructor(s)
27 
28 //standard constructor for a empty file
30 
31 //constructor which already loads a file
33  if (!open(filename, false))
34  std::cout << "Error while opening file: " << filename << std::endl;
35 }
36 
38  const std::string &searchString,
39  const std::string &replaceString) {
40  size_t position;
41  position = paramString.find(searchString);
42  if (position == std::string::npos)
43  return false;
44  paramString.replace(position, searchString.length(), replaceString);
45  return true;
46 }
47 
48 bool L1GtVhdlTemplateFile::open(const std::string &fileName, bool internal) {
49  const char paramIndicator = '#';
50  const char commentIndicator = '%';
51  char buffer[2000];
52  std::string stringBuffer;
53 
54  std::fstream inputFile(fileName.c_str(), std::ios::in);
55  //check weather file has been opened successfully
56  if (!inputFile.is_open())
57  return false;
58 
59  //store content of the template in Vector lines
60  while (!inputFile.eof()) {
61  inputFile.getline(buffer, 2000);
62  stringBuffer = buffer;
63  //Remove DOS seperators (For example if the template file was created under NT)
64  if (stringBuffer[stringBuffer.length() - 1] == 13) {
65  stringBuffer.replace(stringBuffer.length() - 1, 1, "");
66  }
67  //the current buffer + a seperator to the vector lines
68  lines_.push_back(stringBuffer /*+"\n"*/);
69  }
70 
71  inputFile.close();
72 
73  if (internal) {
74  //Delete lines containing parameters after moving them to parameterMap_
75  std::vector<std::string>::iterator iter = lines_.begin();
76  while (iter != lines_.end()) {
77  while ((*iter)[0] == commentIndicator && (*iter)[1] == commentIndicator)
78  lines_.erase(iter);
79 
80  if ((*iter)[0] == paramIndicator) {
81  std::vector<std::string>::iterator iter2 = iter;
82 
83  // get the first line of content
84  iter2++;
85 
86  while (iter2 != lines_.end()) {
87  if ((*iter2)[0] == paramIndicator && (*iter2)[1] == paramIndicator) {
88  iter2++;
89  break;
90  }
91 
92  parameterMap_[(*iter).substr(1)] += (*iter2);
93 
94  // overtake the newlines
95  std::vector<std::string>::iterator tmpIter = iter2;
96  tmpIter++;
97 
98  // check weather the next line is the end of the block
99  if (!((*tmpIter)[0] == paramIndicator && (*tmpIter)[1] == paramIndicator))
100  parameterMap_[(*iter).substr(1)] += "\n";
101 
102  iter2++;
103  }
104 
105  // there has been a syntax error in the internal template
106  // stop the routine
107  if (iter2 == lines_.end())
108  return false;
109 
110  // deletes the content, thas has been added to parameter map before
111  // (iter one at the moment is at the beginnig of the block, iter2 at its end)
112  lines_.erase(iter, iter2);
113  }
114 
115  // just for security
116  if (iter != lines_.end())
117  iter++;
118  }
119 
120  //remove empty lines
121  iter = lines_.begin();
122  while (iter != lines_.end()) {
123  if ((*iter).empty() || (*iter).length() == 0 || (*iter) == " ")
124  lines_.erase(iter);
125  else
126  iter++;
127  }
128  }
129 
130  return true;
131 }
132 
134  std::ofstream outputFile(fileName.c_str());
135  std::vector<std::string>::iterator iter = lines_.begin();
136 
137  //Write content of lines_ into the outputfile.
138  while (iter != lines_.end()) {
139  //std::cout<<"Last sign: "<<*iter[(*iter).length()-3];
140  outputFile << *iter << std::endl;
141  iter++;
142  }
143 
144  outputFile.close();
145 
146  return true;
147 }
148 
150  bool success = false;
151 
152  std::vector<std::string>::iterator iter = lines_.begin();
153  while (iter != lines_.end()) {
154  //The substitution parameter always appears as follows: $(parameter)
155  while (findAndReplaceString(*iter, ("$(" + searchString + ")"), replaceString)) {
156  findAndReplaceString(*iter, ("$(" + searchString + ")"), replaceString);
157  success = true;
158  }
159  iter++;
160  }
161 
162  return success;
163 }
164 
165 bool L1GtVhdlTemplateFile::insert(const std::string &atLine, const std::vector<std::string> &content) {
166  bool success = false;
167  std::vector<std::string>::iterator iter = lines_.begin();
168 
169  //Loop until the substitution parameter is discovered the first time
170  while (iter != lines_.end()) {
171  //check, weather the current line is containing the substitution parameter
172  if ((*iter).find(atLine) != std::string::npos) {
173  //Delete the line with the subsitution parameter
174  iter = lines_.erase(iter);
175  //insert the content of file
176  lines_.insert(iter, content.begin(), content.end());
177 
178  success = true;
179  break;
180  }
181 
182  iter++;
183  }
184 
185  return success;
186 }
187 
189  std::vector<std::string> temp = _file.returnLines();
190 
191  if (insert(atLine, temp))
192  return true;
193 
194  return false;
195 }
196 
198  //empty
199  return true;
200 }
201 
203  std::vector<std::string>::const_iterator iter = lines_.begin();
204  while (iter != lines_.end()) {
205  std::cout << *iter << std::endl;
206  iter++;
207  }
208 }
209 
210 std::vector<std::string> L1GtVhdlTemplateFile::returnLines() const { return lines_; }
211 
213  std::cout << "Enter parametermap" << std::endl;
214 
215  std::map<std::string, std::string>::const_iterator iter = parameterMap_.begin();
216 
217  while (iter != parameterMap_.end()) {
218  std::cout << (*iter).first << ": " << (*iter).second << std::endl;
219  iter++;
220  ;
221  }
222 }
223 
224 std::map<std::string, std::string> L1GtVhdlTemplateFile::returnParameterMap() const { return parameterMap_; }
225 
227  std::vector<std::string> &parameters) const {
228  // check, weather the current line is containing a substitution parameter
229  // the routine is making sure, that it's not extracting a parameter from
230  // a comment
231  if (int pos1 = str.find("$(") != std::string::npos && str.substr(0, 2) != "--") {
232  int pos2 = str.find(')');
233  // get the substituion parameter
234  std::string tempStr = (str.substr(pos1 + 1, (pos2 - pos1 - 1)));
235  // return a pair with the substitution parameter and the
236  // the rest of the string after the substitution parameter
237 
238  // here a should be checked, weather the vector is already containing
239  // the parameter befor adding it.
240 
241  parameters.push_back(tempStr);
242  //recursive call
243  while (extractParametersFromString(str.substr(pos2), parameters))
245 
246  return true;
247  } else {
248  return false;
249  }
250 
251  return true;
252 }
253 
255  std::vector<std::string> temp;
256  std::vector<std::string>::const_iterator iter = lines_.begin();
257 
258  // loop until the substitution parameter is discovered the first time
259  while (iter != lines_.end()) {
261  iter++;
262  }
263 
264  return temp;
265 }
266 
268 
270  for (unsigned int i = 0; i < file.lines_.size(); i++) {
271  lines_.push_back(file.lines_.at(i));
272  }
273 }
274 
276  bool success = false;
277 
278  std::vector<std::string>::iterator iter = lines_.begin();
279  while (iter != lines_.end()) {
280  size_t position;
281  position = (*iter).find(str);
282 
283  if (position != std::string::npos) {
284  lines_.erase(iter);
285  success = true;
286  } else
287  iter++;
288  }
289  return success;
290 }
291 
293  std::vector<std::string>::iterator iter = lines_.begin();
294 
295  while (iter != lines_.end()) {
296  if ((*iter).empty() || (*iter).length() == 0 || (*iter) == " ")
297  lines_.erase(iter);
298  else
299  iter++;
300  }
301 
302  return true;
303 }
304 
305 bool L1GtVhdlTemplateFile::isBlank(const char &chr) const {
306  if (chr == ' ')
307  return true;
308  return false;
309 }
310 
311 bool L1GtVhdlTemplateFile::split(const std::string &param, std::vector<std::string> &result) const {
312  unsigned int i = 0;
313  while (isBlank(param[i])) {
314  i++;
315  }
316 
317  std::string temp = param.substr(i);
318  std::size_t pos = temp.find(' ');
319 
320  if (pos != std::string::npos) {
321  std::string temp2 = temp.substr(0, pos);
322  result.push_back(temp2);
323  while (split(temp.substr(pos), result))
324  split(temp.substr(pos), result);
325 
326  } else if (!isBlank(temp[pos + 1])) {
327  result.push_back(temp);
328  return false;
329  } else
330  return false;
331 
332  return false;
333 }
334 
335 void L1GtVhdlTemplateFile::getConditionsFromAlgo(std::string condString, std::vector<std::string> &result) const {
336  std::vector<std::string> operators;
337 
338  operators.push_back("AND");
339  operators.push_back("OR");
340  operators.push_back("NOT");
341  operators.push_back("(");
342  operators.push_back(")");
343 
344  for (unsigned int i = 0; i < operators.size(); i++) {
345  while (findAndReplaceString(condString, operators.at(i), ""))
346  findAndReplaceString(condString, operators.at(i), "");
347  }
348 
349  split(condString, result);
350 }
351 
353  std::vector<std::string>::const_iterator iter = lines_.begin();
354  std::ostringstream buffer;
355 
356  while (iter != lines_.end()) {
357  buffer << (*iter) << std::endl;
358  iter++;
359  }
360 
361  return buffer.str();
362 }
363 
365  return parameterMap_[indentifier];
366 }
std::map< std::string, std::string > parameterMap_
containing the header information of internal files
static const bool findAndReplaceString(std::string &paramString, const std::string &searchString, const std::string &replaceString)
replaces searchString with replaceString at it&#39;s first occurance in string
bool substitute(const std::string &searchString, const std::string &replaceString)
replaces searchString with replaceString
std::string lines2String() const
returns a string with the content of vector lines
bool save(const std::string &fileName)
saves the content of the template file to a local file (the content of parameterMap_ will not be save...
std::vector< std::string > getSubstitutionParametersFromTemplate() const
returns a vector with all substitution parameters that are found in the template file ...
void getConditionsFromAlgo(std::string condString, std::vector< std::string > &result) const
extracts all conditions from a algorithm
void replaceString(std::string &demangledName, std::string const &from, std::string const &to)
bool removeLineWithContent(const std::string &str)
removes all lines that contain the str
L1GtVhdlTemplateFile()
standard constructor
void print() const
prints the content of the VHDL File (only lines_)
std::vector< std::string > returnLines() const
returns a string vector with the current content of the VHDL File
void append(const std::string &str)
adds a line at the end of the the file with the content of str
std::string getInternalParameter(const std::string &indentifier)
returns a parameter of a internal template file
bool insert(const std::string &atLine, const std::vector< std::string > &content)
replaces the whole line containing atLine and inserts content instead of it
std::vector< std::string > lines_
containing the content of the VHDL file
std::map< std::string, std::string > returnParameterMap() const
returns parameter map
bool isBlank(const char &chr) const
checks weather a char is a blank
bool split(const std::string &param, std::vector< std::string > &result) const
seperates a string at all blanks and saves the elements in result
static int position[264][3]
Definition: ReadPGInfo.cc:289
void printParameterMap() const
prints the parameter map
bool extractParametersFromString(const std::string &str, std::vector< std::string > &parameters) const
#define str(s)
bool removeEmptyLines()
deletes all empty lines in a template file
bool open(const std::string &fileName, bool internal=false)
opens a template file. If the header information shall be parsed intern has to be set to true ...