]> git.saurik.com Git - wxWidgets.git/blame - interface/wx/dir.h
Convert wxFSW_EVENT_{WARNING,ERROR} to string correctly.
[wxWidgets.git] / interface / wx / dir.h
CommitLineData
23324ae1
FM
1/////////////////////////////////////////////////////////////////////////////
2// Name: dir.h
bc85d676 3// Purpose: interface of wxDir and wxDirTraverser
23324ae1
FM
4// Author: wxWidgets team
5// RCS-ID: $Id$
526954c5 6// Licence: wxWindows licence
23324ae1
FM
7/////////////////////////////////////////////////////////////////////////////
8
bc85d676
BP
9/**
10 Possible return values of wxDirTraverser callback functions.
11*/
12enum wxDirTraverseResult
13{
14 wxDIR_IGNORE = -1, ///< Ignore this directory but continue with others.
15 wxDIR_STOP, ///< Stop traversing.
16 wxDIR_CONTINUE ///< Continue into this directory.
17};
18
23324ae1
FM
19/**
20 @class wxDirTraverser
7c913512 21
bc85d676
BP
22 wxDirTraverser is an abstract interface which must be implemented by
23 objects passed to wxDir::Traverse() function.
7c913512 24
bc85d676 25 Example of use (this works almost like wxDir::GetAllFiles()):
7c913512 26
23324ae1
FM
27 @code
28 class wxDirTraverserSimple : public wxDirTraverser
bc85d676
BP
29 {
30 public:
31 wxDirTraverserSimple(wxArrayString& files) : m_files(files) { }
32
33 virtual wxDirTraverseResult OnFile(const wxString& filename)
23324ae1 34 {
bc85d676
BP
35 m_files.Add(filename);
36 return wxDIR_CONTINUE;
37 }
38
39 virtual wxDirTraverseResult OnDir(const wxString& WXUNUSED(dirname))
40 {
41 return wxDIR_CONTINUE;
42 }
43
44 private:
45 wxArrayString& m_files;
46 };
47
48 // get the names of all files in the array
49 wxArrayString files;
50 wxDirTraverserSimple traverser(files);
51
52 wxDir dir(dirname);
53 dir.Traverse(traverser);
23324ae1 54 @endcode
7c913512 55
23324ae1
FM
56 @library{wxbase}
57 @category{file}
58*/
7c913512 59class wxDirTraverser
23324ae1
FM
60{
61public:
62 /**
bc85d676
BP
63 This function is called for each directory. It may return ::wxDIR_STOP
64 to abort traversing completely, ::wxDIR_IGNORE to skip this directory
65 but continue with others or ::wxDIR_CONTINUE to enumerate all files and
23324ae1 66 subdirectories in this directory.
bc85d676
BP
67
68 This is a pure virtual function and must be implemented in the derived
69 class.
23324ae1 70 */
b91c4601 71 virtual wxDirTraverseResult OnDir(const wxString& dirname) = 0;
23324ae1
FM
72
73 /**
bc85d676
BP
74 This function is called for each file. It may return ::wxDIR_STOP to
75 abort traversing (for example, if the file being searched is found) or
76 ::wxDIR_CONTINUE to proceed.
77
78 This is a pure virtual function and must be implemented in the derived
79 class.
23324ae1 80 */
b91c4601 81 virtual wxDirTraverseResult OnFile(const wxString& filename) = 0;
23324ae1
FM
82
83 /**
84 This function is called for each directory which we failed to open for
bc85d676
BP
85 enumerating. It may return ::wxDIR_STOP to abort traversing completely,
86 ::wxDIR_IGNORE to skip this directory but continue with others or
87 ::wxDIR_CONTINUE to retry opening this directory once again.
88
89 The base class version always returns ::wxDIR_IGNORE.
23324ae1
FM
90 */
91 virtual wxDirTraverseResult OnOpenError(const wxString& openerrorname);
92};
93
94
e54c96f1 95
bc85d676 96/**
c55f46d0
VZ
97 These flags affect the behaviour of GetFirst/GetNext() and Traverse(),
98 determining what types are included in the list of items they produce.
bc85d676 99*/
e8f3bf98 100enum wxDirFlags
bc85d676
BP
101{
102 wxDIR_FILES = 0x0001, ///< Includes files.
103 wxDIR_DIRS = 0x0002, ///< Includes directories.
104 wxDIR_HIDDEN = 0x0004, ///< Includes hidden files.
105 wxDIR_DOTDOT = 0x0008, ///< Includes "." and "..".
106
c55f46d0
VZ
107 /**
108 Don't follow symbolic links during the directory traversal.
109
110 This flag is ignored under systems not supporting symbolic links (i.e.
111 non-Unix ones).
112
113 Notice that this flag is @e not included in wxDIR_DEFAULT and so the
114 default behaviour of wxDir::Traverse() is to follow symbolic links,
115 even if they lead outside of the directory being traversed.
116
117 @since 2.9.5
118 */
119 wxDIR_NO_FOLLOW = 0x0010,
120
121 /**
122 Default directory traversal flags include both files and directories,
123 even hidden.
124
125 Notice that by default wxDIR_NO_FOLLOW is @e not included, meaning that
126 symbolic links are followed by default. If this is not desired, you
127 must pass that flag explicitly.
128 */
bc85d676
BP
129 wxDIR_DEFAULT = wxDIR_FILES | wxDIR_DIRS | wxDIR_HIDDEN
130};
131
23324ae1
FM
132/**
133 @class wxDir
7c913512 134
23324ae1 135 wxDir is a portable equivalent of Unix open/read/closedir functions which
bc85d676
BP
136 allow enumerating of the files in a directory. wxDir allows to enumerate
137 files as well as directories.
7c913512
FM
138
139 wxDir also provides a flexible way to enumerate files recursively using
bc85d676 140 Traverse() or a simpler GetAllFiles() function.
7c913512 141
23324ae1 142 Example of use:
7c913512 143
23324ae1
FM
144 @code
145 wxDir dir(wxGetCwd());
7c913512 146
bc85d676
BP
147 if ( !dir.IsOpened() )
148 {
149 // deal with the error here - wxDir would already log an error message
150 // explaining the exact reason of the failure
151 return;
152 }
7c913512 153
bc85d676 154 puts("Enumerating object files in current directory:");
7c913512 155
bc85d676 156 wxString filename;
7c913512 157
bc85d676
BP
158 bool cont = dir.GetFirst(&filename, filespec, flags);
159 while ( cont )
160 {
161 printf("%s\n", filename.c_str());
7c913512 162
bc85d676
BP
163 cont = dir.GetNext(&filename);
164 }
23324ae1 165 @endcode
7c913512 166
23324ae1
FM
167 @library{wxbase}
168 @category{file}
169*/
7c913512 170class wxDir
23324ae1
FM
171{
172public:
23324ae1 173 /**
bc85d676 174 Default constructor, use Open() afterwards.
23324ae1
FM
175 */
176 wxDir();
bc85d676
BP
177 /**
178 Opens the directory for enumeration, use IsOpened() to test for errors.
179 */
7c913512 180 wxDir(const wxString& dir);
23324ae1
FM
181
182 /**
6619c4af
VZ
183 Destructor cleans up the associated resources by calling Close().
184
185 It is not virtual and so this class is not meant to be used
186 polymorphically.
23324ae1
FM
187 */
188 ~wxDir();
189
6619c4af
VZ
190 /**
191 Close the directory.
192
193 The object can't be used after closing it, but Open() may be called
194 again to reopen it later.
195
196 @since 2.9.5
197 */
198 void Close();
199
23324ae1 200 /**
bc85d676 201 Test for existence of a directory with the given name.
23324ae1
FM
202 */
203 static bool Exists(const wxString& dir);
204
205 /**
bc85d676
BP
206 The function returns the path of the first file matching the given
207 @a filespec or an empty string if there are no files matching it.
208
209 The @a flags parameter may or may not include ::wxDIR_FILES, the
4cc4bfaf 210 function always behaves as if it were specified. By default, @a flags
bc85d676
BP
211 includes ::wxDIR_DIRS and so the function recurses into the
212 subdirectories but if this flag is not specified, the function
213 restricts the search only to the directory @a dirname itself.
e8f3bf98 214 See ::wxDirFlags for the list of the possible flags.
bc85d676
BP
215
216 @see Traverse()
23324ae1
FM
217 */
218 static wxString FindFirst(const wxString& dirname,
219 const wxString& filespec,
220 int flags = wxDIR_DEFAULT);
221
222 /**
bc85d676
BP
223 The function appends the names of all the files under directory
224 @a dirname to the array @a files (note that its old content is
225 preserved). Only files matching the @a filespec are taken, with empty
226 spec matching all the files.
227
228 The @a flags parameter should always include ::wxDIR_FILES or the array
229 would be unchanged and should include ::wxDIR_DIRS flag to recurse into
23324ae1 230 subdirectories (both flags are included in the value by default).
e8f3bf98 231 See ::wxDirFlags for the list of the possible flags.
7f01b1fe
FM
232
233 @return Returns the total number of files found while traversing
234 the directory @a dirname (i.e. the number of entries appended
235 to the @a files array).
bc85d676
BP
236
237 @see Traverse()
23324ae1 238 */
bc85d676 239 static size_t GetAllFiles(const wxString& dirname, wxArrayString* files,
23324ae1
FM
240 const wxString& filespec = wxEmptyString,
241 int flags = wxDIR_DEFAULT);
242
243 /**
4cc4bfaf 244 Start enumerating all files matching @a filespec (or all files if it is
23324ae1 245 empty) and @e flags, return @true on success.
e8f3bf98 246 See ::wxDirFlags for the list of the possible flags.
23324ae1
FM
247 */
248 bool GetFirst(wxString* filename,
249 const wxString& filespec = wxEmptyString,
328f5751 250 int flags = wxDIR_DEFAULT) const;
23324ae1
FM
251
252 /**
c9f6f0a8
VZ
253 Returns the name of the directory itself.
254
255 The returned string does not have the trailing path separator (slash or
256 backslash).
257
258 Notice that in spite of this the last character of the returned string
259 can still be the path separator if this directory is the root one.
260 Because of this, don't append ::wxFILE_SEP_PATH to the returned value
261 if you do need a slash-terminated directory name but use
262 GetNameWithSep() instead to avoid having duplicate consecutive slashes.
23324ae1 263 */
328f5751 264 wxString GetName() const;
23324ae1 265
c9f6f0a8
VZ
266 /**
267 Returns the name of the directory with the path separator appended.
268
269 The last character of the returned string is always ::wxFILE_SEP_PATH
270 unless the string is empty, indicating that this directory is invalid.
271
272 @see GetName()
273
274 @since 2.9.4
275 */
276 wxString GetNameWithSep() const;
277
23324ae1 278 /**
bc85d676
BP
279 Continue enumerating files which satisfy the criteria specified by the
280 last call to GetFirst().
23324ae1 281 */
328f5751 282 bool GetNext(wxString* filename) const;
23324ae1
FM
283
284 /**
285 Returns the size (in bytes) of all files recursively found in @c dir or
286 @c wxInvalidSize in case of error.
bc85d676 287
4c51a665 288 In case it happens that while traversing folders a file's size cannot
bc85d676
BP
289 be read, that file is added to the @a filesSkipped array, if not @NULL,
290 and then skipped. This usually happens with some special folders which
291 are locked by the operating system or by another process. Remember that
292 when the size of @a filesSkipped is not zero, then the returned value
293 is not 100% accurate and, if the skipped files were big, it could be
23324ae1 294 far from real size of the directory.
bc85d676
BP
295
296 @see wxFileName::GetHumanReadableSize(), wxGetDiskSpace()
23324ae1
FM
297 */
298 static wxULongLong GetTotalSize(const wxString& dir,
4cc4bfaf 299 wxArrayString* filesSkipped = NULL);
23324ae1
FM
300
301 /**
7c913512 302 Returns @true if the directory contains any files matching the given
bc85d676 303 @a filespec. If @a filespec is empty, look for any files at all. In any
23324ae1
FM
304 case, even hidden files are taken into account.
305 */
106dcc2c 306 bool HasFiles(const wxString& filespec = wxEmptyString) const;
23324ae1
FM
307
308 /**
309 Returns @true if the directory contains any subdirectories (if a non
bc85d676 310 empty @a filespec is given, only check for directories matching it).
23324ae1
FM
311 The hidden subdirectories are taken into account as well.
312 */
106dcc2c 313 bool HasSubDirs(const wxString& dirspec = wxEmptyString) const;
23324ae1
FM
314
315 /**
bc85d676
BP
316 Returns @true if the directory was successfully opened by a previous
317 call to Open().
23324ae1 318 */
328f5751 319 bool IsOpened() const;
23324ae1 320
d38315df
FM
321 /**
322 Creates a directory.
323
324 This is just an alias for wxFileName::Mkdir(); refer to that function
325 for more info.
326 */
327 static bool Make(const wxString &dir, int perm = wxS_DIR_DEFAULT,
328 int flags = 0);
329
23324ae1 330 /**
bc85d676
BP
331 Open the directory for enumerating, returns @true on success or @false
332 if an error occurred.
23324ae1
FM
333 */
334 bool Open(const wxString& dir);
335
d38315df
FM
336 /**
337 Removes a directory.
338
339 This is just an alias for wxFileName::Rmdir(); refer to that function
340 for more info.
341 */
342 static bool Remove(const wxString &dir, int flags = 0);
343
23324ae1 344 /**
3d65f646
VZ
345 Enumerate all files and directories under the given directory.
346
347 If @a flags contains ::wxDIR_DIRS this enumeration is recursive, i.e.
348 all the subdirectories of the given one and the files inside them will
349 be traversed. Otherwise only the files in this directory itself are.
350
351 If @a flags doesn't contain ::wxDIR_FILES then only subdirectories are
352 examined but not normal files. It doesn't make sense to not specify
353 either ::wxDIR_DIRS or ::wxDIR_FILES and usually both of them should be
354 specified, as is the case by default.
bc85d676 355
7f01b1fe 356 For each directory found, @ref wxDirTraverser::OnDir() "sink.OnDir()"
bc85d676
BP
357 is called and @ref wxDirTraverser::OnFile() "sink.OnFile()" is called
358 for every file. Depending on the return value, the enumeration may
3d65f646
VZ
359 continue or stop. If entering a subdirectory fails, @ref
360 wxDirTraverser::OnOpenError() "sink.OnOpenError()" is called.
bc85d676
BP
361
362 The function returns the total number of files found or @c "(size_t)-1"
363 on error.
364
3d65f646
VZ
365 See ::wxDirFlags for the full list of the possible flags.
366
bc85d676 367 @see GetAllFiles()
23324ae1
FM
368 */
369 size_t Traverse(wxDirTraverser& sink,
370 const wxString& filespec = wxEmptyString,
adaaa686 371 int flags = wxDIR_DEFAULT) const;
23324ae1 372};
e54c96f1 373