KaliVeda
Toolkit for HIC analysis
KVDMS.cpp
1 //Created by KVClassFactory on Thu Oct 18 10:38:52 2012
2 //Author: John Frankland
3 
4 #include "KVDMS.h"
5 #include "TSystem.h"
6 #include "TList.h"
7 #include "TObjArray.h"
8 #include "TObjString.h"
9 #include "TROOT.h"
10 #include "Riostream.h"
11 
12 #include "KVUniqueNameList.h"
13 
14 using namespace std;
15 
17 
18 
19 
22 KVDMS::KVDMS(const Char_t* name, const Char_t* title)
23  : KVBase(name, title)
24 {
25  // Default ctor with name and title
26 }
27 
28 
29 
39 
40 Bool_t KVDMS::buildCommand(const Char_t* scmd, const Char_t* args, Option_t* opts)
41 {
42  // scmd = one of the DMS commands (init, ls, get, etc.)
43  // args = (optional) list of arguments for command (filename, etc.)
44  // opts = (optional) list of options for command
45  // If the command give in 'scmd' is found in the path specified by the
46  // user's PATH environment variables, this method returns kTRUE and
47  // private member variables fexec and fcmd will hold the full path to
48  // the executable and the full command line to pass to gSystem->Exec.
49  // If executable is not found, returns kFALSE.
50 
51  fexec = scmd;
52  if (!FindExecutable(fexec)) {
53  fexec = "";
54  fcmd = "";
55  return kFALSE;
56  }
57  fcmd = fexec + " ";
58  fcmd += opts;
59  fcmd += " ";
60  fcmd += args;
61  return kTRUE;
62 }
63 
64 
65 
70 
72 {
73  // Execute last command initialised with buildCommand() and return
74  // the value given by the operating system.
75  // Returns -1 if no command has been defined.
76 
77  if (fcmd == "") return -1;
78  return gSystem->Exec(fcmd.Data());
79 }
80 
81 
82 
86 
88 {
89  // Execute last command initialised with buildCommand() and return
90  // the value given by the operating system in a string.
91 
92  if (fcmd == "") return TString("");
93  fout = gSystem->GetFromPipe(fcmd.Data());
94  return fout;
95 }
96 
97 
98 
102 
104 {
105  // 'line' is a line in a directory listing
106  // This method should return kTRUE if the line corresponds to a container
107 
108  return (line.BeginsWith("C-"));
109 }
110 
111 
112 
119 
121 {
122  // \return list with info (name, size, modification date)
123  // on all files & containers in current directory
124  // (default) or in given directory.
125  //
126  // List is filled with DMSFile_t objects which belong to the list,
127 
128  longlist(directory);
129  if (fout == "") {
130  Error("GetFullListing", "Unknown directory %s", directory);
131  return {};
132  }
133 
134  std::unique_ptr<TObjArray> toks(fout.Tokenize("\n"));
135  KVUniqueNameList list(kTRUE);
136  list.SetOwner(kTRUE);
137  list.SetName(((TObjString*)(*toks)[0])->String().Remove(TString::kBoth, ' ').Data());
138  for (int i = 1; i < toks->GetEntries(); i++) {
139  TString tmp = ((TObjString*)(*toks)[i])->String().Remove(TString::kBoth, ' ');
140  DMSFile_t* f = new DMSFile_t;
141  if (IsContainer(tmp)) { // container
142  f->SetName(gSystem->BaseName(tmp.Data()));
143  f->SetIsContainer();
144  }
145  else {
146  ExtractFileInfos(tmp, f);
147  }
148  list.Add(f);
149  }
150  return list;
151 }
152 
153 
154 
158 
160 {
161  // Returns kTRUE if 'path' exists (file or directory, absolute or relative pathname).
162  // If so, KVDMSFile_t object will be filled with information on file/container
163 
164  TString filename = gSystem->BaseName(path);
165  TString dirname = gSystem->DirName(path);
166  if (DirectoryContains(filename.Data(), dirname.Data())) {
167  longlist(path);
168  fout.Remove(TString::kBoth, ' ');
169  fs.SetName(filename.Data());
170  if (IsContainer(fout)) { // container
171  fs.SetIsContainer();
172  }
173  else {
174  ExtractFileInfos(fout, &fs);
175  }
176  return kTRUE;
177  }
178  return kFALSE;
179 }
180 
181 
182 
188 
190 {
191  // Create and fill TList with just the names of files & containers in current directory
192  // (default) or in given directory.
193  //
194  // TList is filled with DMSFile_t objects which belong to the list
195 
196  list(directory);
197  if (fout == "") {
198  Error("GetListing", "Unknown directory %s", directory);
199  return {};
200  }
201 
202  std::unique_ptr<TObjArray> toks(fout.Tokenize("\n"));
203  KVUniqueNameList list(kTRUE);
204  list.SetOwner();
205  list.SetName(((TObjString*)(*toks)[0])->String().Remove(TString::kBoth, ' ').Data());
206  for (int i = 1; i < toks->GetEntries(); i++) {
207  TString tmp = ((TObjString*)(*toks)[i])->String().Remove(TString::kBoth, ' ');
208  DMSFile_t* f = new DMSFile_t;
209  if (IsContainer(tmp)) { // container
210  f->SetName(gSystem->BaseName(tmp.Data()));
211  f->SetIsContainer();
212  }
213  else {
214  f->SetName(tmp.Data());
215  }
216  list.Add(f);
217  }
218  return list;
219 }
220 
221 
222 
226 
227 Bool_t KVDMS::DirectoryContains(const Char_t* name, const Char_t* directory)
228 {
229  // Returns true if the current directory (default) or the given directory
230  // contains a file or a container with given name.
231 
232  auto list = GetListing(directory);
233  if (!list)
234  return kFALSE;
235  return list.FindObject(name);
236 }
237 
238 
240 
241 // DMSFile_t
243 //
244 // Describes Data Management System file/container attributes
246 
247 
248 
251 void DMSFile_t::ls(Option_t* /*opt*/) const
252 {
253  // List file/container attributes
254 
256  if (IsContainer())
257  cout << GetName() << "/" << endl;
258  else
259  cout << GetName() << "\t" << GetSize() << "\t" << GetModTime().AsString() << endl;
260 }
261 
262 
int Int_t
#define f(i)
bool Bool_t
char Char_t
constexpr Bool_t kFALSE
constexpr Bool_t kTRUE
const char Option_t
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize fs
char name[80]
R__EXTERN TSystem * gSystem
Base class for KaliVeda framework.
Definition: KVBase.h:139
Abstract base class for interfaces to Data Management Systems (SRB, IRODS, etc.)
Definition: KVDMS.h:73
Bool_t GetPathInfo(const Char_t *path, DMSFile_t &fs)
Definition: KVDMS.cpp:159
KVUniqueNameList GetListing(const Char_t *directory="")
Definition: KVDMS.cpp:189
KVUniqueNameList GetFullListing(const Char_t *directory="")
Definition: KVDMS.cpp:120
TString pipeCommand()
Definition: KVDMS.cpp:87
Bool_t DirectoryContains(const Char_t *name, const Char_t *directory="")
Definition: KVDMS.cpp:227
Int_t execCommand()
Definition: KVDMS.cpp:71
Bool_t buildCommand(const Char_t *scmd, const Char_t *args="", Option_t *opts="")
Definition: KVDMS.cpp:40
Bool_t IsContainer(TString &) const
Definition: KVDMS.cpp:103
void SetOwner(Bool_t enable=kTRUE) override
Optimised list in which named objects can only be placed once.
void Add(TObject *obj) override
void SetName(const char *name)
static void IndentLevel()
const char * Data() const
virtual const char * DirName(const char *pathname)
virtual Int_t Exec(const char *shellcmd)
virtual const char * BaseName(const char *pathname)
virtual TString GetFromPipe(const char *command)
TLine * line
void Error(const char *location, const char *fmt,...)
const char * String
ClassImp(TPyArg)