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/mac/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
); 
 129     while( noErr 
== err 
) 
 131         HFSUniStr255 uniname 
; 
 133         FSCatalogInfo catalogInfo
; 
 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 
) ; 
 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(m_filespec
, name 
, false) ) 
 186 // ---------------------------------------------------------------------------- 
 188 // ---------------------------------------------------------------------------- 
 191 bool wxDir::Exists(const wxString
& dir
) 
 193     return wxDirExists(dir
); 
 196 // ---------------------------------------------------------------------------- 
 197 // wxDir construction/destruction 
 198 // ---------------------------------------------------------------------------- 
 200 wxDir::wxDir(const wxString
& dirname
) 
 207 bool wxDir::Open(const wxString
& dirname
) 
 210     m_data 
= new wxDirData(dirname
); 
 215 bool wxDir::IsOpened() const 
 217     return m_data 
!= NULL
; 
 220 wxString 
wxDir::GetName() const 
 225         name 
= m_data
->GetName(); 
 226         if ( !name
.empty() && (name
.Last() == _T('/')) ) 
 228             // chop off the last (back)slash 
 229             name
.Truncate(name
.length() - 1); 
 242 // ---------------------------------------------------------------------------- 
 244 // ---------------------------------------------------------------------------- 
 246 bool wxDir::GetFirst(wxString 
*filename
, 
 247                      const wxString
& filespec
, 
 250     wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") ); 
 254     m_data
->SetFileSpec(filespec
); 
 255     m_data
->SetFlags(flags
); 
 257     return GetNext(filename
); 
 260 bool wxDir::GetNext(wxString 
*filename
) const 
 262     wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") ); 
 264     wxCHECK_MSG( filename
, false, _T("bad pointer in wxDir::GetNext()") ); 
 266     return m_data
->Read(filename
);