source: trunk/MagicSoft/Mars/mbase/MDirIter.cc@ 2386

Last change on this file since 2386 was 2377, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 8.6 KB
Line 
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
71ClassImp(MDirIter);
72
73using 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//
90Int_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//
157void *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//
174void 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//
185TString 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//
195Bool_t MDirIter::MatchFilter(const TString &n, const TString &f) const
196{
197 return f.IsNull() || !n(TRegexp(f, kTRUE)).IsNull();
198}
199
200// --------------------------------------------------------------------------
201//
202// Check whether fqp is a directory.
203// Returns -1 if fqp couldn't be accesed, 0 if it is a directory,
204// 1 otherwise
205//
206Int_t MDirIter::IsDir(const char *fqp) const
207{
208 Long_t t[4];
209 if (gSystem->GetPathInfo(fqp, t, t+1, t+2, t+3))
210 return -1;
211
212 if (t[2]==3)
213 return 0;
214
215 return 1;
216}
217
218// --------------------------------------------------------------------------
219//
220// Check whether the current entry in the directory n is valid or not.
221// Entries must:
222// - not be . or ..
223// - match the associated filter
224// - match the global filter
225// - not be a directory
226// - have read permission
227//
228Bool_t MDirIter::Check(const TString n) const
229{
230 // Check . and ..
231 if (n=="." || n=="..")
232 return kFALSE;
233
234 // Check associated filter
235 if (!MatchFilter(n, fCurrentPath->GetTitle()))
236 return kFALSE;
237
238 // Check global filter
239 if (!MatchFilter(n, fFilter))
240 return kFALSE;
241
242 // Check for file or directory
243 const TString fqp = ConcatFileName(fCurrentPath->GetName(), n);
244 if (IsDir(fqp)<=0)
245 return kFALSE;
246
247 // Check for rread perissions
248 return !gSystem->AccessPathName(fqp, kReadPermission);
249}
250
251// --------------------------------------------------------------------------
252//
253// Return the Next file in the directory which is valid (see Check())
254// nocheck==1 returns the next entry unchecked
255//
256TString MDirIter::Next(Bool_t nocheck)
257{
258 fDirPtr = Open();
259 if (!fDirPtr)
260 return "";
261
262 // Get next entry in dir, if existing check validity
263 const char *n = gSystem->GetDirEntry(fDirPtr);
264 if (n)
265 return nocheck || Check(n) ? ConcatFileName(fCurrentPath->GetName(), n) : Next();
266
267 // Otherwise close directory and try to get next entry
268 Close();
269 return Next();
270}
271
272// --------------------------------------------------------------------------
273//
274// Print a single entry in the list
275//
276void MDirIter::PrintEntry(const TObject &o) const
277{
278 TString p = o.GetName();
279 TString f = o.GetTitle();
280 cout << p;
281 if (!f.IsNull())
282 cout << " <" << f << ">";
283 cout << endl;
284}
285
286// --------------------------------------------------------------------------
287//
288// Print all scheduled directories. If "all" is specified also all
289// matching entries are printed.
290//
291void MDirIter::Print(const Option_t *o) const
292{
293 TString s(o);
294 if (!s.Contains("all", TString::kIgnoreCase))
295 {
296 TIter Next(&fList);
297 TObject *o=NULL;
298 while ((o=Next()))
299 PrintEntry(*o);
300 return;
301 }
302
303 MDirIter Next(*this);
304 TString name;
305 TString d;
306 while (!(name=Next()).IsNull())
307 {
308 const TString p = Next.fCurrentPath->GetName();
309 if (p!=d)
310 {
311 d=p;
312 PrintEntry(*Next.fCurrentPath);
313 }
314 cout << " " << name << endl;
315 }
316}
Note: See TracBrowser for help on using the repository browser.