#include <sbuild-dirstream.h>
Public Types | |
enum | error_code { DIR_OPEN, DIR_READ } |
Error codes. More... | |
typedef custom_error< error_code > | error |
Exception type. | |
Public Member Functions | |
dirstream (std::string const &dir) | |
The constructor. | |
virtual | ~dirstream () |
The destructor. | |
void | open (std::string const &dirname) |
Open a directory for reading. | |
void | close () |
Close the directory. | |
bool | eof () const |
Check for End Of File. | |
bool | bad () const |
Check for errors. | |
operator bool () | |
Check if the dirstream status is good. | |
bool | operator! () |
Check if the dirstream status is bad. | |
Private Member Functions | |
void | read (int quantity=1) |
Read dirents from the underlying DIR stream into the data deque. | |
Private Attributes | |
std::string | dirname |
The directory name. | |
DIR * | dir |
The underlying DIR stream. | |
std::deque< direntry > | data |
A list of direntries represents the directory stream as a LIFO stack. | |
bool | error_status |
Error status. | |
bool | eof_status |
End of File status. | |
Friends | |
dirstream & | operator>> (dirstream &stream, direntry &entry) |
The overloaded extraction operator. |
This is a wrapper around the opendir(3), readdir(3) and closedir(3) functions, which are used to read a stream of "dirents" through multiple readdir() calls.
dirstream calls opendir() and closedir() automatically, and represents each dirent as a dirstream::direntry. Like reading from and istream by pulling data out with the >> "extraction operator", direntries are also extracted from the dirstream with the >> operator.
Exception type.
dirstream::dirstream | ( | std::string const & | dir | ) |
dirstream::~dirstream | ( | ) | [virtual] |
void dirstream::open | ( | std::string const & | dirname | ) |
Open a directory for reading.
This uses the opendir(3) call to open the underlying DIR stream. Any previously open directory is closed before opening the new one. The dirstream error state is set if the open fails, and an exception will be thrown.
dirname | the directory to read. |
References dir, DIR_OPEN, eof_status, error_status, and read().
Referenced by dirstream().
void dirstream::close | ( | ) |
Close the directory.
This uses the closedir(3) call to close the underlying DIR stream. All cached data is deleted and the error state set until open() is called.
References data, dir, dirname, eof_status, and error_status.
Referenced by ~dirstream().
bool dirstream::eof | ( | ) | const |
Check for End Of File.
Note that the end of file status is only set adter a read fails, so this should be checked after each read.
References eof_status.
Referenced by operator bool(), and operator!().
bool dirstream::bad | ( | ) | const |
Check for errors.
If there is an error, the dirstream is unusable until the next open() call.
References error_status.
Referenced by operator bool(), and operator!().
sbuild::dirstream::operator bool | ( | ) |
bool sbuild::dirstream::operator! | ( | ) |
void dirstream::read | ( | int | quantity = 1 |
) | [private] |
Read dirents from the underlying DIR stream into the data deque.
If the read fails, the error state will be set, and an exception will be thrown.
quantity | the number of dirents to read. |
References data, dir, DIR_READ, dirname, and error_status.
Referenced by open(), and sbuild::operator>>().
std::string sbuild::dirstream::dirname [private] |
DIR* sbuild::dirstream::dir [private] |
std::deque<direntry> sbuild::dirstream::data [private] |
A list of direntries represents the directory stream as a LIFO stack.
Referenced by close(), sbuild::operator>>(), and read().
bool sbuild::dirstream::error_status [private] |
bool sbuild::dirstream::eof_status [private] |