1 | /* ======================================================================== *\
|
---|
2 | !
|
---|
3 | ! *
|
---|
4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction
|
---|
5 | ! * Software. It is distributed to you in the hope that it can be a useful
|
---|
6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
---|
7 | ! * It is distributed WITHOUT ANY WARRANTY.
|
---|
8 | ! *
|
---|
9 | ! * Permission to use, copy, modify and distribute this software and its
|
---|
10 | ! * documentation for any purpose is hereby granted without fee,
|
---|
11 | ! * provided that the above copyright notice appear in all copies and
|
---|
12 | ! * that both that copyright notice and this permission notice appear
|
---|
13 | ! * in supporting documentation. It is provided "as is" without express
|
---|
14 | ! * or implied warranty.
|
---|
15 | ! *
|
---|
16 | !
|
---|
17 | !
|
---|
18 | ! Author(s): Thomas Bretz, 6/2003 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2008
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 | /////////////////////////////////////////////////////////////////////////////
|
---|
26 | //
|
---|
27 | // MDirIter
|
---|
28 | //
|
---|
29 | // Iterator for files in several directories (with filters)
|
---|
30 | //
|
---|
31 | // Use this class if you want to get all filenames in a directory
|
---|
32 | // one-by-one.
|
---|
33 | //
|
---|
34 | // You can specify more than one directory (also recursivly) and you
|
---|
35 | // can use filters (eg. *.root)
|
---|
36 | //
|
---|
37 | // Here is an example which will print all *.root files in the current
|
---|
38 | // directory and all its subdirectories and all Gamma*.root files in
|
---|
39 | // the directory ../data.
|
---|
40 | //
|
---|
41 | // ------------------------------------------------------------------------
|
---|
42 | //
|
---|
43 | // // Instatiate the iterator
|
---|
44 | // MDirIter Next();
|
---|
45 | //
|
---|
46 | // // Add the current directory (for *.root files) and recursive
|
---|
47 | // // directories with the same filter
|
---|
48 | // Next.AddDirectory(".", "*.root", kTRUE);
|
---|
49 | // // Add the directory ../data, too (filter only Gamma*.root files)
|
---|
50 | // Next.AddDirectory("../data", "Gamma*.root");
|
---|
51 | //
|
---|
52 | // TString name;
|
---|
53 | // while (!(name=Next()).IsNull())
|
---|
54 | // cout << name << endl;
|
---|
55 | //
|
---|
56 | // ------------------------------------------------------------------------
|
---|
57 | //
|
---|
58 | // WARNING: If you specify relative directories (like in the example) the
|
---|
59 | // result may depend on the current working directory! Better use
|
---|
60 | // absolute paths.
|
---|
61 | //
|
---|
62 | /////////////////////////////////////////////////////////////////////////////
|
---|
63 | #include "MDirIter.h"
|
---|
64 |
|
---|
65 | #include <iostream>
|
---|
66 |
|
---|
67 | #include <TList.h>
|
---|
68 | #include <TNamed.h>
|
---|
69 | #include <TRegexp.h>
|
---|
70 | #include <TSystem.h>
|
---|
71 |
|
---|
72 | ClassImp(MDirIter);
|
---|
73 |
|
---|
74 | using namespace std;
|
---|
75 |
|
---|
76 | // --------------------------------------------------------------------------
|
---|
77 | //
|
---|
78 | // Add a directory, eg dir="../data"
|
---|
79 | // Using a filter (wildcards) will only return files matching this filter.
|
---|
80 | // recursive is the number of recursive directories (use 0 for none and -1
|
---|
81 | // for all)
|
---|
82 | // Returns the number of directories added.
|
---|
83 | // If a directory is added using a filter and the directory is already
|
---|
84 | // existing without a filter the filter is replaced.
|
---|
85 | // If any directory to be added is already existing with a different
|
---|
86 | // filter a new entry is created, eg:
|
---|
87 | // already existing: ../data <*.root>
|
---|
88 | // new entry: ../data <G*>
|
---|
89 | // The filters are or'ed.
|
---|
90 | //
|
---|
91 | Int_t MDirIter::AddDirectory(const char *d, const char *filter, Int_t recursive)
|
---|
92 | {
|
---|
93 | TString dir(d);
|
---|
94 |
|
---|
95 | // Sanity check
|
---|
96 | if (dir.IsNull())
|
---|
97 | return 0;
|
---|
98 |
|
---|
99 | #if ROOT_VERSION_CODE < ROOT_VERSION(3,05,05)
|
---|
100 | if (dir[dir.Length()-1]!='/')
|
---|
101 | dir += '/';
|
---|
102 | #else
|
---|
103 | if (!dir.EndsWith("/"))
|
---|
104 | dir += '/';
|
---|
105 | #endif
|
---|
106 | gSystem->ExpandPathName(dir);
|
---|
107 |
|
---|
108 | // Try to find dir in the list of existing dirs
|
---|
109 | TObject *o = fList.FindObject(dir);
|
---|
110 | if (o)
|
---|
111 | {
|
---|
112 | const TString t(o->GetTitle());
|
---|
113 |
|
---|
114 | // Check whether the existing dir has an associated filter
|
---|
115 | if (t.IsNull())
|
---|
116 | {
|
---|
117 | // Replace old filter by new one
|
---|
118 | ((TNamed*)o)->SetTitle(filter);
|
---|
119 | return 0;
|
---|
120 | }
|
---|
121 |
|
---|
122 | // If the filters are the same no action is taken
|
---|
123 | if (t==filter)
|
---|
124 | return 0;
|
---|
125 | }
|
---|
126 |
|
---|
127 | fList.Add(new TNamed((const char*)dir, filter ? filter : ""));
|
---|
128 |
|
---|
129 | // No recuresive directories, return
|
---|
130 | if (recursive==0)
|
---|
131 | return 1;
|
---|
132 |
|
---|
133 | Int_t rc = 1;
|
---|
134 |
|
---|
135 | // Create an iterator to iterate over all entries in the directory
|
---|
136 | MDirIter NextD(dir);
|
---|
137 |
|
---|
138 | TString c;
|
---|
139 | while (!(c=NextD(kTRUE)).IsNull())
|
---|
140 | {
|
---|
141 | // Do not process . and .. entries
|
---|
142 | if (c.EndsWith("/.") || c.EndsWith("/.."))
|
---|
143 | continue;
|
---|
144 |
|
---|
145 | // If entry is a directory add it with a lower recursivity
|
---|
146 | if (IsDir(c)==0)
|
---|
147 | rc += AddDirectory(c, filter, recursive-1);
|
---|
148 | }
|
---|
149 | return rc;
|
---|
150 | }
|
---|
151 |
|
---|
152 | // --------------------------------------------------------------------------
|
---|
153 | //
|
---|
154 | // Adds all entries from iter to this object
|
---|
155 | //
|
---|
156 | void MDirIter::Add(const MDirIter &iter)
|
---|
157 | {
|
---|
158 | TIter NextD(&iter.fList);
|
---|
159 | TObject *o=0;
|
---|
160 | while ((o=NextD()))
|
---|
161 | fList.Add(o->Clone());
|
---|
162 | }
|
---|
163 |
|
---|
164 | // --------------------------------------------------------------------------
|
---|
165 | //
|
---|
166 | // Return the pointer to the current directory. If the pointer is NULL
|
---|
167 | // a new directory is opened. If no new directory can be opened NULL is
|
---|
168 | // returned.
|
---|
169 | //
|
---|
170 | void *MDirIter::Open()
|
---|
171 | {
|
---|
172 | // Check whether a directory is already open
|
---|
173 | if (fDirPtr)
|
---|
174 | return fDirPtr;
|
---|
175 |
|
---|
176 | // Get Next entry of list
|
---|
177 | fCurrentPath=fNext();
|
---|
178 |
|
---|
179 | // Open directory if new entry was found
|
---|
180 | return fCurrentPath ? gSystem->OpenDirectory(fCurrentPath->GetName()) : NULL;
|
---|
181 | }
|
---|
182 |
|
---|
183 | // --------------------------------------------------------------------------
|
---|
184 | //
|
---|
185 | // Close directory is opened. Set fDirPtr=NULL
|
---|
186 | //
|
---|
187 | void MDirIter::Close()
|
---|
188 | {
|
---|
189 | if (fDirPtr)
|
---|
190 | gSystem->FreeDirectory(fDirPtr);
|
---|
191 | fDirPtr = NULL;
|
---|
192 | }
|
---|
193 |
|
---|
194 | // --------------------------------------------------------------------------
|
---|
195 | //
|
---|
196 | // Returns the concatenation of 'dir' and 'name'
|
---|
197 | //
|
---|
198 | TString MDirIter::ConcatFileName(const char *dir, const char *name) const
|
---|
199 | {
|
---|
200 | return TString(dir)+name;
|
---|
201 | }
|
---|
202 |
|
---|
203 | // --------------------------------------------------------------------------
|
---|
204 | //
|
---|
205 | // As the filter string may contain a + character, we have to replace
|
---|
206 | // this filter by a new filter contaning a \+ at all locations where a +
|
---|
207 | // was in the original filter.
|
---|
208 | //
|
---|
209 | // We replace:
|
---|
210 | // . by \\.
|
---|
211 | // + by \\+
|
---|
212 | // * by [^\\/:]*
|
---|
213 | // ? by .
|
---|
214 | //
|
---|
215 | // And surround the filter by ^ and $.
|
---|
216 | //
|
---|
217 | // For more details you can have a look at the template:
|
---|
218 | // TRegexp::MakeWildcard
|
---|
219 | //
|
---|
220 | const TRegexp MDirIter::MakeRegexp(TString n) const
|
---|
221 | {
|
---|
222 | n.Prepend("^");
|
---|
223 | n.ReplaceAll(".", "\\.");
|
---|
224 | n.ReplaceAll("+", "\\+");
|
---|
225 | n.ReplaceAll("*", "[^\\/:]*");
|
---|
226 | n.Append("$");
|
---|
227 |
|
---|
228 | return TRegexp(n, kFALSE);
|
---|
229 | }
|
---|
230 |
|
---|
231 | // --------------------------------------------------------------------------
|
---|
232 | //
|
---|
233 | // Check whether the given name n matches the filter f.
|
---|
234 | // Filters are of the form TRegexp(f, kTRUE)
|
---|
235 | //
|
---|
236 | Bool_t MDirIter::MatchFilter(const TString &n, const TString &f) const
|
---|
237 | {
|
---|
238 |
|
---|
239 | return f.IsNull() || !n(MakeRegexp(f)).IsNull();
|
---|
240 | }
|
---|
241 |
|
---|
242 | // --------------------------------------------------------------------------
|
---|
243 | //
|
---|
244 | // Check whether fqp is a directory.
|
---|
245 | // Returns -1 if fqp couldn't be accesed, 0 if it is a directory,
|
---|
246 | // 1 otherwise
|
---|
247 | //
|
---|
248 | Int_t MDirIter::IsDir(const char *fqp) const
|
---|
249 | {
|
---|
250 | Long_t t[4];
|
---|
251 | if (gSystem->GetPathInfo(fqp, t, t+1, t+2, t+3))
|
---|
252 | return -1;
|
---|
253 |
|
---|
254 | if (t[2]==3)
|
---|
255 | return 0;
|
---|
256 |
|
---|
257 | return 1;
|
---|
258 | }
|
---|
259 |
|
---|
260 | // --------------------------------------------------------------------------
|
---|
261 | //
|
---|
262 | // Check whether the current entry in the directory n is valid or not.
|
---|
263 | // Entries must:
|
---|
264 | // - not be . or ..
|
---|
265 | // - match the associated filter
|
---|
266 | // - match the global filter
|
---|
267 | // - not be a directory
|
---|
268 | // - have read permission
|
---|
269 | //
|
---|
270 | Bool_t MDirIter::CheckEntry(const TString n) const
|
---|
271 | {
|
---|
272 | // Check . and ..
|
---|
273 | if (n=="." || n=="..")
|
---|
274 | return kFALSE;
|
---|
275 |
|
---|
276 | // Check associated filter
|
---|
277 | if (!MatchFilter(n, fCurrentPath->GetTitle()))
|
---|
278 | return kFALSE;
|
---|
279 |
|
---|
280 | // Check global filter
|
---|
281 | if (!MatchFilter(n, fFilter))
|
---|
282 | return kFALSE;
|
---|
283 |
|
---|
284 | // Check for file or directory
|
---|
285 | const TString fqp = ConcatFileName(fCurrentPath->GetName(), n);
|
---|
286 | if (IsDir(fqp)<=0)
|
---|
287 | return kFALSE;
|
---|
288 |
|
---|
289 | // Check for rread perissions
|
---|
290 | return !gSystem->AccessPathName(fqp, kReadPermission);
|
---|
291 |
|
---|
292 | }
|
---|
293 |
|
---|
294 | // --------------------------------------------------------------------------
|
---|
295 | //
|
---|
296 | // Reset the iteration and strat from scratch. To do this correctly we have
|
---|
297 | // to reset the list of directories to iterate _and_ to close the current
|
---|
298 | // directory. When you call Next() the next time the first directory will
|
---|
299 | // be reopened again and you'll get the first entry.
|
---|
300 | //
|
---|
301 | // Do not try to only close the current directory or to reset the directory
|
---|
302 | // list only. This might not give the expected result!
|
---|
303 | //
|
---|
304 | void MDirIter::Reset()
|
---|
305 | {
|
---|
306 | Close();
|
---|
307 | fNext.Reset();
|
---|
308 | }
|
---|
309 |
|
---|
310 | // --------------------------------------------------------------------------
|
---|
311 | //
|
---|
312 | // Return the Next file in the directory which is valid (see Check())
|
---|
313 | // nocheck==1 returns the next entry unchecked
|
---|
314 | //
|
---|
315 | TString MDirIter::Next(Bool_t nocheck)
|
---|
316 | {
|
---|
317 | fDirPtr = Open();
|
---|
318 | if (!fDirPtr)
|
---|
319 | return "";
|
---|
320 |
|
---|
321 | // Get next entry in dir, if existing check validity
|
---|
322 | const char *n = gSystem->GetDirEntry(fDirPtr);
|
---|
323 | if (n)
|
---|
324 | return nocheck || CheckEntry(n) ? ConcatFileName(fCurrentPath->GetName(), n) : Next();
|
---|
325 |
|
---|
326 | // Otherwise close directory and try to get next entry
|
---|
327 | Close();
|
---|
328 | return Next();
|
---|
329 | }
|
---|
330 |
|
---|
331 | // --------------------------------------------------------------------------
|
---|
332 | //
|
---|
333 | // Print a single entry in the list
|
---|
334 | //
|
---|
335 | void MDirIter::PrintEntry(const TObject &o) const
|
---|
336 | {
|
---|
337 | TString p = o.GetName();
|
---|
338 | TString f = o.GetTitle();
|
---|
339 | cout << p;
|
---|
340 | if (!f.IsNull())
|
---|
341 | cout << " <" << f << ">";
|
---|
342 | cout << endl;
|
---|
343 | }
|
---|
344 |
|
---|
345 | // --------------------------------------------------------------------------
|
---|
346 | //
|
---|
347 | // Print all scheduled directories. If "all" is specified also all
|
---|
348 | // matching entries are printed.
|
---|
349 | //
|
---|
350 | void MDirIter::Print(const Option_t *opt) const
|
---|
351 | {
|
---|
352 | TString s(opt);
|
---|
353 | if (s.Contains("dbg", TString::kIgnoreCase))
|
---|
354 | fList.Print();
|
---|
355 |
|
---|
356 | if (!s.Contains("all", TString::kIgnoreCase))
|
---|
357 | {
|
---|
358 | TIter NextD(&fList);
|
---|
359 | TObject *o=NULL;
|
---|
360 | while ((o=NextD()))
|
---|
361 | PrintEntry(*o);
|
---|
362 | return;
|
---|
363 | }
|
---|
364 |
|
---|
365 | MDirIter NextD(*this);
|
---|
366 | TString name;
|
---|
367 | TString d;
|
---|
368 | while (!(name=NextD()).IsNull())
|
---|
369 | {
|
---|
370 | const TString p = NextD.fCurrentPath->GetName();
|
---|
371 | if (p!=d)
|
---|
372 | {
|
---|
373 | d=p;
|
---|
374 | PrintEntry(*NextD.fCurrentPath);
|
---|
375 | }
|
---|
376 | cout << " " << name << endl;
|
---|
377 | }
|
---|
378 | }
|
---|
379 |
|
---|
380 | // --------------------------------------------------------------------------
|
---|
381 | //
|
---|
382 | // Loop over all contents (files). Sort the files alphabetically.
|
---|
383 | // Delete the contents of this DirIter and add all sorted files
|
---|
384 | // to this DirIter.
|
---|
385 | //
|
---|
386 | void MDirIter::Sort()
|
---|
387 | {
|
---|
388 | MDirIter NextD(*this);
|
---|
389 |
|
---|
390 | TList l;
|
---|
391 | l.SetOwner();
|
---|
392 |
|
---|
393 | TString name;
|
---|
394 | while (!(name=NextD()).IsNull())
|
---|
395 | l.Add(new TNamed(name.Data(), ""));
|
---|
396 |
|
---|
397 | l.Sort();
|
---|
398 |
|
---|
399 | fList.Delete();
|
---|
400 | Close();
|
---|
401 | fFilter = "";
|
---|
402 |
|
---|
403 | TIter NextN(&l);
|
---|
404 | TObject *o=0;
|
---|
405 | while ((o=NextN()))
|
---|
406 | {
|
---|
407 | TString dir = o->GetName();
|
---|
408 | TString fname = o->GetName();
|
---|
409 |
|
---|
410 | const Int_t last = dir.Last('/');
|
---|
411 | if (last<0)
|
---|
412 | continue;
|
---|
413 |
|
---|
414 | dir.Remove(last);
|
---|
415 | fname.Remove(0, last+1);
|
---|
416 |
|
---|
417 | AddDirectory(dir, fname);
|
---|
418 | }
|
---|
419 | }
|
---|