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