Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:22:21

0001 /**
0002  * \class L1GtVhdlTemplateFile
0003  *
0004  *
0005  * Description: a class to deal with VHDL template files
0006  *
0007  * Implementation:
0008  *    <TODO: enter implementation details>
0009  *
0010  * \author: Philipp Wagner
0011  *
0012  *
0013  */
0014 
0015 // this class header
0016 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtVhdlTemplateFile.h"
0017 
0018 // system include files
0019 #include <iostream>
0020 #include <fstream>
0021 #include <sstream>
0022 #include <map>
0023 #include <string>
0024 #include <vector>
0025 
0026 // constructor(s)
0027 
0028 //standard constructor for a empty file
0029 L1GtVhdlTemplateFile::L1GtVhdlTemplateFile() { intern_ = false; }
0030 
0031 //constructor which already loads a file
0032 L1GtVhdlTemplateFile::L1GtVhdlTemplateFile(const std::string &filename) {
0033   if (!open(filename, false))
0034     std::cout << "Error while opening file: " << filename << std::endl;
0035 }
0036 
0037 const bool L1GtVhdlTemplateFile::findAndReplaceString(std::string &paramString,
0038                                                       const std::string &searchString,
0039                                                       const std::string &replaceString) {
0040   size_t position;
0041   position = paramString.find(searchString);
0042   if (position == std::string::npos)
0043     return false;
0044   paramString.replace(position, searchString.length(), replaceString);
0045   return true;
0046 }
0047 
0048 bool L1GtVhdlTemplateFile::open(const std::string &fileName, bool internal) {
0049   const char paramIndicator = '#';
0050   const char commentIndicator = '%';
0051   char buffer[2000];
0052   std::string stringBuffer;
0053 
0054   std::fstream inputFile(fileName.c_str(), std::ios::in);
0055   //check weather file has been opened successfully
0056   if (!inputFile.is_open())
0057     return false;
0058 
0059   //store content of the template in Vector lines
0060   while (!inputFile.eof()) {
0061     inputFile.getline(buffer, 2000);
0062     stringBuffer = buffer;
0063     //Remove DOS seperators (For example if the template file was created under NT)
0064     if (stringBuffer[stringBuffer.length() - 1] == 13) {
0065       stringBuffer.replace(stringBuffer.length() - 1, 1, "");
0066     }
0067     //the current buffer + a seperator to the vector lines
0068     lines_.push_back(stringBuffer /*+"\n"*/);
0069   }
0070 
0071   inputFile.close();
0072 
0073   if (internal) {
0074     //Delete lines containing parameters after moving them to parameterMap_
0075     std::vector<std::string>::iterator iter = lines_.begin();
0076     while (iter != lines_.end()) {
0077       while ((*iter)[0] == commentIndicator && (*iter)[1] == commentIndicator)
0078         lines_.erase(iter);
0079 
0080       if ((*iter)[0] == paramIndicator) {
0081         std::vector<std::string>::iterator iter2 = iter;
0082 
0083         // get the first line of content
0084         iter2++;
0085 
0086         while (iter2 != lines_.end()) {
0087           if ((*iter2)[0] == paramIndicator && (*iter2)[1] == paramIndicator) {
0088             iter2++;
0089             break;
0090           }
0091 
0092           parameterMap_[(*iter).substr(1)] += (*iter2);
0093 
0094           // overtake the newlines
0095           std::vector<std::string>::iterator tmpIter = iter2;
0096           tmpIter++;
0097 
0098           // check weather the next line is the end of the block
0099           if (!((*tmpIter)[0] == paramIndicator && (*tmpIter)[1] == paramIndicator))
0100             parameterMap_[(*iter).substr(1)] += "\n";
0101 
0102           iter2++;
0103         }
0104 
0105         // there has been a syntax error in the internal template
0106         // stop the routine
0107         if (iter2 == lines_.end())
0108           return false;
0109 
0110         // deletes the content, thas has been added to parameter map before
0111         // (iter one at the moment is at the beginnig of the block, iter2 at its end)
0112         lines_.erase(iter, iter2);
0113       }
0114 
0115       // just for security
0116       if (iter != lines_.end())
0117         iter++;
0118     }
0119 
0120     //remove empty lines
0121     iter = lines_.begin();
0122     while (iter != lines_.end()) {
0123       if ((*iter).empty() || (*iter).length() == 0 || (*iter) == "    ")
0124         lines_.erase(iter);
0125       else
0126         iter++;
0127     }
0128   }
0129 
0130   return true;
0131 }
0132 
0133 bool L1GtVhdlTemplateFile::save(const std::string &fileName) {
0134   std::ofstream outputFile(fileName.c_str());
0135   std::vector<std::string>::iterator iter = lines_.begin();
0136 
0137   //Write content of lines_ into the outputfile.
0138   while (iter != lines_.end()) {
0139     //std::cout<<"Last sign: "<<*iter[(*iter).length()-3];
0140     outputFile << *iter << std::endl;
0141     iter++;
0142   }
0143 
0144   outputFile.close();
0145 
0146   return true;
0147 }
0148 
0149 bool L1GtVhdlTemplateFile::substitute(const std::string &searchString, const std::string &replaceString) {
0150   bool success = false;
0151 
0152   std::vector<std::string>::iterator iter = lines_.begin();
0153   while (iter != lines_.end()) {
0154     //The substitution parameter always appears as follows: $(parameter)
0155     while (findAndReplaceString(*iter, ("$(" + searchString + ")"), replaceString)) {
0156       findAndReplaceString(*iter, ("$(" + searchString + ")"), replaceString);
0157       success = true;
0158     }
0159     iter++;
0160   }
0161 
0162   return success;
0163 }
0164 
0165 bool L1GtVhdlTemplateFile::insert(const std::string &atLine, const std::vector<std::string> &content) {
0166   bool success = false;
0167   std::vector<std::string>::iterator iter = lines_.begin();
0168 
0169   //Loop until the substitution parameter is discovered the first time
0170   while (iter != lines_.end()) {
0171     //check, weather the current line is containing the substitution parameter
0172     if ((*iter).find(atLine) != std::string::npos) {
0173       //Delete the line with the subsitution parameter
0174       iter = lines_.erase(iter);
0175       //insert the content of file
0176       lines_.insert(iter, content.begin(), content.end());
0177 
0178       success = true;
0179       break;
0180     }
0181 
0182     iter++;
0183   }
0184 
0185   return success;
0186 }
0187 
0188 bool L1GtVhdlTemplateFile::insert(const std::string atLine, const L1GtVhdlTemplateFile &_file) {
0189   std::vector<std::string> temp = _file.returnLines();
0190 
0191   if (insert(atLine, temp))
0192     return true;
0193 
0194   return false;
0195 }
0196 
0197 bool L1GtVhdlTemplateFile::close() {
0198   //empty
0199   return true;
0200 }
0201 
0202 void L1GtVhdlTemplateFile::print() const {
0203   std::vector<std::string>::const_iterator iter = lines_.begin();
0204   while (iter != lines_.end()) {
0205     std::cout << *iter << std::endl;
0206     iter++;
0207   }
0208 }
0209 
0210 std::vector<std::string> L1GtVhdlTemplateFile::returnLines() const { return lines_; }
0211 
0212 void L1GtVhdlTemplateFile::printParameterMap() const {
0213   std::cout << "Enter parametermap" << std::endl;
0214 
0215   std::map<std::string, std::string>::const_iterator iter = parameterMap_.begin();
0216 
0217   while (iter != parameterMap_.end()) {
0218     std::cout << (*iter).first << ": " << (*iter).second << std::endl;
0219     iter++;
0220     ;
0221   }
0222 }
0223 
0224 std::map<std::string, std::string> L1GtVhdlTemplateFile::returnParameterMap() const { return parameterMap_; }
0225 
0226 bool L1GtVhdlTemplateFile::extractParametersFromString(const std::string &str,
0227                                                        std::vector<std::string> &parameters) const {
0228   // check, weather the current line is containing a substitution parameter
0229   // the routine is making sure, that it's not extracting a parameter from
0230   // a comment
0231   if (int pos1 = str.find("$(") != std::string::npos && str.substr(0, 2) != "--") {
0232     int pos2 = str.find(')');
0233     // get the substituion parameter
0234     std::string tempStr = (str.substr(pos1 + 1, (pos2 - pos1 - 1)));
0235     // return a pair with the substitution parameter and the
0236     // the rest of the string after the substitution parameter
0237 
0238     // here a should be checked, weather the vector is already containing
0239     // the parameter befor adding it.
0240 
0241     parameters.push_back(tempStr);
0242     //recursive call
0243     while (extractParametersFromString(str.substr(pos2), parameters))
0244       extractParametersFromString(str.substr(pos2), parameters);
0245 
0246     return true;
0247   } else {
0248     return false;
0249   }
0250 
0251   return true;
0252 }
0253 
0254 std::vector<std::string> L1GtVhdlTemplateFile::getSubstitutionParametersFromTemplate() const {
0255   std::vector<std::string> temp;
0256   std::vector<std::string>::const_iterator iter = lines_.begin();
0257 
0258   // loop until the substitution parameter is discovered the first time
0259   while (iter != lines_.end()) {
0260     extractParametersFromString((*iter), temp);
0261     iter++;
0262   }
0263 
0264   return temp;
0265 }
0266 
0267 void L1GtVhdlTemplateFile::append(const std::string &str) { lines_.push_back(str); }
0268 
0269 void L1GtVhdlTemplateFile::append(const L1GtVhdlTemplateFile &file) {
0270   for (unsigned int i = 0; i < file.lines_.size(); i++) {
0271     lines_.push_back(file.lines_.at(i));
0272   }
0273 }
0274 
0275 bool L1GtVhdlTemplateFile::removeLineWithContent(const std::string &str) {
0276   bool success = false;
0277 
0278   std::vector<std::string>::iterator iter = lines_.begin();
0279   while (iter != lines_.end()) {
0280     size_t position;
0281     position = (*iter).find(str);
0282 
0283     if (position != std::string::npos) {
0284       lines_.erase(iter);
0285       success = true;
0286     } else
0287       iter++;
0288   }
0289   return success;
0290 }
0291 
0292 bool L1GtVhdlTemplateFile::removeEmptyLines() {
0293   std::vector<std::string>::iterator iter = lines_.begin();
0294 
0295   while (iter != lines_.end()) {
0296     if ((*iter).empty() || (*iter).length() == 0 || (*iter) == "    ")
0297       lines_.erase(iter);
0298     else
0299       iter++;
0300   }
0301 
0302   return true;
0303 }
0304 
0305 bool L1GtVhdlTemplateFile::isBlank(const char &chr) const {
0306   if (chr == ' ')
0307     return true;
0308   return false;
0309 }
0310 
0311 bool L1GtVhdlTemplateFile::split(const std::string &param, std::vector<std::string> &result) const {
0312   unsigned int i = 0;
0313   while (isBlank(param[i])) {
0314     i++;
0315   }
0316 
0317   std::string temp = param.substr(i);
0318   std::size_t pos = temp.find(' ');
0319 
0320   if (pos != std::string::npos) {
0321     std::string temp2 = temp.substr(0, pos);
0322     result.push_back(temp2);
0323     while (split(temp.substr(pos), result))
0324       split(temp.substr(pos), result);
0325 
0326   } else if (!isBlank(temp[pos + 1])) {
0327     result.push_back(temp);
0328     return false;
0329   } else
0330     return false;
0331 
0332   return false;
0333 }
0334 
0335 void L1GtVhdlTemplateFile::getConditionsFromAlgo(std::string condString, std::vector<std::string> &result) const {
0336   std::vector<std::string> operators;
0337 
0338   operators.push_back("AND");
0339   operators.push_back("OR");
0340   operators.push_back("NOT");
0341   operators.push_back("(");
0342   operators.push_back(")");
0343 
0344   for (unsigned int i = 0; i < operators.size(); i++) {
0345     while (findAndReplaceString(condString, operators.at(i), ""))
0346       findAndReplaceString(condString, operators.at(i), "");
0347   }
0348 
0349   split(condString, result);
0350 }
0351 
0352 std::string L1GtVhdlTemplateFile::lines2String() const {
0353   std::vector<std::string>::const_iterator iter = lines_.begin();
0354   std::ostringstream buffer;
0355 
0356   while (iter != lines_.end()) {
0357     buffer << (*iter) << std::endl;
0358     iter++;
0359   }
0360 
0361   return buffer.str();
0362 }
0363 
0364 std::string L1GtVhdlTemplateFile::getInternalParameter(const std::string &indentifier) {
0365   return parameterMap_[indentifier];
0366 }