Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
25 ClassImp(TSystemDirectory);
26 
27 ////////////////////////////////////////////////////////////////////////////////
28 /// Create a system directory object.
29 
30 TSystemDirectory::TSystemDirectory()
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 
49 TSystemDirectory::TSystemDirectory(const TSystemDirectory& sd) :
50  TSystemFile(sd),
51  fDirsInBrowser(sd.fDirsInBrowser),
52  fFilesInBrowser(sd.fFilesInBrowser)
53 {
54 }
55 
56 ////////////////////////////////////////////////////////////////////////////////
57 /// Assignment operator
58 
59 TSystemDirectory& TSystemDirectory::operator=(const TSystemDirectory& sd)
60 {
61  if(this!=&sd) {
62  TSystemFile::operator=(sd);
63  fDirsInBrowser=sd.fDirsInBrowser;
64  fFilesInBrowser=sd.fFilesInBrowser;
65  }
66  return *this;
67 }
68 
69 ////////////////////////////////////////////////////////////////////////////////
70 /// Delete system directory object.
71 
72 TSystemDirectory::~TSystemDirectory()
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 
84 TList *TSystemDirectory::GetListOfFiles() const
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 
116 void TSystemDirectory::SetDirectory(const char *name)
117 {
118  SetName(name);
119  SetTitle(name);
120 }
121 
122 ////////////////////////////////////////////////////////////////////////////////
123 /// Check if name is a directory.
124 
125 Bool_t TSystemDirectory::IsItDirectory(const char *name) const
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 
144 void TSystemDirectory::Browse(TBrowser *b)
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.
149  if (!fDirsInBrowser) fDirsInBrowser = new TOrdCollection;
150  if (!fFilesInBrowser) fFilesInBrowser = new TOrdCollection(10);
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 
202 TSystemDirectory *TSystemDirectory::FindDirObj(const char *name)
203 {
204  int size = fDirsInBrowser->GetSize();
205  for (int i = 0; i < size; i++) {
206  TSystemDirectory *obj = (TSystemDirectory *) fDirsInBrowser->At(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 }