#include "TSystemDirectory.h"
#include "TSystem.h"
#include "TBrowser.h"
#include "TOrdCollection.h"
#include "TList.h"
ClassImp(TSystemDirectory);
TSystemDirectory::TSystemDirectory()
{
fDirsInBrowser = 0;
fFilesInBrowser = 0;
}
TSystemDirectory::TSystemDirectory(const char *dirname, const char *path) :
TSystemFile(dirname, path)
{
fDirsInBrowser = 0;
fFilesInBrowser = 0;
}
TSystemDirectory::TSystemDirectory(const TSystemDirectory& sd) :
TSystemFile(sd),
fDirsInBrowser(sd.fDirsInBrowser),
fFilesInBrowser(sd.fFilesInBrowser)
{
}
TSystemDirectory& TSystemDirectory::operator=(const TSystemDirectory& sd)
{
if(this!=&sd) {
TSystemFile::operator=(sd);
fDirsInBrowser=sd.fDirsInBrowser;
fFilesInBrowser=sd.fFilesInBrowser;
}
return *this;
}
TSystemDirectory::~TSystemDirectory()
{
delete fDirsInBrowser;
delete fFilesInBrowser;
}
TList *TSystemDirectory::GetListOfFiles() const
{
void *dir = gSystem->OpenDirectory(GetTitle());
if (!dir) return 0;
const char *file = 0;
TList *contents = new TList;
contents->SetOwner();
while ((file = gSystem->GetDirEntry(dir))) {
if (IsItDirectory(file)) {
TString sdirpath;
if (file[0] == '.' && file[1] == '\0')
sdirpath = GetTitle();
else if (file[0] == '.' && file[1] == '.' && file[2] == '.')
sdirpath = gSystem->DirName(GetTitle());
else {
sdirpath = GetTitle();
if (!sdirpath.EndsWith("/"))
sdirpath += "/";
sdirpath += file;
}
contents->Add(new TSystemDirectory(file, sdirpath.Data()));
} else
contents->Add(new TSystemFile(file, GetTitle()));
}
gSystem->FreeDirectory(dir);
return contents;
}
void TSystemDirectory::SetDirectory(const char *name)
{
SetName(name);
SetTitle(name);
}
Bool_t TSystemDirectory::IsItDirectory(const char *name) const
{
Long64_t size;
Long_t id, flags, modtime;
const char *dirfile = GetTitle();
gSystem->ChangeDirectory(dirfile);
flags = id = size = modtime = 0;
gSystem->GetPathInfo(name, &id, &size, &flags, &modtime);
Int_t isdir = (Int_t)flags & 2;
return isdir ? kTRUE : kFALSE;
}
void TSystemDirectory::Browse(TBrowser *b)
{
if (!fDirsInBrowser) fDirsInBrowser = new TOrdCollection;
if (!fFilesInBrowser) fFilesInBrowser = new TOrdCollection(10);
const char *name = GetTitle();
TSystemFile *sfile;
TSystemDirectory *sdir;
const char *file;
gSystem->ChangeDirectory(name);
if (GetName()[0] == '.' && GetName()[1] == '.')
SetName(gSystem->BaseName(name));
void *dir = gSystem->OpenDirectory(name);
if (!dir)
return;
while ((file = gSystem->GetDirEntry(dir))) {
if (b->TestBit(TBrowser::kNoHidden) && file[0] == '.' && file[1] != '.' )
continue;
if (IsItDirectory(file)) {
TString sdirpath;
if (!strcmp(file, "."))
sdirpath = name;
else if (!strcmp(file, ".."))
sdirpath = gSystem->DirName(name);
else {
sdirpath = name;
if (!sdirpath.EndsWith("/"))
sdirpath += "/";
sdirpath += file;
}
if (!(sdir = FindDirObj(sdirpath.Data()))) {
sdir = new TSystemDirectory(file, sdirpath.Data());
fDirsInBrowser->Add(sdir);
}
b->Add(sdir, file);
} else {
if (!(sfile = FindFileObj(file, gSystem->WorkingDirectory()))) {
sfile = new TSystemFile(file, gSystem->WorkingDirectory());
fFilesInBrowser->Add(sfile);
}
b->Add(sfile, file);
}
}
gSystem->FreeDirectory(dir);
}
TSystemDirectory *TSystemDirectory::FindDirObj(const char *name)
{
int size = fDirsInBrowser->GetSize();
for (int i = 0; i < size; i++) {
TSystemDirectory *obj = (TSystemDirectory *) fDirsInBrowser->At(i);
if (!strcmp(name, obj->GetTitle()))
return obj;
}
return 0;
}
TSystemFile *TSystemDirectory::FindFileObj(const char *name, const char *dir)
{
int size = fFilesInBrowser->GetSize();
for (int i = 0; i < size; i++) {
TSystemFile *obj = (TSystemFile *) fFilesInBrowser->At(i);
if (!strcmp(name, obj->GetName()) && !strcmp(dir, obj->GetTitle()))
return obj;
}
return 0;
}