]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/msw/dir.cpp | |
3 | // Purpose: wxDir implementation for Win32 | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 08.12.99 | |
7 | // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // For compilers that support precompilation, includes "wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #ifndef WX_PRECOMP | |
27 | #include "wx/intl.h" | |
28 | #include "wx/log.h" | |
29 | #endif // PCH | |
30 | ||
31 | #include "wx/dir.h" | |
32 | ||
33 | #ifdef __WINDOWS__ | |
34 | #include "wx/msw/private.h" | |
35 | #endif | |
36 | ||
37 | // ---------------------------------------------------------------------------- | |
38 | // define the types and functions used for file searching | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
41 | namespace | |
42 | { | |
43 | ||
44 | typedef WIN32_FIND_DATA FIND_STRUCT; | |
45 | typedef HANDLE FIND_DATA; | |
46 | typedef DWORD FIND_ATTR; | |
47 | ||
48 | inline FIND_DATA InitFindData() | |
49 | { | |
50 | return INVALID_HANDLE_VALUE; | |
51 | } | |
52 | ||
53 | inline bool IsFindDataOk(FIND_DATA fd) | |
54 | { | |
55 | return fd != INVALID_HANDLE_VALUE; | |
56 | } | |
57 | ||
58 | inline void FreeFindData(FIND_DATA fd) | |
59 | { | |
60 | if ( !::FindClose(fd) ) | |
61 | { | |
62 | wxLogLastError(wxT("FindClose")); | |
63 | } | |
64 | } | |
65 | ||
66 | const wxChar *GetNameFromFindData(const FIND_STRUCT *finddata) | |
67 | { | |
68 | return finddata->cFileName; | |
69 | } | |
70 | ||
71 | // Helper function checking that the contents of the given FIND_STRUCT really | |
72 | // match our filter. We need to do it ourselves as native Windows functions | |
73 | // apply the filter to both the long and the short names of the file, so | |
74 | // something like "*.bar" matches "foo.bar.baz" too and not only "foo.bar", so | |
75 | // we have to double check that we have a real match. | |
76 | inline bool | |
77 | CheckFoundMatch(const FIND_STRUCT* finddata, const wxString& filter) | |
78 | { | |
79 | // If there is no filter, the found file must be the one we really are | |
80 | // looking for. | |
81 | if ( filter.empty() ) | |
82 | return true; | |
83 | ||
84 | // Otherwise do check the match validity. Notice that we must do it | |
85 | // case-insensitively because the case of the file names is not supposed to | |
86 | // matter under Windows. | |
87 | wxString fn(GetNameFromFindData(finddata)); | |
88 | ||
89 | // However if the filter contains only special characters (which is a | |
90 | // common case), we can skip the case conversion. | |
91 | if ( filter.find_first_not_of(wxS("*?.")) == wxString::npos ) | |
92 | return fn.Matches(filter); | |
93 | ||
94 | return fn.MakeUpper().Matches(filter.Upper()); | |
95 | } | |
96 | ||
97 | inline bool | |
98 | FindNext(FIND_DATA fd, const wxString& filter, FIND_STRUCT *finddata) | |
99 | { | |
100 | for ( ;; ) | |
101 | { | |
102 | if ( !::FindNextFile(fd, finddata) ) | |
103 | return false; | |
104 | ||
105 | // If we did find something, check that it really matches. | |
106 | if ( CheckFoundMatch(finddata, filter) ) | |
107 | return true; | |
108 | } | |
109 | } | |
110 | ||
111 | inline FIND_DATA | |
112 | FindFirst(const wxString& spec, | |
113 | const wxString& filter, | |
114 | FIND_STRUCT *finddata) | |
115 | { | |
116 | FIND_DATA fd = ::FindFirstFile(spec.t_str(), finddata); | |
117 | ||
118 | // As in FindNext() above, we need to check that the file name we found | |
119 | // really matches our filter and look for the next match if it doesn't. | |
120 | if ( IsFindDataOk(fd) && !CheckFoundMatch(finddata, filter) ) | |
121 | { | |
122 | if ( !FindNext(fd, filter, finddata) ) | |
123 | { | |
124 | // As we return the invalid handle from here to indicate that we | |
125 | // didn't find anything, close the one we initially received | |
126 | // ourselves. | |
127 | FreeFindData(fd); | |
128 | ||
129 | return INVALID_HANDLE_VALUE; | |
130 | } | |
131 | } | |
132 | ||
133 | return fd; | |
134 | } | |
135 | ||
136 | inline FIND_ATTR GetAttrFromFindData(FIND_STRUCT *finddata) | |
137 | { | |
138 | return finddata->dwFileAttributes; | |
139 | } | |
140 | ||
141 | inline bool IsDir(FIND_ATTR attr) | |
142 | { | |
143 | return (attr & FILE_ATTRIBUTE_DIRECTORY) != 0; | |
144 | } | |
145 | ||
146 | inline bool IsHidden(FIND_ATTR attr) | |
147 | { | |
148 | return (attr & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) != 0; | |
149 | } | |
150 | ||
151 | } // anonymous namespace | |
152 | ||
153 | // ---------------------------------------------------------------------------- | |
154 | // constants | |
155 | // ---------------------------------------------------------------------------- | |
156 | ||
157 | #ifndef MAX_PATH | |
158 | #define MAX_PATH 260 // from VC++ headers | |
159 | #endif | |
160 | ||
161 | // ---------------------------------------------------------------------------- | |
162 | // macros | |
163 | // ---------------------------------------------------------------------------- | |
164 | ||
165 | #define M_DIR ((wxDirData *)m_data) | |
166 | ||
167 | // ---------------------------------------------------------------------------- | |
168 | // private classes | |
169 | // ---------------------------------------------------------------------------- | |
170 | ||
171 | // this class stores everything we need to enumerate the files | |
172 | class wxDirData | |
173 | { | |
174 | public: | |
175 | wxDirData(const wxString& dirname); | |
176 | ~wxDirData(); | |
177 | ||
178 | void SetFileSpec(const wxString& filespec) { m_filespec = filespec; } | |
179 | void SetFlags(int flags) { m_flags = flags; } | |
180 | ||
181 | void Close(); | |
182 | void Rewind(); | |
183 | bool Read(wxString *filename); | |
184 | ||
185 | const wxString& GetName() const { return m_dirname; } | |
186 | ||
187 | private: | |
188 | FIND_DATA m_finddata; | |
189 | ||
190 | wxString m_dirname; | |
191 | wxString m_filespec; | |
192 | ||
193 | int m_flags; | |
194 | ||
195 | wxDECLARE_NO_COPY_CLASS(wxDirData); | |
196 | }; | |
197 | ||
198 | // ============================================================================ | |
199 | // implementation | |
200 | // ============================================================================ | |
201 | ||
202 | // ---------------------------------------------------------------------------- | |
203 | // wxDirData | |
204 | // ---------------------------------------------------------------------------- | |
205 | ||
206 | wxDirData::wxDirData(const wxString& dirname) | |
207 | : m_dirname(dirname) | |
208 | { | |
209 | m_finddata = InitFindData(); | |
210 | } | |
211 | ||
212 | wxDirData::~wxDirData() | |
213 | { | |
214 | Close(); | |
215 | } | |
216 | ||
217 | void wxDirData::Close() | |
218 | { | |
219 | if ( IsFindDataOk(m_finddata) ) | |
220 | { | |
221 | FreeFindData(m_finddata); | |
222 | ||
223 | m_finddata = InitFindData(); | |
224 | } | |
225 | } | |
226 | ||
227 | void wxDirData::Rewind() | |
228 | { | |
229 | Close(); | |
230 | } | |
231 | ||
232 | bool wxDirData::Read(wxString *filename) | |
233 | { | |
234 | bool first = false; | |
235 | ||
236 | WIN32_FIND_DATA finddata; | |
237 | #define PTR_TO_FINDDATA (&finddata) | |
238 | ||
239 | if ( !IsFindDataOk(m_finddata) ) | |
240 | { | |
241 | // open first | |
242 | wxString filespec = m_dirname; | |
243 | if ( !wxEndsWithPathSeparator(filespec) ) | |
244 | { | |
245 | filespec += wxT('\\'); | |
246 | } | |
247 | if ( !m_filespec ) | |
248 | filespec += wxT("*.*"); | |
249 | else | |
250 | filespec += m_filespec; | |
251 | ||
252 | m_finddata = FindFirst(filespec, m_filespec, PTR_TO_FINDDATA); | |
253 | ||
254 | first = true; | |
255 | } | |
256 | ||
257 | if ( !IsFindDataOk(m_finddata) ) | |
258 | { | |
259 | #ifdef __WIN32__ | |
260 | DWORD err = ::GetLastError(); | |
261 | ||
262 | if ( err != ERROR_FILE_NOT_FOUND && err != ERROR_NO_MORE_FILES ) | |
263 | { | |
264 | wxLogSysError(err, _("Cannot enumerate files in directory '%s'"), | |
265 | m_dirname.c_str()); | |
266 | } | |
267 | #endif // __WIN32__ | |
268 | //else: not an error, just no (such) files | |
269 | ||
270 | return false; | |
271 | } | |
272 | ||
273 | const wxChar *name; | |
274 | FIND_ATTR attr; | |
275 | ||
276 | for ( ;; ) | |
277 | { | |
278 | if ( first ) | |
279 | { | |
280 | first = false; | |
281 | } | |
282 | else | |
283 | { | |
284 | if ( !FindNext(m_finddata, m_filespec, PTR_TO_FINDDATA) ) | |
285 | { | |
286 | #ifdef __WIN32__ | |
287 | DWORD err = ::GetLastError(); | |
288 | ||
289 | if ( err != ERROR_NO_MORE_FILES ) | |
290 | { | |
291 | wxLogLastError(wxT("FindNext")); | |
292 | } | |
293 | #endif // __WIN32__ | |
294 | //else: not an error, just no more (such) files | |
295 | ||
296 | return false; | |
297 | } | |
298 | } | |
299 | ||
300 | name = GetNameFromFindData(PTR_TO_FINDDATA); | |
301 | attr = GetAttrFromFindData(PTR_TO_FINDDATA); | |
302 | ||
303 | // don't return "." and ".." unless asked for | |
304 | if ( name[0] == wxT('.') && | |
305 | ((name[1] == wxT('.') && name[2] == wxT('\0')) || | |
306 | (name[1] == wxT('\0'))) ) | |
307 | { | |
308 | if ( !(m_flags & wxDIR_DOTDOT) ) | |
309 | continue; | |
310 | } | |
311 | ||
312 | // check the type now | |
313 | if ( !(m_flags & wxDIR_FILES) && !IsDir(attr) ) | |
314 | { | |
315 | // it's a file, but we don't want them | |
316 | continue; | |
317 | } | |
318 | else if ( !(m_flags & wxDIR_DIRS) && IsDir(attr) ) | |
319 | { | |
320 | // it's a dir, and we don't want it | |
321 | continue; | |
322 | } | |
323 | ||
324 | // finally, check whether it's a hidden file | |
325 | if ( !(m_flags & wxDIR_HIDDEN) ) | |
326 | { | |
327 | if ( IsHidden(attr) ) | |
328 | { | |
329 | // it's a hidden file, skip it | |
330 | continue; | |
331 | } | |
332 | } | |
333 | ||
334 | *filename = name; | |
335 | ||
336 | break; | |
337 | } | |
338 | ||
339 | return true; | |
340 | } | |
341 | ||
342 | // ---------------------------------------------------------------------------- | |
343 | // wxDir construction/destruction | |
344 | // ---------------------------------------------------------------------------- | |
345 | ||
346 | wxDir::wxDir(const wxString& dirname) | |
347 | { | |
348 | m_data = NULL; | |
349 | ||
350 | (void)Open(dirname); | |
351 | } | |
352 | ||
353 | bool wxDir::Open(const wxString& dirname) | |
354 | { | |
355 | delete M_DIR; | |
356 | ||
357 | // The Unix code does a similar test | |
358 | if (wxDirExists(dirname)) | |
359 | { | |
360 | m_data = new wxDirData(dirname); | |
361 | ||
362 | return true; | |
363 | } | |
364 | else | |
365 | { | |
366 | m_data = NULL; | |
367 | ||
368 | return false; | |
369 | } | |
370 | } | |
371 | ||
372 | bool wxDir::IsOpened() const | |
373 | { | |
374 | return m_data != NULL; | |
375 | } | |
376 | ||
377 | wxString wxDir::GetName() const | |
378 | { | |
379 | wxString name; | |
380 | if ( m_data ) | |
381 | { | |
382 | name = M_DIR->GetName(); | |
383 | if ( !name.empty() ) | |
384 | { | |
385 | // bring to canonical Windows form | |
386 | name.Replace(wxT("/"), wxT("\\")); | |
387 | ||
388 | if ( name.Last() == wxT('\\') ) | |
389 | { | |
390 | // chop off the last (back)slash | |
391 | name.Truncate(name.length() - 1); | |
392 | } | |
393 | } | |
394 | } | |
395 | ||
396 | return name; | |
397 | } | |
398 | ||
399 | void wxDir::Close() | |
400 | { | |
401 | if ( m_data ) | |
402 | { | |
403 | delete m_data; | |
404 | m_data = NULL; | |
405 | } | |
406 | } | |
407 | ||
408 | // ---------------------------------------------------------------------------- | |
409 | // wxDir enumerating | |
410 | // ---------------------------------------------------------------------------- | |
411 | ||
412 | bool wxDir::GetFirst(wxString *filename, | |
413 | const wxString& filespec, | |
414 | int flags) const | |
415 | { | |
416 | wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") ); | |
417 | ||
418 | M_DIR->Rewind(); | |
419 | ||
420 | M_DIR->SetFileSpec(filespec); | |
421 | M_DIR->SetFlags(flags); | |
422 | ||
423 | return GetNext(filename); | |
424 | } | |
425 | ||
426 | bool wxDir::GetNext(wxString *filename) const | |
427 | { | |
428 | wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") ); | |
429 | ||
430 | wxCHECK_MSG( filename, false, wxT("bad pointer in wxDir::GetNext()") ); | |
431 | ||
432 | return M_DIR->Read(filename); | |
433 | } | |
434 | ||
435 | // ---------------------------------------------------------------------------- | |
436 | // wxGetDirectoryTimes: used by wxFileName::GetTimes() | |
437 | // ---------------------------------------------------------------------------- | |
438 | ||
439 | #ifdef __WIN32__ | |
440 | ||
441 | extern bool | |
442 | wxGetDirectoryTimes(const wxString& dirname, | |
443 | FILETIME *ftAccess, FILETIME *ftCreate, FILETIME *ftMod) | |
444 | { | |
445 | #ifdef __WXWINCE__ | |
446 | // FindFirst() is going to fail | |
447 | wxASSERT_MSG( !dirname.empty(), | |
448 | wxT("incorrect directory name format in wxGetDirectoryTimes") ); | |
449 | #else | |
450 | // FindFirst() is going to fail | |
451 | wxASSERT_MSG( !dirname.empty() && dirname.Last() != wxT('\\'), | |
452 | wxT("incorrect directory name format in wxGetDirectoryTimes") ); | |
453 | #endif | |
454 | ||
455 | FIND_STRUCT fs; | |
456 | FIND_DATA fd = FindFirst(dirname, wxEmptyString, &fs); | |
457 | if ( !IsFindDataOk(fd) ) | |
458 | { | |
459 | return false; | |
460 | } | |
461 | ||
462 | *ftAccess = fs.ftLastAccessTime; | |
463 | *ftCreate = fs.ftCreationTime; | |
464 | *ftMod = fs.ftLastWriteTime; | |
465 | ||
466 | FindClose(fd); | |
467 | ||
468 | return true; | |
469 | } | |
470 | ||
471 | #endif // __WIN32__ | |
472 |