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