CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/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> temp = file.returnLines();
00242 
00243     if (insert(atLine,temp)) return true;
00244 
00245     return false;
00246 }
00247 
00248 
00249 bool L1GtVhdlTemplateFile::close()
00250 {
00251     //empty
00252     return true;
00253 }
00254 
00255 
00256 void L1GtVhdlTemplateFile::print()
00257 {
00258     std::vector<std::string>::iterator iter = lines_.begin();
00259     while( iter != lines_.end())
00260     {
00261         std::cout<<*iter<<std::endl;
00262         iter++;
00263     }
00264 
00265 }
00266 
00267 
00268 std::vector<std::string> L1GtVhdlTemplateFile::returnLines()
00269 {
00270     return lines_;
00271 }
00272 
00273 
00274 void L1GtVhdlTemplateFile::printParameterMap()
00275 {
00276     std::cout<<"Enter parametermap"<<std::endl;
00277 
00278     std::map<std::string,std::string>::iterator iter =  parameterMap_.begin();
00279 
00280     while( iter != parameterMap_.end())
00281     {
00282         std::cout<<(*iter).first<<": "<<(*iter).second<<std::endl;
00283         iter++;;
00284     }
00285 }
00286 
00287 
00288 std::map<std::string,std::string> L1GtVhdlTemplateFile::returnParameterMap()
00289 {
00290     return parameterMap_;
00291 }
00292 
00293 
00294 bool L1GtVhdlTemplateFile::extractParametersFromString(const std::string &str, std::vector<std::string> &parameters)
00295 {
00296     // check, weather the current line is containing a substitution parameter
00297     // the routine is making sure, that it's not extracting a parameter from
00298     // a comment
00299     if (int pos1=str.find("$(")!=std::string::npos && str.substr(0,2)!="--")
00300     {
00301         int pos2=str.find(")");
00302         // get the substituion parameter
00303         std::string tempStr=(str.substr(pos1+1,(pos2-pos1-1)));
00304         // return a pair with the substitution parameter and the
00305         // the rest of the string after the substitution parameter
00306 
00307         // here a should be checked, weather the vector is already containing
00308         // the parameter befor adding it.
00309 
00310         parameters.push_back(tempStr);
00311         //recursive call
00312         while (extractParametersFromString(str.substr(pos2), parameters)) extractParametersFromString(str.substr(pos2), parameters);
00313 
00314         return true;
00315     }
00316     else
00317     {
00318         return false;
00319     }
00320     
00321     return true;
00322 }
00323 
00324 
00325 std::vector<std::string> L1GtVhdlTemplateFile::getSubstitutionParametersFromTemplate()
00326 {
00327     std::vector<std::string> temp;
00328     std::vector<std::string>::iterator iter = lines_.begin();
00329 
00330     // loop until the substitution parameter is discovered the first time
00331     while( iter != lines_.end() )
00332     {
00333         extractParametersFromString((*iter), temp);
00334         iter++;
00335     }
00336 
00337     return temp;
00338 
00339 }
00340 
00341 
00342 void L1GtVhdlTemplateFile::append(const std::string &str)
00343 {
00344     lines_.push_back(str);
00345 }
00346 
00347 
00348 void L1GtVhdlTemplateFile::append(const L1GtVhdlTemplateFile& file)
00349 {
00350     for (unsigned int i=0; i<file.lines_.size(); i++)
00351     {
00352         lines_.push_back(file.lines_.at(i));
00353     }
00354 }
00355 
00356 
00357 bool L1GtVhdlTemplateFile::removeLineWithContent(const std::string &str)
00358 {
00359     bool success = false;
00360 
00361     std::vector<std::string>::iterator iter = lines_.begin();
00362     while( iter != lines_.end())
00363     {
00364         size_t position;
00365         position = (*iter).find(str);
00366 
00367         if (position != std::string::npos)
00368         {
00369             lines_.erase(iter);
00370             success=true;
00371         } else iter++;
00372     }
00373     return success;
00374 }
00375 
00376 
00377 bool L1GtVhdlTemplateFile::removeEmptyLines()
00378 {
00379     std::vector<std::string>::iterator iter = lines_.begin();
00380 
00381     while( iter != lines_.end() )
00382     {
00383         if ((*iter)=="" || (*iter).length()==0 || (*iter)=="    ") lines_.erase(iter); else
00384             iter++;
00385     }
00386 
00387     return true;
00388 }
00389 
00390 
00391 bool L1GtVhdlTemplateFile::isBlank(const char &chr)
00392 {
00393     if (chr==' ') return true;
00394     return false;
00395 
00396 }
00397 
00398 
00399 bool L1GtVhdlTemplateFile::split(const std::string &param, std::vector<std::string> &result)
00400 {
00401     unsigned int i = 0;
00402     while (isBlank(param[i]))
00403     {
00404         i++;
00405     }
00406 
00407     std::string temp = param.substr(i);
00408     std::size_t pos = temp.find(" ");
00409 
00410     if (pos != std::string::npos)
00411     {
00412         std::string temp2 = temp.substr(0, pos);
00413         result.push_back(temp2);
00414         while (split(temp.substr(pos),result)) split(temp.substr(pos),result);
00415 
00416     } else if (!isBlank(temp[pos+1]))
00417     {
00418         result.push_back(temp);
00419         return false;
00420     } else
00421     return false;
00422 
00423     return false;
00424 }
00425 
00426 
00427 void L1GtVhdlTemplateFile::getConditionsFromAlgo(std::string condString, std::vector<std::string> &result)
00428 {
00429     std::vector<std::string> operators;
00430 
00431     operators.push_back("AND");
00432     operators.push_back("OR");
00433     operators.push_back("NOT");
00434     operators.push_back("(");
00435     operators.push_back(")");
00436 
00437     for (unsigned int i =0; i<operators.size(); i++)
00438     {
00439         while (findAndReplaceString(condString, operators.at(i), "")) findAndReplaceString(condString, operators.at(i), "");
00440     }
00441 
00442     split(condString,result);
00443 
00444 }
00445 
00446 
00447 std::string L1GtVhdlTemplateFile::lines2String()
00448 {
00449     std::vector<std::string>::iterator iter = lines_.begin();
00450     std::ostringstream buffer;
00451 
00452     while( iter != lines_.end() )
00453     {
00454         buffer<<(*iter)<<std::endl;
00455         iter++;
00456 
00457     }
00458 
00459     return buffer.str();
00460 }
00461 
00462 
00463 std::string L1GtVhdlTemplateFile::getInternalParameter(const std::string &indentifier)
00464 {
00465     return parameterMap_[indentifier];
00466 }