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