Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
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
25
26////////////////////////////////////////////////////////////////////////////////
27/// Create a system directory object.
28
32
33////////////////////////////////////////////////////////////////////////////////
34/// Create a system directory object.
35
36TSystemDirectory::TSystemDirectory(const char *dirname, const char *path) :
37 TSystemFile(dirname, path)
38{
39}
40
41////////////////////////////////////////////////////////////////////////////////
42/// Delete system directory object.
43
49
50////////////////////////////////////////////////////////////////////////////////
51/// Returns a TList of TSystemFile objects representing the contents
52/// of the directory. It's the responsibility of the user to delete
53/// the list (the list owns the contained objects).
54/// Returns 0 in case of errors.
55
57{
58 void *dir = gSystem->OpenDirectory(GetTitle());
59 if (!dir) return nullptr;
60
61 const char *file = nullptr;
62 TList *contents = new TList;
63 contents->SetOwner();
64 while ((file = gSystem->GetDirEntry(dir))) {
65 if (IsItDirectory(file)) {
66 TString sdirpath;
67 if (file[0] == '.' && file[1] == '\0')
68 sdirpath = GetTitle();
69 else if (file[0] == '.' && file[1] == '.' && file[2] == '.')
70 sdirpath = gSystem->GetDirName(GetTitle());
71 else {
72 sdirpath = GetTitle();
73 if (!sdirpath.EndsWith("/"))
74 sdirpath += "/";
75 sdirpath += file;
76 }
77 contents->Add(new TSystemDirectory(file, sdirpath.Data()));
78 } else
79 contents->Add(new TSystemFile(file, GetTitle()));
80 }
81 gSystem->FreeDirectory(dir);
82 return contents;
83}
84
85////////////////////////////////////////////////////////////////////////////////
86/// Create a system directory object.
87
89{
92}
93
94////////////////////////////////////////////////////////////////////////////////
95/// Check if name is a directory.
96
98{
100 Long_t id, flags, modtime;
101 const char *dirfile = GetTitle();
102 TString savDir = gSystem->WorkingDirectory();
103
104 gSystem->ChangeDirectory(dirfile);
105 flags = id = size = modtime = 0;
106 gSystem->GetPathInfo(name, &id, &size, &flags, &modtime);
107 Int_t isdir = (Int_t)flags & 2;
108
109 gSystem->ChangeDirectory(savDir);
110 return isdir ? kTRUE : kFALSE;
111}
112
113////////////////////////////////////////////////////////////////////////////////
114/// Browse OS system directories.
115
117{
118 // Collections to keep track of all browser objects that have been
119 // generated. It's main goal is to prevent the continuous
120 // allocations of new objects with the same names during browsing.
123
124 const char *name = GetTitle();
125 TSystemFile *sfile;
126 TSystemDirectory *sdir;
127 const char *file;
128
129 gSystem->ChangeDirectory(name);
130
131 if (GetName()[0] == '.' && GetName()[1] == '.')
132 SetName(gSystem->BaseName(name));
133
134 void *dir = gSystem->OpenDirectory(name);
135
136 if (!dir)
137 return;
138
139 while ((file = gSystem->GetDirEntry(dir))) {
140 if (b->TestBit(TBrowser::kNoHidden) && file[0] == '.' && file[1] != '.' )
141 continue;
142 if (IsItDirectory(file)) {
143 TString sdirpath;
144 if (!strcmp(file, "."))
145 sdirpath = name;
146 else if (!strcmp(file, ".."))
147 sdirpath = gSystem->GetDirName(name);
148 else {
149 sdirpath = name;
150 if (!sdirpath.EndsWith("/"))
151 sdirpath += "/";
152 sdirpath += file;
153 }
154 if (!(sdir = FindDirObj(sdirpath.Data()))) {
155 sdir = new TSystemDirectory(file, sdirpath.Data());
156 fDirsInBrowser->Add(sdir);
157 }
158 b->Add(sdir, file);
159 } else {
160 if (!(sfile = FindFileObj(file, gSystem->WorkingDirectory()))) {
161 sfile = new TSystemFile(file, gSystem->WorkingDirectory());
162 fFilesInBrowser->Add(sfile);
163 }
164 b->Add(sfile, file);
165 }
166 }
167 gSystem->FreeDirectory(dir);
168}
169
170////////////////////////////////////////////////////////////////////////////////
171/// Method that returns system directory object if it
172/// exists in list, 0 otherwise.
173
175{
176 int size = fDirsInBrowser ? fDirsInBrowser->GetSize() : 0;
177 for (int i = 0; i < size; i++) {
179 if (!strcmp(name, obj->GetTitle()))
180 return obj;
181 }
182 return nullptr;
183}
184
185////////////////////////////////////////////////////////////////////////////////
186/// Method that returns system file object if it exists in
187/// list, 0 otherwise.
188
189TSystemFile *TSystemDirectory::FindFileObj(const char *name, const char *dir)
190{
191 int size = fFilesInBrowser ? fFilesInBrowser->GetSize() : 0;
192 for (int i = 0; i < size; i++) {
193 TSystemFile *obj = (TSystemFile *) fFilesInBrowser->At(i);
194 if (!strcmp(name, obj->GetName()) && !strcmp(dir, obj->GetTitle()))
195 return obj;
196 }
197 return nullptr;
198}
#define b(i)
Definition RSha256.hxx:100
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
long Long_t
Signed long integer 4 bytes (long). Size depends on architecture.
Definition RtypesCore.h:68
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:83
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
XFontStruct * id
Definition TGX11.cxx:147
char name[80]
Definition TGX11.cxx:148
externTSystem * gSystem
Definition TSystem.h:582
Using a TBrowser one can browse all ROOT objects.
Definition TBrowser.h:37
@ kNoHidden
Definition TBrowser.h:55
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
A doubly linked list.
Definition TList.h:38
void Add(TObject *obj) override
Definition TList.h:81
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
Ordered collection.
Basic string class.
Definition TString.h:138
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition TString.cxx:2250
const char * Data() const
Definition TString.h:384
virtual TList * GetListOfFiles() const
Returns a TList of TSystemFile objects representing the contents of the directory.
virtual void SetDirectory(const char *name)
Create a system directory object.
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.
TSystemDirectory(const TSystemDirectory &)=delete
TOrdCollection * fFilesInBrowser
void SetName(const char *name) override
Set the name of the TNamed.
TSystemDirectory * FindDirObj(const char *name)
Method that returns system directory object if it exists in list, 0 otherwise.
void Browse(TBrowser *b) override
Browse OS system directories.
void SetTitle(const char *title) override
Set the title of the TNamed.
TOrdCollection * fDirsInBrowser
TSystemFile()
TSystemFile default constructor.