1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDir implementation for Win32
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "dir.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 // For _A_SUBDIR, etc.
32 #if defined(__BORLANDC__) && defined(__WIN16__)
42 #include "wx/filefn.h" // for wxPathExists()
44 // ----------------------------------------------------------------------------
45 // define the types and functions used for file searching
46 // ----------------------------------------------------------------------------
48 // under Win16 use compiler-specific functions
54 typedef struct _find_t FIND_STRUCT
;
55 #elif defined(__BORLANDC__)
58 typedef struct ffblk FIND_STRUCT
;
60 #error "No directory searching functions for this compiler"
63 typedef FIND_STRUCT
*FIND_DATA
;
64 typedef char FIND_ATTR
;
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
); }
70 static inline FIND_DATA
FindFirst(const wxString
& spec
,
71 FIND_STRUCT
* WXUNUSED(finddata
))
73 // attribute to find all files
74 static const FIND_ATTR attr
= 0x3F;
76 FIND_DATA fd
= (FIND_DATA
)malloc(sizeof(FIND_STRUCT
));
80 _dos_findfirst(spec
, attr
, fd
) == 0
82 findfirst(spec
, fd
, attr
) == 0
96 static inline bool FindNext(FIND_DATA fd
, FIND_STRUCT
* WXUNUSED(finddata
))
99 return _dos_findnext(fd
) == 0;
101 return findnext(fd
) == 0;
105 static const wxChar
*GetNameFromFindData(FIND_STRUCT
*finddata
)
108 return finddata
->name
;
110 return finddata
->ff_name
;
114 static const FIND_ATTR
GetAttrFromFindData(FIND_STRUCT
*finddata
)
117 return finddata
->attrib
;
119 return finddata
->ff_attrib
;
123 static inline bool IsDir(FIND_ATTR attr
)
125 return (attr
& _A_SUBDIR
) != 0;
128 static inline bool IsHidden(FIND_ATTR attr
)
130 return (attr
& (_A_SYSTEM
| _A_HIDDEN
)) != 0;
135 typedef WIN32_FIND_DATA FIND_STRUCT
;
136 typedef HANDLE FIND_DATA
;
137 typedef DWORD FIND_ATTR
;
139 static inline FIND_DATA
InitFindData() { return INVALID_HANDLE_VALUE
; }
141 static inline bool IsFindDataOk(FIND_DATA fd
)
143 return fd
!= INVALID_HANDLE_VALUE
;
146 static inline void FreeFindData(FIND_DATA fd
)
148 if ( !::FindClose(fd
) )
150 wxLogLastError(_T("FindClose"));
154 static inline FIND_DATA
FindFirst(const wxString
& spec
,
155 FIND_STRUCT
*finddata
)
157 return ::FindFirstFile(spec
, finddata
);
160 static inline bool FindNext(FIND_DATA fd
, FIND_STRUCT
*finddata
)
162 return ::FindNextFile(fd
, finddata
) != 0;
165 static const wxChar
*GetNameFromFindData(FIND_STRUCT
*finddata
)
167 return finddata
->cFileName
;
170 static const FIND_ATTR
GetAttrFromFindData(FIND_STRUCT
*finddata
)
172 return finddata
->dwFileAttributes
;
175 static inline bool IsDir(FIND_ATTR attr
)
177 return (attr
& FILE_ATTRIBUTE_DIRECTORY
) != 0;
180 static inline bool IsHidden(FIND_ATTR attr
)
182 return (attr
& (FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
)) != 0;
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
191 #define MAX_PATH 260 // from VC++ headers
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
198 #define M_DIR ((wxDirData *)m_data)
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
204 // this class stores everything we need to enumerate the files
208 wxDirData(const wxString
& dirname
);
211 void SetFileSpec(const wxString
& filespec
) { m_filespec
= filespec
; }
212 void SetFlags(int flags
) { m_flags
= flags
; }
216 bool Read(wxString
*filename
);
218 const wxString
& GetName() const { return m_dirname
; }
221 FIND_DATA m_finddata
;
228 DECLARE_NO_COPY_CLASS(wxDirData
)
231 // ============================================================================
233 // ============================================================================
235 // ----------------------------------------------------------------------------
237 // ----------------------------------------------------------------------------
239 wxDirData::wxDirData(const wxString
& dirname
)
242 m_finddata
= InitFindData();
245 wxDirData::~wxDirData()
250 void wxDirData::Close()
252 if ( IsFindDataOk(m_finddata
) )
254 FreeFindData(m_finddata
);
256 m_finddata
= InitFindData();
260 void wxDirData::Rewind()
265 bool wxDirData::Read(wxString
*filename
)
270 WIN32_FIND_DATA finddata
;
271 #define PTR_TO_FINDDATA (&finddata)
273 #define PTR_TO_FINDDATA (m_finddata)
276 if ( !IsFindDataOk(m_finddata
) )
279 wxString filespec
= m_dirname
;
280 if ( !wxEndsWithPathSeparator(filespec
) )
282 filespec
+= _T('\\');
284 filespec
+= (!m_filespec
? _T("*.*") : m_filespec
.c_str());
286 m_finddata
= FindFirst(filespec
, PTR_TO_FINDDATA
);
291 if ( !IsFindDataOk(m_finddata
) )
294 DWORD err
= ::GetLastError();
296 if ( err
!= ERROR_FILE_NOT_FOUND
)
298 wxLogSysError(err
, _("Can not enumerate files in directory '%s'"),
302 //else: not an error, just no (such) files
318 if ( !FindNext(m_finddata
, PTR_TO_FINDDATA
) )
321 DWORD err
= ::GetLastError();
323 if ( err
!= ERROR_NO_MORE_FILES
)
325 wxLogLastError(_T("FindNext"));
328 //else: not an error, just no more (such) files
334 name
= GetNameFromFindData(PTR_TO_FINDDATA
);
335 attr
= GetAttrFromFindData(PTR_TO_FINDDATA
);
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'))) )
342 if ( !(m_flags
& wxDIR_DOTDOT
) )
346 // check the type now
347 if ( !(m_flags
& wxDIR_FILES
) && !IsDir(attr
) )
349 // it's a file, but we don't want them
352 else if ( !(m_flags
& wxDIR_DIRS
) && IsDir(attr
) )
354 // it's a dir, and we don't want it
358 // finally, check whether it's a hidden file
359 if ( !(m_flags
& wxDIR_HIDDEN
) )
361 if ( IsHidden(attr
) )
363 // it's a hidden file, skip it
376 // ----------------------------------------------------------------------------
378 // ----------------------------------------------------------------------------
381 bool wxDir::Exists(const wxString
& dir
)
383 return wxPathExists(dir
);
386 // ----------------------------------------------------------------------------
387 // wxDir construction/destruction
388 // ----------------------------------------------------------------------------
390 wxDir::wxDir(const wxString
& dirname
)
397 bool wxDir::Open(const wxString
& dirname
)
400 m_data
= new wxDirData(dirname
);
405 bool wxDir::IsOpened() const
407 return m_data
!= NULL
;
410 wxString
wxDir::GetName() const
415 name
= M_DIR
->GetName();
418 // bring to canonical Windows form
419 name
.Replace(_T("/"), _T("\\"));
421 if ( name
.Last() == _T('\\') )
423 // chop off the last (back)slash
424 name
.Truncate(name
.length() - 1);
437 // ----------------------------------------------------------------------------
439 // ----------------------------------------------------------------------------
441 bool wxDir::GetFirst(wxString
*filename
,
442 const wxString
& filespec
,
445 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
449 M_DIR
->SetFileSpec(filespec
);
450 M_DIR
->SetFlags(flags
);
452 return GetNext(filename
);
455 bool wxDir::GetNext(wxString
*filename
) const
457 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
459 wxCHECK_MSG( filename
, FALSE
, _T("bad pointer in wxDir::GetNext()") );
461 return M_DIR
->Read(filename
);
464 // ----------------------------------------------------------------------------
465 // wxGetDirectoryTimes: used by wxFileName::GetTimes()
466 // ----------------------------------------------------------------------------
471 wxGetDirectoryTimes(const wxString
& dirname
,
472 FILETIME
*ftAccess
, FILETIME
*ftCreate
, FILETIME
*ftMod
)
474 // FindFirst() is going to fail
475 wxASSERT_MSG( !dirname
.empty() && dirname
.Last() != _T('\\'),
476 _T("incorrect directory name format in wxGetDirectoryTimes") );
479 FIND_DATA fd
= FindFirst(dirname
, &fs
);
480 if ( !IsFindDataOk(fd
) )
485 *ftAccess
= fs
.ftLastAccessTime
;
486 *ftCreate
= fs
.ftCreationTime
;
487 *ftMod
= fs
.ftLastWriteTime
;