Boost logo

Boost :

From: Chris Frey (cdfrey_at_[hidden])
Date: 2005-06-02 00:52:55


Hi,

I recently had a need to do directory lookups in C++ and thought I'd
take a look at boost::filesystem. I ran the sample_ls.cpp from the
documentation, and it worked great.

The problem is that directory_iterator appears to return a pointer to a
path object. This path object is then passed to functions such as
is_directory() to find the type.

On systems that support type information in the directory entry itself,
this structure limits what data can be returned in an iteration.

I would suggest something like (roughly):

        class path;
        class dirent;
        class directory_iterator {
                // ...
                const dirent * operator-> () const;
                // ...
        };

        class dirent {
                // some easy method to convert to path
                operator path ();
                // or perhaps a safer method would just be
                // to duplicate the path functions
                const std::string & leaf() const; // etc.

                // and then possible optimized versions
                bool is_directory() const;

                // ...
        };

The members of dirent would mirror the available functions that use
the path object. If no optimization is possible, it just calls what
the user would have had to call anyway. But if it is possible to
optimize certain items (such as a struct dirent containing d_type),
this would be used, possibly saving a call to stat().

Normally optimization should be left as implementation details... but
in this case, I believe the class design limits what optimization
is allowed. I would be pleased if I'm wrong on this.

Thanks for reading this far.
- Chris


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk