Go to the documentation of this file.00001 #include "RecoLuminosity/ROOTSchema/interface/FileToolKit.h"
00002
00003
00004 #include <fstream>
00005
00006
00007 #include <sys/types.h>
00008 #include <sys/stat.h>
00009 #include <unistd.h>
00010
00011
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
00023 string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00024
00025 string::size_type pos = str.find_first_of(delimiters, lastPos);
00026
00027 while (string::npos != pos || string::npos != lastPos)
00028 {
00029
00030 tokens.push_back(str.substr(lastPos, pos - lastPos));
00031
00032 lastPos = str.find_first_not_of(delimiters, pos);
00033
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 errsv = 0;
00044
00045 vector< string > tkDirName;
00046 string currentDirName = "";
00047
00048 Tokenize(dirName, tkDirName, "/");
00049
00050 if(dirName[0] == '/') currentDirName += "/";
00051
00052 struct stat mStat;
00053
00054 for(unsigned int i = 0; i < tkDirName.size(); ++i ){
00055
00056 currentDirName += tkDirName[i];
00057 currentDirName += "/";
00058
00059 errno = 0;
00060 stat( currentDirName.c_str(), &mStat );
00061 errsv = errno;
00062
00063 if( errsv == 2 ){
00064
00065 errno = 0;
00066 mkdir( currentDirName.c_str(), writeMode);
00067 errsv = errno;
00068 if( errno == 0 ){
00069 errno = 0;
00070 chmod( currentDirName.c_str(), writeMode);
00071 errsv = errno;
00072 }
00073 }
00074 }
00075 return errsv;
00076 }
00077
00078 void FileToolKit::MakeEmptyWebPage( const std::string &fileName,
00079 const std::string &title ){
00080
00081 std::fstream fileStream;
00082
00083 fileStream.open( fileName.c_str(), std::fstream::out);
00084
00085 fileStream << "<html>" << std::endl;
00086 fileStream << "<title>" << std::endl;
00087 fileStream << title << std::endl;
00088 fileStream << "</title>" << std::endl;
00089 fileStream << "<body>" << std::endl;
00090 fileStream << "</body>" << std::endl;
00091 fileStream << "</html>" << std::endl;
00092
00093 fileStream.close();
00094 }
00095
00096 void FileToolKit::InsertLineAfter( const std::string &fileName,
00097 const std::string &newLine,
00098 const std::string &searchLine){
00099
00100 bool bMatch = false;
00101
00102 std::vector< std::string > fileContents;
00103 char lineBuffer[256];
00104
00105 std::fstream fileStream;
00106
00107
00108 fileStream.open(fileName.c_str(), std::fstream::in );
00109 while( fileStream.good() ){
00110 fileStream.getline( lineBuffer, 256 );
00111
00112 fileContents.push_back( lineBuffer );
00113
00114 if( strcmp( lineBuffer, searchLine.c_str() ) == 0 ){
00115 fileContents.push_back( newLine );
00116 bMatch = true;
00117 }
00118 }
00119 fileStream.close();
00120
00121
00122 if(bMatch){
00123 std::fstream fileStream2;
00124
00125 fileStream2.open( fileName.c_str(), std::fstream::out );
00126
00127 for(unsigned int iline = 0; iline < fileContents.size(); ++iline)
00128 fileStream2 << fileContents[iline] << std::endl;
00129
00130 fileStream2.close();
00131
00132
00133 }
00134 }
00135
00136 void FileToolKit::InsertLineBefore( const std::string &fileName,
00137 const std::string &newLine,
00138 const std::string &searchLine ){
00139
00140 bool bMatch = false;
00141
00142 std::vector< std::string > fileContents;
00143 char lineBuffer[256];
00144
00145 std::fstream fileStream;
00146
00147 fileStream.open(fileName.c_str());
00148 while( fileStream.good() ){
00149 fileStream.getline( lineBuffer, 256 );
00150 if(strcmp(lineBuffer, searchLine.c_str()) == 0){
00151 fileContents.push_back( newLine );
00152 bMatch = true;
00153 }
00154 fileContents.push_back( lineBuffer );
00155 }
00156 fileStream.close();
00157
00158 if(bMatch){
00159 std::fstream fileStream2;
00160
00161 fileStream2.open( fileName.c_str(), std::fstream::out );
00162
00163 for(unsigned int iline = 0; iline < fileContents.size(); ++iline){
00164 fileStream2 << fileContents[iline] << std::endl;
00165 }
00166
00167 fileStream2.close();
00168
00169
00170 }
00171 }
00172
00173 bool FileToolKit::fileExists( const std::string &fileName){
00174
00175 std::fstream filestr;
00176
00177 filestr.open (fileName.c_str(), std::fstream::in );
00178 if (filestr.is_open()){
00179 filestr.close();
00180 return true;
00181 }
00182 else
00183 {
00184 return false;
00185 }
00186 }