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
15Describes 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{
34}
35
36////////////////////////////////////////////////////////////////////////////////
37/// Create a system directory object.
38
39TSystemDirectory::TSystemDirectory(const char *dirname, const char *path) :
40 TSystemFile(dirname, path)
41{
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 }
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 }
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
217TSystemFile *TSystemDirectory::FindFileObj(const char *name, const char *dir)
218{
219 int size = fFilesInBrowser->GetSize();
220 for (int i = 0; i < size; i++) {
222 if (!strcmp(name, obj->GetName()) && !strcmp(dir, obj->GetTitle()))
223 return obj;
224 }
225 return 0;
226}
#define b(i)
Definition: RSha256.hxx:100
int Int_t
Definition: RtypesCore.h:43
const Bool_t kFALSE
Definition: RtypesCore.h:90
long Long_t
Definition: RtypesCore.h:52
long long Long64_t
Definition: RtypesCore.h:71
const Bool_t kTRUE
Definition: RtypesCore.h:89
#define ClassImp(name)
Definition: Rtypes.h:361
XFontStruct * id
Definition: TGX11.cxx:108
char name[80]
Definition: TGX11.cxx:109
R__EXTERN TSystem * gSystem
Definition: TSystem.h:556
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:37
@ kNoHidden
Definition: TBrowser.h:53
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
Definition: TCollection.h:182
A doubly linked list.
Definition: TList.h:44
virtual void Add(TObject *obj)
Definition: TList.h:87
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
Definition: TNamed.cxx:51
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Ordered collection.
TObject * At(Int_t idx) const
Returns the object at position idx. Returns 0 if idx is out of range.
virtual void Add(TObject *obj)
Basic string class.
Definition: TString.h:131
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition: TString.cxx:2177
const char * Data() const
Definition: TString.h:364
Describes an Operating System directory for the browser.
virtual TList * GetListOfFiles() const
Returns a TList of TSystemFile objects representing the contents of the directory.
TSystemDirectory & operator=(const TSystemDirectory &)
Assignment operator.
virtual void SetDirectory(const char *name)
Create a system directory object.
void SetName(const char *name)
Set the name of the TNamed.
virtual ~TSystemDirectory()
Delete system directory object.
TSystemFile * FindFileObj(const char *name, const char *dir)
Method that returns system file object if it exists in list, 0 otherwise.
TSystemDirectory()
Create a system directory object.
Bool_t IsItDirectory(const char *name) const
Check if name is a directory.
void SetTitle(const char *title)
Set the title of the TNamed.
TOrdCollection * fFilesInBrowser
TSystemDirectory * FindDirObj(const char *name)
Method that returns system directory object if it exists in list, 0 otherwise.
virtual void Browse(TBrowser *b)
Browse OS system directories.
TOrdCollection * fDirsInBrowser
A TSystemFile describes an operating system file.
Definition: TSystemFile.h:29
TSystemFile()
TSystemFile default constructor.
Definition: TSystemFile.cxx:30
virtual void FreeDirectory(void *dirp)
Free a directory.
Definition: TSystem.cxx:841
virtual void * OpenDirectory(const char *name)
Open a directory. Returns 0 if directory does not exist.
Definition: TSystem.cxx:832
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:1393
virtual const char * GetDirEntry(void *dirp)
Get a directory entry. Returns 0 if no more entries.
Definition: TSystem.cxx:849
virtual Bool_t ChangeDirectory(const char *path)
Change directory.
Definition: TSystem.cxx:858
virtual const char * BaseName(const char *pathname)
Base name of a file name. Base name of /user/root is root.
Definition: TSystem.cxx:930
virtual const char * WorkingDirectory()
Return working directory.
Definition: TSystem.cxx:867
virtual TString GetDirName(const char *pathname)
Return the directory name in pathname.
Definition: TSystem.cxx:1027
Definition: file.py:1