Sponsored Link •
Listing 4 shows the
definition of the
which is where all the action happens. Because
affords only single-pass manipulation, only the Input Iterator concept  is supported. Iterator
copy construction and copy assignment semantics are supported by the use
of a reference-counted shared handle, of type
readdir_sequence::const_iterator::rds_shared_handle , to support iterator
instance copying. Multiple concurrent enumerations may be conducted by
begin() but since we're dealing with file systems,
whose contents may change at any time as a result of other processes,
subsequent ranges obtained from
begin() may not contain the
opendir() API returns all the entries in a particular
directory, irrespective of whether they are files or directories, and
includes the dots directories—"." and "..". Specifying files
without directories, or vice versa, as the second argument to the
readdir_sequence constructor causes only the matching entries
to be returned, which is a nice convenience. If neither are specified, it
defaults to returning both, because this conforms to the API's behaviour
and also because it is more efficient, as you'll see shortly. Since most
directory enumeration—in my experience, at least—is not
concerned with the dots directories, they are elided from the enumeration
range by default. Specifying the
readdir_sequence::includeDots flag causes them to be included
in the range.
Entry filtering is performed within
Detection of files and/or directories is done by calling
stat(), but this is only called when one type or the other is
to be returned; hence no unnecessary costs ensue . If the entry does not
for loop is not broken, and the next entry is
retrieved. Dot elision is similarly done by detecting whether the entry
is "." or ".." .
NULL the enumeration is
complete, and the iterator enters a state whereby a comparison with that
end() would evaluate to true, terminating the
client iteration loop (assuming it's written correctly!).
In my next article, I'll describe the mapping of the UNIX
glob() API, which supports a more refined STL Iterator model
, and presents a
number of different challenges to providing a simple and efficient
If you're interested, you can download the STLSoft libraries here.
std::basic_string, since you can use algorithms for them. It is compatible with the IOStreams, while having no knowledge of them whatsoever. But I'm not given to attempting to sell its use to others; we can settle on it being an internal STLSoft implementation component.
readdir_sequenceis not written to be thread-safe, so
rds_shared_handledoes not use atomic integer operations. If you want thread-safety, you must handle that in client code.
Discuss this article in the Articles Forum topic, Reading Unix-style Directories via STL-compliant Sequences.