]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dir.cpp
suppressed wrong debug error messages from SetFocus
[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;
227};
228
229// ============================================================================
230// implementation
231// ============================================================================
232
233// ----------------------------------------------------------------------------
234// wxDirData
235// ----------------------------------------------------------------------------
236
237wxDirData::wxDirData(const wxString& dirname)
238 : m_dirname(dirname)
239{
8f177c8e 240 m_finddata = InitFindData();
1944c6bd
VZ
241}
242
243wxDirData::~wxDirData()
244{
245 Close();
246}
247
248void wxDirData::Close()
249{
8f177c8e 250 if ( IsFindDataOk(m_finddata) )
1944c6bd 251 {
8f177c8e 252 FreeFindData(m_finddata);
4afd7529 253
8f177c8e 254 m_finddata = InitFindData();
1944c6bd
VZ
255 }
256}
257
258void wxDirData::Rewind()
259{
260 Close();
261}
262
263bool wxDirData::Read(wxString *filename)
264{
265 bool first = FALSE;
266
8f177c8e 267#ifdef __WIN32__
1944c6bd 268 WIN32_FIND_DATA finddata;
8f177c8e
VZ
269 #define PTR_TO_FINDDATA (&finddata)
270#else // Win16
271 #define PTR_TO_FINDDATA (m_finddata)
272#endif
273
274 if ( !IsFindDataOk(m_finddata) )
1944c6bd
VZ
275 {
276 // open first
ad0dc53b
VZ
277 wxString filespec = m_dirname;
278 if ( !wxEndsWithPathSeparator(filespec) )
279 {
280 filespec += _T('\\');
281 }
282 filespec += (!m_filespec ? _T("*.*") : m_filespec.c_str());
4afd7529 283
8f177c8e 284 m_finddata = FindFirst(filespec, PTR_TO_FINDDATA);
1944c6bd
VZ
285
286 first = TRUE;
287 }
288
8f177c8e 289 if ( !IsFindDataOk(m_finddata) )
1944c6bd 290 {
8f177c8e 291#ifdef __WIN32__
1944c6bd
VZ
292 DWORD err = ::GetLastError();
293
4afd7529 294 if ( err != ERROR_FILE_NOT_FOUND )
1944c6bd
VZ
295 {
296 wxLogSysError(err, _("Can not enumerate files in directory '%s'"),
297 m_dirname.c_str());
298 }
8f177c8e 299#endif // __WIN32__
1944c6bd
VZ
300 //else: not an error, just no (such) files
301
302 return FALSE;
303 }
304
1944c6bd 305 const wxChar *name;
8f177c8e 306 FIND_ATTR attr;
1944c6bd 307
4afd7529 308 for ( ;; )
1944c6bd
VZ
309 {
310 if ( first )
311 {
312 first = FALSE;
313 }
314 else
315 {
8f177c8e 316 if ( !FindNext(m_finddata, PTR_TO_FINDDATA) )
1944c6bd 317 {
8f177c8e 318#ifdef __WIN32__
1944c6bd
VZ
319 DWORD err = ::GetLastError();
320
321 if ( err != ERROR_NO_MORE_FILES )
322 {
323 wxLogLastError(_T("FindNext"));
324 }
8f177c8e 325#endif // __WIN32__
1944c6bd
VZ
326 //else: not an error, just no more (such) files
327
328 return FALSE;
329 }
330 }
331
8f177c8e
VZ
332 name = GetNameFromFindData(PTR_TO_FINDDATA);
333 attr = GetAttrFromFindData(PTR_TO_FINDDATA);
1944c6bd
VZ
334
335 // don't return "." and ".." unless asked for
336 if ( name[0] == _T('.') &&
337 ((name[1] == _T('.') && name[2] == _T('\0')) ||
338 (name[1] == _T('\0'))) )
339 {
340 if ( !(m_flags & wxDIR_DOTDOT) )
341 continue;
342 }
343
344 // check the type now
8f177c8e 345 if ( !(m_flags & wxDIR_FILES) && !IsDir(attr) )
1944c6bd
VZ
346 {
347 // it's a file, but we don't want them
348 continue;
349 }
8f177c8e 350 else if ( !(m_flags & wxDIR_DIRS) && IsDir(attr) )
1944c6bd
VZ
351 {
352 // it's a dir, and we don't want it
353 continue;
354 }
355
356 // finally, check whether it's a hidden file
357 if ( !(m_flags & wxDIR_HIDDEN) )
358 {
8f177c8e 359 if ( IsHidden(attr) )
4afd7529
VZ
360 {
361 // it's a hidden file, skip it
362 continue;
363 }
1944c6bd 364 }
1944c6bd 365
4afd7529
VZ
366 *filename = name;
367
368 break;
369 }
1944c6bd
VZ
370
371 return TRUE;
372}
373
374// ----------------------------------------------------------------------------
375// wxDir helpers
376// ----------------------------------------------------------------------------
377
378/* static */
379bool wxDir::Exists(const wxString& dir)
380{
381 return wxPathExists(dir);
382}
383
384// ----------------------------------------------------------------------------
385// wxDir construction/destruction
386// ----------------------------------------------------------------------------
387
388wxDir::wxDir(const wxString& dirname)
389{
390 m_data = NULL;
391
392 (void)Open(dirname);
393}
394
395bool wxDir::Open(const wxString& dirname)
396{
397 delete M_DIR;
398 m_data = new wxDirData(dirname);
399
400 return TRUE;
401}
402
403bool wxDir::IsOpened() const
404{
405 return m_data != NULL;
406}
407
35332784
VZ
408wxString wxDir::GetName() const
409{
410 wxString name;
411 if ( m_data )
412 {
413 name = M_DIR->GetName();
414 if ( !name.empty() )
415 {
416 // bring to canonical Windows form
417 name.Replace(_T("/"), _T("\\"));
418
419 if ( name.Last() == _T('\\') )
420 {
421 // chop off the last (back)slash
422 name.Truncate(name.length() - 1);
423 }
424 }
425 }
426
427 return name;
428}
429
1944c6bd
VZ
430wxDir::~wxDir()
431{
432 delete M_DIR;
433}
434
435// ----------------------------------------------------------------------------
436// wxDir enumerating
437// ----------------------------------------------------------------------------
438
439bool wxDir::GetFirst(wxString *filename,
440 const wxString& filespec,
441 int flags) const
442{
443 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
444
445 M_DIR->Rewind();
446
447 M_DIR->SetFileSpec(filespec);
448 M_DIR->SetFlags(flags);
449
450 return GetNext(filename);
451}
452
453bool wxDir::GetNext(wxString *filename) const
454{
455 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
456
457 wxCHECK_MSG( filename, FALSE, _T("bad pointer in wxDir::GetNext()") );
458
459 return M_DIR->Read(filename);
460}