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