00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "TSystemDirectory.h"
00024 #include "TSystem.h"
00025 #include "TBrowser.h"
00026 #include "TOrdCollection.h"
00027 #include "TList.h"
00028
00029
00030 ClassImp(TSystemDirectory);
00031
00032
00033 TSystemDirectory::TSystemDirectory()
00034 {
00035
00036
00037 fDirsInBrowser = 0;
00038 fFilesInBrowser = 0;
00039 }
00040
00041
00042 TSystemDirectory::TSystemDirectory(const char *dirname, const char *path) :
00043 TSystemFile(dirname, path)
00044 {
00045
00046
00047 fDirsInBrowser = 0;
00048 fFilesInBrowser = 0;
00049 }
00050
00051
00052 TSystemDirectory::TSystemDirectory(const TSystemDirectory& sd) :
00053 TSystemFile(sd),
00054 fDirsInBrowser(sd.fDirsInBrowser),
00055 fFilesInBrowser(sd.fFilesInBrowser)
00056 {
00057
00058 }
00059
00060
00061 TSystemDirectory& TSystemDirectory::operator=(const TSystemDirectory& sd)
00062 {
00063
00064 if(this!=&sd) {
00065 TSystemFile::operator=(sd);
00066 fDirsInBrowser=sd.fDirsInBrowser;
00067 fFilesInBrowser=sd.fFilesInBrowser;
00068 }
00069 return *this;
00070 }
00071
00072
00073 TSystemDirectory::~TSystemDirectory()
00074 {
00075
00076
00077 delete fDirsInBrowser;
00078 delete fFilesInBrowser;
00079 }
00080
00081
00082 TList *TSystemDirectory::GetListOfFiles() const
00083 {
00084
00085
00086
00087
00088
00089 void *dir = gSystem->OpenDirectory(GetTitle());
00090 if (!dir) return 0;
00091
00092 const char *file = 0;
00093 TList *contents = new TList;
00094 contents->SetOwner();
00095 while ((file = gSystem->GetDirEntry(dir))) {
00096 if (IsItDirectory(file)) {
00097 TString sdirpath;
00098 if (file[0] == '.' && file[1] == '\0')
00099 sdirpath = GetTitle();
00100 else if (file[0] == '.' && file[1] == '.' && file[2] == '.')
00101 sdirpath = gSystem->DirName(GetTitle());
00102 else {
00103 sdirpath = GetTitle();
00104 if (!sdirpath.EndsWith("/"))
00105 sdirpath += "/";
00106 sdirpath += file;
00107 }
00108 contents->Add(new TSystemDirectory(file, sdirpath.Data()));
00109 } else
00110 contents->Add(new TSystemFile(file, GetTitle()));
00111 }
00112 gSystem->FreeDirectory(dir);
00113 return contents;
00114 }
00115
00116
00117 void TSystemDirectory::SetDirectory(const char *name)
00118 {
00119
00120
00121 SetName(name);
00122 SetTitle(name);
00123 }
00124
00125
00126 Bool_t TSystemDirectory::IsItDirectory(const char *name) const
00127 {
00128
00129
00130 Long64_t size;
00131 Long_t id, flags, modtime;
00132 const char *dirfile = GetTitle();
00133
00134 gSystem->ChangeDirectory(dirfile);
00135 flags = id = size = modtime = 0;
00136 gSystem->GetPathInfo(name, &id, &size, &flags, &modtime);
00137 Int_t isdir = (Int_t)flags & 2;
00138
00139 return isdir ? kTRUE : kFALSE;
00140 }
00141
00142
00143 void TSystemDirectory::Browse(TBrowser *b)
00144 {
00145
00146
00147
00148
00149
00150 if (!fDirsInBrowser) fDirsInBrowser = new TOrdCollection;
00151 if (!fFilesInBrowser) fFilesInBrowser = new TOrdCollection(10);
00152
00153 const char *name = GetTitle();
00154 TSystemFile *sfile;
00155 TSystemDirectory *sdir;
00156 const char *file;
00157
00158 gSystem->ChangeDirectory(name);
00159
00160 if (GetName()[0] == '.' && GetName()[1] == '.')
00161 SetName(gSystem->BaseName(name));
00162
00163 void *dir = gSystem->OpenDirectory(name);
00164
00165 if (!dir)
00166 return;
00167
00168 while ((file = gSystem->GetDirEntry(dir))) {
00169 if (b->TestBit(TBrowser::kNoHidden) && file[0] == '.' && file[1] != '.' )
00170 continue;
00171 if (IsItDirectory(file)) {
00172 TString sdirpath;
00173 if (!strcmp(file, "."))
00174 sdirpath = name;
00175 else if (!strcmp(file, ".."))
00176 sdirpath = gSystem->DirName(name);
00177 else {
00178 sdirpath = name;
00179 if (!sdirpath.EndsWith("/"))
00180 sdirpath += "/";
00181 sdirpath += file;
00182 }
00183 if (!(sdir = FindDirObj(sdirpath.Data()))) {
00184 sdir = new TSystemDirectory(file, sdirpath.Data());
00185 fDirsInBrowser->Add(sdir);
00186 }
00187 b->Add(sdir, file);
00188 } else {
00189 if (!(sfile = FindFileObj(file, gSystem->WorkingDirectory()))) {
00190 sfile = new TSystemFile(file, gSystem->WorkingDirectory());
00191 fFilesInBrowser->Add(sfile);
00192 }
00193 b->Add(sfile, file);
00194 }
00195 }
00196 gSystem->FreeDirectory(dir);
00197 }
00198
00199
00200 TSystemDirectory *TSystemDirectory::FindDirObj(const char *name)
00201 {
00202
00203
00204
00205 int size = fDirsInBrowser->GetSize();
00206 for (int i = 0; i < size; i++) {
00207 TSystemDirectory *obj = (TSystemDirectory *) fDirsInBrowser->At(i);
00208 if (!strcmp(name, obj->GetTitle()))
00209 return obj;
00210 }
00211 return 0;
00212 }
00213
00214
00215 TSystemFile *TSystemDirectory::FindFileObj(const char *name, const char *dir)
00216 {
00217
00218
00219
00220 int size = fFilesInBrowser->GetSize();
00221 for (int i = 0; i < size; i++) {
00222 TSystemFile *obj = (TSystemFile *) fFilesInBrowser->At(i);
00223 if (!strcmp(name, obj->GetName()) && !strcmp(dir, obj->GetTitle()))
00224 return obj;
00225 }
00226 return 0;
00227 }