CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
L1GtVhdlTemplateFile.cc
Go to the documentation of this file.
1 
17 // this class header
19 
20 // system include files
21 #include <iostream>
22 #include <fstream>
23 #include <sstream>
24 #include <map>
25 #include <string>
26 #include <vector>
27 
28 // constructor(s)
29 
30 //standard constructor for a empty file
32 {
33  intern_=false;
34 }
35 
36 
37 //constructor which already loads a file
39 {
40  if (!open(filename,false)) std::cout<<"Error while opening file: "<<filename<<std::endl;
41 }
42 
43 
44 //copy constructor
46 {
47  lines_=rhs.lines_;
48  intern_=rhs.intern_;
50 
51 }
52 
53 
54 // destructor
56 {
57  // empty
58 }
59 
60 
61 const bool L1GtVhdlTemplateFile::findAndReplaceString(std::string &paramString, const std::string &searchString, const std::string &replaceString)
62 {
63  size_t position;
64  position = paramString.find(searchString);
65  if (position == std::string::npos) return false;
66  paramString.replace(position,searchString.length(),replaceString);
67  return true;
68 }
69 
70 
71 bool L1GtVhdlTemplateFile::open(const std::string &fileName, bool internal)
72 {
73 
74  const char paramIndicator='#';
75  const char commentIndicator='%';
76  char buffer[2000];
77  std::string stringBuffer;
78 
79  std::fstream inputFile(fileName.c_str(),std::ios::in);
80  //check weather file has been opened successfully
81  if(!inputFile.is_open()) return false;
82 
83  //store content of the template in Vector lines
84  while(!inputFile.eof())
85  {
86  inputFile.getline(buffer,2000);
87  stringBuffer=buffer;
88  //Remove DOS seperators (For example if the template file was created under NT)
89  if (stringBuffer[stringBuffer.length()-1]==13)
90  {
91  stringBuffer.replace(stringBuffer.length()-1,1,"");
92  }
93  //the current buffer + a seperator to the vector lines
94  lines_.push_back(stringBuffer/*+"\n"*/);
95  }
96 
97  inputFile.close();
98 
99  if (internal)
100  {
101 
102  //Delete lines containing parameters after moving them to parameterMap_
103  std::vector<std::string>::iterator iter = lines_.begin();
104  while( iter != lines_.end() )
105  {
106  while ((*iter)[0]==commentIndicator && (*iter)[1]==commentIndicator)
107  lines_.erase(iter);
108 
109  if ((*iter)[0]==paramIndicator)
110  {
111  std::vector<std::string>::iterator iter2 = iter;
112 
113  // get the first line of content
114  iter2++;
115 
116  while (iter2!=lines_.end())
117  {
118  if ((*iter2)[0]==paramIndicator && (*iter2)[1]==paramIndicator)
119  {
120  iter2++;
121  break;
122  }
123 
124 
125 
126  parameterMap_[(*iter).substr(1)]+=(*iter2);
127 
128  // overtake the newlines
129  std::vector<std::string>::iterator tmpIter= iter2;
130  tmpIter++;
131 
132  // check weather the next line is the end of the block
133  if (!((*tmpIter)[0]==paramIndicator && (*tmpIter)[1]==paramIndicator))
134  parameterMap_[(*iter).substr(1)]+="\n";
135 
136 
137  iter2++;
138  }
139 
140  // there has been a syntax error in the internal template
141  // stop the routine
142  if (iter2==lines_.end())
143  return false;
144 
145  // deletes the content, thas has been added to parameter map before
146  // (iter one at the moment is at the beginnig of the block, iter2 at its end)
147  lines_.erase(iter,iter2 );
148 
149  }
150 
151  // just for security
152  if (iter!=lines_.end()) iter++;
153  }
154 
155  //remove empty lines
156  iter = lines_.begin();
157  while( iter != lines_.end() )
158  {
159  if ((*iter)=="" || (*iter).length()==0 || (*iter)==" ") lines_.erase(iter); else
160  iter++;
161  }
162 
163  }
164 
165  return true;
166 
167 }
168 
169 
170 bool L1GtVhdlTemplateFile::save(const std::string &fileName)
171 {
172  std::ofstream outputFile(fileName.c_str());
173  std::vector<std::string>::iterator iter = lines_.begin();
174 
175  //Write content of lines_ into the outputfile.
176  while( iter != lines_.end() )
177  {
178  //std::cout<<"Last sign: "<<*iter[(*iter).length()-3];
179  outputFile << *iter<<std::endl;
180  iter++;
181  }
182 
183  outputFile.close();
184 
185  return true;
186 
187 }
188 
189 
190 bool L1GtVhdlTemplateFile::substitute(const std::string &searchString, const std::string &replaceString)
191 {
192 
193  bool success = false;
194 
195  std::vector<std::string>::iterator iter = lines_.begin();
196  while( iter != lines_.end())
197  {
198  //The substitution parameter always appears as follows: $(parameter)
199  while (findAndReplaceString(*iter,("$("+searchString+")"), replaceString))
200  {
201  findAndReplaceString(*iter,("$("+searchString+")"), replaceString);
202  success = true;
203  }
204  iter++;
205  }
206 
207  return success;
208 
209 }
210 
211 
212 bool L1GtVhdlTemplateFile::insert(const std::string &atLine, std::vector<std::string> content)
213 {
214  bool success = false;
215  std::vector<std::string>::iterator iter = lines_.begin();
216 
217  //Loop until the substitution parameter is discovered the first time
218  while( iter != lines_.end() )
219  {
220  //check, weather the current line is containing the substitution parameter
221  if ((*iter).find(atLine)!=std::string::npos)
222  {
223  //Delete the line with the subsitution parameter
224  iter = lines_.erase(iter);
225  //insert the content of file
226  lines_.insert(iter,content.begin(),content.end());
227 
228  success=true;
229  break;
230  }
231 
232  iter++;
233  }
234 
235  return success;
236 }
237 
238 
240 {
241  std::vector<std::string> temp = file.returnLines();
242 
243  if (insert(atLine,temp)) return true;
244 
245  return false;
246 }
247 
248 
250 {
251  //empty
252  return true;
253 }
254 
255 
257 {
258  std::vector<std::string>::iterator iter = lines_.begin();
259  while( iter != lines_.end())
260  {
261  std::cout<<*iter<<std::endl;
262  iter++;
263  }
264 
265 }
266 
267 
268 std::vector<std::string> L1GtVhdlTemplateFile::returnLines()
269 {
270  return lines_;
271 }
272 
273 
275 {
276  std::cout<<"Enter parametermap"<<std::endl;
277 
278  std::map<std::string,std::string>::iterator iter = parameterMap_.begin();
279 
280  while( iter != parameterMap_.end())
281  {
282  std::cout<<(*iter).first<<": "<<(*iter).second<<std::endl;
283  iter++;;
284  }
285 }
286 
287 
288 std::map<std::string,std::string> L1GtVhdlTemplateFile::returnParameterMap()
289 {
290  return parameterMap_;
291 }
292 
293 
294 bool L1GtVhdlTemplateFile::extractParametersFromString(const std::string &str, std::vector<std::string> &parameters)
295 {
296  // check, weather the current line is containing a substitution parameter
297  // the routine is making sure, that it's not extracting a parameter from
298  // a comment
299  if (int pos1=str.find("$(")!=std::string::npos && str.substr(0,2)!="--")
300  {
301  int pos2=str.find(")");
302  // get the substituion parameter
303  std::string tempStr=(str.substr(pos1+1,(pos2-pos1-1)));
304  // return a pair with the substitution parameter and the
305  // the rest of the string after the substitution parameter
306 
307  // here a should be checked, weather the vector is already containing
308  // the parameter befor adding it.
309 
310  parameters.push_back(tempStr);
311  //recursive call
312  while (extractParametersFromString(str.substr(pos2), parameters)) extractParametersFromString(str.substr(pos2), parameters);
313 
314  return true;
315  }
316  else
317  {
318  return false;
319  }
320 
321  return true;
322 }
323 
324 
326 {
327  std::vector<std::string> temp;
328  std::vector<std::string>::iterator iter = lines_.begin();
329 
330  // loop until the substitution parameter is discovered the first time
331  while( iter != lines_.end() )
332  {
333  extractParametersFromString((*iter), temp);
334  iter++;
335  }
336 
337  return temp;
338 
339 }
340 
341 
342 void L1GtVhdlTemplateFile::append(const std::string &str)
343 {
344  lines_.push_back(str);
345 }
346 
347 
349 {
350  for (unsigned int i=0; i<file.lines_.size(); i++)
351  {
352  lines_.push_back(file.lines_.at(i));
353  }
354 }
355 
356 
358 {
359  bool success = false;
360 
361  std::vector<std::string>::iterator iter = lines_.begin();
362  while( iter != lines_.end())
363  {
364  size_t position;
365  position = (*iter).find(str);
366 
367  if (position != std::string::npos)
368  {
369  lines_.erase(iter);
370  success=true;
371  } else iter++;
372  }
373  return success;
374 }
375 
376 
378 {
379  std::vector<std::string>::iterator iter = lines_.begin();
380 
381  while( iter != lines_.end() )
382  {
383  if ((*iter)=="" || (*iter).length()==0 || (*iter)==" ") lines_.erase(iter); else
384  iter++;
385  }
386 
387  return true;
388 }
389 
390 
391 bool L1GtVhdlTemplateFile::isBlank(const char &chr)
392 {
393  if (chr==' ') return true;
394  return false;
395 
396 }
397 
398 
399 bool L1GtVhdlTemplateFile::split(const std::string &param, std::vector<std::string> &result)
400 {
401  unsigned int i = 0;
402  while (isBlank(param[i]))
403  {
404  i++;
405  }
406 
407  std::string temp = param.substr(i);
408  std::size_t pos = temp.find(" ");
409 
410  if (pos != std::string::npos)
411  {
412  std::string temp2 = temp.substr(0, pos);
413  result.push_back(temp2);
414  while (split(temp.substr(pos),result)) split(temp.substr(pos),result);
415 
416  } else if (!isBlank(temp[pos+1]))
417  {
418  result.push_back(temp);
419  return false;
420  } else
421  return false;
422 
423  return false;
424 }
425 
426 
427 void L1GtVhdlTemplateFile::getConditionsFromAlgo(std::string condString, std::vector<std::string> &result)
428 {
429  std::vector<std::string> operators;
430 
431  operators.push_back("AND");
432  operators.push_back("OR");
433  operators.push_back("NOT");
434  operators.push_back("(");
435  operators.push_back(")");
436 
437  for (unsigned int i =0; i<operators.size(); i++)
438  {
439  while (findAndReplaceString(condString, operators.at(i), "")) findAndReplaceString(condString, operators.at(i), "");
440  }
441 
442  split(condString,result);
443 
444 }
445 
446 
448 {
449  std::vector<std::string>::iterator iter = lines_.begin();
450  std::ostringstream buffer;
451 
452  while( iter != lines_.end() )
453  {
454  buffer<<(*iter)<<std::endl;
455  iter++;
456 
457  }
458 
459  return buffer.str();
460 }
461 
462 
463 std::string L1GtVhdlTemplateFile::getInternalParameter(const std::string &indentifier)
464 {
465  return parameterMap_[indentifier];
466 }
std::map< std::string, std::string > returnParameterMap()
returns parameter map
int i
Definition: DBlmapReader.cc:9
dictionary parameters
Definition: Parameters.py:2
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
bool isBlank(const char &chr)
checks weather a char is a blank
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...
~L1GtVhdlTemplateFile()
destructor
std::map< std::string, std::string > parameterMap_
containing the header information of internal files
bool removeLineWithContent(const std::string &str)
removes all lines that contain the str
L1GtVhdlTemplateFile()
standard constructor
bool insert(const std::string &atLine, std::vector< std::string > content)
replaces the whole line containing atLine and inserts content instead of it
tuple result
Definition: query.py:137
void append(const std::string &str)
adds a line at the end of the the file with the content of str
void print()
prints the content of the VHDL File (only lines_)
std::vector< std::string > returnLines()
returns a string vector with the current content of the VHDL File
std::string getInternalParameter(const std::string &indentifier)
returns a parameter of a internal template file
std::string lines2String()
returns a string with the content of vector lines
std::vector< std::string > lines_
containing the content of the VHDL file
bool split(const std::string &param, std::vector< std::string > &result)
seperates a string at all blanks and saves the elements in result
void getConditionsFromAlgo(std::string condString, std::vector< std::string > &result)
extracts all conditions from a algorithm
static int position[264][3]
Definition: ReadPGInfo.cc:509
tuple filename
Definition: lut2db_cfg.py:20
bool extractParametersFromString(const std::string &str, std::vector< std::string > &parameters)
tuple cout
Definition: gather_cfg.py:121
void printParameterMap()
prints the parameter map
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 ...
std::vector< std::string > getSubstitutionParametersFromTemplate()
returns a vector with all substitution parameters that are found in the template file ...