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 license
10 /////////////////////////////////////////////////////////////////////////////
16 #pragma interface "dir.h"
20 #include "wx/string.h"
23 // ----------------------------------------------------------------------------
25 // ----------------------------------------------------------------------------
27 // these flags define what kind of filenames is included in the list of files
28 // enumerated by GetFirst/GetNext
31 wxDIR_FILES
= 0x0001, // include files
32 wxDIR_DIRS
= 0x0002, // include directories
33 wxDIR_HIDDEN
= 0x0004, // include hidden files
34 wxDIR_DOTDOT
= 0x0008, // include '.' and '..'
36 // by default, enumerate everything except '.' and '..'
37 wxDIR_DEFAULT
= wxDIR_FILES
| wxDIR_DIRS
| wxDIR_HIDDEN
40 // these constants are possible return value of wxDirTraverser::OnDir()
41 enum wxDirTraverseResult
43 wxDIR_IGNORE
= -1, // ignore this directory but continue with others
44 wxDIR_STOP
, // stop traversing
45 wxDIR_CONTINUE
// continue into this directory
48 // ----------------------------------------------------------------------------
49 // wxDirTraverser: helper class for wxDir::Traverse()
50 // ----------------------------------------------------------------------------
52 class WXDLLEXPORT wxDirTraverser
55 // called for each file found by wxDir::Traverse()
57 // return wxDIR_STOP or wxDIR_CONTINUE from here
58 virtual wxDirTraverseResult
OnFile(const wxString
& filename
) = 0;
60 // called for each directory found by wxDir::Traverse()
62 // return one of the enum elements defined above
63 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
) = 0;
66 // ----------------------------------------------------------------------------
67 // wxDir: portable equivalent of {open/read/close}dir functions
68 // ----------------------------------------------------------------------------
70 class WXDLLEXPORT wxDir
73 // test for existence of a directory with the given name
74 static bool Exists(const wxString
& dir
);
79 // default, use Open()
80 wxDir() { m_data
= NULL
; }
82 // opens the directory for enumeration, use IsOpened() to test success
83 wxDir(const wxString
& dir
);
85 // dtor cleans up the associated ressources
88 // open the directory for enumerating
89 bool Open(const wxString
& dir
);
91 // returns TRUE if the directory was successfully opened
92 bool IsOpened() const;
94 // get the full name of the directory (without '/' at the end)
95 wxString
GetName() const;
97 // file enumeration routines
98 // -------------------------
100 // start enumerating all files matching filespec (or all files if it is
101 // empty) and flags, return TRUE on success
102 bool GetFirst(wxString
*filename
,
103 const wxString
& filespec
= wxEmptyString
,
104 int flags
= wxDIR_DEFAULT
) const;
106 // get next file in the enumeration started with GetFirst()
107 bool GetNext(wxString
*filename
) const;
109 // return true if this directory has any files in it
110 bool HasFiles(const wxString
& spec
= wxEmptyString
);
112 // return true if this directory has any subdirectories
113 bool HasSubDirs(const wxString
& spec
= wxEmptyString
);
115 // enumerate all files in this directory and its subdirectories
117 // return the number of files found
118 size_t Traverse(wxDirTraverser
& sink
,
119 const wxString
& filespec
= wxEmptyString
,
120 int flags
= wxDIR_DEFAULT
) const;
122 // simplest version of Traverse(): get the names of all files under this
123 // directory into filenames array, return the number of files
124 static size_t GetAllFiles(const wxString
& dirname
,
125 wxArrayString
*files
,
126 const wxString
& filespec
= wxEmptyString
,
127 int flags
= wxDIR_DEFAULT
);
130 friend class WXDLLEXPORT wxDirData
;