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/filefn.h"          // for wxDirExists() 
  35 #include "wx/filename.h" 
  36 #include "wx/osx/private.h" 
  38 // ---------------------------------------------------------------------------- 
  40 // ---------------------------------------------------------------------------- 
  42 // this class stores everything we need to enumerate the files 
  46     wxDirData(const wxString
& dirname
); 
  50     void SetFileSpec(const wxString
& filespec
) { m_filespec 
= filespec
; } 
  51     void SetFlags(int flags
) { m_flags 
= flags
; } 
  53     bool Read(wxString 
*filename
); // reads the next 
  56     const wxString
& GetName() const { return m_dirname
; } 
  59     FSIterator              m_iterator 
; 
  67 // ============================================================================ 
  69 // ============================================================================ 
  71 // ---------------------------------------------------------------------------- 
  73 // ---------------------------------------------------------------------------- 
  75 wxDirData::wxDirData(const wxString
& dirname
) 
  78     // throw away the trailing slashes 
  79     size_t n 
= m_dirname
.length(); 
  80     wxCHECK_RET( n
, _T("empty dir name in wxDir") ); 
  82     while ( n 
> 0 && wxIsPathSeparator(m_dirname
[--n
]) ) 
  85     m_dirname
.Truncate(n 
+ 1); 
  89 wxDirData::~wxDirData() 
  94 void wxDirData::Close() 
  98         FSCloseIterator( m_iterator 
) ; 
 103 void wxDirData::Rewind() 
 108 bool wxDirData::Read(wxString 
*filename
) 
 111     OSStatus err 
= noErr 
; 
 112     if ( NULL 
== m_iterator 
) 
 115         err 
= wxMacPathToFSRef( m_dirname 
, &dirRef 
) ; 
 118             err 
= FSOpenIterator(&dirRef
, kFSIterateFlat
, &m_iterator
); 
 128     wxString lowerfilespec 
= m_filespec
.Lower(); 
 130     while( noErr 
== err 
) 
 132         HFSUniStr255 uniname 
; 
 134         FSCatalogInfo catalogInfo
; 
 135         ItemCount fetched 
= 0; 
 137         err 
= FSGetCatalogInfoBulk( m_iterator
, 1, &fetched
, NULL
, kFSCatInfoNodeFlags 
| kFSCatInfoFinderInfo 
, &catalogInfo 
, &fileRef
, NULL
, &uniname 
); 
 139         // expected error codes  
 141         if ( errFSNoMoreItems 
== err 
) 
 143         if ( afpAccessDenied 
== err 
) 
 149         name 
= wxMacHFSUniStrToString( &uniname 
) ; 
 150         wxString lowername 
= name
.Lower(); 
 152         if ( ( name 
== wxT(".") || name 
== wxT("..") ) && !(m_flags 
& wxDIR_DOTDOT
) ) 
 155         if ( ( name
[0U] == '.' ) && !(m_flags 
& wxDIR_HIDDEN 
) ) 
 158         if ( (((FileInfo
*)&catalogInfo
.finderInfo
)->finderFlags 
& kIsInvisible 
) && !(m_flags 
& wxDIR_HIDDEN 
) ) 
 161         // its a dir and we don't want it 
 162         if ( (catalogInfo
.nodeFlags 
& kFSNodeIsDirectoryMask
)  && !(m_flags 
& wxDIR_DIRS
) ) 
 165         // its a file but we don't want it 
 166         if ( (catalogInfo
.nodeFlags 
& kFSNodeIsDirectoryMask
) == 0  && !(m_flags 
& wxDIR_FILES 
) ) 
 169         if ( m_filespec
.empty() || m_filespec 
== wxT("*.*") || m_filespec 
== wxT("*") ) 
 172         else if ( !wxMatchWild(lowerfilespec
, lowername 
, false) ) 
 188 // ---------------------------------------------------------------------------- 
 190 // ---------------------------------------------------------------------------- 
 193 bool wxDir::Exists(const wxString
& dir
) 
 195     return wxDirExists(dir
); 
 198 // ---------------------------------------------------------------------------- 
 199 // wxDir construction/destruction 
 200 // ---------------------------------------------------------------------------- 
 202 wxDir::wxDir(const wxString
& dirname
) 
 209 bool wxDir::Open(const wxString
& dirname
) 
 212     m_data 
= new wxDirData(dirname
); 
 217 bool wxDir::IsOpened() const 
 219     return m_data 
!= NULL
; 
 222 wxString 
wxDir::GetName() const 
 227         name 
= m_data
->GetName(); 
 228         if ( !name
.empty() && (name
.Last() == _T('/')) ) 
 230             // chop off the last (back)slash 
 231             name
.Truncate(name
.length() - 1); 
 244 // ---------------------------------------------------------------------------- 
 246 // ---------------------------------------------------------------------------- 
 248 bool wxDir::GetFirst(wxString 
*filename
, 
 249                      const wxString
& filespec
, 
 252     wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") ); 
 256     m_data
->SetFileSpec(filespec
); 
 257     m_data
->SetFlags(flags
); 
 259     return GetNext(filename
); 
 262 bool wxDir::GetNext(wxString 
*filename
) const 
 264     wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") ); 
 266     wxCHECK_MSG( filename
, false, _T("bad pointer in wxDir::GetNext()") ); 
 268     return m_data
->Read(filename
);