Logo ROOT   6.12/07
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),
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->DirName(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 
157  gSystem->ChangeDirectory(name);
158 
159  if (GetName()[0] == '.' && GetName()[1] == '.')
160  SetName(gSystem->BaseName(name));
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->DirName(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 }
TSystemFile()
TSystemFile default constructor.
Definition: TSystemFile.cxx:30
void Add(TObject *obj, const char *name=0, Int_t check=-1)
Add object with name to browser.
Definition: TBrowser.cxx:261
virtual const char * BaseName(const char *pathname)
Base name of a file name. Base name of /user/root is root.
Definition: TSystem.cxx:932
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual void Add(TObject *obj)
long long Long64_t
Definition: RtypesCore.h:69
virtual const char * WorkingDirectory()
Return working directory.
Definition: TSystem.cxx:869
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:1374
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
TSystemDirectory()
Create a system directory object.
virtual void SetDirectory(const char *name)
Create a system directory object.
virtual Bool_t ChangeDirectory(const char *path)
Change directory.
Definition: TSystem.cxx:860
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition: TObject.h:172
void SetName(const char *name)
Set the name of the TNamed.
Basic string class.
Definition: TString.h:125
int Int_t
Definition: RtypesCore.h:41
virtual const char * DirName(const char *pathname)
Return the directory name in pathname.
Definition: TSystem.cxx:1004
bool Bool_t
Definition: RtypesCore.h:59
virtual const char * GetDirEntry(void *dirp)
Get a directory entry. Returns 0 if no more entries.
Definition: TSystem.cxx:851
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition: TString.cxx:2231
XFontStruct * id
Definition: TGX11.cxx:108
Describes an Operating System directory for the browser.
A doubly linked list.
Definition: TList.h:44
TSystemDirectory & operator=(const TSystemDirectory &)
Assignment operator.
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:37
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
Definition: TNamed.cxx:51
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
void SetTitle(const char *title)
Set the title of the TNamed.
TSystemFile * FindFileObj(const char *name, const char *dir)
Method that returns system file object if it exists in list, 0 otherwise.
virtual void Browse(TBrowser *b)
Browse OS system directories.
virtual void FreeDirectory(void *dirp)
Free a directory.
Definition: TSystem.cxx:843
const Bool_t kFALSE
Definition: RtypesCore.h:88
long Long_t
Definition: RtypesCore.h:50
virtual TList * GetListOfFiles() const
Returns a TList of TSystemFile objects representing the contents of the directory.
#define ClassImp(name)
Definition: Rtypes.h:359
TOrdCollection * fDirsInBrowser
virtual ~TSystemDirectory()
Delete system directory object.
TOrdCollection * fFilesInBrowser
A TSystemFile describes an operating system file.
Definition: TSystemFile.h:29
virtual void Add(TObject *obj)
Definition: TList.h:87
Definition: file.py:1
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
Bool_t IsItDirectory(const char *name) const
Check if name is a directory.
TSystemDirectory * FindDirObj(const char *name)
Method that returns system directory object if it exists in list, 0 otherwise.
virtual void * OpenDirectory(const char *name)
Open a directory. Returns 0 if directory does not exist.
Definition: TSystem.cxx:834
virtual Int_t GetSize() const
Definition: TCollection.h:180
Ordered collection.
TObject * At(Int_t idx) const
Returns the object at position idx. Returns 0 if idx is out of range.
const Bool_t kTRUE
Definition: RtypesCore.h:87
char name[80]
Definition: TGX11.cxx:109
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
const char * Data() const
Definition: TString.h:345