1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDir is a class for enumerating the files in a directory
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 #include "wx/string.h"
17 class WXDLLIMPEXP_BASE wxArrayString
;
19 // ----------------------------------------------------------------------------
21 // ----------------------------------------------------------------------------
23 // these flags define what kind of filenames is included in the list of files
24 // enumerated by GetFirst/GetNext
27 wxDIR_FILES
= 0x0001, // include files
28 wxDIR_DIRS
= 0x0002, // include directories
29 wxDIR_HIDDEN
= 0x0004, // include hidden files
30 wxDIR_DOTDOT
= 0x0008, // include '.' and '..'
32 // by default, enumerate everything except '.' and '..'
33 wxDIR_DEFAULT
= wxDIR_FILES
| wxDIR_DIRS
| wxDIR_HIDDEN
36 // these constants are possible return value of wxDirTraverser::OnDir()
37 enum wxDirTraverseResult
39 wxDIR_IGNORE
= -1, // ignore this directory but continue with others
40 wxDIR_STOP
, // stop traversing
41 wxDIR_CONTINUE
// continue into this directory
44 // ----------------------------------------------------------------------------
45 // wxDirTraverser: helper class for wxDir::Traverse()
46 // ----------------------------------------------------------------------------
48 class WXDLLIMPEXP_BASE wxDirTraverser
51 /// a virtual dtor has been provided since this class has virtual members
52 virtual ~wxDirTraverser() { }
53 // called for each file found by wxDir::Traverse()
55 // return wxDIR_STOP or wxDIR_CONTINUE from here (wxDIR_IGNORE doesn't
57 virtual wxDirTraverseResult
OnFile(const wxString
& filename
) = 0;
59 // called for each directory found by wxDir::Traverse()
61 // return one of the enum elements defined above
62 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
) = 0;
64 // called for each directory which we couldn't open during our traversal
65 // of the directory tyree
67 // this method can also return either wxDIR_STOP, wxDIR_IGNORE or
68 // wxDIR_CONTINUE but the latter is treated specially: it means to retry
69 // opening the directory and so may lead to infinite loop if it is
70 // returned unconditionally, be careful with this!
72 // the base class version always returns wxDIR_IGNORE
73 virtual wxDirTraverseResult
OnOpenError(const wxString
& dirname
);
76 // ----------------------------------------------------------------------------
77 // wxDir: portable equivalent of {open/read/close}dir functions
78 // ----------------------------------------------------------------------------
80 class WXDLLIMPEXP_BASE wxDirData
;
82 class WXDLLIMPEXP_BASE wxDir
85 // test for existence of a directory with the given name
86 static bool Exists(const wxString
& dir
);
91 // default, use Open()
92 wxDir() { m_data
= NULL
; }
94 // opens the directory for enumeration, use IsOpened() to test success
95 wxDir(const wxString
& dir
);
97 // dtor cleans up the associated ressources
100 // open the directory for enumerating
101 bool Open(const wxString
& dir
);
103 // returns true if the directory was successfully opened
104 bool IsOpened() const;
106 // get the full name of the directory (without '/' at the end)
107 wxString
GetName() const;
109 // file enumeration routines
110 // -------------------------
112 // start enumerating all files matching filespec (or all files if it is
113 // empty) and flags, return true on success
114 bool GetFirst(wxString
*filename
,
115 const wxString
& filespec
= wxEmptyString
,
116 int flags
= wxDIR_DEFAULT
) const;
118 // get next file in the enumeration started with GetFirst()
119 bool GetNext(wxString
*filename
) const;
121 // return true if this directory has any files in it
122 bool HasFiles(const wxString
& spec
= wxEmptyString
);
124 // return true if this directory has any subdirectories
125 bool HasSubDirs(const wxString
& spec
= wxEmptyString
);
127 // enumerate all files in this directory and its subdirectories
129 // return the number of files found
130 size_t Traverse(wxDirTraverser
& sink
,
131 const wxString
& filespec
= wxEmptyString
,
132 int flags
= wxDIR_DEFAULT
) const;
134 // simplest version of Traverse(): get the names of all files under this
135 // directory into filenames array, return the number of files
136 static size_t GetAllFiles(const wxString
& dirname
,
137 wxArrayString
*files
,
138 const wxString
& filespec
= wxEmptyString
,
139 int flags
= wxDIR_DEFAULT
);
142 friend class wxDirData
;
146 DECLARE_NO_COPY_CLASS(wxDir
)