From c673f0a9ebda897230418853b2938a71f784732b Mon Sep 17 00:00:00 2001 From: Fons Rademakers <Fons.Rademakers@cern.ch> Date: Mon, 30 Apr 2007 10:44:33 +0000 Subject: [PATCH] From Jan and Gerri: Added two static methods to create a list of TFileInfo's: TList *TFileInfo::CreateList(const char *file) where 'file' is a text file with each line defining a file URL TList *TFileInfo::CreateListMatching(const char *files) where 'files' is i) directory path; all regular files in the directory are added ii) a file path; a list with a single element is created iii) a file-path containing wildcards '*' in the file name; a list with all the files consistent with the path are added. git-svn-id: http://root.cern.ch/svn/root/trunk@18621 27541ba8-7e3a-0410-8455-c3a389f83636 --- base/inc/TFileInfo.h | 10 ++-- base/src/TFileInfo.cxx | 132 ++++++++++++++++++++++++++++++----------- 2 files changed, 102 insertions(+), 40 deletions(-) diff --git a/base/inc/TFileInfo.h b/base/inc/TFileInfo.h index 1940dbefda5..f324bf489e0 100644 --- a/base/inc/TFileInfo.h +++ b/base/inc/TFileInfo.h @@ -1,4 +1,4 @@ -// @(#)root/base:$Name: $:$Id: TFileInfo.h,v 1.6 2006/05/15 11:01:13 rdm Exp $ +// @(#)root/base:$Name: $:$Id: TFileInfo.h,v 1.7 2006/05/23 04:47:35 brun Exp $ // Author: Andreas-Joachim Peters 20/9/2005 /************************************************************************* @@ -60,9 +60,8 @@ private: TObject *fMetaDataObject; //-> generic file meta data object -protected: - TFileInfo(const TFileInfo&); - TFileInfo& operator=(const TFileInfo&); + TFileInfo(const TFileInfo&); // not implemented + TFileInfo& operator=(const TFileInfo&); // not implemented public: TFileInfo(const char *url=0, Long64_t size=-1, const char *uuid=0, @@ -100,6 +99,9 @@ public: void Print(Option_t *options="") const; + static TList *CreateList(const char *file); + static TList *CreateListMatching(const char *files); + ClassDef(TFileInfo,1) // Describes generic file info including meta information }; diff --git a/base/src/TFileInfo.cxx b/base/src/TFileInfo.cxx index 7ffb3dfffe8..6ff0f617b4c 100644 --- a/base/src/TFileInfo.cxx +++ b/base/src/TFileInfo.cxx @@ -1,4 +1,4 @@ -// @(#)root/base:$Name: $:$Id: TFileInfo.cxx,v 1.8 2006/05/26 09:01:58 brun Exp $ +// @(#)root/base:$Name: $:$Id: TFileInfo.cxx,v 1.9 2006/07/09 05:27:53 brun Exp $ // Author: Andreas-Joachim Peters 20/9/2005 /************************************************************************* @@ -19,6 +19,9 @@ #include "TFileInfo.h" #include "Riostream.h" +#include "TSystem.h" +#include "TRegexp.h" +#include "TError.h" ClassImp(TFileInfo) @@ -54,41 +57,6 @@ TFileInfo::TFileInfo(const char *url , Long64_t size, const char *uuid, } } -//______________________________________________________________________________ -TFileInfo::TFileInfo(const TFileInfo& fi) : - TNamed(fi), - fCurrentUrl(fi.fCurrentUrl), - fUrlList(fi.fUrlList), - fSize(fi.fSize), - fUUID(fi.fUUID), - fMD5(fi.fMD5), - fEntries(fi.fEntries), - fFirst(fi.fFirst), - fLast(fi.fLast), - fMetaDataObject(fi.fMetaDataObject) -{ - //copy constructor -} - -//______________________________________________________________________________ -TFileInfo& TFileInfo::operator=(const TFileInfo& fi) -{ - //assignment operator - if(this!=&fi) { - TNamed::operator=(fi); - fCurrentUrl=fi.fCurrentUrl; - fUrlList=fi.fUrlList; - fSize=fi.fSize; - fUUID=fi.fUUID; - fMD5=fi.fMD5; - fEntries=fi.fEntries; - fFirst=fi.fFirst; - fLast=fi.fLast; - fMetaDataObject=fi.fMetaDataObject; - } - return *this; -} - //______________________________________________________________________________ TFileInfo::~TFileInfo() { @@ -211,3 +179,95 @@ void TFileInfo::Print(Option_t * /* option */) const cout << " URL: " << url << endl; } } + +//______________________________________________________________________________ +TList *TFileInfo::CreateList(const char *file) +{ + // Open the text 'file' and create TList of TFileInfo objects. + // The 'file' must include one url per line. + // The function returns a TList of TFileInfos (possibly empty) or + // 0 if 'file' can not be opened. + + Int_t fileCount = 0; + ifstream f; + TList* fileList = 0; + f.open(gSystem->ExpandPathName(file), ifstream::out); + if (f.is_open()) { + fileList = new TList; + while (f.good()) { + TString line; + line.ReadToDelim(f); + if (!line.IsWhitespace()) { + fileList->Add(new TFileInfo(line.Data())); + fileCount++; + } + } + f.close(); + } else { + ::Error("TFileInfo::CreateList", "unable to open file %s", file); + } + return fileList; +} + +//______________________________________________________________________________ +TList *TFileInfo::CreateListMatching(const char *files) +{ + // Find all the files described by 'files' and return a TList of corresponding + // TFileInfo objects. 'files' can include wildcards after the last slash. + // If 'files' is the full path of a file, a list with only one element is created. + // If no files match the selection, 0 is returned + + if (!files || strlen(files) <= 0) { + Printf("TFileInfo::CreateListMatching: input path undefined"); + return 0; + } + + // Create the list + TList *fileList = new TList(); + fileList->SetOwner(); + + // If files points to a single file, fill the list and exit + FileStat_t st; + if (gSystem->GetPathInfo(files, st) == 0 && R_ISREG(st.fMode)) { + // Regular, single file + fileList->Add(new TFileInfo(files)); + return fileList; + } + + void *dataSetDir = gSystem->OpenDirectory(gSystem->DirName(files)); + if (!dataSetDir) { + // Directory cannot be open + ::Error("TFileInfo::CreateListMatching", "directory %s cannot be open", + gSystem->DirName(files)); + return 0; + } + const char* ent; + TString filesExp(gSystem->BaseName(files)); + if (filesExp.Length() <= 0) + filesExp = "*"; + filesExp.ReplaceAll("*",".*"); + TRegexp rg(filesExp); + while ((ent = gSystem->GetDirEntry(dataSetDir))) { + TString entryString(ent); + if (entryString.Index(rg) != kNPOS) { + // matching dir entry + TString fn(Form("%s/%s",gSystem->DirName(files), ent)); + gSystem->GetPathInfo(fn, st); + if (R_ISREG(st.fMode)) + // Regular file + fileList->Add(new TFileInfo(fn)); + } + } + + // Close the directory + gSystem->FreeDirectory(dataSetDir); + + if (fileList->GetSize() == 0) { + ::Error("TFileInfo::CreateListMatching", + "no files match your selection, the list was not created"); + delete fileList; + return 0; + } + // Done + return fileList; +} -- GitLab