DirectoryIterator.h

Classes

DirectoryIterator -- Traverse the contents of a directory (full description)

class DirectoryIterator

Interface

Public Members
DirectoryIterator()
DirectoryIterator (const Directory& dir)
DirectoryIterator (const Directory& dir, const Regex& regExpression)
DirectoryIterator (const DirectoryIterator& that)
DirectoryIterator& operator= (const DirectoryIterator& that)
~DirectoryIterator()
void operator++()
void operator++(int)
String name() const
File file() const
void reset()
Bool pastEnd() const
Private Members
void init()

Description

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

Member Description

DirectoryIterator()

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

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.

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.

DirectoryIterator (const DirectoryIterator& that)

Copy constructor (copy semantics). The iterator will be positioned at the beginning.

DirectoryIterator& operator= (const DirectoryIterator& that)

Assignment (copy semantics). The iterator will be positioned at the beginning.

~DirectoryIterator()

void operator++()
void operator++(int)

Position on the next matching entry in the directory.
An exception is thrown if the iterator is already past the end.

String name() const

Returns the file name at the current position.
An exception is thrown if the iterator is already past the end.

File 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 reset()

Reposition the directory stream on the first entry.

Bool pastEnd() const

Checks if the iterator is past the end.

void init()

Initialize the iterator.