1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/dirmac.cpp
3 // Purpose: wxDir implementation for Mac
4 // Author: Stefan Csomor
7 // Copyright: (c) 1999 Stefan Csomor <csomor@advanced.ch>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
33 #include "wx/filename.h"
34 #include "wx/osx/private.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // this class stores everything we need to enumerate the files
44 wxDirData(const wxString
& dirname
);
48 void SetFileSpec(const wxString
& filespec
) { m_filespec
= filespec
; }
49 void SetFlags(int flags
) { m_flags
= flags
; }
51 bool Read(wxString
*filename
); // reads the next
54 const wxString
& GetName() const { return m_dirname
; }
57 FSIterator m_iterator
;
65 // ============================================================================
67 // ============================================================================
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 wxDirData::wxDirData(const wxString
& dirname
)
76 // throw away the trailing slashes
77 size_t n
= m_dirname
.length();
78 wxCHECK_RET( n
, wxT("empty dir name in wxDir") );
80 while ( n
> 0 && wxIsPathSeparator(m_dirname
[--n
]) )
83 m_dirname
.Truncate(n
+ 1);
87 wxDirData::~wxDirData()
92 void wxDirData::Close()
96 FSCloseIterator( m_iterator
) ;
101 void wxDirData::Rewind()
106 bool wxDirData::Read(wxString
*filename
)
109 OSStatus err
= noErr
;
110 if ( NULL
== m_iterator
)
113 err
= wxMacPathToFSRef( m_dirname
, &dirRef
) ;
116 err
= FSOpenIterator(&dirRef
, kFSIterateFlat
, &m_iterator
);
126 wxString lowerfilespec
= m_filespec
.Lower();
128 while( noErr
== err
)
130 HFSUniStr255 uniname
;
132 FSCatalogInfo catalogInfo
;
133 ItemCount fetched
= 0;
135 err
= FSGetCatalogInfoBulk( m_iterator
, 1, &fetched
, NULL
, kFSCatInfoNodeFlags
| kFSCatInfoFinderInfo
, &catalogInfo
, &fileRef
, NULL
, &uniname
);
137 // expected error codes
139 if ( errFSNoMoreItems
== err
)
141 if ( afpAccessDenied
== err
)
147 name
= wxMacHFSUniStrToString( &uniname
) ;
148 wxString lowername
= name
.Lower();
150 if ( ( name
== wxT(".") || name
== wxT("..") ) && !(m_flags
& wxDIR_DOTDOT
) )
153 if ( ( name
[0U] == '.' ) && !(m_flags
& wxDIR_HIDDEN
) )
156 if ( (((FileInfo
*)&catalogInfo
.finderInfo
)->finderFlags
& kIsInvisible
) && !(m_flags
& wxDIR_HIDDEN
) )
159 // its a dir and we don't want it
160 if ( (catalogInfo
.nodeFlags
& kFSNodeIsDirectoryMask
) && !(m_flags
& wxDIR_DIRS
) )
163 // its a file but we don't want it
164 if ( (catalogInfo
.nodeFlags
& kFSNodeIsDirectoryMask
) == 0 && !(m_flags
& wxDIR_FILES
) )
167 if ( m_filespec
.empty() || m_filespec
== wxT("*.*") || m_filespec
== wxT("*") )
170 else if ( !wxMatchWild(lowerfilespec
, lowername
, false) )
186 // ----------------------------------------------------------------------------
187 // wxDir construction/destruction
188 // ----------------------------------------------------------------------------
190 wxDir::wxDir(const wxString
& dirname
)
197 bool wxDir::Open(const wxString
& dirname
)
200 m_data
= new wxDirData(dirname
);
205 bool wxDir::IsOpened() const
207 return m_data
!= NULL
;
210 wxString
wxDir::GetName() const
215 name
= m_data
->GetName();
216 if ( !name
.empty() && (name
.Last() == wxT('/')) )
218 // chop off the last (back)slash
219 name
.Truncate(name
.length() - 1);
231 // ----------------------------------------------------------------------------
233 // ----------------------------------------------------------------------------
235 bool wxDir::GetFirst(wxString
*filename
,
236 const wxString
& filespec
,
239 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
243 m_data
->SetFileSpec(filespec
);
244 m_data
->SetFlags(flags
);
246 return GetNext(filename
);
249 bool wxDir::GetNext(wxString
*filename
) const
251 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
253 wxCHECK_MSG( filename
, false, wxT("bad pointer in wxDir::GetNext()") );
255 return m_data
->Read(filename
);