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