WvStreams
wvdiriter.h
1 /* -*- Mode: C++ -*-
2  * Worldvisions Weaver Software:
3  * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4  *
5  * Directory iterator. Recursively uses opendir and readdir, so you don't
6  * have to. Basically implements 'find'.
7  *
8  */
9 
10 #ifndef __WVDIRITER_H
11 #define __WVDIRITER_H
12 
13 #include <dirent.h>
14 #include <sys/stat.h>
15 #include <sys/types.h>
16 
17 #include "wvstring.h"
18 #include "wvlinklist.h"
19 #include "strutils.h"
20 
21 struct WvDirEnt : public stat
22 /***************************/
23 {
24  // we already have everything from struct stat, but let's also include
25  // some variations on the filename for convenience.
26  WvString fullname; // contains: startdir/path/file
27  WvString name; // contains: file
28  WvString relname; // contains: path/file
29 };
30 
31 class WvDirIter
32 /*************/
33 {
34 private:
35  bool recurse;
36  bool go_up;
37  bool skip_mounts;
38  bool found_top;
39 
40  WvDirEnt topdir;
41  WvDirEnt info;
42  WvString relpath;
43 
44  struct Dir {
45  Dir( DIR * _d, WvString _dirname )
46  : d( _d ), dirname( _dirname )
47  {}
48  ~Dir()
49  { if( d ) closedir( d ); }
50 
51  DIR * d;
52  WvString dirname;
53  };
54 
55  DeclareWvList( Dir );
56  DirList dirs;
57  DirList::Iter dir;
58 
59 public:
60  // the sizeof(stat) helps an assert() in wvdiriter.cc.
61  WvDirIter( WvStringParm dirname,
62  bool _recurse = true, bool _skip_mounts = false,
63  size_t sizeof_stat = sizeof(struct stat) );
64 
65  ~WvDirIter();
66 
67  bool isok() const;
68  bool isdir() const;
69  void rewind();
70  bool next();
71 
72  // calling up() will abandon the current level of recursion, if, for
73  // example, you decide you don't want to continue reading the contents
74  // of the directory you're in. After up(), next() will return the next
75  // entry after the directory you've abandoned, in its parent.
76  void up()
77  { go_up = true; }
78 
79  const WvDirEnt *ptr() const { return &info; }
80  WvIterStuff(const WvDirEnt);
81 
82  int depth() const
83  { return( dirs.count() ); }
84 };
85 
86 #endif
WvString
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:329
WvDirIter
Definition: wvdiriter.h:31
WvDirEnt
Definition: wvdiriter.h:21