TSystemDirectory.cxx

Go to the documentation of this file.
00001 // @(#)root/base:$Id: TSystemDirectory.cxx 20877 2007-11-19 11:17:07Z rdm $
00002 // Author: Christian Bormann  13/10/97
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TSystemDirectory                                                     //
00015 //                                                                      //
00016 // Describes an Operating System directory for the browser.             //
00017 //                                                                      //
00018 // Author: Christian Bormann  30/09/97                                  //
00019 //         http://www.ikf.physik.uni-frankfurt.de/~bormann/             //
00020 //                                                                      //
00021 //////////////////////////////////////////////////////////////////////////
00022 
00023 #include "TSystemDirectory.h"
00024 #include "TSystem.h"
00025 #include "TBrowser.h"
00026 #include "TOrdCollection.h"
00027 #include "TList.h"
00028 
00029 
00030 ClassImp(TSystemDirectory);
00031 
00032 //______________________________________________________________________________
00033 TSystemDirectory::TSystemDirectory()
00034 {
00035    // Create a system directory object.
00036 
00037    fDirsInBrowser  = 0;
00038    fFilesInBrowser = 0;
00039 }
00040 
00041 //______________________________________________________________________________
00042 TSystemDirectory::TSystemDirectory(const char *dirname, const char *path) :
00043    TSystemFile(dirname, path)
00044 {
00045    // Create a system directory object.
00046 
00047    fDirsInBrowser  = 0;
00048    fFilesInBrowser = 0;
00049 }
00050 
00051 //______________________________________________________________________________
00052 TSystemDirectory::TSystemDirectory(const TSystemDirectory& sd) :
00053   TSystemFile(sd),
00054   fDirsInBrowser(sd.fDirsInBrowser),
00055   fFilesInBrowser(sd.fFilesInBrowser)
00056 { 
00057    //copy constructor
00058 }
00059 
00060 //______________________________________________________________________________
00061 TSystemDirectory& TSystemDirectory::operator=(const TSystemDirectory& sd)
00062 {
00063    //assignment operator
00064    if(this!=&sd) {
00065       TSystemFile::operator=(sd);
00066       fDirsInBrowser=sd.fDirsInBrowser;
00067       fFilesInBrowser=sd.fFilesInBrowser;
00068    } 
00069    return *this;
00070 }
00071 
00072 //______________________________________________________________________________
00073 TSystemDirectory::~TSystemDirectory()
00074 {
00075    // Delete system directory object.
00076 
00077    delete fDirsInBrowser;
00078    delete fFilesInBrowser;
00079 }
00080 
00081 //______________________________________________________________________________
00082 TList *TSystemDirectory::GetListOfFiles() const
00083 {
00084    // Returns a TList of TSystemFile objects representing the contents
00085    // of the directory. It's the responsibility of the user to delete
00086    // the list (the list owns the contained objects).
00087    // Returns 0 in case of errors.
00088 
00089    void *dir = gSystem->OpenDirectory(GetTitle());
00090    if (!dir) return 0;
00091 
00092    const char *file = 0;
00093    TList *contents  = new TList;
00094    contents->SetOwner();
00095    while ((file = gSystem->GetDirEntry(dir))) {
00096       if (IsItDirectory(file)) {
00097          TString sdirpath;
00098          if (file[0] == '.' && file[1] == '\0')
00099             sdirpath = GetTitle();
00100          else if (file[0] == '.' && file[1] == '.' && file[2] == '.')
00101             sdirpath = gSystem->DirName(GetTitle());
00102          else {
00103             sdirpath = GetTitle();
00104             if (!sdirpath.EndsWith("/"))
00105                sdirpath += "/";
00106             sdirpath += file;
00107          }
00108          contents->Add(new TSystemDirectory(file, sdirpath.Data()));
00109       } else
00110          contents->Add(new TSystemFile(file, GetTitle()));
00111    }
00112    gSystem->FreeDirectory(dir);
00113    return contents;
00114 }
00115 
00116 //______________________________________________________________________________
00117 void TSystemDirectory::SetDirectory(const char *name)
00118 {
00119    // Create a system directory object.
00120 
00121    SetName(name);
00122    SetTitle(name);
00123 }
00124 
00125 //______________________________________________________________________________
00126 Bool_t TSystemDirectory::IsItDirectory(const char *name) const
00127 {
00128    // Check if name is a directory.
00129 
00130    Long64_t size;
00131    Long_t id, flags, modtime;
00132    const char *dirfile = GetTitle();
00133 
00134    gSystem->ChangeDirectory(dirfile);
00135    flags = id = size = modtime = 0;
00136    gSystem->GetPathInfo(name, &id, &size, &flags, &modtime);
00137    Int_t isdir = (Int_t)flags & 2;
00138 
00139    return isdir ? kTRUE : kFALSE;
00140 }
00141 
00142 //______________________________________________________________________________
00143 void TSystemDirectory::Browse(TBrowser *b)
00144 {
00145    // Browse OS system directories.
00146 
00147    // Collections to keep track of all browser objects that have been
00148    // generated. It's main goal is to prevent the contineous
00149    // allocations of new objects with the same names during browsing.
00150    if (!fDirsInBrowser)  fDirsInBrowser  = new TOrdCollection;
00151    if (!fFilesInBrowser) fFilesInBrowser = new TOrdCollection(10);
00152 
00153    const char *name = GetTitle();
00154    TSystemFile *sfile;
00155    TSystemDirectory *sdir;
00156    const char *file;
00157 
00158    gSystem->ChangeDirectory(name);
00159 
00160    if (GetName()[0] == '.' && GetName()[1] == '.')
00161       SetName(gSystem->BaseName(name));
00162 
00163    void *dir = gSystem->OpenDirectory(name);
00164 
00165    if (!dir)
00166       return;
00167 
00168    while ((file = gSystem->GetDirEntry(dir))) {
00169       if (b->TestBit(TBrowser::kNoHidden) && file[0] == '.' && file[1] != '.' )
00170          continue;
00171       if (IsItDirectory(file)) {
00172          TString sdirpath;
00173          if (!strcmp(file, "."))
00174             sdirpath = name;
00175          else if (!strcmp(file, ".."))
00176             sdirpath = gSystem->DirName(name);
00177          else {
00178             sdirpath =  name;
00179             if (!sdirpath.EndsWith("/"))
00180                sdirpath += "/";
00181             sdirpath += file;
00182          }
00183          if (!(sdir = FindDirObj(sdirpath.Data()))) {
00184             sdir = new TSystemDirectory(file, sdirpath.Data());
00185             fDirsInBrowser->Add(sdir);
00186          }
00187          b->Add(sdir, file);
00188       } else {
00189          if (!(sfile = FindFileObj(file, gSystem->WorkingDirectory()))) {
00190             sfile = new TSystemFile(file, gSystem->WorkingDirectory());
00191             fFilesInBrowser->Add(sfile);
00192          }
00193          b->Add(sfile, file);
00194       }
00195    }
00196    gSystem->FreeDirectory(dir);
00197 }
00198 
00199 //______________________________________________________________________________
00200 TSystemDirectory *TSystemDirectory::FindDirObj(const char *name)
00201 {
00202    // Method that returns system directory object if it
00203    // exists in list, 0 otherwise.
00204 
00205    int size = fDirsInBrowser->GetSize();
00206    for (int i = 0; i < size; i++) {
00207       TSystemDirectory *obj = (TSystemDirectory *) fDirsInBrowser->At(i);
00208       if (!strcmp(name, obj->GetTitle()))
00209          return obj;
00210    }
00211    return 0;
00212 }
00213 
00214 //______________________________________________________________________________
00215 TSystemFile *TSystemDirectory::FindFileObj(const char *name, const char *dir)
00216 {
00217    // Method that returns system file object if it exists in
00218    // list, 0 otherwise.
00219 
00220    int size = fFilesInBrowser->GetSize();
00221    for (int i = 0; i < size; i++) {
00222       TSystemFile *obj = (TSystemFile *) fFilesInBrowser->At(i);
00223       if (!strcmp(name, obj->GetName()) && !strcmp(dir, obj->GetTitle()))
00224          return obj;
00225    }
00226    return 0;
00227 }

Generated on Tue Jul 5 14:11:24 2011 for ROOT_528-00b_version by  doxygen 1.5.1