core/vul/vul_file_iterator.h
Go to the documentation of this file.
00001 // This is core/vul/vul_file_iterator.h
00002 #ifndef vul_file_iterator_h_
00003 #define vul_file_iterator_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief class to iterate through directories and/or "glob" patterns (*.*)
00010 // \author awf@robots.ox.ac.uk
00011 // \date 27 Nov 00
00012 //
00013 // \verbatim
00014 //  Modifications
00015 //   PDA (Manchester) 21/03/2001: Tidied up the documentation
00016 //   Peter Vanroose   27/05/2001: Corrected the documentation
00017 //   Ian Scott        12/06/2003: Added filen?m[abc].* notation to unix and dos version
00018 // \endverbatim
00019 
00020 #include <vcl_string.h>
00021 
00022 struct vul_file_iterator_data;
00023 
00024 //: Iterate through directories and/or "glob" patterns (*.*)
00025 // It is efficient to use
00026 // \code
00027 //   for (vul_file_iterator fn="/dir/*"; fn; ++fn) {
00028 //     ... use fn() as filename
00029 //   }
00030 // \endcode
00031 // simply to list the contents of a directory.  If you really
00032 // want just the *.ext files, it is efficient to use
00033 // \code
00034 //   for (vul_file_iterator fn="/dir/*.ext"; fn; ++fn) {
00035 //     ... use fn() as filename
00036 //   }
00037 // \endcode
00038 // rather than opendir/glob/etc.
00039 //
00040 // Valid glob patterns are unix-like - '?' matches precisely one character
00041 // '*' matches any sequence (including empty), [abc] matches either 'a' or 'b' or 'c'
00042 //
00043 // \note There is no implicit ordering of the files in a directory; 
00044 // the order is OS-dependent and is not guaranteed by this class.
00045 // You may wish, therefore, to store the filenames returned and sort them yourself,
00046 // if you want to process files in (for example) alphanumeric order.
00047 class vul_file_iterator
00048 {
00049   VCL_SAFE_BOOL_DEFINE;
00050  public:
00051 
00052   vul_file_iterator() : p(0) {}
00053 
00054   //: Initialize, and scan to get first file from "glob"
00055   vul_file_iterator(char const* glob);
00056 
00057   //: Initialize, and scan to get first file from "glob"
00058   vul_file_iterator(vcl_string const& glob);
00059 
00060   ~vul_file_iterator();
00061 
00062   //: Ask if done.
00063   // Won't spin the disk
00064   operator safe_bool() const;
00065 
00066   //: Inverse boolean value
00067   bool operator!() const;
00068 
00069   //: Return the currently pointed-to pathname.
00070   // Won't spin the disk
00071   char const* operator()();
00072 
00073   //: Return the non-directory part of the current pathname.
00074   char const* filename();
00075 
00076   //: Return the match for the i'th glob wildcard character (* or ?).
00077   // Uses the most recent glob result.
00078   char const* match(int i);
00079 
00080   //: Increment to the next file
00081   // Will spin the disk
00082   vul_file_iterator& operator++();
00083 
00084   //: Run a new match
00085   void reset(char const* glob);
00086 
00087  protected:
00088   vul_file_iterator_data* p;
00089 
00090  private:
00091   // postfix++ privatized.
00092   vul_file_iterator operator++(int) { return vul_file_iterator(); }
00093 };
00094 
00095 #endif // vul_file_iterator_h_