]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: dir.h | |
3 | // Purpose: documentation for wxDirTraverser class | |
4 | // Author: wxWidgets team | |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /** | |
10 | @class wxDirTraverser | |
11 | @wxheader{dir.h} | |
7c913512 | 12 | |
23324ae1 FM |
13 | wxDirTraverser is an abstract interface which must be implemented by objects |
14 | passed to wxDir::Traverse function. | |
7c913512 | 15 | |
23324ae1 | 16 | Example of use (this works almost like wxDir::GetAllFiles): |
7c913512 | 17 | |
23324ae1 FM |
18 | @code |
19 | class wxDirTraverserSimple : public wxDirTraverser | |
20 | { | |
21 | public: | |
22 | wxDirTraverserSimple(wxArrayString& files) : m_files(files) { } | |
7c913512 | 23 | |
23324ae1 FM |
24 | virtual wxDirTraverseResult OnFile(const wxString& filename) |
25 | { | |
26 | m_files.Add(filename); | |
27 | return wxDIR_CONTINUE; | |
28 | } | |
7c913512 | 29 | |
23324ae1 FM |
30 | virtual wxDirTraverseResult OnDir(const wxString& WXUNUSED(dirname)) |
31 | { | |
32 | return wxDIR_CONTINUE; | |
33 | } | |
7c913512 | 34 | |
23324ae1 FM |
35 | private: |
36 | wxArrayString& m_files; | |
37 | }; | |
7c913512 | 38 | |
23324ae1 FM |
39 | // get the names of all files in the array |
40 | wxArrayString files; | |
41 | wxDirTraverserSimple traverser(files); | |
7c913512 | 42 | |
23324ae1 FM |
43 | wxDir dir(dirname); |
44 | dir.Traverse(traverser); | |
45 | @endcode | |
7c913512 | 46 | |
23324ae1 FM |
47 | @library{wxbase} |
48 | @category{file} | |
49 | */ | |
7c913512 | 50 | class wxDirTraverser |
23324ae1 FM |
51 | { |
52 | public: | |
53 | /** | |
7c913512 | 54 | This function is called for each directory. It may return @c wxSIR_STOP |
23324ae1 FM |
55 | to abort traversing completely, @c wxDIR_IGNORE to skip this directory but |
56 | continue with others or @c wxDIR_CONTINUE to enumerate all files and | |
57 | subdirectories in this directory. | |
23324ae1 FM |
58 | This is a pure virtual function and must be implemented in the derived class. |
59 | */ | |
60 | virtual wxDirTraverseResult OnDir(const wxString& dirname); | |
61 | ||
62 | /** | |
63 | This function is called for each file. It may return @c wxDIR_STOP to abort | |
7c913512 | 64 | traversing (for example, if the file being searched is found) or |
23324ae1 | 65 | @c wxDIR_CONTINUE to proceed. |
23324ae1 FM |
66 | This is a pure virtual function and must be implemented in the derived class. |
67 | */ | |
68 | virtual wxDirTraverseResult OnFile(const wxString& filename); | |
69 | ||
70 | /** | |
71 | This function is called for each directory which we failed to open for | |
72 | enumerating. It may return @c wxSIR_STOP to abort traversing completely, | |
7c913512 | 73 | @c wxDIR_IGNORE to skip this directory but continue with others or |
23324ae1 | 74 | @c wxDIR_CONTINUE to retry opening this directory once again. |
23324ae1 FM |
75 | The base class version always returns @c wxDIR_IGNORE. |
76 | */ | |
77 | virtual wxDirTraverseResult OnOpenError(const wxString& openerrorname); | |
78 | }; | |
79 | ||
80 | ||
81 | /** | |
82 | @class wxDir | |
83 | @wxheader{dir.h} | |
7c913512 | 84 | |
23324ae1 FM |
85 | wxDir is a portable equivalent of Unix open/read/closedir functions which |
86 | allow enumerating of the files in a directory. wxDir allows to enumerate files | |
87 | as well as directories. | |
7c913512 FM |
88 | |
89 | wxDir also provides a flexible way to enumerate files recursively using | |
90 | wxDir::Traverse or a simpler | |
23324ae1 | 91 | wxDir::GetAllFiles function. |
7c913512 | 92 | |
23324ae1 | 93 | Example of use: |
7c913512 | 94 | |
23324ae1 FM |
95 | @code |
96 | wxDir dir(wxGetCwd()); | |
7c913512 | 97 | |
23324ae1 FM |
98 | if ( !dir.IsOpened() ) |
99 | { | |
100 | // deal with the error here - wxDir would already log an error message | |
101 | // explaining the exact reason of the failure | |
102 | return; | |
103 | } | |
7c913512 | 104 | |
23324ae1 | 105 | puts("Enumerating object files in current directory:"); |
7c913512 | 106 | |
23324ae1 | 107 | wxString filename; |
7c913512 | 108 | |
23324ae1 FM |
109 | bool cont = dir.GetFirst(, filespec, flags); |
110 | while ( cont ) | |
111 | { | |
112 | printf("%s\n", filename.c_str()); | |
7c913512 | 113 | |
23324ae1 FM |
114 | cont = dir.GetNext(); |
115 | } | |
116 | @endcode | |
7c913512 | 117 | |
23324ae1 FM |
118 | @library{wxbase} |
119 | @category{file} | |
120 | */ | |
7c913512 | 121 | class wxDir |
23324ae1 FM |
122 | { |
123 | public: | |
124 | //@{ | |
125 | /** | |
7c913512 | 126 | Opens the directory for enumeration, use IsOpened() |
23324ae1 FM |
127 | to test for errors. |
128 | */ | |
129 | wxDir(); | |
7c913512 | 130 | wxDir(const wxString& dir); |
23324ae1 FM |
131 | //@} |
132 | ||
133 | /** | |
134 | Destructor cleans up the associated resources. It is not virtual and so this | |
135 | class is not meant to be used polymorphically. | |
136 | */ | |
137 | ~wxDir(); | |
138 | ||
139 | /** | |
140 | Test for existence of a directory with the given name | |
141 | */ | |
142 | static bool Exists(const wxString& dir); | |
143 | ||
144 | /** | |
145 | The function returns the path of the first file matching the given @e filespec | |
146 | or an empty string if there are no files matching it. | |
4cc4bfaf FM |
147 | The @a flags parameter may or may not include @c wxDIR_FILES, the |
148 | function always behaves as if it were specified. By default, @a flags | |
23324ae1 FM |
149 | includes @c wxDIR_DIRS and so the function recurses into the subdirectories |
150 | but if this flag is not specified, the function restricts the search only to | |
4cc4bfaf | 151 | the directory @a dirname itself. |
23324ae1 FM |
152 | See also: Traverse() |
153 | */ | |
154 | static wxString FindFirst(const wxString& dirname, | |
155 | const wxString& filespec, | |
156 | int flags = wxDIR_DEFAULT); | |
157 | ||
158 | /** | |
4cc4bfaf FM |
159 | The function appends the names of all the files under directory @a dirname |
160 | to the array @a files (note that its old content is preserved). Only files | |
161 | matching the @a filespec are taken, with empty spec matching all the files. | |
162 | The @a flags parameter should always include @c wxDIR_FILES or the array | |
23324ae1 FM |
163 | would be unchanged and should include @c wxDIR_DIRS flag to recurse into |
164 | subdirectories (both flags are included in the value by default). | |
23324ae1 FM |
165 | See also: Traverse() |
166 | */ | |
167 | static size_t GetAllFiles(const wxString& dirname, | |
4cc4bfaf | 168 | wxArrayString* files, |
23324ae1 FM |
169 | const wxString& filespec = wxEmptyString, |
170 | int flags = wxDIR_DEFAULT); | |
171 | ||
172 | /** | |
4cc4bfaf | 173 | Start enumerating all files matching @a filespec (or all files if it is |
23324ae1 FM |
174 | empty) and @e flags, return @true on success. |
175 | */ | |
176 | bool GetFirst(wxString* filename, | |
177 | const wxString& filespec = wxEmptyString, | |
328f5751 | 178 | int flags = wxDIR_DEFAULT) const; |
23324ae1 FM |
179 | |
180 | /** | |
181 | Returns the name of the directory itself. The returned string does not have the | |
182 | trailing path separator (slash or backslash). | |
183 | */ | |
328f5751 | 184 | wxString GetName() const; |
23324ae1 FM |
185 | |
186 | /** | |
187 | Continue enumerating files which satisfy the criteria specified by the last | |
188 | call to GetFirst(). | |
189 | */ | |
328f5751 | 190 | bool GetNext(wxString* filename) const; |
23324ae1 FM |
191 | |
192 | /** | |
193 | Returns the size (in bytes) of all files recursively found in @c dir or | |
194 | @c wxInvalidSize in case of error. | |
23324ae1 FM |
195 | In case it happens that while traversing folders a file's size can not be read, |
196 | that file is added to the @c filesSkipped array, if not @NULL, and then | |
197 | skipped. | |
198 | This usually happens with some special folders which are locked by the | |
199 | operating system | |
200 | or by another process. Remember that when @c filesSkipped-GetCount() is not | |
201 | zero, | |
202 | then the returned value is not 100% accurate and, if the skipped files were | |
203 | big, it could be | |
204 | far from real size of the directory. | |
23324ae1 FM |
205 | See also: wxFileName::GetHumanReadableSize, |
206 | wxGetDiskSpace | |
207 | */ | |
208 | static wxULongLong GetTotalSize(const wxString& dir, | |
4cc4bfaf | 209 | wxArrayString* filesSkipped = NULL); |
23324ae1 FM |
210 | |
211 | /** | |
7c913512 | 212 | Returns @true if the directory contains any files matching the given |
4cc4bfaf | 213 | @e filespec. If @a filespec is empty, look for any files at all. In any |
23324ae1 FM |
214 | case, even hidden files are taken into account. |
215 | */ | |
216 | bool HasFiles(const wxString& filespec = wxEmptyString); | |
217 | ||
218 | /** | |
219 | Returns @true if the directory contains any subdirectories (if a non | |
220 | empty @e filespec is given, only check for directories matching it). | |
221 | The hidden subdirectories are taken into account as well. | |
222 | */ | |
223 | bool HasSubDirs(const wxString& dirspec = wxEmptyString); | |
224 | ||
225 | /** | |
7c913512 | 226 | Returns @true if the directory was successfully opened by a previous call to |
23324ae1 FM |
227 | Open(). |
228 | */ | |
328f5751 | 229 | bool IsOpened() const; |
23324ae1 FM |
230 | |
231 | /** | |
232 | Open the directory for enumerating, returns @true on success | |
233 | or @false if an error occurred. | |
234 | */ | |
235 | bool Open(const wxString& dir); | |
236 | ||
237 | /** | |
238 | Enumerate all files and directories under the given directory recursively | |
7c913512 | 239 | calling the element of the provided wxDirTraverser |
23324ae1 | 240 | object for each of them. |
7c913512 | 241 | More precisely, the function will really recurse into subdirectories if |
4cc4bfaf | 242 | @a flags contains @c wxDIR_DIRS flag. It will ignore the files (but |
23324ae1 FM |
243 | still possibly recurse into subdirectories) if @c wxDIR_FILES flag is |
244 | given. | |
23324ae1 FM |
245 | For each found directory, @ref wxDirTraverser::ondir sink.OnDir is called |
246 | and @ref wxDirTraverser::onfile sink.OnFile is called for every file. | |
247 | Depending on the return value, the enumeration may continue or stop. | |
23324ae1 FM |
248 | The function returns the total number of files found or @c (size_t)-1 on |
249 | error. | |
23324ae1 FM |
250 | See also: GetAllFiles() |
251 | */ | |
252 | size_t Traverse(wxDirTraverser& sink, | |
253 | const wxString& filespec = wxEmptyString, | |
254 | int flags = wxDIR_DEFAULT); | |
255 | }; |