1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mac/dirmac.cpp
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"
34 #include "wx/filename.h"
35 #include "wx/osx/private.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // this class stores everything we need to enumerate the files
45 wxDirData(const wxString
& dirname
);
49 void SetFileSpec(const wxString
& filespec
) { m_filespec
= filespec
; }
50 void SetFlags(int flags
) { m_flags
= flags
; }
52 bool Read(wxString
*filename
); // reads the next
55 const wxString
& GetName() const { return m_dirname
; }
58 FSIterator m_iterator
;
66 // ============================================================================
68 // ============================================================================
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 wxDirData::wxDirData(const wxString
& dirname
)
77 // throw away the trailing slashes
78 size_t n
= m_dirname
.length();
79 wxCHECK_RET( n
, wxT("empty dir name in wxDir") );
81 while ( n
> 0 && wxIsPathSeparator(m_dirname
[--n
]) )
84 m_dirname
.Truncate(n
+ 1);
88 wxDirData::~wxDirData()
93 void wxDirData::Close()
97 FSCloseIterator( m_iterator
) ;
102 void wxDirData::Rewind()
107 bool wxDirData::Read(wxString
*filename
)
110 OSStatus err
= noErr
;
111 if ( NULL
== m_iterator
)
114 err
= wxMacPathToFSRef( m_dirname
, &dirRef
) ;
117 err
= FSOpenIterator(&dirRef
, kFSIterateFlat
, &m_iterator
);
127 wxString lowerfilespec
= m_filespec
.Lower();
129 while( noErr
== err
)
131 HFSUniStr255 uniname
;
133 FSCatalogInfo catalogInfo
;
134 ItemCount fetched
= 0;
136 err
= FSGetCatalogInfoBulk( m_iterator
, 1, &fetched
, NULL
, kFSCatInfoNodeFlags
| kFSCatInfoFinderInfo
, &catalogInfo
, &fileRef
, NULL
, &uniname
);
138 // expected error codes
140 if ( errFSNoMoreItems
== err
)
142 if ( afpAccessDenied
== err
)
148 name
= wxMacHFSUniStrToString( &uniname
) ;
149 wxString lowername
= name
.Lower();
151 if ( ( name
== wxT(".") || name
== wxT("..") ) && !(m_flags
& wxDIR_DOTDOT
) )
154 if ( ( name
[0U] == '.' ) && !(m_flags
& wxDIR_HIDDEN
) )
157 if ( (((FileInfo
*)&catalogInfo
.finderInfo
)->finderFlags
& kIsInvisible
) && !(m_flags
& wxDIR_HIDDEN
) )
160 // its a dir and we don't want it
161 if ( (catalogInfo
.nodeFlags
& kFSNodeIsDirectoryMask
) && !(m_flags
& wxDIR_DIRS
) )
164 // its a file but we don't want it
165 if ( (catalogInfo
.nodeFlags
& kFSNodeIsDirectoryMask
) == 0 && !(m_flags
& wxDIR_FILES
) )
168 if ( m_filespec
.empty() || m_filespec
== wxT("*.*") || m_filespec
== wxT("*") )
171 else if ( !wxMatchWild(lowerfilespec
, lowername
, false) )
187 // ----------------------------------------------------------------------------
188 // wxDir construction/destruction
189 // ----------------------------------------------------------------------------
191 wxDir::wxDir(const wxString
& dirname
)
198 bool wxDir::Open(const wxString
& dirname
)
201 m_data
= new wxDirData(dirname
);
206 bool wxDir::IsOpened() const
208 return m_data
!= NULL
;
211 wxString
wxDir::GetName() const
216 name
= m_data
->GetName();
217 if ( !name
.empty() && (name
.Last() == wxT('/')) )
219 // chop off the last (back)slash
220 name
.Truncate(name
.length() - 1);
232 // ----------------------------------------------------------------------------
234 // ----------------------------------------------------------------------------
236 bool wxDir::GetFirst(wxString
*filename
,
237 const wxString
& filespec
,
240 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
244 m_data
->SetFileSpec(filespec
);
245 m_data
->SetFlags(flags
);
247 return GetNext(filename
);
250 bool wxDir::GetNext(wxString
*filename
) const
252 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
254 wxCHECK_MSG( filename
, false, wxT("bad pointer in wxDir::GetNext()") );
256 return m_data
->Read(filename
);