Boost logo

Boost :

From: me22 (me22.ca_at_[hidden])
Date: 2005-12-15 23:49:03


Hi all,

The discussion a little bit ago regarding using path's grammar in a
custom virtual filesystem reminded me of my experience when I made a
simple unpacking program using boost::filesystem::path. One thing
that wasn't as trivial as I might have liked was descending
recursively down the internal hierarchy.

It would have been nice to have head() and tail() decomposition
functions ( though with possibly better names ) analogous to the
branch_path() and leaf() functions, but for the front of the path.
That way I could have simply looked for the head() element at the
current level and recursed with tail(). ( Though I ended up doing it
by having my get_file function delegate to one that worked with an
iterator range, which is more efficient, but... )

The implementation is, of course, fairly trivial, something along the
lines of the following:
std::string path::head() {
    if ( empty() ) return std::string();
    return *begin();
}
path path::tail() {
    if ( empty() ) return path();
    return std::accumulate( next(begin()), end(), std::divides<path>() );
}
And the obvious has_head() and has_tail().

The usage would be something similar to:
file &directory::get_file(boost::filesystem::path p) {
    if ( !p.has_tail() ) return files[ p.head() ];
    return subdirectories[ p.head() ].get_file( p.tail() );
}
Assuming appropriate ( not part of boost ) directory and file classes.

Opinions? Interest?

- Scott McMurray

P.S. I'm particularly fond of the accumulate/divides hack :P


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