CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/RecoLuminosity/ROOTSchema/src/FileToolKit.cc

Go to the documentation of this file.
00001 #include "RecoLuminosity/ROOTSchema/interface/FileToolKit.h"
00002 
00003 // STL Headers
00004 #include <fstream>
00005 
00006 // Linux
00007 #include <sys/types.h>
00008 #include <sys/stat.h>
00009 #include <unistd.h>
00010 
00011 // C
00012 #include <cerrno>
00013 #include <cstdio>
00014 #include <cstring>
00015 
00016 void FileToolKit::Tokenize(const std::string& str,
00017                            std::vector< std::string >& tokens,
00018                            const std::string& delimiters )
00019 {
00020   using std::string;
00021   
00022   // Skip delimiters at beginning.
00023   string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00024   // Find first "non-delimiter".
00025   string::size_type pos     = str.find_first_of(delimiters, lastPos);
00026 
00027   while (string::npos != pos || string::npos != lastPos)
00028     {
00029       // Found a token, add it to the vector.
00030       tokens.push_back(str.substr(lastPos, pos - lastPos));
00031       // Skip delimiters.  Note the "not_of"
00032       lastPos = str.find_first_not_of(delimiters, pos);
00033       // Find next "non-delimiter"
00034       pos = str.find_first_of(delimiters, lastPos);
00035     }
00036 }
00037 
00038 int FileToolKit::MakeDir( std::string dirName, mode_t writeMode ){
00039   
00040   using std::vector;
00041   using std::string;
00042 
00043   int result;
00044   int errsv = 0;
00045   
00046   vector< string > tkDirName;
00047   string currentDirName = "";
00048 
00049   Tokenize(dirName, tkDirName, "/");
00050 
00051   if(dirName[0] == '/')  currentDirName += "/";
00052  
00053   struct stat mStat;
00054  
00055   for(unsigned int i = 0; i < tkDirName.size(); ++i ){
00056 
00057     currentDirName += tkDirName[i];
00058     currentDirName += "/";
00059 
00060     errno = 0;
00061     stat( currentDirName.c_str(), &mStat );
00062     errsv = errno;
00063     
00064     if( errsv == 2 ){  // No such file or directory 
00065       
00066       errno = 0;
00067       result = mkdir( currentDirName.c_str(), writeMode);
00068       errsv = errno;
00069       if( errno == 0 ){
00070         errno = 0;
00071         result = chmod( currentDirName.c_str(), writeMode);
00072         errsv = errno;
00073       }
00074     }
00075   }
00076   return errsv;
00077 }
00078 
00079 void FileToolKit::MakeEmptyWebPage( const std::string &fileName, 
00080                                     const std::string &title ){
00081 
00082   std::fstream fileStream;
00083 
00084   fileStream.open( fileName.c_str(), std::fstream::out);
00085 
00086   fileStream << "<html>"   << std::endl;
00087   fileStream << "<title>"  << std::endl;
00088   fileStream << title      << std::endl;
00089   fileStream << "</title>" << std::endl; 
00090   fileStream << "<body>"   << std::endl;
00091   fileStream << "</body>"  << std::endl;
00092   fileStream << "</html>"  << std::endl; 
00093 
00094   fileStream.close();
00095 }
00096 
00097 void FileToolKit::InsertLineAfter( const std::string &fileName, 
00098                                    const std::string &newLine,
00099                                    const std::string &searchLine){
00100 
00101   bool bMatch = false;
00102 
00103   std::vector< std::string > fileContents;
00104   char lineBuffer[256];
00105 
00106   std::fstream fileStream;
00107 
00108   // Read file into memory and insert new line.
00109   fileStream.open(fileName.c_str(), std::fstream::in );
00110   while( fileStream.good() ){
00111     fileStream.getline( lineBuffer, 256 );
00112 
00113     fileContents.push_back( lineBuffer );
00114 
00115     if( strcmp( lineBuffer, searchLine.c_str() ) == 0 ){
00116       fileContents.push_back( newLine );
00117       bMatch = true;
00118     }
00119   }
00120   fileStream.close();
00121 
00122   // If search line was found, write file from buffer.
00123   if(bMatch){
00124     std::fstream fileStream2;
00125 
00126     fileStream2.open( fileName.c_str(), std::fstream::out );
00127 
00128     for(unsigned int iline = 0; iline < fileContents.size(); ++iline)
00129       fileStream2 << fileContents[iline] << std::endl;
00130     
00131     fileStream2.close();  
00132 
00133     //rename( ( fileName  ).c_str(), fileName.c_str() );
00134   }
00135 }
00136 
00137 void FileToolKit::InsertLineBefore( const std::string &fileName, 
00138                                     const std::string &newLine,
00139                                     const std::string &searchLine ){
00140 
00141   bool bMatch = false;
00142 
00143   std::vector< std::string > fileContents;
00144   char lineBuffer[256];
00145 
00146   std::fstream fileStream;
00147 
00148   fileStream.open(fileName.c_str());
00149   while( fileStream.good() ){
00150     fileStream.getline( lineBuffer, 256 );
00151     if(strcmp(lineBuffer, searchLine.c_str()) == 0){
00152       fileContents.push_back( newLine );
00153       bMatch = true;
00154     }
00155     fileContents.push_back( lineBuffer );
00156   }
00157   fileStream.close();
00158 
00159   if(bMatch){
00160     std::fstream fileStream2;
00161 
00162     fileStream2.open( fileName.c_str(), std::fstream::out  );
00163 
00164     for(unsigned int iline = 0; iline < fileContents.size(); ++iline){
00165       fileStream2 << fileContents[iline] << std::endl;
00166     }
00167     
00168     fileStream2.close();  
00169 
00170     //rename( (fileName ).c_str(), fileName.c_str());
00171   }
00172 }
00173 
00174 bool FileToolKit::fileExists( const std::string &fileName){
00175   
00176   std::fstream filestr;
00177 
00178   filestr.open (fileName.c_str(), std::fstream::in );
00179   if (filestr.is_open()){
00180     filestr.close();
00181     return true;
00182   }
00183   else
00184   {
00185     return false;
00186   }
00187 }