Boost logo

Boost :

From: Vladimir Prus (ghost_at_[hidden])
Date: 2004-04-26 04:02:30


Martin wrote:

> I have used the filesystem library for some time now and noticed that
> there is no portable way of iterating over files in a directory that
> matches a pattern.
>
> In my applications I often need to find all files in a directory that
> matches "*.txt" or "input.*".
>
> I think it would be a good idea to add another constructor to the
> directory_iterator that support some primitive wilcards. The support could
> be limited to "*" at the end or followed by a "."
> (e.g. "*", "a*.*", "*.txt", "a.*")

Well, supposing that one has a 'shell wildcard matcher' it should be
possible to do:

    typedef filter_iterator<wildcard_matcher, directory_iterator> it;
    for(it i = it(wildcard_matcher("*.txt), directory_iterator("dir")), e;
        i != e;
        ++i)
    {
    }

which is not altogether nice, but workable. So, we only need somebody write
the wildcard_matcher, or better yet, function to convert from wildcard
syntax into regex syntax.

Actually, I'd love to have some better syntax. E.g. with views it would be
possible to write:

   for_each(filter(files("dir"), wildcard_matcher("*.txt")), /* lambda */ );

which is good except for the need to use lambda -- which might not be good
in all cases, so someone should push 'foreach' macro

Or maybe, filter_iterator could forward arguments to constructors, like:

   for(it i = it("*.txt", "dir), e; i != e; ++i)

But without a way to separate arguments to functor and to underlying
iterator this will only work for a case of one argument for each.

Well, I guess I should stop dreaming and get to work.

- Volodya


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