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