WvStreams
wvdiriter.h
00001 /* -*- Mode: C++ -*-
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  *
00005  * Directory iterator.  Recursively uses opendir and readdir, so you don't
00006  * have to.  Basically implements 'find'.
00007  *
00008  */
00009 
00010 #ifndef __WVDIRITER_H
00011 #define __WVDIRITER_H
00012 
00013 #include <dirent.h>
00014 #include <sys/stat.h>
00015 #include <sys/types.h>
00016 
00017 #include "wvstring.h"
00018 #include "wvlinklist.h"
00019 #include "strutils.h"
00020 
00021 struct WvDirEnt : public stat
00022 /***************************/
00023 {
00024     // we already have everything from struct stat, but let's also include
00025     // some variations on the filename for convenience.
00026     WvString        fullname; // contains: startdir/path/file
00027     WvString        name;     // contains: file
00028     WvString        relname;  // contains: path/file
00029 };
00030 
00031 class WvDirIter
00032 /*************/
00033 {
00034 private:
00035     bool        recurse;
00036     bool        go_up;
00037     bool        skip_mounts;
00038     bool        found_top;
00039 
00040     WvDirEnt        topdir;
00041     WvDirEnt        info;
00042     WvString        relpath;
00043 
00044     struct Dir {
00045         Dir( DIR * _d, WvString _dirname )
00046             : d( _d ), dirname( _dirname )
00047             {}
00048         ~Dir()
00049             { if( d ) closedir( d ); }
00050 
00051         DIR *    d;
00052         WvString dirname;
00053     };
00054 
00055     DeclareWvList( Dir );
00056     DirList       dirs;
00057     DirList::Iter dir;
00058     
00059 public:
00060     // the sizeof(stat) helps an assert() in wvdiriter.cc.
00061     WvDirIter( WvStringParm dirname,
00062                bool _recurse = true, bool _skip_mounts = false,
00063                size_t sizeof_stat = sizeof(struct stat) );
00064     
00065     ~WvDirIter();
00066 
00067     bool isok() const;
00068     bool isdir() const;
00069     void rewind();
00070     bool next();
00071 
00072     // calling up() will abandon the current level of recursion, if, for
00073     // example, you decide you don't want to continue reading the contents
00074     // of the directory you're in.  After up(), next() will return the next
00075     // entry after the directory you've abandoned, in its parent.
00076     void up()
00077         { go_up = true; }
00078     
00079     const WvDirEnt *ptr() const { return &info; }
00080     WvIterStuff(const WvDirEnt);
00081     
00082     int depth() const
00083         { return( dirs.count() ); }
00084 };
00085 
00086 #endif