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"
37 #include "wx/filefn.h" // for wxPathExists()
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
46 #define MAX_PATH 260 // from VC++ headers
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 #define M_DIR ((wxDirData *)m_data)
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // this class stores everything we need to enumerate the files
63 wxDirData(const wxString
& dirname
);
66 void SetFileSpec(const wxString
& filespec
) { m_filespec
= filespec
; }
67 void SetFlags(int flags
) { m_flags
= flags
; }
71 bool Read(wxString
*filename
);
82 // ============================================================================
84 // ============================================================================
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 wxDirData
::wxDirData(const wxString
& dirname
)
93 m_handle
= INVALID_HANDLE_VALUE
;
95 // throw away the trailing slashes
96 size_t n
= m_dirname
.length();
97 wxCHECK_RET( n
, _T("empty dir name in wxDir") );
99 while ( n
> 0 && wxIsPathSeparator(m_dirname
[--n
]) )
102 m_dirname
.Truncate(n
+ 1);
105 wxDirData
::~wxDirData()
110 void wxDirData
::Close()
112 if ( m_handle
!= INVALID_HANDLE_VALUE
)
114 if ( !::FindClose(m_handle
) )
116 wxLogLastError(_T("FindClose"));
121 void wxDirData
::Rewind()
126 bool wxDirData
::Read(wxString
*filename
)
130 WIN32_FIND_DATA finddata
;
131 if ( m_handle
== INVALID_HANDLE_VALUE
)
134 m_handle
= ::FindFirstFile(!m_filespec ?
_T("*.*") : m_filespec
.c_str(),
140 if ( m_handle
== INVALID_HANDLE_VALUE
)
142 DWORD err
= ::GetLastError();
144 if ( err
!= ERROR_NO_MORE_FILES
)
146 wxLogSysError(err
, _("Can not enumerate files in directory '%s'"),
149 //else: not an error, just no (such) files
154 bool matches
= FALSE
;
167 if ( !::FindNextFile(m_handle
, &finddata
) )
169 DWORD err
= ::GetLastError();
171 if ( err
!= ERROR_NO_MORE_FILES
)
173 wxLogLastError(_T("FindNext"));
175 //else: not an error, just no more (such) files
181 name
= finddata
.cFileName
;
182 attr
= finddata
.dwFileAttributes
;
184 // don't return "." and ".." unless asked for
185 if ( name
[0] == _T('.') &&
186 ((name
[1] == _T('.') && name
[2] == _T('\0')) ||
187 (name
[1] == _T('\0'))) )
189 if ( !(m_flags
& wxDIR_DOTDOT
) )
193 // check the type now
194 if ( !(m_flags
& wxDIR_FILES
) && !(attr
& FILE_ATTRIBUTE_DIRECTORY
) )
196 // it's a file, but we don't want them
199 else if ( !(m_flags
& wxDIR_DIRS
) && (attr
& FILE_ATTRIBUTE_DIRECTORY
) )
201 // it's a dir, and we don't want it
205 // finally, check whether it's a hidden file
206 if ( !(m_flags
& wxDIR_HIDDEN
) )
208 matches
= !(attr
& (FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
));
217 // ----------------------------------------------------------------------------
219 // ----------------------------------------------------------------------------
222 bool wxDir
::Exists(const wxString
& dir
)
224 return wxPathExists(dir
);
227 // ----------------------------------------------------------------------------
228 // wxDir construction/destruction
229 // ----------------------------------------------------------------------------
231 wxDir
::wxDir(const wxString
& dirname
)
238 bool wxDir
::Open(const wxString
& dirname
)
241 m_data
= new wxDirData(dirname
);
246 bool wxDir
::IsOpened() const
248 return m_data
!= NULL
;
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
260 bool wxDir
::GetFirst(wxString
*filename
,
261 const wxString
& filespec
,
264 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
268 M_DIR
->SetFileSpec(filespec
);
269 M_DIR
->SetFlags(flags
);
271 return GetNext(filename
);
274 bool wxDir
::GetNext(wxString
*filename
) const
276 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
278 wxCHECK_MSG( filename
, FALSE
, _T("bad pointer in wxDir::GetNext()") );
280 return M_DIR
->Read(filename
);