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