X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/5fde6fcc9b551340a194ae4c726db5ab64b5c594..162d3be3ca27d875ca81f5466b12bc0e399896c3:/src/mac/carbon/dirmac.cpp diff --git a/src/mac/carbon/dirmac.cpp b/src/mac/carbon/dirmac.cpp index e37f079382..8669577a49 100644 --- a/src/mac/carbon/dirmac.cpp +++ b/src/mac/carbon/dirmac.cpp @@ -6,7 +6,7 @@ // Created: 08.12.99 // RCS-ID: $Id$ // Copyright: (c) 1999 Stefan Csomor -// Licence: wxWindows license +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// // ============================================================================ @@ -17,10 +17,6 @@ // headers // ---------------------------------------------------------------------------- -#ifdef __GNUG__ - #pragma implementation "dir.h" -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -34,18 +30,16 @@ #endif // PCH #include "wx/dir.h" -#include "wx/filefn.h" // for wxPathExists() +#include "wx/filefn.h" // for wxDirExists() -#ifndef __WXMAC_X__ +#ifndef __DARWIN__ #include #endif -#if defined(__WXMAC__) && !defined(__UNIX__) - #include "morefile.h" - #include "moreextr.h" - #include "fullpath.h" - #include "fspcompa.h" -#endif +#include "wx/filename.h" +#include "wx/mac/private.h" + +#include "MoreFilesX.h" // ---------------------------------------------------------------------------- // constants @@ -72,18 +66,17 @@ public: wxDirData(const wxString& dirname); ~wxDirData(); + void Close() ; void SetFileSpec(const wxString& filespec) { m_filespec = filespec; } void SetFlags(int flags) { m_flags = flags; } - bool Read(wxString *filename); // reads the next + bool Read(wxString *filename); // reads the next void Rewind() ; + const wxString& GetName() const { return m_dirname; } + private: - CInfoPBRec m_CPB ; - wxInt16 m_index ; - long m_dirId ; - Str255 m_name ; - Boolean m_isDir ; + FSIterator m_iterator ; wxString m_dirname; wxString m_filespec; @@ -110,99 +103,104 @@ wxDirData::wxDirData(const wxString& dirname) ; m_dirname.Truncate(n + 1); - - FSSpec fsspec ; - - wxUnixFilename2FSSpec( m_dirname , &fsspec ) ; - m_CPB.hFileInfo.ioVRefNum = fsspec.vRefNum ; - m_CPB.hFileInfo.ioNamePtr = m_name ; - m_index = 0 ; - -#ifdef __WXMAC_X__ - // TODO: what are we supposed to do for Mac OS X -#else - FSpGetDirectoryID( &fsspec , &m_dirId , &m_isDir ) ; -#endif + m_iterator = NULL ; } wxDirData::~wxDirData() { + Close() ; } -void wxDirData::Rewind() +void wxDirData::Close() { - m_index = 0 ; + if ( m_iterator ) + { + FSCloseIterator( m_iterator ) ; + m_iterator = NULL ; + } +} + +void wxDirData::Rewind() +{ + Close() ; } bool wxDirData::Read(wxString *filename) { - if ( !m_isDir ) - return FALSE ; - -#if TARGET_CARBON - char c_name[256] ; -#endif wxString result; - - short err = noErr ; - - while ( err == noErr ) - { - m_index++ ; - m_CPB.dirInfo.ioFDirIndex = m_index; - m_CPB.dirInfo.ioDrDirID = m_dirId; /* we need to do this every time */ - err = PBGetCatInfoSync((CInfoPBPtr)&m_CPB); - if ( err != noErr ) - break ; - -#if TARGET_CARBON - p2cstrcpy( c_name, m_name ) ; - strcpy( (char *)m_name, c_name); -#else - p2cstr( m_name ) ; -#endif - if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) != 0 && (m_flags & wxDIR_DIRS) ) // we have a directory - break ; - - if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) == 0 && !(m_flags & wxDIR_FILES ) ) // its a file but we don't want it - continue ; - - if ( ( m_CPB.hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible ) && !(m_flags & wxDIR_HIDDEN) ) // its hidden but we don't want it - continue ; - - wxString file( m_name ) ; - if ( m_filespec.IsEmpty() || m_filespec == "*.*" ) - { - } - else if ( m_filespec.Length() > 1 && m_filespec.Left(1) =="*" ) - { - if ( file.Right( m_filespec.Length() - 1 ).Upper() != m_filespec.Mid(1).Upper() ) - { - continue ; - } - } - else if ( m_filespec.Length() > 1 && m_filespec.Right(1) == "*" ) - { - if ( file.Left( m_filespec.Length() - 1 ).Upper() != m_filespec.Left( m_filespec.Length() - 1 ).Upper() ) - { - continue ; - } - } - else if ( file.Upper() != m_filespec.Upper() ) - { - continue ; - } - - break ; - } - if ( err != noErr ) - { - return FALSE ; - } - - *filename = (char*) m_name ; - - return TRUE; + OSStatus err = noErr ; + if ( NULL == m_iterator ) + { + FSRef dirRef; + err = wxMacPathToFSRef( m_dirname , &dirRef ) ; + if ( err == noErr ) + { + err = FSOpenIterator(&dirRef, kFSIterateFlat, &m_iterator); + } + if ( err ) + { + Close() ; + return false ; + } + } + + wxString name ; + + while( noErr == err ) + { + HFSUniStr255 uniname ; + FSRef fileRef; + FSCatalogInfo catalogInfo; + UInt32 fetched = 0; + + err = FSGetCatalogInfoBulk( m_iterator, 1, &fetched, NULL, kFSCatInfoNodeFlags | kFSCatInfoFinderInfo , &catalogInfo , &fileRef, NULL, &uniname ); + + // expected error codes + + if ( errFSNoMoreItems == err ) + return false ; + if ( afpAccessDenied == err ) + return false ; + + if ( noErr != err ) + break ; + + name = wxMacHFSUniStrToString( &uniname ) ; + + if ( ( name == wxT(".") || name == wxT("..") ) && !(m_flags & wxDIR_DOTDOT) ) + continue; + + if ( ( name[0U] == '.' ) && !(m_flags & wxDIR_HIDDEN ) ) + continue ; + + if ( (((FileInfo*)&catalogInfo.finderInfo)->finderFlags & kIsInvisible ) && !(m_flags & wxDIR_HIDDEN ) ) + continue ; + + // its a dir and we don't want it + if ( (catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) && !(m_flags & wxDIR_DIRS) ) + continue ; + + // its a file but we don't want it + if ( (catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) == 0 && !(m_flags & wxDIR_FILES ) ) + continue ; + + if ( m_filespec.empty() || m_filespec == wxT("*.*") || m_filespec == wxT("*") ) + { + } + else if ( !wxMatchWild(m_filespec, name , false) ) + { + continue ; + } + + break ; + } + if ( err != noErr ) + { + return false ; + } + + *filename = name ; + return true; } // ---------------------------------------------------------------------------- @@ -212,7 +210,7 @@ bool wxDirData::Read(wxString *filename) /* static */ bool wxDir::Exists(const wxString& dir) { - return wxPathExists(dir); + return wxDirExists(dir); } // ---------------------------------------------------------------------------- @@ -231,7 +229,7 @@ bool wxDir::Open(const wxString& dirname) delete M_DIR; m_data = new wxDirData(dirname); - return TRUE; + return true; } bool wxDir::IsOpened() const @@ -239,9 +237,28 @@ bool wxDir::IsOpened() const return m_data != NULL; } +wxString wxDir::GetName() const +{ + wxString name; + if ( m_data ) + { + name = M_DIR->GetName(); + if ( !name.empty() && (name.Last() == _T('/')) ) + { + // chop off the last (back)slash + name.Truncate(name.length() - 1); + } + } + + return name; +} + wxDir::~wxDir() { - delete M_DIR; + if (M_DIR != NULL) { + delete M_DIR; + m_data = NULL; + } } // ---------------------------------------------------------------------------- @@ -252,7 +269,7 @@ bool wxDir::GetFirst(wxString *filename, const wxString& filespec, int flags) const { - wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") ); + wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") ); M_DIR->Rewind(); @@ -264,9 +281,9 @@ bool wxDir::GetFirst(wxString *filename, bool wxDir::GetNext(wxString *filename) const { - wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") ); + wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") ); - wxCHECK_MSG( filename, FALSE, _T("bad pointer in wxDir::GetNext()") ); + wxCHECK_MSG( filename, false, _T("bad pointer in wxDir::GetNext()") ); return M_DIR->Read(filename); }