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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "dir.h"
20 #include "wx/string.h"
23 class WXDLLIMPEXP_BASE wxArrayString
;
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
29 // these flags define what kind of filenames is included in the list of files
30 // enumerated by GetFirst/GetNext
33 wxDIR_FILES
= 0x0001, // include files
34 wxDIR_DIRS
= 0x0002, // include directories
35 wxDIR_HIDDEN
= 0x0004, // include hidden files
36 wxDIR_DOTDOT
= 0x0008, // include '.' and '..'
38 // by default, enumerate everything except '.' and '..'
39 wxDIR_DEFAULT
= wxDIR_FILES
| wxDIR_DIRS
| wxDIR_HIDDEN
42 // these constants are possible return value of wxDirTraverser::OnDir()
43 enum wxDirTraverseResult
45 wxDIR_IGNORE
= -1, // ignore this directory but continue with others
46 wxDIR_STOP
, // stop traversing
47 wxDIR_CONTINUE
// continue into this directory
50 // ----------------------------------------------------------------------------
51 // wxDirTraverser: helper class for wxDir::Traverse()
52 // ----------------------------------------------------------------------------
54 class WXDLLIMPEXP_BASE wxDirTraverser
57 /// a virtual dtor has been provided since this class has virtual members
58 virtual ~wxDirTraverser() { }
59 // called for each file found by wxDir::Traverse()
61 // return wxDIR_STOP or wxDIR_CONTINUE from here (wxDIR_IGNORE doesn't
63 virtual wxDirTraverseResult
OnFile(const wxString
& filename
) = 0;
65 // called for each directory found by wxDir::Traverse()
67 // return one of the enum elements defined above
68 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
) = 0;
70 // called for each directory which we couldn't open during our traversal
71 // of the directory tyree
73 // this method can also return either wxDIR_STOP, wxDIR_IGNORE or
74 // wxDIR_CONTINUE but the latter is treated specially: it means to retry
75 // opening the directory and so may lead to infinite loop if it is
76 // returned unconditionally, be careful with this!
78 // the base class version always returns wxDIR_IGNORE
79 virtual wxDirTraverseResult
OnOpenError(const wxString
& dirname
);
82 // ----------------------------------------------------------------------------
83 // wxDir: portable equivalent of {open/read/close}dir functions
84 // ----------------------------------------------------------------------------
86 class WXDLLIMPEXP_BASE wxDirData
;
88 class WXDLLIMPEXP_BASE wxDir
91 // test for existence of a directory with the given name
92 static bool Exists(const wxString
& dir
);
97 // default, use Open()
98 wxDir() { m_data
= NULL
; }
100 // opens the directory for enumeration, use IsOpened() to test success
101 wxDir(const wxString
& dir
);
103 // dtor cleans up the associated ressources
106 // open the directory for enumerating
107 bool Open(const wxString
& dir
);
109 // returns true if the directory was successfully opened
110 bool IsOpened() const;
112 // get the full name of the directory (without '/' at the end)
113 wxString
GetName() const;
115 // file enumeration routines
116 // -------------------------
118 // start enumerating all files matching filespec (or all files if it is
119 // empty) and flags, return true on success
120 bool GetFirst(wxString
*filename
,
121 const wxString
& filespec
= wxEmptyString
,
122 int flags
= wxDIR_DEFAULT
) const;
124 // get next file in the enumeration started with GetFirst()
125 bool GetNext(wxString
*filename
) const;
127 // return true if this directory has any files in it
128 bool HasFiles(const wxString
& spec
= wxEmptyString
);
130 // return true if this directory has any subdirectories
131 bool HasSubDirs(const wxString
& spec
= wxEmptyString
);
133 // enumerate all files in this directory and its subdirectories
135 // return the number of files found
136 size_t Traverse(wxDirTraverser
& sink
,
137 const wxString
& filespec
= wxEmptyString
,
138 int flags
= wxDIR_DEFAULT
) const;
140 // simplest version of Traverse(): get the names of all files under this
141 // directory into filenames array, return the number of files
142 static size_t GetAllFiles(const wxString
& dirname
,
143 wxArrayString
*files
,
144 const wxString
& filespec
= wxEmptyString
,
145 int flags
= wxDIR_DEFAULT
);
148 friend class wxDirData
;
152 DECLARE_NO_COPY_CLASS(wxDir
)