00001 #include <libgen.h>
00002 #include <fstream>
00003
00004 #include "TKey.h"
00005 #include "TSystem.h"
00006 #include "TTree.h"
00007 #include "TFile.h"
00008 #include "TIterator.h"
00009
00010 #include "MessageService/MsgService.h"
00011 #include "Util/UtilString.h"
00012
00013 #include "AtNuUtils/UtilFile.h"
00014
00015 CVSID("$Id: UtilFile.cxx,v 1.2 2006/07/19 22:41:29 gmieg Exp $");
00016
00017 using namespace std;
00018
00019 bool UtilFile::CheckDir(const char* DirName, bool MakeDir) {
00020 MSG("Util",Msg::kVerbose) << "UtilFile::CheckDir" << endl;
00021
00022 string cwd = gSystem->pwd();
00023
00024 if (gSystem->cd(DirName)) {
00025 gSystem->cd(cwd.c_str());
00026 return true;
00027 }
00028
00029 if (!MakeDir) return false;
00030
00031 gSystem->mkdir(DirName,kTRUE);
00032 if (gSystem->cd(DirName)) {
00033 gSystem->cd(cwd.c_str());
00034 return true;
00035 }
00036
00037 gSystem->cd(cwd.c_str());
00038
00039 return false;
00040 }
00041
00042 bool UtilFile::CheckFile(string FileName, bool TestCreate) {
00043 MSG("Util",Msg::kVerbose) << "UtilFile::CheckFile" << endl;
00044
00045 if (! gSystem->Exec(("ls "+FileName+">&/dev/null").c_str())) {
00046 return true;
00047 }
00048
00049 if (! TestCreate) return false;
00050
00051 if (! gSystem->Exec(("touch "+FileName+">&/dev/null").c_str())) {
00052 gSystem->Unlink(FileName.c_str());
00053 return true;
00054 }
00055
00056 return false;
00057 }
00058
00059 vector<string> UtilFile::DirFileList(string DirName, string Suf,
00060 bool IncludePath, bool IncludeSuf,
00061 vector<string> FList) {
00062 MSG("Util",Msg::kVerbose) << "UtilFile::DirFileList" << endl;
00063
00064 MSG("Util",Msg::kDebug) << "Checking " << DirName << " for suffix " << Suf << endl;
00065
00066 if(!UtilFile::CheckDir(DirName.c_str(),false)) return FList;
00067
00068 void *dir = gSystem->OpenDirectory(DirName.c_str());
00069
00070 if (!dir) {
00071 MSG("Util",Msg::kWarning)<< "Error opening " << DirName << endl;
00072 return FList;
00073 }
00074
00075 const char* file;
00076 string fname;
00077
00078
00079 while ((file = gSystem->GetDirEntry(dir))){
00080 if(IncludePath) fname = DirName+"/"+file;
00081 else fname = file;
00082 if (fname.length() > Suf.length() &&
00083 fname.find(Suf) == fname.length()-Suf.length()) {
00084 if(IncludeSuf) FList.push_back(fname);
00085 else FList.push_back(fname.substr(0,fname.find(Suf)));
00086 }
00087 }
00088 gSystem->FreeDirectory(dir);
00089 MSG("Util",Msg::kDebug) << "Return element:" << FList.size() << endl;
00090 return FList;
00091 }
00092
00093 vector<string> UtilFile::FileFileList(string FileName, string Suf,
00094 bool IncludePath, bool IncludeSuf,
00095 vector<string> FList) {
00096 MSG("Util",Msg::kVerbose) << "UtilFile::FileFileList" << endl;
00097
00098 MSG("Util",Msg::kDebug) << "Checking " << FileName << " for suffix " << Suf
00099 << "(" << Suf.length() << ")" << endl;
00100
00101 char FName[256];
00102 string fname;
00103
00104 while (1) {
00105 string::size_type ispace = FileName.find(" ");
00106 string ThisFile = FileName.substr(0,ispace);
00107
00108 MSG("Util",Msg::kDebug) << "Reading List File " << ThisFile << endl;
00109
00110 ifstream RFile(ThisFile.c_str());
00111 while (1) {
00112 RFile.getline(FName, 256);
00113
00114 MSG("Util",Msg::kDebug) << " " << FName << endl;
00115 if(!RFile.good()) break;
00116 if (IncludePath) fname = FName;
00117 else fname = basename(FName);
00118 MSG("Util",Msg::kDebug) << " =" << fname << endl;
00119 if (fname.length() > Suf.length() && (Suf.length() == 0 ||
00120 fname.find(Suf) == fname.length()-Suf.length())) {
00121 if(IncludeSuf) FList.push_back(fname);
00122 else FList.push_back(fname.substr(0,fname.find(Suf)));
00123 }
00124 }
00125 RFile.close();
00126
00127 if(ispace == string::npos) break;
00128 FileName.erase(0,ispace+1);
00129
00130 }
00131 MSG("Util",Msg::kDebug) << "Return element:" << FList.size() << endl;
00132 return FList;
00133 }
00134
00135 void UtilFile::FileListOut(string FileName, vector<string> FList,
00136 bool Append) {
00137 ofstream lfile;
00138 ios_base::openmode out_mode = ios::out;
00139 if(Append) out_mode = out_mode | ios::app;
00140
00141 lfile.open(FileName.c_str(),out_mode);
00142 if (! lfile.is_open() ) {
00143 MSG("Util",Msg::kWarning)<< "Couldn't open list file " << FileName << endl;
00144 return;
00145 }
00146
00147 for(unsigned int i=0; i<FList.size(); i++) lfile << FList[i] << endl;
00148
00149 lfile.close();
00150 }
00151
00152 void UtilFile::FileListOut(string FileName, string FStr, bool Append) {
00153 ofstream lfile;
00154 ios_base::openmode out_mode = ios::out;
00155 if(Append) out_mode = out_mode | ios::app;
00156
00157 lfile.open(FileName.c_str(),out_mode);
00158 if (! lfile.is_open() ) {
00159 MSG("Util",Msg::kWarning)<< "Couldn't open list file " << FileName << endl;
00160 return;
00161 }
00162
00163 lfile << FStr << endl;
00164
00165 lfile.close();
00166 }