Logo ROOT  
Reference Guide
TSystemDirectory.cxx
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Christian Bormann 13/10/97
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /** \class TSystemDirectory
13 \ingroup Base
14 
15 Describes an Operating System directory for the browser.
16 */
17 
18 #include "TSystemDirectory.h"
19 #include "TSystem.h"
20 #include "TBrowser.h"
21 #include "TOrdCollection.h"
22 #include "TList.h"
23 
24 
26 
27 ////////////////////////////////////////////////////////////////////////////////
28 /// Create a system directory object.
29 
31 {
32  fDirsInBrowser = 0;
33  fFilesInBrowser = 0;
34 }
35 
36 ////////////////////////////////////////////////////////////////////////////////
37 /// Create a system directory object.
38 
39 TSystemDirectory::TSystemDirectory(const char *dirname, const char *path) :
40  TSystemFile(dirname, path)
41 {
42  fDirsInBrowser = 0;
43  fFilesInBrowser = 0;
44 }
45 
46 ////////////////////////////////////////////////////////////////////////////////
47 /// Copy constructor
48 
50  TSystemFile(sd),
51  fDirsInBrowser(sd.fDirsInBrowser),
52  fFilesInBrowser(sd.fFilesInBrowser)
53 {
54 }
55 
56 ////////////////////////////////////////////////////////////////////////////////
57 /// Assignment operator
58 
60 {
61  if(this!=&sd) {
65  }
66  return *this;
67 }
68 
69 ////////////////////////////////////////////////////////////////////////////////
70 /// Delete system directory object.
71 
73 {
74  delete fDirsInBrowser;
75  delete fFilesInBrowser;
76 }
77 
78 ////////////////////////////////////////////////////////////////////////////////
79 /// Returns a TList of TSystemFile objects representing the contents
80 /// of the directory. It's the responsibility of the user to delete
81 /// the list (the list owns the contained objects).
82 /// Returns 0 in case of errors.
83 
85 {
86  void *dir = gSystem->OpenDirectory(GetTitle());
87  if (!dir) return 0;
88 
89  const char *file = 0;
90  TList *contents = new TList;
91  contents->SetOwner();
92  while ((file = gSystem->GetDirEntry(dir))) {
93  if (IsItDirectory(file)) {
94  TString sdirpath;
95  if (file[0] == '.' && file[1] == '\0')
96  sdirpath = GetTitle();
97  else if (file[0] == '.' && file[1] == '.' && file[2] == '.')
98  sdirpath = gSystem->GetDirName(GetTitle());
99  else {
100  sdirpath = GetTitle();
101  if (!sdirpath.EndsWith("/"))
102  sdirpath += "/";
103  sdirpath += file;
104  }
105  contents->Add(new TSystemDirectory(file, sdirpath.Data()));
106  } else
107  contents->Add(new TSystemFile(file, GetTitle()));
108  }
109  gSystem->FreeDirectory(dir);
110  return contents;
111 }
112 
113 ////////////////////////////////////////////////////////////////////////////////
114 /// Create a system directory object.
115 
117 {
118  SetName(name);
119  SetTitle(name);
120 }
121 
122 ////////////////////////////////////////////////////////////////////////////////
123 /// Check if name is a directory.
124 
126 {
127  Long64_t size;
128  Long_t id, flags, modtime;
129  const char *dirfile = GetTitle();
130  TString savDir = gSystem->WorkingDirectory();
131 
132  gSystem->ChangeDirectory(dirfile);
133  flags = id = size = modtime = 0;
134  gSystem->GetPathInfo(name, &id, &size, &flags, &modtime);
135  Int_t isdir = (Int_t)flags & 2;
136 
137  gSystem->ChangeDirectory(savDir);
138  return isdir ? kTRUE : kFALSE;
139 }
140 
141 ////////////////////////////////////////////////////////////////////////////////
142 /// Browse OS system directories.
143 
145 {
146  // Collections to keep track of all browser objects that have been
147  // generated. It's main goal is to prevent the continuous
148  // allocations of new objects with the same names during browsing.
151 
152  const char *name = GetTitle();
153  TSystemFile *sfile;
154  TSystemDirectory *sdir;
155  const char *file;
156 
158 
159  if (GetName()[0] == '.' && GetName()[1] == '.')
161 
162  void *dir = gSystem->OpenDirectory(name);
163 
164  if (!dir)
165  return;
166 
167  while ((file = gSystem->GetDirEntry(dir))) {
168  if (b->TestBit(TBrowser::kNoHidden) && file[0] == '.' && file[1] != '.' )
169  continue;
170  if (IsItDirectory(file)) {
171  TString sdirpath;
172  if (!strcmp(file, "."))
173  sdirpath = name;
174  else if (!strcmp(file, ".."))
175  sdirpath = gSystem->GetDirName(name);
176  else {
177  sdirpath = name;
178  if (!sdirpath.EndsWith("/"))
179  sdirpath += "/";
180  sdirpath += file;
181  }
182  if (!(sdir = FindDirObj(sdirpath.Data()))) {
183  sdir = new TSystemDirectory(file, sdirpath.Data());
184  fDirsInBrowser->Add(sdir);
185  }
186  b->Add(sdir, file);
187  } else {
188  if (!(sfile = FindFileObj(file, gSystem->WorkingDirectory()))) {
189  sfile = new TSystemFile(file, gSystem->WorkingDirectory());
190  fFilesInBrowser->Add(sfile);
191  }
192  b->Add(sfile, file);
193  }
194  }
195  gSystem->FreeDirectory(dir);
196 }
197 
198 ////////////////////////////////////////////////////////////////////////////////
199 /// Method that returns system directory object if it
200 /// exists in list, 0 otherwise.
201 
203 {
204  int size = fDirsInBrowser->GetSize();
205  for (int i = 0; i < size; i++) {
207  if (!strcmp(name, obj->GetTitle()))
208  return obj;
209  }
210  return 0;
211 }
212 
213 ////////////////////////////////////////////////////////////////////////////////
214 /// Method that returns system file object if it exists in
215 /// list, 0 otherwise.
216 
217 TSystemFile *TSystemDirectory::FindFileObj(const char *name, const char *dir)
218 {
219  int size = fFilesInBrowser->GetSize();
220  for (int i = 0; i < size; i++) {
221  TSystemFile *obj = (TSystemFile *) fFilesInBrowser->At(i);
222  if (!strcmp(name, obj->GetName()) && !strcmp(dir, obj->GetTitle()))
223  return obj;
224  }
225  return 0;
226 }
TSystem::GetPathInfo
int GetPathInfo(const char *path, Long_t *id, Long_t *size, Long_t *flags, Long_t *modtime)
Get info about a file: id, size, flags, modification time.
Definition: TSystem.cxx:1396
TSystemDirectory::TSystemDirectory
TSystemDirectory()
Create a system directory object.
Definition: TSystemDirectory.cxx:30
TSystemDirectory::GetListOfFiles
virtual TList * GetListOfFiles() const
Returns a TList of TSystemFile objects representing the contents of the directory.
Definition: TSystemDirectory.cxx:84
TBrowser
Definition: TBrowser.h:37
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TSystem::ChangeDirectory
virtual Bool_t ChangeDirectory(const char *path)
Change directory.
Definition: TSystem.cxx:861
TSeqCollection::Add
virtual void Add(TObject *obj)
Definition: TSeqCollection.h:44
TSystemDirectory::IsItDirectory
Bool_t IsItDirectory(const char *name) const
Check if name is a directory.
Definition: TSystemDirectory.cxx:125
TSystem::BaseName
virtual const char * BaseName(const char *pathname)
Base name of a file name. Base name of /user/root is root.
Definition: TSystem.cxx:933
TString::Data
const char * Data() const
Definition: TString.h:369
TSystem::FreeDirectory
virtual void FreeDirectory(void *dirp)
Free a directory.
Definition: TSystem.cxx:844
TNamed::operator=
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
Definition: TNamed.cxx:51
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TNamed::GetTitle
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:54
Long64_t
long long Long64_t
Definition: RtypesCore.h:73
TOrdCollection
Definition: TOrdCollection.h:32
TCollection::SetOwner
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
Definition: TCollection.cxx:746
TSystemDirectory
Definition: TSystemDirectory.h:32
TSystemDirectory::SetTitle
void SetTitle(const char *title)
Set the title of the TNamed.
Definition: TSystemDirectory.h:67
TSystemDirectory::FindDirObj
TSystemDirectory * FindDirObj(const char *name)
Method that returns system directory object if it exists in list, 0 otherwise.
Definition: TSystemDirectory.cxx:202
Int_t
int Int_t
Definition: RtypesCore.h:45
TBrowser.h
TString::EndsWith
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition: TString.cxx:2177
TList.h
TString
Definition: TString.h:136
b
#define b(i)
Definition: RSha256.hxx:118
TSystem::GetDirName
virtual TString GetDirName(const char *pathname)
Return the directory name in pathname.
Definition: TSystem.cxx:1030
bool
TSystem::WorkingDirectory
virtual const char * WorkingDirectory()
Return working directory.
Definition: TSystem.cxx:870
id
XFontStruct * id
Definition: TGX11.cxx:109
TSystemDirectory::fFilesInBrowser
TOrdCollection * fFilesInBrowser
Definition: TSystemDirectory.h:36
TSystemDirectory::SetDirectory
virtual void SetDirectory(const char *name)
Create a system directory object.
Definition: TSystemDirectory.cxx:116
TOrdCollection::At
TObject * At(Int_t idx) const
Returns the object at position idx. Returns 0 if idx is out of range.
Definition: TOrdCollection.cxx:160
TSystemDirectory.h
TOrdCollection.h
TSystemDirectory::~TSystemDirectory
virtual ~TSystemDirectory()
Delete system directory object.
Definition: TSystemDirectory.cxx:72
TSystemDirectory::operator=
TSystemDirectory & operator=(const TSystemDirectory &)
Assignment operator.
Definition: TSystemDirectory.cxx:59
TBrowser::kNoHidden
@ kNoHidden
Definition: TBrowser.h:53
TSystem.h
TSystemFile
Definition: TSystemFile.h:29
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TSystemDirectory::SetName
void SetName(const char *name)
Set the name of the TNamed.
Definition: TSystemDirectory.h:66
Long_t
long Long_t
Definition: RtypesCore.h:54
TSystemDirectory::FindFileObj
TSystemFile * FindFileObj(const char *name, const char *dir)
Method that returns system file object if it exists in list, 0 otherwise.
Definition: TSystemDirectory.cxx:217
gSystem
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
TSystem::OpenDirectory
virtual void * OpenDirectory(const char *name)
Open a directory. Returns 0 if directory does not exist.
Definition: TSystem.cxx:835
TCollection::GetSize
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
Definition: TCollection.h:182
file
Definition: file.py:1
TList::Add
virtual void Add(TObject *obj)
Definition: TList.h:87
TSystem::GetDirEntry
virtual const char * GetDirEntry(void *dirp)
Get a directory entry. Returns 0 if no more entries.
Definition: TSystem.cxx:852
name
char name[80]
Definition: TGX11.cxx:110
TSystemDirectory::fDirsInBrowser
TOrdCollection * fDirsInBrowser
Definition: TSystemDirectory.h:35
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
TSystemDirectory::Browse
virtual void Browse(TBrowser *b)
Browse OS system directories.
Definition: TSystemDirectory.cxx:144
TSystemFile::TSystemFile
TSystemFile()
TSystemFile default constructor.
Definition: TSystemFile.cxx:30
TList
Definition: TList.h:44
int