CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/L1TriggerConfig/L1GtConfigProducers/src/L1GtVhdlTemplateFile.cc

Go to the documentation of this file.
00001 
00017 // this class header
00018 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtVhdlTemplateFile.h"
00019 
00020 // system include files
00021 #include <iostream>
00022 #include <fstream>
00023 #include <sstream>
00024 #include <map>
00025 #include <string>
00026 #include <vector>
00027 
00028 // constructor(s)
00029 
00030 //standard constructor for a empty file
00031 L1GtVhdlTemplateFile::L1GtVhdlTemplateFile()
00032 {
00033     intern_=false;
00034 }
00035 
00036 
00037 //constructor which already loads a file
00038 L1GtVhdlTemplateFile::L1GtVhdlTemplateFile(const std::string &filename)
00039 {
00040     if (!open(filename,false)) std::cout<<"Error while opening file: "<<filename<<std::endl;
00041 }
00042 
00043 
00044 //copy constructor
00045 L1GtVhdlTemplateFile::L1GtVhdlTemplateFile(const L1GtVhdlTemplateFile& rhs)
00046 {
00047     lines_=rhs.lines_;
00048     intern_=rhs.intern_;
00049     parameterMap_=rhs.parameterMap_;
00050 
00051 }
00052 
00053 
00054 // destructor
00055 L1GtVhdlTemplateFile::~L1GtVhdlTemplateFile()
00056 {
00057     // empty
00058 }
00059 
00060 
00061 const bool L1GtVhdlTemplateFile::findAndReplaceString(std::string &paramString, const std::string &searchString, const std::string &replaceString)
00062 {
00063     size_t position;
00064     position = paramString.find(searchString);
00065     if (position == std::string::npos) return false;
00066     paramString.replace(position,searchString.length(),replaceString);
00067     return true;
00068 }
00069 
00070 
00071 bool L1GtVhdlTemplateFile::open(const std::string &fileName, bool internal)
00072 {
00073 
00074     const char paramIndicator='#';
00075     const char commentIndicator='%';
00076     char buffer[2000];
00077     std::string stringBuffer;
00078 
00079     std::fstream inputFile(fileName.c_str(),std::ios::in);
00080     //check weather file has been opened successfully
00081     if(!inputFile.is_open()) return false;
00082 
00083     //store content of the template in Vector lines
00084     while(!inputFile.eof())
00085     {
00086         inputFile.getline(buffer,2000);
00087         stringBuffer=buffer;
00088         //Remove DOS seperators (For example if the template file was created under NT)
00089         if (stringBuffer[stringBuffer.length()-1]==13)
00090         {
00091             stringBuffer.replace(stringBuffer.length()-1,1,"");
00092         }
00093         //the current buffer + a seperator to the vector lines
00094         lines_.push_back(stringBuffer/*+"\n"*/);
00095     }
00096 
00097     inputFile.close();
00098 
00099     if (internal)
00100     {
00101         
00102         //Delete lines containing parameters after moving them to parameterMap_
00103         std::vector<std::string>::iterator iter = lines_.begin();
00104         while( iter != lines_.end() )
00105         {
00106             while ((*iter)[0]==commentIndicator && (*iter)[1]==commentIndicator)
00107                 lines_.erase(iter);
00108             
00109             if ((*iter)[0]==paramIndicator)
00110             {
00111                 std::vector<std::string>::iterator iter2 = iter;
00112                 
00113                 // get the first line of content
00114                 iter2++;
00115                 
00116                 while (iter2!=lines_.end())
00117                 {
00118                     if ((*iter2)[0]==paramIndicator && (*iter2)[1]==paramIndicator)
00119                     {
00120                         iter2++;
00121                         break;
00122                     }
00123                     
00124                    
00125                     
00126                     parameterMap_[(*iter).substr(1)]+=(*iter2);
00127                     
00128                     // overtake the newlines
00129                     std::vector<std::string>::iterator tmpIter= iter2;
00130                     tmpIter++;
00131                    
00132                     // check weather the next line is the end of the block
00133                     if (!((*tmpIter)[0]==paramIndicator && (*tmpIter)[1]==paramIndicator))
00134                         parameterMap_[(*iter).substr(1)]+="\n";
00135                     
00136                    
00137                     iter2++;
00138                 }
00139                 
00140                 // there has been a syntax error in the internal template
00141                 // stop the routine
00142                 if (iter2==lines_.end()) 
00143                     return false;
00144  
00145                 // deletes the content, thas has been added to parameter map before
00146                 // (iter one at the moment is at the beginnig of the block, iter2 at its end)
00147                 lines_.erase(iter,iter2 );
00148                 
00149             }
00150             
00151             // just for security
00152             if (iter!=lines_.end()) iter++;
00153         }
00154 
00155         //remove empty lines
00156         iter = lines_.begin();
00157         while( iter != lines_.end() )
00158         {
00159             if ((*iter)=="" || (*iter).length()==0 || (*iter)=="    ") lines_.erase(iter); else
00160                 iter++;
00161         }
00162 
00163     }
00164 
00165     return true;
00166 
00167 }
00168 
00169 
00170 bool L1GtVhdlTemplateFile::save(const std::string &fileName)
00171 {
00172     std::ofstream outputFile(fileName.c_str());
00173     std::vector<std::string>::iterator iter = lines_.begin();
00174 
00175     //Write content of lines_ into the outputfile.
00176     while( iter != lines_.end() )
00177     {
00178         //std::cout<<"Last sign: "<<*iter[(*iter).length()-3];
00179         outputFile << *iter<<std::endl;
00180         iter++;
00181     }
00182 
00183     outputFile.close();
00184    
00185     return true;
00186 
00187 }
00188 
00189 
00190 bool L1GtVhdlTemplateFile::substitute(const std::string &searchString, const std::string &replaceString)
00191 {
00192 
00193     bool success = false;
00194 
00195     std::vector<std::string>::iterator iter = lines_.begin();
00196     while( iter != lines_.end())
00197     {
00198         //The substitution parameter always appears as follows: $(parameter)
00199         while (findAndReplaceString(*iter,("$("+searchString+")"), replaceString))
00200         {
00201             findAndReplaceString(*iter,("$("+searchString+")"), replaceString);
00202             success = true;
00203         }
00204         iter++;
00205     }
00206 
00207     return success;
00208 
00209 }
00210 
00211 
00212 bool L1GtVhdlTemplateFile::insert(const std::string &atLine, std::vector<std::string> content)
00213 {
00214     bool success = false;
00215     std::vector<std::string>::iterator iter = lines_.begin();
00216 
00217     //Loop until the substitution parameter is discovered the first time
00218     while( iter != lines_.end() )
00219     {
00220         //check, weather the current line is containing the substitution parameter
00221         if ((*iter).find(atLine)!=std::string::npos)
00222         {
00223             //Delete the line with the subsitution parameter
00224             iter = lines_.erase(iter);
00225             //insert the content of file
00226             lines_.insert(iter,content.begin(),content.end());
00227 
00228             success=true;
00229             break;
00230         }
00231 
00232         iter++;
00233     }
00234 
00235     return success;
00236 }
00237 
00238 
00239 bool L1GtVhdlTemplateFile::insert(const std::string atLine, L1GtVhdlTemplateFile file)
00240 {
00241     std::vector<std::string>::iterator iter = lines_.begin();
00242     std::vector<std::string> temp = file.returnLines();
00243 
00244     if (insert(atLine,temp)) return true;
00245 
00246     return false;
00247 }
00248 
00249 
00250 bool L1GtVhdlTemplateFile::close()
00251 {
00252     //empty
00253     return true;
00254 }
00255 
00256 
00257 void L1GtVhdlTemplateFile::print()
00258 {
00259     std::vector<std::string>::iterator iter = lines_.begin();
00260     while( iter != lines_.end())
00261     {
00262         std::cout<<*iter<<std::endl;
00263         iter++;
00264     }
00265 
00266 }
00267 
00268 
00269 std::vector<std::string> L1GtVhdlTemplateFile::returnLines()
00270 {
00271     return lines_;
00272 }
00273 
00274 
00275 void L1GtVhdlTemplateFile::printParameterMap()
00276 {
00277     std::cout<<"Enter parametermap"<<std::endl;
00278 
00279     std::map<std::string,std::string>::iterator iter =  parameterMap_.begin();
00280 
00281     while( iter != parameterMap_.end())
00282     {
00283         std::cout<<(*iter).first<<": "<<(*iter).second<<std::endl;
00284         iter++;;
00285     }
00286 }
00287 
00288 
00289 std::map<std::string,std::string> L1GtVhdlTemplateFile::returnParameterMap()
00290 {
00291     return parameterMap_;
00292 }
00293 
00294 
00295 bool L1GtVhdlTemplateFile::extractParametersFromString(const std::string &str, std::vector<std::string> &parameters)
00296 {
00297     // check, weather the current line is containing a substitution parameter
00298     // the routine is making sure, that it's not extracting a parameter from
00299     // a comment
00300     if (int pos1=str.find("$(")!=std::string::npos && str.substr(0,2)!="--")
00301     {
00302         int pos2=str.find(")");
00303         // get the substituion parameter
00304         std::string tempStr=(str.substr(pos1+1,(pos2-pos1-1)));
00305         // return a pair with the substitution parameter and the
00306         // the rest of the string after the substitution parameter
00307 
00308         // here a should be checked, weather the vector is already containing
00309         // the parameter befor adding it.
00310 
00311         parameters.push_back(tempStr);
00312         //recursive call
00313         while (extractParametersFromString(str.substr(pos2), parameters)) extractParametersFromString(str.substr(pos2), parameters);
00314 
00315         return true;
00316     }
00317     else
00318     {
00319         return false;
00320     }
00321     
00322     return true;
00323 }
00324 
00325 
00326 std::vector<std::string> L1GtVhdlTemplateFile::getSubstitutionParametersFromTemplate()
00327 {
00328     std::vector<std::string> temp;
00329     std::vector<std::string>::iterator iter = lines_.begin();
00330 
00331     // loop until the substitution parameter is discovered the first time
00332     while( iter != lines_.end() )
00333     {
00334         extractParametersFromString((*iter), temp);
00335         iter++;
00336     }
00337 
00338     return temp;
00339 
00340 }
00341 
00342 
00343 void L1GtVhdlTemplateFile::append(const std::string &str)
00344 {
00345     lines_.push_back(str);
00346 }
00347 
00348 
00349 void L1GtVhdlTemplateFile::append(const L1GtVhdlTemplateFile& file)
00350 {
00351     for (unsigned int i=0; i<file.lines_.size(); i++)
00352     {
00353         lines_.push_back(file.lines_.at(i));
00354     }
00355 }
00356 
00357 
00358 bool L1GtVhdlTemplateFile::removeLineWithContent(const std::string &str)
00359 {
00360     bool success = false;
00361 
00362     std::vector<std::string>::iterator iter = lines_.begin();
00363     while( iter != lines_.end())
00364     {
00365         size_t position;
00366         position = (*iter).find(str);
00367 
00368         if (position != std::string::npos)
00369         {
00370             lines_.erase(iter);
00371             success=true;
00372         } else iter++;
00373     }
00374     return success;
00375 }
00376 
00377 
00378 bool L1GtVhdlTemplateFile::removeEmptyLines()
00379 {
00380     std::vector<std::string>::iterator iter = lines_.begin();
00381 
00382     while( iter != lines_.end() )
00383     {
00384         if ((*iter)=="" || (*iter).length()==0 || (*iter)=="    ") lines_.erase(iter); else
00385             iter++;
00386     }
00387 
00388     return true;
00389 }
00390 
00391 
00392 bool L1GtVhdlTemplateFile::isBlank(const char &chr)
00393 {
00394     if (chr==' ') return true;
00395     return false;
00396 
00397 }
00398 
00399 
00400 bool L1GtVhdlTemplateFile::split(const std::string &param, std::vector<std::string> &result)
00401 {
00402     unsigned int i = 0;
00403     while (isBlank(param[i]))
00404     {
00405         i++;
00406     }
00407 
00408     std::string temp = param.substr(i);
00409     std::size_t pos = temp.find(" ");
00410 
00411     if (pos != std::string::npos)
00412     {
00413         std::string temp2 = temp.substr(0, pos);
00414         result.push_back(temp2);
00415         while (split(temp.substr(pos),result)) split(temp.substr(pos),result);
00416 
00417     } else if (!isBlank(temp[pos+1]))
00418     {
00419         result.push_back(temp);
00420         return false;
00421     } else
00422     return false;
00423 
00424     return false;
00425 }
00426 
00427 
00428 void L1GtVhdlTemplateFile::getConditionsFromAlgo(std::string condString, std::vector<std::string> &result)
00429 {
00430     std::vector<std::string> operators;
00431 
00432     operators.push_back("AND");
00433     operators.push_back("OR");
00434     operators.push_back("NOT");
00435     operators.push_back("(");
00436     operators.push_back(")");
00437 
00438     for (unsigned int i =0; i<operators.size(); i++)
00439     {
00440         while (findAndReplaceString(condString, operators.at(i), "")) findAndReplaceString(condString, operators.at(i), "");
00441     }
00442 
00443     split(condString,result);
00444 
00445 }
00446 
00447 
00448 std::string L1GtVhdlTemplateFile::lines2String()
00449 {
00450     std::vector<std::string>::iterator iter = lines_.begin();
00451     std::ostringstream buffer;
00452 
00453     while( iter != lines_.end() )
00454     {
00455         buffer<<(*iter)<<std::endl;
00456         iter++;
00457 
00458     }
00459 
00460     return buffer.str();
00461 }
00462 
00463 
00464 std::string L1GtVhdlTemplateFile::getInternalParameter(const std::string &indentifier)
00465 {
00466     return parameterMap_[indentifier];
00467 }