]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dir.cpp
indicate whether this is a selection or a deselection in wxCommandEvent generated...
[wxWidgets.git] / src / msw / dir.cpp
CommitLineData
1944c6bd
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: 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 license
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "dir.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
f6081a04
JS
31// For _A_SUBDIR, etc.
32#if defined(__BORLANDC__) && defined(__WIN16__)
33#include <dos.h>
34#endif
35
1944c6bd
VZ
36#ifndef WX_PRECOMP
37 #include "wx/intl.h"
38 #include "wx/log.h"
39#endif // PCH
40
41#include "wx/dir.h"
42#include "wx/filefn.h" // for wxPathExists()
43
8f177c8e
VZ
44// ----------------------------------------------------------------------------
45// define the types and functions used for file searching
46// ----------------------------------------------------------------------------
47
48// under Win16 use compiler-specific functions
49#ifdef __WIN16__
50 #ifdef __VISUALC__
51 #include <dos.h>
52 #include <errno.h>
53
54 typedef struct _find_t FIND_STRUCT;
55 #elif defined(__BORLANDC__)
56 #include <dir.h>
57
58 typedef struct ffblk FIND_STRUCT;
59 #else
60 #error "No directory searching functions for this compiler"
61 #endif
62
63 typedef FIND_STRUCT *FIND_DATA;
64 typedef char FIND_ATTR;
65
66 static inline FIND_DATA InitFindData() { return (FIND_DATA)NULL; }
67 static inline bool IsFindDataOk(FIND_DATA fd) { return fd != NULL; }
68 static inline void FreeFindData(FIND_DATA fd) { free(fd); }
69
70 static inline FIND_DATA FindFirst(const wxString& spec,
71 FIND_STRUCT * WXUNUSED(finddata))
72 {
73 // attribute to find all files
74 static const FIND_ATTR attr = 0x3F;
75
76 FIND_DATA fd = (FIND_DATA)malloc(sizeof(FIND_STRUCT));
77
78 if (
79 #ifdef __VISUALC__
80 _dos_findfirst(spec, attr, fd) == 0
81 #else // Borland
82 findfirst(spec, fd, attr) == 0
83 #endif
84 )
85 {
86 return fd;
87 }
88 else
89 {
90 free(fd);
91
92 return NULL;
93 }
94 }
95
96 static inline bool FindNext(FIND_DATA fd, FIND_STRUCT * WXUNUSED(finddata))
97 {
98 #ifdef __VISUALC__
99 return _dos_findnext(fd) == 0;
100 #else // Borland
101 return findnext(fd) == 0;
102 #endif
103 }
104
105 static const wxChar *GetNameFromFindData(FIND_STRUCT *finddata)
106 {
107 #ifdef __VISUALC__
108 return finddata->name;
109 #else // Borland
110 return finddata->ff_name;
111 #endif
112 }
113
114 static const FIND_ATTR GetAttrFromFindData(FIND_STRUCT *finddata)
115 {
116 #ifdef __VISUALC__
117 return finddata->attrib;
118 #else // Borland
119 return finddata->ff_attrib;
120 #endif
121 }
122
123 static inline bool IsDir(FIND_ATTR attr)
124 {
125 return (attr & _A_SUBDIR) != 0;
126 }
127
128 static inline bool IsHidden(FIND_ATTR attr)
129 {
130 return (attr & (_A_SYSTEM | _A_HIDDEN)) != 0;
131 }
132#else // Win32
133 #include <windows.h>
134
135 typedef WIN32_FIND_DATA FIND_STRUCT;
136 typedef HANDLE FIND_DATA;
137 typedef DWORD FIND_ATTR;
138
139 static inline FIND_DATA InitFindData() { return INVALID_HANDLE_VALUE; }
140
141 static inline bool IsFindDataOk(FIND_DATA fd)
142 {
143 return fd != INVALID_HANDLE_VALUE;
144 }
145
146 static inline void FreeFindData(FIND_DATA fd)
147 {
148 if ( !::FindClose(fd) )
149 {
150 wxLogLastError(_T("FindClose"));
151 }
152 }
153
154 static inline FIND_DATA FindFirst(const wxString& spec,
155 FIND_STRUCT *finddata)
156 {
c5a1681b 157 return ::FindFirstFile(spec, finddata);
8f177c8e
VZ
158 }
159
160 static inline bool FindNext(FIND_DATA fd, FIND_STRUCT *finddata)
161 {
162 return ::FindNextFile(fd, finddata) != 0;
163 }
164
165 static const wxChar *GetNameFromFindData(FIND_STRUCT *finddata)
166 {
167 return finddata->cFileName;
168 }
169
170 static const FIND_ATTR GetAttrFromFindData(FIND_STRUCT *finddata)
171 {
172 return finddata->dwFileAttributes;
173 }
174
175 static inline bool IsDir(FIND_ATTR attr)
176 {
177 return (attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
178 }
179
180 static inline bool IsHidden(FIND_ATTR attr)
181 {
182 return (attr & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) != 0;
183 }
184#endif // __WIN16__
1944c6bd
VZ
185
186// ----------------------------------------------------------------------------
187// constants
188// ----------------------------------------------------------------------------
189
190#ifndef MAX_PATH
191 #define MAX_PATH 260 // from VC++ headers
192#endif
193
194// ----------------------------------------------------------------------------
195// macros
196// ----------------------------------------------------------------------------
197
198#define M_DIR ((wxDirData *)m_data)
199
200// ----------------------------------------------------------------------------
201// private classes
202// ----------------------------------------------------------------------------
203
204// this class stores everything we need to enumerate the files
205class wxDirData
206{
207public:
208 wxDirData(const wxString& dirname);
209 ~wxDirData();
210
211 void SetFileSpec(const wxString& filespec) { m_filespec = filespec; }
212 void SetFlags(int flags) { m_flags = flags; }
213
214 void Close();
215 void Rewind();
216 bool Read(wxString *filename);
217
35332784
VZ
218 const wxString& GetName() const { return m_dirname; }
219
1944c6bd 220private:
8f177c8e 221 FIND_DATA m_finddata;
1944c6bd
VZ
222
223 wxString m_dirname;
224 wxString m_filespec;
225
226 int m_flags;
22f3361e
VZ
227
228 DECLARE_NO_COPY_CLASS(wxDirData)
1944c6bd
VZ
229};
230
231// ============================================================================
232// implementation
233// ============================================================================
234
235// ----------------------------------------------------------------------------
236// wxDirData
237// ----------------------------------------------------------------------------
238
239wxDirData::wxDirData(const wxString& dirname)
240 : m_dirname(dirname)
241{
8f177c8e 242 m_finddata = InitFindData();
1944c6bd
VZ
243}
244
245wxDirData::~wxDirData()
246{
247 Close();
248}
249
250void wxDirData::Close()
251{
8f177c8e 252 if ( IsFindDataOk(m_finddata) )
1944c6bd 253 {
8f177c8e 254 FreeFindData(m_finddata);
4afd7529 255
8f177c8e 256 m_finddata = InitFindData();
1944c6bd
VZ
257 }
258}
259
260void wxDirData::Rewind()
261{
262 Close();
263}
264
265bool wxDirData::Read(wxString *filename)
266{
267 bool first = FALSE;
268
8f177c8e 269#ifdef __WIN32__
1944c6bd 270 WIN32_FIND_DATA finddata;
8f177c8e
VZ
271 #define PTR_TO_FINDDATA (&finddata)
272#else // Win16
273 #define PTR_TO_FINDDATA (m_finddata)
274#endif
275
276 if ( !IsFindDataOk(m_finddata) )
1944c6bd
VZ
277 {
278 // open first
ad0dc53b
VZ
279 wxString filespec = m_dirname;
280 if ( !wxEndsWithPathSeparator(filespec) )
281 {
282 filespec += _T('\\');
283 }
284 filespec += (!m_filespec ? _T("*.*") : m_filespec.c_str());
4afd7529 285
8f177c8e 286 m_finddata = FindFirst(filespec, PTR_TO_FINDDATA);
1944c6bd
VZ
287
288 first = TRUE;
289 }
290
8f177c8e 291 if ( !IsFindDataOk(m_finddata) )
1944c6bd 292 {
8f177c8e 293#ifdef __WIN32__
1944c6bd
VZ
294 DWORD err = ::GetLastError();
295
4afd7529 296 if ( err != ERROR_FILE_NOT_FOUND )
1944c6bd
VZ
297 {
298 wxLogSysError(err, _("Can not enumerate files in directory '%s'"),
299 m_dirname.c_str());
300 }
8f177c8e 301#endif // __WIN32__
1944c6bd
VZ
302 //else: not an error, just no (such) files
303
304 return FALSE;
305 }
306
1944c6bd 307 const wxChar *name;
8f177c8e 308 FIND_ATTR attr;
1944c6bd 309
4afd7529 310 for ( ;; )
1944c6bd
VZ
311 {
312 if ( first )
313 {
314 first = FALSE;
315 }
316 else
317 {
8f177c8e 318 if ( !FindNext(m_finddata, PTR_TO_FINDDATA) )
1944c6bd 319 {
8f177c8e 320#ifdef __WIN32__
1944c6bd
VZ
321 DWORD err = ::GetLastError();
322
323 if ( err != ERROR_NO_MORE_FILES )
324 {
325 wxLogLastError(_T("FindNext"));
326 }
8f177c8e 327#endif // __WIN32__
1944c6bd
VZ
328 //else: not an error, just no more (such) files
329
330 return FALSE;
331 }
332 }
333
8f177c8e
VZ
334 name = GetNameFromFindData(PTR_TO_FINDDATA);
335 attr = GetAttrFromFindData(PTR_TO_FINDDATA);
1944c6bd
VZ
336
337 // don't return "." and ".." unless asked for
338 if ( name[0] == _T('.') &&
339 ((name[1] == _T('.') && name[2] == _T('\0')) ||
340 (name[1] == _T('\0'))) )
341 {
342 if ( !(m_flags & wxDIR_DOTDOT) )
343 continue;
344 }
345
346 // check the type now
8f177c8e 347 if ( !(m_flags & wxDIR_FILES) && !IsDir(attr) )
1944c6bd
VZ
348 {
349 // it's a file, but we don't want them
350 continue;
351 }
8f177c8e 352 else if ( !(m_flags & wxDIR_DIRS) && IsDir(attr) )
1944c6bd
VZ
353 {
354 // it's a dir, and we don't want it
355 continue;
356 }
357
358 // finally, check whether it's a hidden file
359 if ( !(m_flags & wxDIR_HIDDEN) )
360 {
8f177c8e 361 if ( IsHidden(attr) )
4afd7529
VZ
362 {
363 // it's a hidden file, skip it
364 continue;
365 }
1944c6bd 366 }
1944c6bd 367
4afd7529
VZ
368 *filename = name;
369
370 break;
371 }
1944c6bd
VZ
372
373 return TRUE;
374}
375
376// ----------------------------------------------------------------------------
377// wxDir helpers
378// ----------------------------------------------------------------------------
379
380/* static */
381bool wxDir::Exists(const wxString& dir)
382{
383 return wxPathExists(dir);
384}
385
386// ----------------------------------------------------------------------------
387// wxDir construction/destruction
388// ----------------------------------------------------------------------------
389
390wxDir::wxDir(const wxString& dirname)
391{
392 m_data = NULL;
393
394 (void)Open(dirname);
395}
396
397bool wxDir::Open(const wxString& dirname)
398{
399 delete M_DIR;
400 m_data = new wxDirData(dirname);
401
402 return TRUE;
403}
404
405bool wxDir::IsOpened() const
406{
407 return m_data != NULL;
408}
409
35332784
VZ
410wxString wxDir::GetName() const
411{
412 wxString name;
413 if ( m_data )
414 {
415 name = M_DIR->GetName();
416 if ( !name.empty() )
417 {
418 // bring to canonical Windows form
419 name.Replace(_T("/"), _T("\\"));
420
421 if ( name.Last() == _T('\\') )
422 {
423 // chop off the last (back)slash
424 name.Truncate(name.length() - 1);
425 }
426 }
427 }
428
429 return name;
430}
431
1944c6bd
VZ
432wxDir::~wxDir()
433{
434 delete M_DIR;
435}
436
437// ----------------------------------------------------------------------------
438// wxDir enumerating
439// ----------------------------------------------------------------------------
440
441bool wxDir::GetFirst(wxString *filename,
442 const wxString& filespec,
443 int flags) const
444{
445 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
446
447 M_DIR->Rewind();
448
449 M_DIR->SetFileSpec(filespec);
450 M_DIR->SetFlags(flags);
451
452 return GetNext(filename);
453}
454
455bool wxDir::GetNext(wxString *filename) const
456{
457 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
458
459 wxCHECK_MSG( filename, FALSE, _T("bad pointer in wxDir::GetNext()") );
460
461 return M_DIR->Read(filename);
462}
2b5f62a0
VZ
463
464// ----------------------------------------------------------------------------
465// wxGetDirectoryTimes: used by wxFileName::GetTimes()
466// ----------------------------------------------------------------------------
467
468#ifdef __WIN32__
469
470extern bool
471wxGetDirectoryTimes(const wxString& dirname,
472 FILETIME *ftAccess, FILETIME *ftCreate, FILETIME *ftMod)
473{
474 // FindFirst() is going to fail
475 wxASSERT_MSG( !dirname.empty() && dirname.Last() != _T('\\'),
476 _T("incorrect directory name format in wxGetDirectoryTimes") );
477
478 FIND_STRUCT fs;
479 FIND_DATA fd = FindFirst(dirname, &fs);
480 if ( !IsFindDataOk(fd) )
481 {
482 return FALSE;
483 }
484
485 *ftAccess = fs.ftLastAccessTime;
486 *ftCreate = fs.ftCreationTime;
487 *ftMod = fs.ftLastWriteTime;
488
489 FindClose(fd);
490
491 return TRUE;
492}
493
494#endif // __WIN32__
495