]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/dir.cpp
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
;
96 wxDirData::~wxDirData()
101 void wxDirData::Close()
103 if ( m_handle
!= INVALID_HANDLE_VALUE
)
105 if ( !::FindClose(m_handle
) )
107 wxLogLastError(_T("FindClose"));
110 m_handle
= INVALID_HANDLE_VALUE
;
114 void wxDirData::Rewind()
119 bool wxDirData::Read(wxString
*filename
)
123 WIN32_FIND_DATA finddata
;
124 if ( m_handle
== INVALID_HANDLE_VALUE
)
128 filespec
<< m_dirname
<< _T('\\')
129 << (!m_filespec
? _T("*.*") : m_filespec
.c_str());
131 m_handle
= ::FindFirstFile(filespec
, &finddata
);
136 if ( m_handle
== INVALID_HANDLE_VALUE
)
138 DWORD err
= ::GetLastError();
140 if ( err
!= ERROR_FILE_NOT_FOUND
)
142 wxLogSysError(err
, _("Can not enumerate files in directory '%s'"),
145 //else: not an error, just no (such) files
161 if ( !::FindNextFile(m_handle
, &finddata
) )
163 DWORD err
= ::GetLastError();
165 if ( err
!= ERROR_NO_MORE_FILES
)
167 wxLogLastError(_T("FindNext"));
169 //else: not an error, just no more (such) files
175 name
= finddata
.cFileName
;
176 attr
= finddata
.dwFileAttributes
;
178 // don't return "." and ".." unless asked for
179 if ( name
[0] == _T('.') &&
180 ((name
[1] == _T('.') && name
[2] == _T('\0')) ||
181 (name
[1] == _T('\0'))) )
183 if ( !(m_flags
& wxDIR_DOTDOT
) )
187 // check the type now
188 if ( !(m_flags
& wxDIR_FILES
) && !(attr
& FILE_ATTRIBUTE_DIRECTORY
) )
190 // it's a file, but we don't want them
193 else if ( !(m_flags
& wxDIR_DIRS
) && (attr
& FILE_ATTRIBUTE_DIRECTORY
) )
195 // it's a dir, and we don't want it
199 // finally, check whether it's a hidden file
200 if ( !(m_flags
& wxDIR_HIDDEN
) )
202 if ( attr
& (FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_SYSTEM
) )
204 // it's a hidden file, skip it
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
);