casacore
Loading...
Searching...
No Matches
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: casa-feedback@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#ifndef CASA_DIRECTORYITERATOR_H
27#define CASA_DIRECTORYITERATOR_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/OS/File.h>
32#include <casacore/casa/OS/Directory.h>
33#include <casacore/casa/Utilities/Regex.h>
34
35#include <dirent.h> // needed for DIR
36
37
38namespace casacore { //# NAMESPACE CASACORE - BEGIN
39
40// <summary>
41// Traverse the contents of a directory
42// </summary>
43// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
44// </reviewed>
45
46// <use visibility=export>
47
48// <prerequisite>
49// <li> Basic knowledge of the UNIX file system
50// <li> <linkto class=Directory>Directory</linkto>
51// <li> possibly <linkto class=Regex>Regex</linkto>
52// </prerequisite>
53
54// <synopsis>
55// DirectoryIterator allows to traverse a directory. In this way all
56// file names in a directory can be gotten. Files . and .. will
57// always be skipped.
58// <p>
59// By means of a regular expression it is possible to traverse the
60// directory selectively. That is, only the file names matching the regular
61// expression will be returned. Note that the regular expression is
62// a true regular expression (as defined by class <linkto class=Regex>
63// Regex</linkto> and not a file expression as used in shells.
64// Thus to get all .cc files, one has to specify ".*\.cc" and not "*.cc".
65// <p>
66// The <linkto class=File>File</linkto> class can be used to determine if
67// a file represents a symlink, directory or regular file.
68// </synopsis>
69
70// <example>
71// <srcblock>
72// Directory dir("testdir");
73// // Get all .cc files.
74// // Note that Regex is a true regular expression and not a
75// // simplified file expression (like *.cc) as used in shells.
76// DirectoryIterator dirIter(dir, ".*.\cc");
77// while (!dirIter.pastEnd()){
78// cout << dirIter.name() << endl;
79// dirIter++;
80// }
81// </srcblock>
82// </example>
83
84// <motivation>
85// With this class it is easy to iterate through a directory.
86// </motivation>
87
88// <todo asof=$DATE$>
89// <li> Allow file expressions like *.cc.
90// However, it's probably better to make that part of Regex.
91// </todo>
92
93
95{
96public:
97
98 // Construct the iterator for the working directory.
99 // All entries (except . and ..) will be traversed.
100 // It positions the iterator on the first entry.
102
103 // Construct the iterator for the given directory.
104 // All entries (except . and ..) will be traversed.
105 // It positions the iterator on the first entry.
107
108 // Construct the iterator for the given directory.
109 // All entries matching the regular expression will be traversed.
110 // It positions the iterator on the first entry.
111 DirectoryIterator (const Directory& dir, const Regex& regExpression);
112
113 // Copy constructor (copy semantics).
114 // The iterator will be positioned at the beginning.
116
117 // Assignment (copy semantics).
118 // The iterator will be positioned at the beginning.
120
122
123 // Position on the next matching entry in the directory.
124 // <br>An exception is thrown if the iterator is already past the end.
125 // <group>
127 void operator++(int);
128 // </group>
129
130 // Returns the file name at the current position.
131 // <br>An exception is thrown if the iterator is already past the end.
132 String name() const;
133
134 // Returns a File object for the file at the current position.
135 // Note that this adds the path of the directory to get the
136 // correct path for the file.
137 // <br>An exception is thrown if the iterator is already past the end.
138 File file() const;
139
140 // Reposition the directory stream on the first entry.
141 void reset();
142
143 // Checks if the iterator is past the end.
144 Bool pastEnd() const;
145
146private:
147 // Initialize the iterator.
148 void init();
149
150 // This variable is used for seeking in the directory.
151 // The directory is opened and closed once during the lifetime
152 // of the class.
154
155 // This structure is used for information of the directory.
157
158 // Boolean to check if the directory stream has past the end
160
161 // class directory
163
164 // Regular expression if given, with this variable it is possible
165 // to compare files with regular expression.
167
168#if defined(AIPS_CRAY_PGI)
169 // Cray XT3 does not support readdir on compute nodes.
170 // Use scandir instead.
171 dirent** itsNameList;
172 int itsNrNames;
173 int itsNameInx;
174#endif
175};
176
177
178
179} //# NAMESPACE CASACORE - END
180
181#endif
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.
DirectoryIterator & operator=(const DirectoryIterator &that)
Assignment (copy semantics).
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:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40