1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDir implementation for Mac
4 // Author: Stefan Csomor
8 // Copyright: (c) 1999 Stefan Csomor <csomor@advanced.ch>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
33 #include "wx/filefn.h" // for wxDirExists()
39 #include "wx/filename.h"
40 #include "wx/mac/private.h"
42 #include "MoreFilesX.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
49 #define MAX_PATH 260 // from VC++ headers
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 #define M_DIR ((wxDirData *)m_data)
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // this class stores everything we need to enumerate the files
66 wxDirData(const wxString
& dirname
);
70 void SetFileSpec(const wxString
& filespec
) { m_filespec
= filespec
; }
71 void SetFlags(int flags
) { m_flags
= flags
; }
73 bool Read(wxString
*filename
); // reads the next
76 const wxString
& GetName() const { return m_dirname
; }
79 FSIterator m_iterator
;
87 // ============================================================================
89 // ============================================================================
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 wxDirData::wxDirData(const wxString
& dirname
)
98 // throw away the trailing slashes
99 size_t n
= m_dirname
.length();
100 wxCHECK_RET( n
, _T("empty dir name in wxDir") );
102 while ( n
> 0 && wxIsPathSeparator(m_dirname
[--n
]) )
105 m_dirname
.Truncate(n
+ 1);
109 wxDirData::~wxDirData()
114 void wxDirData::Close()
118 FSCloseIterator( m_iterator
) ;
123 void wxDirData::Rewind()
128 bool wxDirData::Read(wxString
*filename
)
131 OSStatus err
= noErr
;
132 if ( NULL
== m_iterator
)
135 err
= wxMacPathToFSRef( m_dirname
, &dirRef
) ;
138 err
= FSOpenIterator(&dirRef
, kFSIterateFlat
, &m_iterator
);
149 while( noErr
== err
)
151 HFSUniStr255 uniname
;
153 FSCatalogInfo catalogInfo
;
156 err
= FSGetCatalogInfoBulk( m_iterator
, 1, &fetched
, NULL
, kFSCatInfoNodeFlags
| kFSCatInfoFinderInfo
, &catalogInfo
, &fileRef
, NULL
, &uniname
);
157 if ( errFSNoMoreItems
== err
)
160 wxASSERT( noErr
== err
) ;
165 name
= wxMacHFSUniStrToString( &uniname
) ;
167 if ( ( name
== wxT(".") || name
== wxT("..") ) && !(m_flags
& wxDIR_DOTDOT
) )
170 if ( ( name
[0U] == '.' ) && !(m_flags
& wxDIR_HIDDEN
) )
173 if ( (((FileInfo
*)&catalogInfo
.finderInfo
)->finderFlags
& kIsInvisible
) && !(m_flags
& wxDIR_HIDDEN
) )
176 // its a dir and we don't want it
177 if ( (catalogInfo
.nodeFlags
& kFSNodeIsDirectoryMask
) && !(m_flags
& wxDIR_DIRS
) )
180 // its a file but we don't want it
181 if ( (catalogInfo
.nodeFlags
& kFSNodeIsDirectoryMask
) == 0 && !(m_flags
& wxDIR_FILES
) )
184 if ( m_filespec
.empty() || m_filespec
== wxT("*.*") || m_filespec
== wxT("*") )
187 else if ( !wxMatchWild(m_filespec
, name
, false) )
203 // ----------------------------------------------------------------------------
205 // ----------------------------------------------------------------------------
208 bool wxDir::Exists(const wxString
& dir
)
210 return wxDirExists(dir
);
213 // ----------------------------------------------------------------------------
214 // wxDir construction/destruction
215 // ----------------------------------------------------------------------------
217 wxDir::wxDir(const wxString
& dirname
)
224 bool wxDir::Open(const wxString
& dirname
)
227 m_data
= new wxDirData(dirname
);
232 bool wxDir::IsOpened() const
234 return m_data
!= NULL
;
237 wxString
wxDir::GetName() const
242 name
= M_DIR
->GetName();
243 if ( !name
.empty() && (name
.Last() == _T('/')) )
245 // chop off the last (back)slash
246 name
.Truncate(name
.length() - 1);
261 // ----------------------------------------------------------------------------
263 // ----------------------------------------------------------------------------
265 bool wxDir::GetFirst(wxString
*filename
,
266 const wxString
& filespec
,
269 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
273 M_DIR
->SetFileSpec(filespec
);
274 M_DIR
->SetFlags(flags
);
276 return GetNext(filename
);
279 bool wxDir::GetNext(wxString
*filename
) const
281 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
283 wxCHECK_MSG( filename
, false, _T("bad pointer in wxDir::GetNext()") );
285 return M_DIR
->Read(filename
);