]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dir.cpp
Recognize VC12 (a.k.a. MSVS 2013) and define __VISUALC12__ for it.
[wxWidgets.git] / src / msw / dir.cpp
CommitLineData
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
42namespace
43{
44
3a5bcc4d
VZ
45typedef WIN32_FIND_DATA FIND_STRUCT;
46typedef HANDLE FIND_DATA;
47typedef DWORD FIND_ATTR;
8f177c8e 48
145bbf1f
VZ
49inline FIND_DATA InitFindData()
50{
51 return INVALID_HANDLE_VALUE;
52}
8f177c8e 53
145bbf1f 54inline bool IsFindDataOk(FIND_DATA fd)
3a5bcc4d 55{
8f177c8e 56 return fd != INVALID_HANDLE_VALUE;
3a5bcc4d 57}
8f177c8e 58
145bbf1f 59inline 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 67const 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.
77inline bool
78CheckFoundMatch(const FIND_STRUCT* finddata, const wxString& filter)
3a5bcc4d 79{
512b03db
VZ
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());
3a5bcc4d 96}
8f177c8e 97
4daceaac
VZ
98inline bool
99FindNext(FIND_DATA fd, const wxString& filter, FIND_STRUCT *finddata)
3a5bcc4d 100{
4daceaac
VZ
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
112inline FIND_DATA
113FindFirst(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;
3a5bcc4d 135}
8f177c8e 136
145bbf1f 137inline FIND_ATTR GetAttrFromFindData(FIND_STRUCT *finddata)
3a5bcc4d 138{
145bbf1f 139 return finddata->dwFileAttributes;
3a5bcc4d 140}
8f177c8e 141
145bbf1f 142inline bool IsDir(FIND_ATTR attr)
3a5bcc4d 143{
145bbf1f 144 return (attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
3a5bcc4d 145}
8f177c8e 146
145bbf1f 147inline bool IsHidden(FIND_ATTR attr)
3a5bcc4d 148{
145bbf1f 149 return (attr & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) != 0;
3a5bcc4d 150}
1944c6bd 151
145bbf1f
VZ
152} // anonymous namespace
153
1944c6bd
VZ
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
173class wxDirData
174{
175public:
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
35332784
VZ
186 const wxString& GetName() const { return m_dirname; }
187
1944c6bd 188private:
8f177c8e 189 FIND_DATA m_finddata;
1944c6bd
VZ
190
191 wxString m_dirname;
192 wxString m_filespec;
193
194 int m_flags;
22f3361e 195
c0c133e1 196 wxDECLARE_NO_COPY_CLASS(wxDirData);
1944c6bd
VZ
197};
198
199// ============================================================================
200// implementation
201// ============================================================================
202
203// ----------------------------------------------------------------------------
204// wxDirData
205// ----------------------------------------------------------------------------
206
207wxDirData::wxDirData(const wxString& dirname)
208 : m_dirname(dirname)
209{
8f177c8e 210 m_finddata = InitFindData();
1944c6bd
VZ
211}
212
213wxDirData::~wxDirData()
214{
215 Close();
216}
217
218void wxDirData::Close()
219{
8f177c8e 220 if ( IsFindDataOk(m_finddata) )
1944c6bd 221 {
8f177c8e 222 FreeFindData(m_finddata);
4afd7529 223
8f177c8e 224 m_finddata = InitFindData();
1944c6bd
VZ
225 }
226}
227
228void wxDirData::Rewind()
229{
230 Close();
231}
232
233bool wxDirData::Read(wxString *filename)
234{
d71cc120 235 bool first = false;
1944c6bd
VZ
236
237 WIN32_FIND_DATA finddata;
8f177c8e 238 #define PTR_TO_FINDDATA (&finddata)
8f177c8e
VZ
239
240 if ( !IsFindDataOk(m_finddata) )
1944c6bd
VZ
241 {
242 // open first
ad0dc53b
VZ
243 wxString filespec = m_dirname;
244 if ( !wxEndsWithPathSeparator(filespec) )
245 {
9a83f860 246 filespec += wxT('\\');
ad0dc53b 247 }
c9f78968 248 if ( !m_filespec )
9a83f860 249 filespec += wxT("*.*");
c9f78968
VS
250 else
251 filespec += m_filespec;
4afd7529 252
4daceaac 253 m_finddata = FindFirst(filespec, m_filespec, PTR_TO_FINDDATA);
1944c6bd 254
d71cc120 255 first = true;
1944c6bd
VZ
256 }
257
8f177c8e 258 if ( !IsFindDataOk(m_finddata) )
1944c6bd 259 {
8f177c8e 260#ifdef __WIN32__
1944c6bd
VZ
261 DWORD err = ::GetLastError();
262
5613bc20 263 if ( err != ERROR_FILE_NOT_FOUND && err != ERROR_NO_MORE_FILES )
1944c6bd 264 {
4c51a665 265 wxLogSysError(err, _("Cannot enumerate files in directory '%s'"),
1944c6bd
VZ
266 m_dirname.c_str());
267 }
8f177c8e 268#endif // __WIN32__
1944c6bd
VZ
269 //else: not an error, just no (such) files
270
d71cc120 271 return false;
1944c6bd
VZ
272 }
273
1944c6bd 274 const wxChar *name;
8f177c8e 275 FIND_ATTR attr;
1944c6bd 276
4afd7529 277 for ( ;; )
1944c6bd
VZ
278 {
279 if ( first )
280 {
d71cc120 281 first = false;
1944c6bd
VZ
282 }
283 else
284 {
4daceaac 285 if ( !FindNext(m_finddata, m_filespec, PTR_TO_FINDDATA) )
1944c6bd 286 {
8f177c8e 287#ifdef __WIN32__
1944c6bd
VZ
288 DWORD err = ::GetLastError();
289
290 if ( err != ERROR_NO_MORE_FILES )
291 {
9a83f860 292 wxLogLastError(wxT("FindNext"));
1944c6bd 293 }
8f177c8e 294#endif // __WIN32__
1944c6bd
VZ
295 //else: not an error, just no more (such) files
296
d71cc120 297 return false;
1944c6bd
VZ
298 }
299 }
300
8f177c8e
VZ
301 name = GetNameFromFindData(PTR_TO_FINDDATA);
302 attr = GetAttrFromFindData(PTR_TO_FINDDATA);
1944c6bd
VZ
303
304 // don't return "." and ".." unless asked for
9a83f860
VZ
305 if ( name[0] == wxT('.') &&
306 ((name[1] == wxT('.') && name[2] == wxT('\0')) ||
307 (name[1] == wxT('\0'))) )
1944c6bd
VZ
308 {
309 if ( !(m_flags & wxDIR_DOTDOT) )
310 continue;
311 }
312
313 // check the type now
8f177c8e 314 if ( !(m_flags & wxDIR_FILES) && !IsDir(attr) )
1944c6bd
VZ
315 {
316 // it's a file, but we don't want them
317 continue;
318 }
8f177c8e 319 else if ( !(m_flags & wxDIR_DIRS) && IsDir(attr) )
1944c6bd
VZ
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 {
8f177c8e 328 if ( IsHidden(attr) )
4afd7529
VZ
329 {
330 // it's a hidden file, skip it
331 continue;
332 }
1944c6bd 333 }
1944c6bd 334
4afd7529
VZ
335 *filename = name;
336
337 break;
338 }
1944c6bd 339
d71cc120 340 return true;
1944c6bd
VZ
341}
342
1944c6bd
VZ
343// ----------------------------------------------------------------------------
344// wxDir construction/destruction
345// ----------------------------------------------------------------------------
346
347wxDir::wxDir(const wxString& dirname)
348{
349 m_data = NULL;
350
351 (void)Open(dirname);
352}
353
354bool wxDir::Open(const wxString& dirname)
355{
356 delete M_DIR;
03647350 357
89432923
RR
358 // The Unix code does a similar test
359 if (wxDirExists(dirname))
360 {
361 m_data = new wxDirData(dirname);
1944c6bd 362
89432923
RR
363 return true;
364 }
365 else
366 {
367 m_data = NULL;
03647350 368
89432923
RR
369 return false;
370 }
1944c6bd
VZ
371}
372
373bool wxDir::IsOpened() const
374{
375 return m_data != NULL;
376}
377
35332784
VZ
378wxString 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
9a83f860 387 name.Replace(wxT("/"), wxT("\\"));
35332784 388
9a83f860 389 if ( name.Last() == wxT('\\') )
35332784
VZ
390 {
391 // chop off the last (back)slash
392 name.Truncate(name.length() - 1);
393 }
394 }
395 }
396
397 return name;
398}
399
6619c4af 400void wxDir::Close()
1944c6bd 401{
6619c4af
VZ
402 if ( m_data )
403 {
404 delete m_data;
405 m_data = NULL;
406 }
1944c6bd
VZ
407}
408
409// ----------------------------------------------------------------------------
410// wxDir enumerating
411// ----------------------------------------------------------------------------
412
413bool wxDir::GetFirst(wxString *filename,
414 const wxString& filespec,
415 int flags) const
416{
9a83f860 417 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
1944c6bd
VZ
418
419 M_DIR->Rewind();
420
421 M_DIR->SetFileSpec(filespec);
422 M_DIR->SetFlags(flags);
423
424 return GetNext(filename);
425}
426
427bool wxDir::GetNext(wxString *filename) const
428{
9a83f860 429 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
1944c6bd 430
9a83f860 431 wxCHECK_MSG( filename, false, wxT("bad pointer in wxDir::GetNext()") );
1944c6bd
VZ
432
433 return M_DIR->Read(filename);
434}
2b5f62a0
VZ
435
436// ----------------------------------------------------------------------------
437// wxGetDirectoryTimes: used by wxFileName::GetTimes()
438// ----------------------------------------------------------------------------
439
440#ifdef __WIN32__
441
442extern bool
443wxGetDirectoryTimes(const wxString& dirname,
444 FILETIME *ftAccess, FILETIME *ftCreate, FILETIME *ftMod)
445{
4de1b652
JS
446#ifdef __WXWINCE__
447 // FindFirst() is going to fail
448 wxASSERT_MSG( !dirname.empty(),
9a83f860 449 wxT("incorrect directory name format in wxGetDirectoryTimes") );
4de1b652 450#else
2b5f62a0 451 // FindFirst() is going to fail
9a83f860
VZ
452 wxASSERT_MSG( !dirname.empty() && dirname.Last() != wxT('\\'),
453 wxT("incorrect directory name format in wxGetDirectoryTimes") );
4de1b652 454#endif
2b5f62a0
VZ
455
456 FIND_STRUCT fs;
4daceaac 457 FIND_DATA fd = FindFirst(dirname, wxEmptyString, &fs);
2b5f62a0
VZ
458 if ( !IsFindDataOk(fd) )
459 {
d71cc120 460 return false;
2b5f62a0
VZ
461 }
462
463 *ftAccess = fs.ftLastAccessTime;
464 *ftCreate = fs.ftCreationTime;
465 *ftMod = fs.ftLastWriteTime;
466
467 FindClose(fd);
468
d71cc120 469 return true;
2b5f62a0
VZ
470}
471
472#endif // __WIN32__
473