casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Private Member Functions | Private Attributes | List of all members
casacore::DirectoryIterator Class Reference

Traverse the contents of a directory. More...

#include <DirectoryIterator.h>

Public Member Functions

 DirectoryIterator ()
 Construct the iterator for the working directory. More...
 
 DirectoryIterator (const Directory &dir)
 Construct the iterator for the given directory. More...
 
 DirectoryIterator (const Directory &dir, const Regex &regExpression)
 Construct the iterator for the given directory. More...
 
 DirectoryIterator (const DirectoryIterator &that)
 Copy constructor (copy semantics). More...
 
DirectoryIteratoroperator= (const DirectoryIterator &that)
 Assignment (copy semantics). More...
 
 ~DirectoryIterator ()
 
void operator++ ()
 Position on the next matching entry in the directory. More...
 
void operator++ (int)
 
String name () const
 Returns the file name at the current position. More...
 
File file () const
 Returns a File object for the file at the current position. More...
 
void reset ()
 Reposition the directory stream on the first entry. More...
 
Bool pastEnd () const
 Checks if the iterator is past the end. More...
 

Private Member Functions

void init ()
 Initialize the iterator. More...
 

Private Attributes

DIR * itsDirectoryDescriptor
 This variable is used for seeking in the directory. More...
 
dirent * itsDirectoryEntry
 This structure is used for information of the directory. More...
 
Bool itsEnd
 Boolean to check if the directory stream has past the end. More...
 
Directory itsDirectory
 class directory More...
 
Regex itsExpression
 Regular expression if given, with this variable it is possible to compare files with regular expression. More...
 

Detailed Description

Traverse the contents of a directory.

Review Status

Reviewed By:
UNKNOWN
Date Reviewed:
before2004/08/25

Intended use:

Public interface

Prerequisite

Synopsis

DirectoryIterator allows to traverse a directory. In this way all file names in a directory can be gotten. Files. and.. will always be skipped.

By means of a regular expression it is possible to traverse the directory selectively. That is, only the file names matching the regular expression will be returned. Note that the regular expression is a true regular expression (as defined by class Regex and not a file expression as used in shells. Thus to get all.cc files, one has to specify ".*\.cc" and not "*.cc".

The File class can be used to determine if a file represents a symlink, directory or regular file.

Example

Directory dir("testdir");
// Get all.cc files.
// Note that Regex is a true regular expression and not a
// simplified file expression (like *.cc) as used in shells.
DirectoryIterator dirIter(dir, ".*.\cc");
while (!dirIter.pastEnd()){
cout << dirIter.name() << endl;
dirIter++;
}

Motivation

With this class it is easy to iterate through a directory.

To Do

Definition at line 96 of file DirectoryIterator.h.

Constructor & Destructor Documentation

casacore::DirectoryIterator::DirectoryIterator ( )

Construct the iterator for the working directory.

All entries (except. and..) will be traversed. It positions the iterator on the first entry.

casacore::DirectoryIterator::DirectoryIterator ( const Directory dir)

Construct the iterator for the given directory.

All entries (except. and..) will be traversed. It positions the iterator on the first entry.

casacore::DirectoryIterator::DirectoryIterator ( const Directory dir,
const Regex regExpression 
)

Construct the iterator for the given directory.

All entries matching the regular expression will be traversed. It positions the iterator on the first entry.

casacore::DirectoryIterator::DirectoryIterator ( const DirectoryIterator that)

Copy constructor (copy semantics).

The iterator will be positioned at the beginning.

casacore::DirectoryIterator::~DirectoryIterator ( )

Member Function Documentation

File casacore::DirectoryIterator::file ( ) const

Returns a File object for the file at the current position.

Note that this adds the path of the directory to get the correct path for the file.
An exception is thrown if the iterator is already past the end.

void casacore::DirectoryIterator::init ( )
private

Initialize the iterator.

String casacore::DirectoryIterator::name ( ) const

Returns the file name at the current position.


An exception is thrown if the iterator is already past the end.

void casacore::DirectoryIterator::operator++ ( )

Position on the next matching entry in the directory.


An exception is thrown if the iterator is already past the end.

void casacore::DirectoryIterator::operator++ ( int  )
DirectoryIterator& casacore::DirectoryIterator::operator= ( const DirectoryIterator that)

Assignment (copy semantics).

The iterator will be positioned at the beginning.

Bool casacore::DirectoryIterator::pastEnd ( ) const

Checks if the iterator is past the end.

void casacore::DirectoryIterator::reset ( )

Reposition the directory stream on the first entry.

Member Data Documentation

Directory casacore::DirectoryIterator::itsDirectory
private

class directory

Definition at line 164 of file DirectoryIterator.h.

DIR* casacore::DirectoryIterator::itsDirectoryDescriptor
private

This variable is used for seeking in the directory.

The directory is opened and closed once during the lifetime of the class.

Definition at line 155 of file DirectoryIterator.h.

dirent* casacore::DirectoryIterator::itsDirectoryEntry
private

This structure is used for information of the directory.

Definition at line 158 of file DirectoryIterator.h.

Bool casacore::DirectoryIterator::itsEnd
private

Boolean to check if the directory stream has past the end.

Definition at line 161 of file DirectoryIterator.h.

Regex casacore::DirectoryIterator::itsExpression
private

Regular expression if given, with this variable it is possible to compare files with regular expression.

Definition at line 168 of file DirectoryIterator.h.


The documentation for this class was generated from the following file: