Boost logo

Boost :

From: Jeff Garland (jeff_at_[hidden])
Date: 2002-02-23 14:00:52


> > So put another way, how does the proposal support the equivalent of
> > this perl code?
> ...
>
> > # delete the files from a given directory, but not the
> directories...
> > opendir(MYDIR, "/usr/junk"); #open handle to a directory path
> /usr/junk
> > foreach($name = readdir(MYDIR)) #iterator in perl gives all files
> and
> > directories
> > {
> > if (-f $name) { #test if the 'path' is a file or directory
> > unlink($name); #delete the file
> > }
> > }
>
> for (dir_it i ("/usr/junk"); i != dir_it (); ++i)
> {
> if (!get <is_directory> (i))
> {
> remove (compose ("/usr/junk", *i));
> }
> }

>and this does already work if you take the remove from std:: and
>define compose as follows (actually as basic_string template)

>std::string compose (std::string const &dirpath, std::string const &path)
>{
> return dirpath + '/' + path;
>}

I see how this works in dir_it, but I still don't see it in the 'design
proposal'. It says:

    Given a path, iteration (meeting std InputIterator requirements) yielding
names

^^^^^^^^^^^^^^
    suitable for use in standard library I/O operations.

The dir_it iterator points at something more than a 'name' because you can ask
it for the attributes. That is, since I understand a 'name' to be a string,
this line is an extension of the design requirements:

> if (!get <is_directory> (i))

And again, I'm asking for the requirements to guarantee this property on all
platforms so that the code above is portable. This is trivially possible since
all platforms that don't support directories can simply return false for this
property.

As for the code interface seems unnecessarily difficult and non-portable. If
you can retrieve the file attributes from the iterator then I should also be
able to delete without having to recompose the name. Aside from the clutter of
having to recompose the name, the suggested solution is non-portable. So why
can't we just have:

 for (dir_it i ("/usr/junk"); i != dir_it (); ++i)
 {
   if (!get <is_directory> (i))
   {
     remove (i);
   }
 }

Jeff


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