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