IoFileListItem Class Reference

#include <IoFileListItem.h>

List of all members.

Public Types

typedef std::map< std::string,
std::string > 
FileStreamMap
typedef FileStreamMap::iterator FileStreamMapItr
typedef
FileStreamMap::const_iterator 
FileStreamMapConstItr

Public Member Functions

 IoFileListItem (const char *filepath, int at, const char *streamlist)
virtual ~IoFileListItem ()
int GetAt () const
const FileStreamMapGetFileStreamMap () const
int GetNumFile () const
virtual std::ostream & Print (std::ostream &os) const
void RemoveFile (const char *filename, const char *streamlist)

Private Member Functions

std::list< std::string > BuildFileIndex (std::string filepath)
void BuildDirIndex (std::string dirpath, std::string subdir, std::vector< std::string > &subdirlist) const
std::string BuildDefStreamList (std::string filename)

Private Attributes

std::string fFilePath
int fAt
FileStreamMap fFileStreamMap

Detailed Description

Definition at line 22 of file IoFileListItem.h.


Member Typedef Documentation

typedef std::map< std::string, std::string > IoFileListItem::FileStreamMap

Definition at line 26 of file IoFileListItem.h.

typedef FileStreamMap::const_iterator IoFileListItem::FileStreamMapConstItr

Definition at line 28 of file IoFileListItem.h.

typedef FileStreamMap::iterator IoFileListItem::FileStreamMapItr

Definition at line 27 of file IoFileListItem.h.


Constructor & Destructor Documentation

IoFileListItem::IoFileListItem ( const char *  filepath,
int  at,
const char *  streamlist 
)

Definition at line 226 of file IoFileListItem.cxx.

References BuildDefStreamList(), BuildFileIndex(), fFileStreamMap, and s().

00227                                                : fFilePath(filepath),fAt(at) {
00228   //
00229   //  Purpose:  Constructor 
00230   //
00231   //  Arguments: filepath      May include wildcards
00232   //             at            Index position to which this file list should
00233   //                           be added for each stream
00234   //             streams       Delimiter separated list of streams (if "*", =>
00235   //                           will open file and use default list of trees)
00236   //
00237   //  Contact:   S. Kasahara
00238   //
00239 
00240   std::list<std::string> filelist = this->BuildFileIndex(filepath);
00241   std::list<std::string>::iterator fileitr;
00242 
00243   std::string streamlist;
00244   std::string s(streams);
00245 
00246   for ( fileitr = filelist.begin(); fileitr != filelist.end(); fileitr++ ) {
00247     std::string filename = *fileitr;
00248     if ( s == "*" ) streamlist = BuildDefStreamList(filename);
00249     else streamlist = s;
00250 
00251     fFileStreamMap[filename] = streamlist;
00252   }
00253 
00254 }

IoFileListItem::~IoFileListItem (  )  [virtual]

Definition at line 256 of file IoFileListItem.cxx.

00256                                 {
00257   //
00258   //  Purpose:  Destructor
00259   //
00260   //  Contact:   S. Kasahara
00261   //
00262 
00263 }


Member Function Documentation

std::string IoFileListItem::BuildDefStreamList ( std::string  filename  )  [private]

Definition at line 37 of file IoFileListItem.cxx.

References PerFileManager::CloseFile(), Nav::GetName(), PerFileManager::Instance(), Per::kRead, and PerFileManager::OpenFile().

Referenced by IoFileListItem().

00037                                                                {
00038 //
00039 // Purpose:  Open the specified file and determine the default list of
00040 //           streams for that file.
00041 //
00042 // Contact:  S. Kasahara
00043 //
00044 
00045   std::string defstreamlist;
00046   
00047   const PerFile* perFile
00048     = PerFileManager::Instance().OpenFile(filename,Per::kRead);
00049   if ( !perFile ) return defstreamlist; // file open failed
00050 
00051   TFile* tfile = perFile -> GetTFile();
00052   // use file keys to determine names of existing trees in input file
00053   tfile -> ReadKeys();
00054   TKey* key;
00055   TIter nextkey(tfile->GetListOfKeys());
00056   std::set<std::string> defstreamset;
00057   while ((key = (TKey*)nextkey())) {
00058     TClass* tclass = gROOT -> GetClass(key->GetClassName());
00059     if ( tclass -> InheritsFrom("TTree") ) {
00060       // Can be TTree or any of its derived classes
00061       std::string newstream = key -> GetName();
00062       defstreamset.insert(newstream);
00063     }
00064   }
00065   PerFileManager::Instance().CloseFile(filename);
00066 
00067   std::set<std::string>::const_iterator citr;
00068   for (citr = defstreamset.begin(); citr != defstreamset.end(); citr++ ) {
00069     std::string newstream = *citr;
00070     if ( defstreamlist.empty() ) defstreamlist = newstream;
00071     else defstreamlist += "," + newstream;
00072   }
00073   
00074   return defstreamlist;
00075 
00076 }

void IoFileListItem::BuildDirIndex ( std::string  dirpath,
std::string  subdir,
std::vector< std::string > &  subdirlist 
) const [private]

Definition at line 180 of file IoFileListItem.cxx.

References gSystem().

Referenced by BuildFileIndex().

00181                                                                          {
00182   // Search for directories matching character string subdir in directory
00183   // dirpath.  subdir may include wildcards.  Matching subdirs are
00184   // pushed to subdirlist.
00185   
00186   std::string separator = "/";
00187   if ( dirpath.find("/") == dirpath.size()-1 ) separator = "";
00188 
00189   if ( strchr(subdir.c_str(),'*') == 0 ) {
00190     // No wildcard in subdirectory.
00191     subdirlist.push_back(dirpath+separator+subdir);
00192     return;
00193   }
00194 
00195   void *dir = gSystem->OpenDirectory(dirpath.c_str());
00196   if ( !dir ) return;
00197   
00198   // kTRUE indicates wildcard present
00199   TRegexp re(subdir.c_str(),kTRUE); 
00200   const char* dirfile;
00201   while ((dirfile = gSystem->GetDirEntry(dir))) {
00202     if ( !strcmp(dirfile,".") || !strcmp(dirfile,"..") ) continue;
00203     if (!strcmp(subdir.c_str(),dirfile) 
00204         || TString(dirfile).Index(re)!=kNPOS) {
00205       // filename matches subdir
00206       std::string fulltestpath = dirpath + separator + std::string(dirfile);
00207       
00208       // check to see that it's a directory
00209       FileStat_t filestatbuf;
00210       int isFailed 
00211            = gSystem->GetPathInfo(fulltestpath.c_str(),filestatbuf);
00212       if ( !isFailed ) {
00213         if ( R_ISDIR(filestatbuf.fMode) ) 
00214           subdirlist.push_back(fulltestpath.c_str());
00215       }  
00216     }
00217   }
00218 
00219   gSystem->FreeDirectory(dir);
00220 
00221   return;
00222 
00223 }

std::list< std::string > IoFileListItem::BuildFileIndex ( std::string  filepath  )  [private]

Definition at line 78 of file IoFileListItem.cxx.

References BuildDirIndex(), gSystem(), and UtilString::StringTok().

Referenced by IoFileListItem().

00078                                                                      {
00079 //
00080 //  Purpose:  Build file index from input filepath. The filename portion of 
00081 //            the path may include wildcard(s), e.g.
00082 //            /mypath/myfile*.root, or
00083 //            ~kasahara/mypath/*p*.root
00084 //            The directory path may include standard shell characters, 
00085 //            e.g. $,.,~  The input filepath will be used to build a list of 
00086 //            fullfilepathnames to be returned to user.
00087 //
00088 //  Contact: S. Kasahara
00089 //
00090 //  Notes: This code is adapted from ROOT's TChain::Add method, but
00091 //         augmented to allow wildcards in the directory path.
00092 //
00093 
00094   std::list<std::string> fileList;
00095 
00096   if ( strchr(filepath.c_str(),'*') == 0 ) {
00097     // No wildcard, just push filepath as is
00098     fileList.push_back(filepath);  
00099     return fileList;
00100   }
00101 
00102   // wildcard present
00103   // Break full directory path up into a set of short strings delimited by "/"
00104   std::vector<std::string> itemizedpath;
00105   UtilString::StringTok(itemizedpath,filepath,"/");
00106   
00107   // if wildcard present in directory path, there may be multiple directories
00108   // to search. dirlist is list of directories to search.
00109   std::vector<std::string> dirlist;
00110   std::string filename;
00111   
00112   if ( itemizedpath.size() <= 1 ) {
00113     // If no '/' specified in input filepath, use working directory.
00114     if ( filepath.find("/") == 0 ) dirlist.push_back("/");
00115     else dirlist.push_back(gSystem->WorkingDirectory());
00116     filename = filepath;
00117   }
00118   else {
00119     // filename is last of segments
00120     filename=itemizedpath[itemizedpath.size()-1]; 
00121 
00122     // Seed directory list
00123     if ( filepath.find("/") == 0 ) 
00124       BuildDirIndex("/",itemizedpath[0],dirlist);
00125     else if ( strchr(itemizedpath[0].c_str(),'*') != 0 ) {
00126       // wildcard, but no initial "/". Assume working directory
00127       BuildDirIndex("./",itemizedpath[0],dirlist);
00128     }
00129     else {
00130       dirlist.push_back(gSystem->ExpandPathName(itemizedpath[0].c_str()));
00131     }
00132     
00133 
00134     for ( unsigned int is = 1; is < itemizedpath.size()-1; is++ ) {
00135       std::vector<std::string> subdirlist;
00136       for (unsigned int idir = 0; idir != dirlist.size(); idir++) {
00137         std::string dirpath = dirlist[idir];
00138         BuildDirIndex(dirpath,itemizedpath[is],subdirlist);
00139       }
00140       dirlist.clear();
00141       for ( unsigned int isub = 0; isub != subdirlist.size(); isub++) {
00142         dirlist.push_back(subdirlist[isub]);
00143       }
00144     }
00145   }
00146   
00147   // List of directory paths is now final, loop to test filename
00148   for ( unsigned int idir = 0; idir < dirlist.size(); idir++ ) {
00149     if ( strchr(filename.c_str(),'*') == 0 ) {
00150       // No wildcard in filename, test existence in this directory
00151       std::string fullfilepath = dirlist[idir]+"/"+filename;
00152       // accesspathname returns false if it was able to access file, bizarre
00153       Bool_t isFail = gSystem->AccessPathName(fullfilepath.c_str());
00154       if ( !isFail ) fileList.push_back(fullfilepath);
00155     }
00156     else {
00157       void *dir = gSystem->OpenDirectory(dirlist[idir].c_str());
00158       if ( dir ) {
00159         TRegexp re(filename.c_str(),kTRUE); // kTRUE indicates wildcard present
00160         const char* dirfile;
00161         while ((dirfile = gSystem->GetDirEntry(dir))) {
00162           if ( !strcmp(dirfile,".") || !strcmp(dirfile,"..") ) continue;
00163           if (   !strcmp(filename.c_str(),dirfile) 
00164               || TString(dirfile).Index(re)!=kNPOS) {
00165             // filename or regular expression matches dirfile 
00166             std::string fullfilepathname = dirlist[idir] + "/"
00167                                          + std::string(dirfile);
00168             fileList.push_back(fullfilepathname);
00169           }
00170         }
00171         gSystem->FreeDirectory(dir);
00172       }
00173     }
00174   }
00175 
00176   return fileList;
00177 
00178 }

int IoFileListItem::GetAt (  )  const [inline]

Definition at line 35 of file IoFileListItem.h.

References fAt.

Referenced by IoInputModule::UpdateFileList().

00035 { return fAt; }

const FileStreamMap& IoFileListItem::GetFileStreamMap (  )  const [inline]

Definition at line 36 of file IoFileListItem.h.

References fFileStreamMap.

Referenced by IoInputModule::AddFile(), and IoInputModule::UpdateFileList().

00036 { return fFileStreamMap; }

int IoFileListItem::GetNumFile (  )  const [inline]

Definition at line 37 of file IoFileListItem.h.

References fFileStreamMap.

Referenced by IoInputModule::RemoveFile().

00037 { return fFileStreamMap.size(); }

std::ostream & IoFileListItem::Print ( std::ostream &  os  )  const [virtual]

Definition at line 266 of file IoFileListItem.cxx.

References fFileStreamMap.

Referenced by operator<<().

00266                                                       {
00267   //
00268   //  Purpose:  Print status of file/stream associations on ostream.
00269   //
00270   //  Contact:   S. Kasahara
00271   //
00272 
00273   FileStreamMapConstItr itr = fFileStreamMap.begin();
00274   for ( ; itr != fFileStreamMap.end(); itr++ ) {
00275     os << itr->first << "\t" << itr->second << endl;
00276   }
00277 
00278   return os;
00279 
00280 }

void IoFileListItem::RemoveFile ( const char *  filename,
const char *  streamlist 
)

Definition at line 283 of file IoFileListItem.cxx.

References fFileStreamMap, find(), s(), and UtilString::StringTok().

Referenced by IoInputModule::RemoveFile().

00283                                                                          {
00284   //
00285   //  Purpose:  Remove specified stream(s) from specified file.
00286   //            If streams = "*", remove file from list.
00287   //            If filename = "*", apply to all files.
00288   //
00289   //  Contact:   S. Kasahara
00290   //
00291 
00292   std::string f(filename);
00293   std::string s(streams);
00294 
00295   FileStreamMapItr fileItr = fFileStreamMap.end();
00296   while ( !fFileStreamMap.empty() && fileItr != fFileStreamMap.begin() ) {
00297     fileItr--;
00298     if ( f == "*" || f == (fileItr->first) ) { 
00299       if ( s == "*" ) {
00300         fFileStreamMap.erase(fileItr);  // remove file from map
00301       }
00302       else {
00303         std::vector<std::string> vrm;
00304         std::vector<std::string>::iterator vrmitr;
00305         UtilString::StringTok(vrm,streams,":,;/ ");
00306 
00307         std::string oldstreams = fileItr->second;
00308         std::vector<std::string> vold;
00309         UtilString::StringTok(vold,oldstreams,":,;/ ");
00310         std::vector<std::string>::iterator volditr = vold.end();
00311         
00312         while ( volditr !=  vold.begin() ) {
00313           volditr--;
00314           vrmitr = std::find(vrm.begin(),vrm.end(),*volditr);
00315           if ( vrmitr != vrm.end() ) vold.erase(volditr);      
00316         }
00317         if ( vold.empty() ) fFileStreamMap.erase(fileItr);
00318         else {
00319           std::string revisedstreams;
00320           for ( volditr = vold.begin(); volditr != vold.end(); volditr++ ) {
00321             if ( revisedstreams.empty() ) revisedstreams = *volditr;
00322             else revisedstreams += "," + *volditr;
00323           }
00324           fileItr->second = revisedstreams;
00325         }
00326       }
00327     }
00328   }
00329   return;
00330 
00331 }


Member Data Documentation

int IoFileListItem::fAt [private]

Definition at line 50 of file IoFileListItem.h.

Referenced by GetAt().

std::string IoFileListItem::fFilePath [private]

Definition at line 49 of file IoFileListItem.h.

Definition at line 51 of file IoFileListItem.h.

Referenced by GetFileStreamMap(), GetNumFile(), IoFileListItem(), Print(), and RemoveFile().


The documentation for this class was generated from the following files:

Generated on 22 Nov 2017 for loon by  doxygen 1.6.1