vul_file_iterator.h
Go to the documentation of this file.
1 // This is core/vul/vul_file_iterator.h
2 #ifndef vul_file_iterator_h_
3 #define vul_file_iterator_h_
4 //:
5 // \file
6 // \brief class to iterate through directories and/or "glob" patterns (*.*)
7 // \author awf@robots.ox.ac.uk
8 // \date 27 Nov 00
9 //
10 // \verbatim
11 // Modifications
12 // PDA (Manchester) 21/03/2001: Tidied up the documentation
13 // Peter Vanroose 27/05/2001: Corrected the documentation
14 // Ian Scott 12/06/2003: Added filen?m[abc].* notation to unix and dos version
15 // \endverbatim
16 
17 #include <string>
18 #ifdef _MSC_VER
19 # include <vcl_msvc_warnings.h>
20 #endif
21 
23 
24 //: Iterate through directories and/or "glob" patterns (*.*)
25 // It is efficient to use
26 // \code
27 // for (vul_file_iterator fn="/dir/*"; fn; ++fn) {
28 // ... use fn() as filename
29 // }
30 // \endcode
31 // simply to list the contents of a directory. If you really
32 // want just the *.ext files, it is efficient to use
33 // \code
34 // for (vul_file_iterator fn="/dir/*.ext"; fn; ++fn) {
35 // ... use fn() as filename
36 // }
37 // \endcode
38 // rather than opendir/glob/etc.
39 //
40 // Valid glob patterns are unix-like - '?' matches precisely one character
41 // '*' matches any sequence (including empty), [abc] matches either 'a' or 'b' or 'c'
42 //
43 // \note There is no implicit ordering of the files in a directory;
44 // the order is OS-dependent and is not guaranteed by this class.
45 // You may wish, therefore, to store the filenames returned and sort them yourself,
46 // if you want to process files in (for example) alphanumeric order.
48 {
49 
50  public:
51 
52  vul_file_iterator() : p(nullptr) {}
53 
54  //: Initialize, and scan to get first file from "glob"
55  vul_file_iterator(char const* glob);
56 
57  //: Initialize, and scan to get first file from "glob"
58  vul_file_iterator(std::string const& glob);
59 
61 
62  //: Ask if done.
63  // Won't spin the disk
64  explicit operator bool() const;
65 
66  //: Inverse boolean value
67  bool operator!() const;
68 
69  //: Return the currently pointed-to pathname.
70  // Won't spin the disk
71  char const* operator()();
72 
73  //: Return the non-directory part of the current pathname.
74  char const* filename();
75 
76  //: Return the match for the i'th glob wildcard character (* or ?).
77  // Uses the most recent glob result.
78  char const* match(int i);
79 
80  //: Increment to the next file
81  // Will spin the disk
83 
84  //: Run a new match
85  void reset(char const* glob);
86 
87  protected:
89 
90  private:
91  // postfix++ privatized.
93 };
94 
95 #endif // vul_file_iterator_h_
char const * filename()
Return the non-directory part of the current pathname.
Declare pimpl, reset, and iteration routines for each OS.
bool operator!() const
Inverse boolean value.
Iterate through directories and/or "glob" patterns (*.
vul_file_iterator operator++(int)
vul_file_iterator_data * p
vul_file_iterator & operator++()
Increment to the next file.
char const * match(int i)
Return the match for the i'th glob wildcard character (* or ?).
void reset(char const *glob)
Run a new match.
char const * operator()()
Return the currently pointed-to pathname.