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
);
158 // expected error codes
160 if ( errFSNoMoreItems
== err
)
162 if ( afpAccessDenied
== err
)
168 name
= wxMacHFSUniStrToString( &uniname
) ;
170 if ( ( name
== wxT(".") || name
== wxT("..") ) && !(m_flags
& wxDIR_DOTDOT
) )
173 if ( ( name
[0U] == '.' ) && !(m_flags
& wxDIR_HIDDEN
) )
176 if ( (((FileInfo
*)&catalogInfo
.finderInfo
)->finderFlags
& kIsInvisible
) && !(m_flags
& wxDIR_HIDDEN
) )
179 // its a dir and we don't want it
180 if ( (catalogInfo
.nodeFlags
& kFSNodeIsDirectoryMask
) && !(m_flags
& wxDIR_DIRS
) )
183 // its a file but we don't want it
184 if ( (catalogInfo
.nodeFlags
& kFSNodeIsDirectoryMask
) == 0 && !(m_flags
& wxDIR_FILES
) )
187 if ( m_filespec
.empty() || m_filespec
== wxT("*.*") || m_filespec
== wxT("*") )
190 else if ( !wxMatchWild(m_filespec
, name
, false) )
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
211 bool wxDir::Exists(const wxString
& dir
)
213 return wxDirExists(dir
);
216 // ----------------------------------------------------------------------------
217 // wxDir construction/destruction
218 // ----------------------------------------------------------------------------
220 wxDir::wxDir(const wxString
& dirname
)
227 bool wxDir::Open(const wxString
& dirname
)
230 m_data
= new wxDirData(dirname
);
235 bool wxDir::IsOpened() const
237 return m_data
!= NULL
;
240 wxString
wxDir::GetName() const
245 name
= M_DIR
->GetName();
246 if ( !name
.empty() && (name
.Last() == _T('/')) )
248 // chop off the last (back)slash
249 name
.Truncate(name
.length() - 1);
264 // ----------------------------------------------------------------------------
266 // ----------------------------------------------------------------------------
268 bool wxDir::GetFirst(wxString
*filename
,
269 const wxString
& filespec
,
272 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
276 M_DIR
->SetFileSpec(filespec
);
277 M_DIR
->SetFlags(flags
);
279 return GetNext(filename
);
282 bool wxDir::GetNext(wxString
*filename
) const
284 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
286 wxCHECK_MSG( filename
, false, _T("bad pointer in wxDir::GetNext()") );
288 return M_DIR
->Read(filename
);