casacore
DirectoryIterator.h
Go to the documentation of this file.
1 //# DirectoryIterator.h: Traverse the contents of a directory
2 //# Copyright (C) 1996
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef CASA_DIRECTORYITERATOR_H
29 #define CASA_DIRECTORYITERATOR_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
33 #include <casacore/casa/OS/File.h>
34 #include <casacore/casa/OS/Directory.h>
35 #include <casacore/casa/Utilities/Regex.h>
36 
37 #include <dirent.h> // needed for DIR
38 
39 
40 namespace casacore { //# NAMESPACE CASACORE - BEGIN
41 
42 // <summary>
43 // Traverse the contents of a directory
44 // </summary>
45 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
46 // </reviewed>
47 
48 // <use visibility=export>
49 
50 // <prerequisite>
51 // <li> Basic knowledge of the UNIX file system
52 // <li> <linkto class=Directory>Directory</linkto>
53 // <li> possibly <linkto class=Regex>Regex</linkto>
54 // </prerequisite>
55 
56 // <synopsis>
57 // DirectoryIterator allows to traverse a directory. In this way all
58 // file names in a directory can be gotten. Files . and .. will
59 // always be skipped.
60 // <p>
61 // By means of a regular expression it is possible to traverse the
62 // directory selectively. That is, only the file names matching the regular
63 // expression will be returned. Note that the regular expression is
64 // a true regular expression (as defined by class <linkto class=Regex>
65 // Regex</linkto> and not a file expression as used in shells.
66 // Thus to get all .cc files, one has to specify ".*\.cc" and not "*.cc".
67 // <p>
68 // The <linkto class=File>File</linkto> class can be used to determine if
69 // a file represents a symlink, directory or regular file.
70 // </synopsis>
71 
72 // <example>
73 // <srcblock>
74 // Directory dir("testdir");
75 // // Get all .cc files.
76 // // Note that Regex is a true regular expression and not a
77 // // simplified file expression (like *.cc) as used in shells.
78 // DirectoryIterator dirIter(dir, ".*.\cc");
79 // while (!dirIter.pastEnd()){
80 // cout << dirIter.name() << endl;
81 // dirIter++;
82 // }
83 // </srcblock>
84 // </example>
85 
86 // <motivation>
87 // With this class it is easy to iterate through a directory.
88 // </motivation>
89 
90 // <todo asof=$DATE$>
91 // <li> Allow file expressions like *.cc.
92 // However, it's probably better to make that part of Regex.
93 // </todo>
94 
95 
97 {
98 public:
99 
100  // Construct the iterator for the working directory.
101  // All entries (except . and ..) will be traversed.
102  // It positions the iterator on the first entry.
104 
105  // Construct the iterator for the given directory.
106  // All entries (except . and ..) will be traversed.
107  // It positions the iterator on the first entry.
109 
110  // Construct the iterator for the given directory.
111  // All entries matching the regular expression will be traversed.
112  // It positions the iterator on the first entry.
113  DirectoryIterator (const Directory& dir, const Regex& regExpression);
114 
115  // Copy constructor (copy semantics).
116  // The iterator will be positioned at the beginning.
118 
119  // Assignment (copy semantics).
120  // The iterator will be positioned at the beginning.
122 
124 
125  // Position on the next matching entry in the directory.
126  // <br>An exception is thrown if the iterator is already past the end.
127  // <group>
128  void operator++();
129  void operator++(int);
130  // </group>
131 
132  // Returns the file name at the current position.
133  // <br>An exception is thrown if the iterator is already past the end.
134  String name() const;
135 
136  // Returns a File object for the file at the current position.
137  // Note that this adds the path of the directory to get the
138  // correct path for the file.
139  // <br>An exception is thrown if the iterator is already past the end.
140  File file() const;
141 
142  // Reposition the directory stream on the first entry.
143  void reset();
144 
145  // Checks if the iterator is past the end.
146  Bool pastEnd() const;
147 
148 private:
149  // Initialize the iterator.
150  void init();
151 
152  // This variable is used for seeking in the directory.
153  // The directory is opened and closed once during the lifetime
154  // of the class.
156 
157  // This structure is used for information of the directory.
159 
160  // Boolean to check if the directory stream has past the end
162 
163  // class directory
165 
166  // Regular expression if given, with this variable it is possible
167  // to compare files with regular expression.
169 
170 #if defined(AIPS_CRAY_PGI)
171  // Cray XT3 does not support readdir on compute nodes.
172  // Use scandir instead.
173  dirent** itsNameList;
174  int itsNrNames;
175  int itsNameInx;
176 #endif
177 };
178 
179 
180 
181 } //# NAMESPACE CASACORE - END
182 
183 #endif
DirectoryIterator & operator=(const DirectoryIterator &that)
Assignment (copy semantics).
String name() const
Returns the file name at the current position.
void init()
Initialize the iterator.
DIR * itsDirectoryDescriptor
This variable is used for seeking in the directory.
DirectoryIterator()
Construct the iterator for the working directory.
DirectoryIterator(const Directory &dir)
Construct the iterator for the given directory.
Bool itsEnd
Boolean to check if the directory stream has past the end.
void operator++()
Position on the next matching entry in the directory.
void reset()
Reposition the directory stream on the first entry.
Bool pastEnd() const
Checks if the iterator is past the end.
DirectoryIterator(const Directory &dir, const Regex &regExpression)
Construct the iterator for the given directory.
File file() const
Returns a File object for the file at the current position.
dirent * itsDirectoryEntry
This structure is used for information of the directory.
DirectoryIterator(const DirectoryIterator &that)
Copy constructor (copy semantics).
Directory itsDirectory
class directory
Regex itsExpression
Regular expression if given, with this variable it is possible to compare files with regular expressi...
String: the storage and methods of handling collections of characters.
Definition: String.h:225
this file contains all the compiler specific defines
Definition: mainpage.dox:28
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42