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-2003
|
---|
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 <TNamed.h>
|
---|
68 | #include <TRegexp.h>
|
---|
69 | #include <TSystem.h>
|
---|
70 |
|
---|
71 | ClassImp(MDirIter);
|
---|
72 |
|
---|
73 | using namespace std;
|
---|
74 |
|
---|
75 | // --------------------------------------------------------------------------
|
---|
76 | //
|
---|
77 | // Add a directory, eg dir="../data"
|
---|
78 | // Using a filter (wildcards) will only return files matching this filter.
|
---|
79 | // recursive is the number of recursive directories (use 0 for none and -1
|
---|
80 | // for all)
|
---|
81 | // Returns the number of directories added.
|
---|
82 | // If a directory is added using a filter and the directory is already
|
---|
83 | // existing without a filter the filter is replaced.
|
---|
84 | // If any directory to be added is already existing with a different
|
---|
85 | // filter a new entry is created, eg:
|
---|
86 | // already existing: ../data <*.root>
|
---|
87 | // new entry: ../data <G*>
|
---|
88 | // The filters are or'ed.
|
---|
89 | //
|
---|
90 | Int_t MDirIter::AddDirectory(const char *d, const char *filter, Int_t recursive)
|
---|
91 | {
|
---|
92 | TString dir = d;
|
---|
93 |
|
---|
94 | // Sanity check
|
---|
95 | if (dir.IsNull())
|
---|
96 | return 0;
|
---|
97 |
|
---|
98 | #if ROOT_VERSION_CODE < ROOT_VERSION(3,05,05)
|
---|
99 | if (dir[dir.Length()-1]!='/')
|
---|
100 | dir += '/';
|
---|
101 | #else
|
---|
102 | if (!dir.EndsWith("/"))
|
---|
103 | dir += '/';
|
---|
104 | #endif
|
---|
105 | gSystem->ExpandPathName(dir);
|
---|
106 |
|
---|
107 | // Try to find dir in the list of existing dirs
|
---|
108 | TObject *o = fList.FindObject(dir);
|
---|
109 | if (o)
|
---|
110 | {
|
---|
111 | const TString t(o->GetTitle());
|
---|
112 |
|
---|
113 | // Check whether the existing dir has an associated filter
|
---|
114 | if (t.IsNull())
|
---|
115 | {
|
---|
116 | // Replace old filter by new one
|
---|
117 | ((TNamed*)o)->SetTitle(filter);
|
---|
118 | return 0;
|
---|
119 | }
|
---|
120 |
|
---|
121 | // If the filters are the same no action is taken
|
---|
122 | if (t==filter)
|
---|
123 | return 0;
|
---|
124 | }
|
---|
125 |
|
---|
126 | fList.Add(new TNamed((const char*)dir, filter ? filter : ""));
|
---|
127 |
|
---|
128 | // No recuresive directories, return
|
---|
129 | if (recursive==0)
|
---|
130 | return 1;
|
---|
131 |
|
---|
132 | Int_t rc = 1;
|
---|
133 |
|
---|
134 | // Create an iterator to iterate over all entries in the directory
|
---|
135 | MDirIter Next(dir);
|
---|
136 |
|
---|
137 | TString c;
|
---|
138 | while (!(c=Next(kTRUE)).IsNull())
|
---|
139 | {
|
---|
140 | // Do not process . and .. entries
|
---|
141 | if (c.EndsWith("/.") || c.EndsWith("/.."))
|
---|
142 | continue;
|
---|
143 |
|
---|
144 | // If entry is a directory add it with a lowere recursivity
|
---|
145 | if (IsDir(c)==0)
|
---|
146 | rc += AddDirectory(c, filter, recursive-1);
|
---|
147 | }
|
---|
148 | return rc;
|
---|
149 | }
|
---|
150 |
|
---|
151 | // --------------------------------------------------------------------------
|
---|
152 | //
|
---|
153 | // Return the pointer to the current directory. If the pointer is NULL
|
---|
154 | // a new directory is opened. If no new directory can be opened NULL is
|
---|
155 | // returned.
|
---|
156 | //
|
---|
157 | void *MDirIter::Open()
|
---|
158 | {
|
---|
159 | // Check whether a directory is already open
|
---|
160 | if (fDirPtr)
|
---|
161 | return fDirPtr;
|
---|
162 |
|
---|
163 | // Get Next entry of list
|
---|
164 | fCurrentPath=fNext();
|
---|
165 |
|
---|
166 | // Open directory if new entry was found
|
---|
167 | return fCurrentPath ? gSystem->OpenDirectory(fCurrentPath->GetName()) : NULL;
|
---|
168 | }
|
---|
169 |
|
---|
170 | // --------------------------------------------------------------------------
|
---|
171 | //
|
---|
172 | // Close directory is opened. Set fDirPtr=NULL
|
---|
173 | //
|
---|
174 | void MDirIter::Close()
|
---|
175 | {
|
---|
176 | if (fDirPtr)
|
---|
177 | gSystem->FreeDirectory(fDirPtr);
|
---|
178 | fDirPtr = NULL;
|
---|
179 | }
|
---|
180 |
|
---|
181 | // --------------------------------------------------------------------------
|
---|
182 | //
|
---|
183 | // Returns the concatenation of 'dir' and 'name'
|
---|
184 | //
|
---|
185 | TString MDirIter::ConcatFileName(const char *dir, const char *name) const
|
---|
186 | {
|
---|
187 | return TString(dir)+name;
|
---|
188 | }
|
---|
189 |
|
---|
190 | // --------------------------------------------------------------------------
|
---|
191 | //
|
---|
192 | // Check whether the given name n matches the filter f.
|
---|
193 | // Filters are of the form TRegexp(f, kTRUE)
|
---|
194 | //
|
---|
195 | Bool_t MDirIter::MatchFilter(const TString &n, const TString &f) const
|
---|
196 | {
|
---|
197 | // As the filter string may contain a + character, we have to replace
|
---|
198 | // this filter by a new filter contaning a \+ at all locations where a +
|
---|
199 | // was in the original filter.
|
---|
200 | TString nf(f);
|
---|
201 | nf.ReplaceAll("+","\\+");
|
---|
202 |
|
---|
203 | return f.IsNull() || !n(TRegexp(nf, kTRUE)).IsNull();
|
---|
204 | }
|
---|
205 |
|
---|
206 | // --------------------------------------------------------------------------
|
---|
207 | //
|
---|
208 | // Check whether fqp is a directory.
|
---|
209 | // Returns -1 if fqp couldn't be accesed, 0 if it is a directory,
|
---|
210 | // 1 otherwise
|
---|
211 | //
|
---|
212 | Int_t MDirIter::IsDir(const char *fqp) const
|
---|
213 | {
|
---|
214 | Long_t t[4];
|
---|
215 | if (gSystem->GetPathInfo(fqp, t, t+1, t+2, t+3))
|
---|
216 | return -1;
|
---|
217 |
|
---|
218 | if (t[2]==3)
|
---|
219 | return 0;
|
---|
220 |
|
---|
221 | return 1;
|
---|
222 | }
|
---|
223 |
|
---|
224 | // --------------------------------------------------------------------------
|
---|
225 | //
|
---|
226 | // Check whether the current entry in the directory n is valid or not.
|
---|
227 | // Entries must:
|
---|
228 | // - not be . or ..
|
---|
229 | // - match the associated filter
|
---|
230 | // - match the global filter
|
---|
231 | // - not be a directory
|
---|
232 | // - have read permission
|
---|
233 | //
|
---|
234 | Bool_t MDirIter::Check(const TString n) const
|
---|
235 | {
|
---|
236 | // Check . and ..
|
---|
237 | if (n=="." || n=="..")
|
---|
238 | return kFALSE;
|
---|
239 |
|
---|
240 | // Check associated filter
|
---|
241 | if (!MatchFilter(n, fCurrentPath->GetTitle()))
|
---|
242 | return kFALSE;
|
---|
243 |
|
---|
244 | // Check global filter
|
---|
245 | if (!MatchFilter(n, fFilter))
|
---|
246 | return kFALSE;
|
---|
247 |
|
---|
248 | // Check for file or directory
|
---|
249 | const TString fqp = ConcatFileName(fCurrentPath->GetName(), n);
|
---|
250 | if (IsDir(fqp)<=0)
|
---|
251 | return kFALSE;
|
---|
252 |
|
---|
253 | // Check for rread perissions
|
---|
254 | return !gSystem->AccessPathName(fqp, kReadPermission);
|
---|
255 |
|
---|
256 | }
|
---|
257 |
|
---|
258 | // --------------------------------------------------------------------------
|
---|
259 | //
|
---|
260 | // Reset the iteration and strat from scratch. To do this correctly we have
|
---|
261 | // to reset the list of directories to iterate _and_ to close the current
|
---|
262 | // directory. When you call Next() the next time the first directory will
|
---|
263 | // be reopened again and you'll get the first entry.
|
---|
264 | //
|
---|
265 | // Do not try to only close the current directory or to reset the directory
|
---|
266 | // list only. This might not give the expected result!
|
---|
267 | //
|
---|
268 | void MDirIter::Reset()
|
---|
269 | {
|
---|
270 | Close();
|
---|
271 | fNext.Reset();
|
---|
272 | }
|
---|
273 |
|
---|
274 | // --------------------------------------------------------------------------
|
---|
275 | //
|
---|
276 | // Return the Next file in the directory which is valid (see Check())
|
---|
277 | // nocheck==1 returns the next entry unchecked
|
---|
278 | //
|
---|
279 | TString MDirIter::Next(Bool_t nocheck)
|
---|
280 | {
|
---|
281 | fDirPtr = Open();
|
---|
282 | if (!fDirPtr)
|
---|
283 | return "";
|
---|
284 |
|
---|
285 | // Get next entry in dir, if existing check validity
|
---|
286 | const char *n = gSystem->GetDirEntry(fDirPtr);
|
---|
287 | if (n)
|
---|
288 | return nocheck || Check(n) ? ConcatFileName(fCurrentPath->GetName(), n) : Next();
|
---|
289 |
|
---|
290 | // Otherwise close directory and try to get next entry
|
---|
291 | Close();
|
---|
292 | return Next();
|
---|
293 | }
|
---|
294 |
|
---|
295 | // --------------------------------------------------------------------------
|
---|
296 | //
|
---|
297 | // Print a single entry in the list
|
---|
298 | //
|
---|
299 | void MDirIter::PrintEntry(const TObject &o) const
|
---|
300 | {
|
---|
301 | TString p = o.GetName();
|
---|
302 | TString f = o.GetTitle();
|
---|
303 | cout << p;
|
---|
304 | if (!f.IsNull())
|
---|
305 | cout << " <" << f << ">";
|
---|
306 | cout << endl;
|
---|
307 | }
|
---|
308 |
|
---|
309 | // --------------------------------------------------------------------------
|
---|
310 | //
|
---|
311 | // Print all scheduled directories. If "all" is specified also all
|
---|
312 | // matching entries are printed.
|
---|
313 | //
|
---|
314 | void MDirIter::Print(const Option_t *o) const
|
---|
315 | {
|
---|
316 | TString s(o);
|
---|
317 | if (!s.Contains("all", TString::kIgnoreCase))
|
---|
318 | {
|
---|
319 | TIter Next(&fList);
|
---|
320 | TObject *o=NULL;
|
---|
321 | while ((o=Next()))
|
---|
322 | PrintEntry(*o);
|
---|
323 | return;
|
---|
324 | }
|
---|
325 |
|
---|
326 | MDirIter Next(*this);
|
---|
327 | TString name;
|
---|
328 | TString d;
|
---|
329 | while (!(name=Next()).IsNull())
|
---|
330 | {
|
---|
331 | const TString p = Next.fCurrentPath->GetName();
|
---|
332 | if (p!=d)
|
---|
333 | {
|
---|
334 | d=p;
|
---|
335 | PrintEntry(*Next.fCurrentPath);
|
---|
336 | }
|
---|
337 | cout << " " << name << endl;
|
---|
338 | }
|
---|
339 | }
|
---|