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>::iterator iter = lines_.begin();
242  std::vector<std::string> temp = file.returnLines();
243 
244  if (insert(atLine,temp)) return true;
245 
246  return false;
247 }
248 
249 
251 {
252  //empty
253  return true;
254 }
255 
256 
258 {
259  std::vector<std::string>::iterator iter = lines_.begin();
260  while( iter != lines_.end())
261  {
262  std::cout<<*iter<<std::endl;
263  iter++;
264  }
265 
266 }
267 
268 
269 std::vector<std::string> L1GtVhdlTemplateFile::returnLines()
270 {
271  return lines_;
272 }
273 
274 
276 {
277  std::cout<<"Enter parametermap"<<std::endl;
278 
279  std::map<std::string,std::string>::iterator iter = parameterMap_.begin();
280 
281  while( iter != parameterMap_.end())
282  {
283  std::cout<<(*iter).first<<": "<<(*iter).second<<std::endl;
284  iter++;;
285  }
286 }
287 
288 
289 std::map<std::string,std::string> L1GtVhdlTemplateFile::returnParameterMap()
290 {
291  return parameterMap_;
292 }
293 
294 
295 bool L1GtVhdlTemplateFile::extractParametersFromString(const std::string &str, std::vector<std::string> &parameters)
296 {
297  // check, weather the current line is containing a substitution parameter
298  // the routine is making sure, that it's not extracting a parameter from
299  // a comment
300  if (int pos1=str.find("$(")!=std::string::npos && str.substr(0,2)!="--")
301  {
302  int pos2=str.find(")");
303  // get the substituion parameter
304  std::string tempStr=(str.substr(pos1+1,(pos2-pos1-1)));
305  // return a pair with the substitution parameter and the
306  // the rest of the string after the substitution parameter
307 
308  // here a should be checked, weather the vector is already containing
309  // the parameter befor adding it.
310 
311  parameters.push_back(tempStr);
312  //recursive call
313  while (extractParametersFromString(str.substr(pos2), parameters)) extractParametersFromString(str.substr(pos2), parameters);
314 
315  return true;
316  }
317  else
318  {
319  return false;
320  }
321 
322  return true;
323 }
324 
325 
327 {
328  std::vector<std::string> temp;
329  std::vector<std::string>::iterator iter = lines_.begin();
330 
331  // loop until the substitution parameter is discovered the first time
332  while( iter != lines_.end() )
333  {
334  extractParametersFromString((*iter), temp);
335  iter++;
336  }
337 
338  return temp;
339 
340 }
341 
342 
343 void L1GtVhdlTemplateFile::append(const std::string &str)
344 {
345  lines_.push_back(str);
346 }
347 
348 
350 {
351  for (unsigned int i=0; i<file.lines_.size(); i++)
352  {
353  lines_.push_back(file.lines_.at(i));
354  }
355 }
356 
357 
359 {
360  bool success = false;
361 
362  std::vector<std::string>::iterator iter = lines_.begin();
363  while( iter != lines_.end())
364  {
365  size_t position;
366  position = (*iter).find(str);
367 
368  if (position != std::string::npos)
369  {
370  lines_.erase(iter);
371  success=true;
372  } else iter++;
373  }
374  return success;
375 }
376 
377 
379 {
380  std::vector<std::string>::iterator iter = lines_.begin();
381 
382  while( iter != lines_.end() )
383  {
384  if ((*iter)=="" || (*iter).length()==0 || (*iter)==" ") lines_.erase(iter); else
385  iter++;
386  }
387 
388  return true;
389 }
390 
391 
392 bool L1GtVhdlTemplateFile::isBlank(const char &chr)
393 {
394  if (chr==' ') return true;
395  return false;
396 
397 }
398 
399 
400 bool L1GtVhdlTemplateFile::split(const std::string &param, std::vector<std::string> &result)
401 {
402  unsigned int i = 0;
403  while (isBlank(param[i]))
404  {
405  i++;
406  }
407 
408  std::string temp = param.substr(i);
409  std::size_t pos = temp.find(" ");
410 
411  if (pos != std::string::npos)
412  {
413  std::string temp2 = temp.substr(0, pos);
414  result.push_back(temp2);
415  while (split(temp.substr(pos),result)) split(temp.substr(pos),result);
416 
417  } else if (!isBlank(temp[pos+1]))
418  {
419  result.push_back(temp);
420  return false;
421  } else
422  return false;
423 
424  return false;
425 }
426 
427 
428 void L1GtVhdlTemplateFile::getConditionsFromAlgo(std::string condString, std::vector<std::string> &result)
429 {
430  std::vector<std::string> operators;
431 
432  operators.push_back("AND");
433  operators.push_back("OR");
434  operators.push_back("NOT");
435  operators.push_back("(");
436  operators.push_back(")");
437 
438  for (unsigned int i =0; i<operators.size(); i++)
439  {
440  while (findAndReplaceString(condString, operators.at(i), "")) findAndReplaceString(condString, operators.at(i), "");
441  }
442 
443  split(condString,result);
444 
445 }
446 
447 
449 {
450  std::vector<std::string>::iterator iter = lines_.begin();
451  std::ostringstream buffer;
452 
453  while( iter != lines_.end() )
454  {
455  buffer<<(*iter)<<std::endl;
456  iter++;
457 
458  }
459 
460  return buffer.str();
461 }
462 
463 
464 std::string L1GtVhdlTemplateFile::getInternalParameter(const std::string &indentifier)
465 {
466  return parameterMap_[indentifier];
467 }
std::map< std::string, std::string > returnParameterMap()
returns parameter map
int i
Definition: DBlmapReader.cc:9
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...
list file
Definition: dbtoweb.py:253
~L1GtVhdlTemplateFile()
destructor
std::map< std::string, std::string > parameterMap_
containing the header information of internal files
static int position[TOTALCHAMBERS][3]
Definition: ReadPGInfo.cc:509
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
tuple filename
Definition: lut2db_cfg.py:20
bool extractParametersFromString(const std::string &str, std::vector< std::string > &parameters)
tuple cout
Definition: gather_cfg.py:41
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 ...