CMS 3D CMS Logo

FileToolKit Class Reference

#include <RecoLuminosity/ROOTSchema/interface/FileToolKit.h>

Inheritance diagram for FileToolKit:

HCAL_HLX::HTMLGenerator

List of all members.

Public Member Functions

bool fileExists (const std::string &fileName)
 FileToolKit ()
void InsertLineAfter (const std::string &fileName, const std::string &newLine, const std::string &searchLine)
void InsertLineBefore (const std::string &fileName, const std::string &newLine, const std::string &searchLine)
int MakeDir (std::string dirName, mode_t writeMode)
void MakeEmptyWebPage (const std::string &fileName, const std::string &title)
void Tokenize (const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
 ~FileToolKit ()


Detailed Description

Definition at line 7 of file FileToolKit.h.


Constructor & Destructor Documentation

FileToolKit::FileToolKit (  )  [inline]

Definition at line 10 of file FileToolKit.h.

00010 {}

FileToolKit::~FileToolKit (  )  [inline]

Definition at line 11 of file FileToolKit.h.

00011 {}


Member Function Documentation

bool FileToolKit::fileExists ( const std::string &  fileName  ) 

Definition at line 182 of file FileToolKit.cc.

References in.

Referenced by HCAL_HLX::HTMLGenerator::GenerateIndexPage(), and HCAL_HLX::HTMLGenerator::GenerateRunPage().

00182                                                       {
00183   
00184   using std::fstream;
00185 
00186   fstream filestr;
00187 
00188   filestr.open (fileName.c_str(), fstream::in );
00189   if (filestr.is_open()){
00190     filestr.close();
00191     return true;
00192   }
00193   else
00194   {
00195     return false;
00196   }
00197 }

void FileToolKit::InsertLineAfter ( const std::string &  fileName,
const std::string &  newLine,
const std::string &  searchLine 
)

Definition at line 100 of file FileToolKit.cc.

References lat::endl(), in, and out.

Referenced by HCAL_HLX::HTMLGenerator::GenerateIndexPage().

00102                                               {
00103 
00104   using std::endl;
00105   using std::fstream;
00106   bool bMatch = false;
00107 
00108   std::vector< std::string > fileContents;
00109   char lineBuffer[256];
00110 
00111   fstream fileStream;
00112 
00113   // Read file into memory and insert new line.
00114   fileStream.open(fileName.c_str(), fstream::in );
00115   while( fileStream.good() ){
00116     fileStream.getline( lineBuffer, 256 );
00117 
00118     fileContents.push_back( lineBuffer );
00119 
00120     if( strcmp( lineBuffer, searchLine.c_str() ) == 0 ){
00121       fileContents.push_back( newLine );
00122       bMatch = true;
00123     }
00124   }
00125   fileStream.close();
00126 
00127   // If search line was found, write file from buffer.
00128   if(bMatch){
00129     fstream fileStream2;
00130 
00131     fileStream2.open( fileName.c_str(), fstream::out );
00132 
00133     for(unsigned int iline = 0; iline < fileContents.size(); ++iline)
00134       fileStream2 << fileContents[iline] << endl;
00135     
00136     fileStream2.close();  
00137 
00138     //rename( ( fileName  ).c_str(), fileName.c_str() );
00139   }
00140 }

void FileToolKit::InsertLineBefore ( const std::string &  fileName,
const std::string &  newLine,
const std::string &  searchLine 
)

Definition at line 142 of file FileToolKit.cc.

References lat::endl(), and out.

Referenced by HCAL_HLX::HTMLGenerator::GenerateIndexPage(), and HCAL_HLX::HTMLGenerator::GenerateRunPage().

00144                                                      {
00145 
00146 
00147   using std::endl;
00148   using std::fstream;
00149   bool bMatch = false;
00150 
00151   std::vector< std::string > fileContents;
00152   char lineBuffer[256];
00153 
00154   fstream fileStream;
00155 
00156   fileStream.open(fileName.c_str());
00157   while( fileStream.good() ){
00158     fileStream.getline( lineBuffer, 256 );
00159     if(strcmp(lineBuffer, searchLine.c_str()) == 0){
00160       fileContents.push_back( newLine );
00161       bMatch = true;
00162     }
00163     fileContents.push_back( lineBuffer );
00164   }
00165   fileStream.close();
00166 
00167   if(bMatch){
00168     fstream fileStream2;
00169 
00170     fileStream2.open( fileName.c_str(), fstream::out  );
00171 
00172     for(unsigned int iline = 0; iline < fileContents.size(); ++iline){
00173       fileStream2 << fileContents[iline] << endl;
00174     }
00175     
00176     fileStream2.close();  
00177 
00178     //rename( (fileName ).c_str(), fileName.c_str());
00179   }
00180 }

int FileToolKit::MakeDir ( std::string  dirName,
mode_t  writeMode 
)

Definition at line 40 of file FileToolKit.cc.

References i, HLT_VtxMuL3::result, and Tokenize().

Referenced by HCAL_HLX::HTMLGenerator::CreateWebPage().

00040                                                              {
00041   
00042   using std::vector;
00043   using std::string;
00044 
00045   int result;
00046   int errsv = 0;
00047   
00048   vector< string > tkDirName;
00049   string currentDirName = "";
00050 
00051   Tokenize(dirName, tkDirName, "/");
00052 
00053   if(dirName[0] == '/')  currentDirName += "/";
00054  
00055   struct stat mStat;
00056  
00057   for(unsigned int i = 0; i < tkDirName.size(); ++i ){
00058 
00059     currentDirName += tkDirName[i];
00060     currentDirName += "/";
00061 
00062     errno = 0;
00063     stat( currentDirName.c_str(), &mStat );
00064     errsv = errno;
00065     
00066     if( errsv == 2 ){  // No such file or directory 
00067       
00068       errno = 0;
00069       result = mkdir( currentDirName.c_str(), writeMode);
00070       errsv = errno;
00071       if( errno == 0 ){
00072         errno = 0;
00073         result = chmod( currentDirName.c_str(), writeMode);
00074         errsv = errno;
00075       }
00076     }
00077   }
00078   return errsv;
00079 }

void FileToolKit::MakeEmptyWebPage ( const std::string &  fileName,
const std::string &  title 
)

Definition at line 81 of file FileToolKit.cc.

References lat::endl(), and out.

Referenced by HCAL_HLX::HTMLGenerator::GenerateIndexPage(), and HCAL_HLX::HTMLGenerator::GenerateRunPage().

00082                                                             {
00083 
00084   std::fstream fileStream;
00085 
00086   fileStream.open( fileName.c_str(), std::fstream::out);
00087 
00088   fileStream << "<html>"   << std::endl;
00089   fileStream << "<title>"  << std::endl;
00090   fileStream << title      << std::endl;
00091   fileStream << "</title>" << std::endl; 
00092   fileStream << "<body>"   << std::endl;
00093   fileStream << "</body>"  << std::endl;
00094   fileStream << "</html>"  << std::endl; 
00095 
00096   fileStream.close();
00097 
00098 }

void FileToolKit::Tokenize ( const std::string &  str,
std::vector< std::string > &  tokens,
const std::string &  delimiters = " " 
)

Definition at line 18 of file FileToolKit.cc.

Referenced by MakeDir().

00021 {
00022   using std::string;
00023   
00024   // Skip delimiters at beginning.
00025   string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00026   // Find first "non-delimiter".
00027   string::size_type pos     = str.find_first_of(delimiters, lastPos);
00028 
00029   while (string::npos != pos || string::npos != lastPos)
00030     {
00031       // Found a token, add it to the vector.
00032       tokens.push_back(str.substr(lastPos, pos - lastPos));
00033       // Skip delimiters.  Note the "not_of"
00034       lastPos = str.find_first_not_of(delimiters, pos);
00035       // Find next "non-delimiter"
00036       pos = str.find_first_of(delimiters, lastPos);
00037     }
00038 }


The documentation for this class was generated from the following files:
Generated on Tue Jun 9 18:20:52 2009 for CMSSW by  doxygen 1.5.4