KaliVeda
Toolkit for HIC analysis
KVSystemDirectory.cpp
1 //Created by KVClassFactory on Mon Sep 24 10:45:52 2012
2 //Author: John Frankland,,,
3 
4 #include "KVSystemDirectory.h"
5 #include "TSystem.h"
6 #include "TList.h"
7 #include "TROOT.h"
8 #include "KVSystemFile.h"
9 #include "KVBase.h"
10 #include <iostream>
11 
13 
14 
15 
16 
20 {
21  // Default constructor
22  fContents = 0;
23  fSubdirs = 0;
24  fTotFiles = 0;
25  fTotDirs = 0;
26 }
27 
28 
29 
36 
38  : TSystemDirectory(dirname, path)
39 {
40  // Constructor with name for directory and path.
41  //
42  // Path can be given as "." for current working directory.
43  //
44  // Shell variables can be used if written as "$(VARIABLE)"
45  fContents = 0;
46  fSubdirs = 0;
47  fTotFiles = 0;
48  fTotDirs = 0;
49  auto Path = KVBase::AbsoluteUnixPath(path);
50  if (Path)
51  SetTitle(Path.value());
52  else
53  throw std::runtime_error(Form("<KVSystemDirectory::KVSystemDirectory> : invalid path %s given to constructor", path));
54  GetListings();
55 }
56 
57 
58 
59 
62 
64 {
65  // Destructor
68 }
69 
70 
71 
76 
78 {
79  // Returns a TList of KVSystemFile objects representing the contents
80  // of the directory.
81  // Returns 0 in case of errors.
82 
83  return fContents;
84 }
85 
86 
87 
92 
94 {
95  // Returns a TList of KVSystemDirectory objects representing the contents
96  // of the directory.
97  // Returns 0 in case of errors.
98 
99  return fSubdirs;
100 }
101 
102 
103 
106 
108 {
109  // Fill lists of files and directories
110 
111  void* dir = gSystem->OpenDirectory(GetTitle());
112  if (!dir) {
113  Error("GetListings", "Cannot open directory %s", GetTitle());
114  return;
115  }
116 
117  const char* file = 0;
118  fContents = new TList;
119  fContents->SetOwner();
120  fSubdirs = new TList;
121  fSubdirs->SetOwner();
122  while ((file = gSystem->GetDirEntry(dir))) {
123  if (IsItDirectory(file)) {
124  TString sdirpath = "";
125  if (file[0] == '.' && file[1] == '\0')
126  continue;
127  else if (file[0] == '.' && file[1] == '.' && file[2] == '\0')
128  continue;
129  else {
130  sdirpath = GetTitle();
131  if (!sdirpath.EndsWith("/"))
132  sdirpath += "/";
133  sdirpath += file;
134  }
135  if (sdirpath != "") {
136  KVSystemDirectory* sd = new KVSystemDirectory(file, sdirpath.Data());
137  fSubdirs->Add(sd);
138  fTotDirs += sd->GetTotalSize();
139  }
140  }
141  else {
142  KVSystemFile* sf = new KVSystemFile(file, GetTitle());
143  fContents->Add(sf);
144  fTotFiles += sf->GetSize();
145  }
146  }
147  gSystem->FreeDirectory(dir);
148 }
149 
150 
151 
155 
157 {
158  // if opt="nosubdirs", do not list contents of subdirectories
159  // default is to list recursively contents of all subdirectories
160 
162  printf("%s :\n[total : %lld / files : %lld bytes]\n\n",
166  GetListOfFiles()->R__FOR_EACH(KVSystemFile, ls)();
167  if (!strcmp(opt, "nosubdirs")) {
168  TIter next(GetListOfDirectories());
169  KVSystemDirectory* sd;
170  while ((sd = (KVSystemDirectory*)next())) {
172  printf("%-60s [total : %lld / files : %lld bytes]\n",
173  sd->GetTitle(), sd->GetTotalSize(), sd->GetTotalFiles());
174  }
175  }
176  else {
177  GetListOfDirectories()->R__FOR_EACH(KVSystemDirectory, ls)();
178  }
181  printf("\n");
182 }
183 
184 
#define SafeDelete(p)
char Char_t
const char Option_t
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
static std::optional< TString > AbsoluteUnixPath(const TString &)
Definition: KVBase.cpp:1729
Extension of ROOT TSystemDirectory class, handling browsing directories on disk.
KVSystemDirectory()
Default constructor.
virtual TList * GetListOfDirectories() const
TList * GetListOfFiles() const override
Long64_t fTotFiles
total size of files in directory
TList * fSubdirs
subdirectories of directory
Long64_t fTotDirs
total size of files in subdirectories
TList * fContents
contents of directory
Long64_t GetTotalFiles() const
virtual void GetListings()
Fill lists of files and directories.
void ls(Option_t *option="") const override
Long64_t GetTotalSize() const
virtual ~KVSystemDirectory()
Destructor.
Extended ROOT TSystemFile with added info on file size etc.
Definition: KVSystemFile.h:18
Long64_t GetSize() const
Definition: KVSystemFile.h:40
virtual void SetOwner(Bool_t enable=kTRUE)
void Add(TObject *obj) override
const char * GetTitle() const override
virtual void Error(const char *method, const char *msgfmt,...) const
static Int_t IncreaseDirLevel()
static void IndentLevel()
static Int_t DecreaseDirLevel()
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
const char * Data() const
Bool_t IsItDirectory(const char *name) const
void SetTitle(const char *title) override
virtual void FreeDirectory(void *dirp)
virtual void * OpenDirectory(const char *name)
virtual const char * GetDirEntry(void *dirp)
ClassImp(TPyArg)