DirectoryIterator Class Reference

#include <Pt/System/Directory.h>

Iterates over entries of a directory. More...

Public Member Functions

 DirectoryIterator ()
 Default constructor.
 DirectoryIterator (const Path &path)
 Constructs with directory path.
 DirectoryIterator (const FileInfo &fi)
 Constructs with directory.
 DirectoryIterator (const DirectoryIterator &it)
 Copy constructor.
 ~DirectoryIterator ()
bool operator!= (const DirectoryIterator &it) const
 Inequality comparison.
const FileInfooperator* () const
 Returns the file the iterator points at.
DirectoryIteratoroperator++ ()
 Advances the iterator to the next file.
const FileInfooperator-> () const
 Returns the file the iterator points at.
DirectoryIteratoroperator= (const DirectoryIterator &it)
 Assignment operator.
bool operator== (const DirectoryIterator &it) const
 Equality comparison.

Detailed Description

The Pt::System::DirectoryIterator can be used to iterate over the contents of directories. It is created with a path to a directory and satisfies the requirements for a forward iterator. The iterator successivly reads the contents of the assoziated directory and returns a FileInfo when dereferenced. Like the stream iterators of the C++ standard library, it changes to a special state when the end of the directory is reached. This state is identical to a default constructed iterator, so instances thereof can serve as the iterator to the end of the directory.

Pt::System::Path path = "/tmp";
for( ; it != end; ++it)
std::cout << "name : " << it->path().toLocal() << std::endl;
catch(const Pt::System::AccessFailed& e)
std::cerr << "failed to access directory" << std::endl;

The constructor throws an AccessFailed exception if the path is not a valid directory. The exception can be avoided by checking the path with FileInfo::type() first, to make sure it is valid. The DirectoryIterator can then be advanced to get the FileInfo for the next file in the directory, until the end of the directory contents is reached.