1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDir is a class for enumerating the files in a directory
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
14 #include "wx/longlong.h"
15 #include "wx/string.h"
16 #include "wx/filefn.h" // for wxS_DIR_DEFAULT
18 class WXDLLIMPEXP_FWD_BASE wxArrayString
;
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // These flags affect the behaviour of GetFirst/GetNext() and Traverse().
25 // They define what types are included in the list of items they produce.
26 // Note that wxDIR_NO_FOLLOW is relevant only on Unix and ignored under systems
27 // not supporting symbolic links.
30 wxDIR_FILES
= 0x0001, // include files
31 wxDIR_DIRS
= 0x0002, // include directories
32 wxDIR_HIDDEN
= 0x0004, // include hidden files
33 wxDIR_DOTDOT
= 0x0008, // include '.' and '..'
34 wxDIR_NO_FOLLOW
= 0x0010, // don't dereference any symlink
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 WXDLLIMPEXP_BASE wxDirTraverser
55 /// a virtual dtor has been provided since this class has virtual members
56 virtual ~wxDirTraverser() { }
57 // called for each file found by wxDir::Traverse()
59 // return wxDIR_STOP or wxDIR_CONTINUE from here (wxDIR_IGNORE doesn't
61 virtual wxDirTraverseResult
OnFile(const wxString
& filename
) = 0;
63 // called for each directory found by wxDir::Traverse()
65 // return one of the enum elements defined above
66 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
) = 0;
68 // called for each directory which we couldn't open during our traversal
69 // of the directory tree
71 // this method can also return either wxDIR_STOP, wxDIR_IGNORE or
72 // wxDIR_CONTINUE but the latter is treated specially: it means to retry
73 // opening the directory and so may lead to infinite loop if it is
74 // returned unconditionally, be careful with this!
76 // the base class version always returns wxDIR_IGNORE
77 virtual wxDirTraverseResult
OnOpenError(const wxString
& dirname
);
80 // ----------------------------------------------------------------------------
81 // wxDir: portable equivalent of {open/read/close}dir functions
82 // ----------------------------------------------------------------------------
84 class WXDLLIMPEXP_FWD_BASE wxDirData
;
86 class WXDLLIMPEXP_BASE wxDir
93 // default, use Open()
94 wxDir() { m_data
= NULL
; }
96 // opens the directory for enumeration, use IsOpened() to test success
97 wxDir(const wxString
& dir
);
99 // dtor calls Close() automatically
100 ~wxDir() { Close(); }
102 // open the directory for enumerating
103 bool Open(const wxString
& dir
);
105 // close the directory, Open() can be called again later
108 // returns true if the directory was successfully opened
109 bool IsOpened() const;
111 // get the full name of the directory (without '/' at the end)
112 wxString
GetName() const;
114 // Same as GetName() but does include the trailing separator, unless the
115 // string is empty (only for invalid directories).
116 wxString
GetNameWithSep() const;
119 // file enumeration routines
120 // -------------------------
122 // start enumerating all files matching filespec (or all files if it is
123 // empty) and flags, return true on success
124 bool GetFirst(wxString
*filename
,
125 const wxString
& filespec
= wxEmptyString
,
126 int flags
= wxDIR_DEFAULT
) const;
128 // get next file in the enumeration started with GetFirst()
129 bool GetNext(wxString
*filename
) const;
131 // return true if this directory has any files in it
132 bool HasFiles(const wxString
& spec
= wxEmptyString
) const;
134 // return true if this directory has any subdirectories
135 bool HasSubDirs(const wxString
& spec
= wxEmptyString
) const;
137 // enumerate all files in this directory and its subdirectories
139 // return the number of files found
140 size_t Traverse(wxDirTraverser
& sink
,
141 const wxString
& filespec
= wxEmptyString
,
142 int flags
= wxDIR_DEFAULT
) const;
144 // simplest version of Traverse(): get the names of all files under this
145 // directory into filenames array, return the number of files
146 static size_t GetAllFiles(const wxString
& dirname
,
147 wxArrayString
*files
,
148 const wxString
& filespec
= wxEmptyString
,
149 int flags
= wxDIR_DEFAULT
);
151 // check if there any files matching the given filespec under the given
152 // directory (i.e. searches recursively), return the file path if found or
153 // empty string otherwise
154 static wxString
FindFirst(const wxString
& dirname
,
155 const wxString
& filespec
,
156 int flags
= wxDIR_DEFAULT
);
159 // returns the size of all directories recursively found in given path
160 static wxULongLong
GetTotalSize(const wxString
&dir
, wxArrayString
*filesSkipped
= NULL
);
161 #endif // wxUSE_LONGLONG
164 // static utilities for directory management
165 // (alias to wxFileName's functions for dirs)
166 // -----------------------------------------
168 // test for existence of a directory with the given name
169 static bool Exists(const wxString
& dir
);
171 static bool Make(const wxString
&dir
, int perm
= wxS_DIR_DEFAULT
,
174 static bool Remove(const wxString
&dir
, int flags
= 0);
178 friend class wxDirData
;
182 wxDECLARE_NO_COPY_CLASS(wxDir
);