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 // ----------------------------------------------------------------------------
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()
43 #include "wx/filename.h"
44 #include "wx/mac/private.h"
46 #include "MoreFilesX.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
53 #define MAX_PATH 260 // from VC++ headers
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 #define M_DIR ((wxDirData *)m_data)
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // this class stores everything we need to enumerate the files
70 wxDirData(const wxString
& dirname
);
74 void SetFileSpec(const wxString
& filespec
) { m_filespec
= filespec
; }
75 void SetFlags(int flags
) { m_flags
= flags
; }
77 bool Read(wxString
*filename
); // reads the next
80 const wxString
& GetName() const { return m_dirname
; }
83 FSIterator m_iterator
;
91 // ============================================================================
93 // ============================================================================
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 wxDirData::wxDirData(const wxString
& dirname
)
102 // throw away the trailing slashes
103 size_t n
= m_dirname
.length();
104 wxCHECK_RET( n
, _T("empty dir name in wxDir") );
106 while ( n
> 0 && wxIsPathSeparator(m_dirname
[--n
]) )
109 m_dirname
.Truncate(n
+ 1);
113 wxDirData::~wxDirData()
118 void wxDirData::Close()
122 FSCloseIterator( m_iterator
) ;
127 void wxDirData::Rewind()
132 bool wxDirData::Read(wxString
*filename
)
135 OSStatus err
= noErr
;
136 if ( NULL
== m_iterator
)
139 err
= wxMacPathToFSRef( m_dirname
, &dirRef
) ;
142 err
= FSOpenIterator(&dirRef
, kFSIterateFlat
, &m_iterator
);
153 while( noErr
== err
)
155 HFSUniStr255 uniname
;
157 FSCatalogInfo catalogInfo
;
160 err
= FSGetCatalogInfoBulk( m_iterator
, 1, &fetched
, NULL
, kFSCatInfoNodeFlags
| kFSCatInfoFinderInfo
, &catalogInfo
, &fileRef
, NULL
, &uniname
);
161 if ( errFSNoMoreItems
== err
)
164 wxASSERT( noErr
== err
) ;
169 name
= wxMacHFSUniStrToString( &uniname
) ;
171 if ( ( name
== wxT(".") || name
== wxT("..") ) && !(m_flags
& wxDIR_DOTDOT
) )
174 if ( ( name
[0U] == '.' ) && !(m_flags
& wxDIR_HIDDEN
) )
177 if ( (((FileInfo
*)&catalogInfo
.finderInfo
)->finderFlags
& kIsInvisible
) && !(m_flags
& wxDIR_HIDDEN
) )
180 // its a dir and we don't want it
181 if ( (catalogInfo
.nodeFlags
& kFSNodeIsDirectoryMask
) && !(m_flags
& wxDIR_DIRS
) )
184 // its a file but we don't want it
185 if ( (catalogInfo
.nodeFlags
& kFSNodeIsDirectoryMask
) == 0 && !(m_flags
& wxDIR_FILES
) )
188 if ( m_filespec
.IsEmpty() || m_filespec
== wxT("*.*") || m_filespec
== wxT("*") )
191 else if ( !wxMatchWild(m_filespec
, name
, FALSE
) )
207 // ----------------------------------------------------------------------------
209 // ----------------------------------------------------------------------------
212 bool wxDir::Exists(const wxString
& dir
)
214 return wxPathExists(dir
);
217 // ----------------------------------------------------------------------------
218 // wxDir construction/destruction
219 // ----------------------------------------------------------------------------
221 wxDir::wxDir(const wxString
& dirname
)
228 bool wxDir::Open(const wxString
& dirname
)
231 m_data
= new wxDirData(dirname
);
236 bool wxDir::IsOpened() const
238 return m_data
!= NULL
;
241 wxString
wxDir::GetName() const
246 name
= M_DIR
->GetName();
247 if ( !name
.empty() && (name
.Last() == _T('/')) )
249 // chop off the last (back)slash
250 name
.Truncate(name
.length() - 1);
265 // ----------------------------------------------------------------------------
267 // ----------------------------------------------------------------------------
269 bool wxDir::GetFirst(wxString
*filename
,
270 const wxString
& filespec
,
273 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
277 M_DIR
->SetFileSpec(filespec
);
278 M_DIR
->SetFlags(flags
);
280 return GetNext(filename
);
283 bool wxDir::GetNext(wxString
*filename
) const
285 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
287 wxCHECK_MSG( filename
, FALSE
, _T("bad pointer in wxDir::GetNext()") );
289 return M_DIR
->Read(filename
);