]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dirmac.cpp
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/mac/private.h"
46 # include "MoreFilesX.h"
48 # include "MoreFiles.h"
49 # include "MoreFilesExtras.h"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
57 #define MAX_PATH 260 // from VC++ headers
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 #define M_DIR ((wxDirData *)m_data)
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // this class stores everything we need to enumerate the files
74 wxDirData(const wxString
& dirname
);
77 void SetFileSpec(const wxString
& filespec
) { m_filespec
= filespec
; }
78 void SetFlags(int flags
) { m_flags
= flags
; }
80 bool Read(wxString
*filename
); // reads the next
83 const wxString
& GetName() const { return m_dirname
; }
98 // ============================================================================
100 // ============================================================================
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 wxDirData::wxDirData(const wxString
& dirname
)
111 // throw away the trailing slashes
112 size_t n
= m_dirname
.length();
113 wxCHECK_RET( n
, _T("empty dir name in wxDir") );
115 while ( n
> 0 && wxIsPathSeparator(m_dirname
[--n
]) )
118 m_dirname
.Truncate(n
+ 1);
123 // get the FSRef associated with the POSIX path
124 err
= FSPathMakeRef((const UInt8
*) m_dirname
.c_str(), &theRef
, NULL
);
125 FSGetVRefNum(&theRef
, &(m_CPB
.hFileInfo
.ioVRefNum
));
127 err
= FSGetNodeID( &theRef
, &m_dirId
, &m_isDir
) ;
131 wxMacFilename2FSSpec( m_dirname
, &fsspec
) ;
132 m_CPB
.hFileInfo
.ioVRefNum
= fsspec
.vRefNum
;
134 err
= FSpGetDirectoryID( &fsspec
, &m_dirId
, &m_isDir
) ;
136 wxASSERT_MSG( (err
== noErr
) || (err
== nsvErr
) , wxT("Error accessing directory " + m_dirname
)) ;
138 m_CPB
.hFileInfo
.ioNamePtr
= m_name
;
142 wxDirData::~wxDirData()
146 void wxDirData::Rewind()
151 bool wxDirData::Read(wxString
*filename
)
160 while ( err
== noErr
)
163 m_CPB
.dirInfo
.ioFDirIndex
= m_index
;
164 m_CPB
.dirInfo
.ioDrDirID
= m_dirId
; /* we need to do this every time */
165 err
= PBGetCatInfoSync((CInfoPBPtr
)&m_CPB
);
169 // its hidden but we don't want it
170 if ( ( m_CPB
.hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
) && !(m_flags
& wxDIR_HIDDEN
) )
173 // under X, names that start with '.' are hidden
174 if ( ( m_name
[1] == '.' ) && !(m_flags
& wxDIR_HIDDEN
) )
178 // under X thats the way the mounting points look like
179 if ( ( m_CPB
.dirInfo
.ioDrDirID
== 0 ) && ( m_flags
& wxDIR_DIRS
) )
182 // we have a directory
183 if ( ( m_CPB
.dirInfo
.ioFlAttrib
& ioDirMask
) != 0 && (m_flags
& wxDIR_DIRS
) )
186 // its a file but we don't want it
187 if ( ( m_CPB
.dirInfo
.ioFlAttrib
& ioDirMask
) == 0 && !(m_flags
& wxDIR_FILES
) )
190 wxString file
= wxMacMakeStringFromPascal( m_name
) ;
191 if ( m_filespec
.IsEmpty() || m_filespec
== wxT("*.*") || m_filespec
== wxT("*") )
194 else if ( m_filespec
.Length() > 1 && m_filespec
.Left(1) == wxT("*") )
196 if ( file
.Right( m_filespec
.Length() - 1 ).Upper() != m_filespec
.Mid(1).Upper() )
201 else if ( m_filespec
.Length() > 1 && m_filespec
.Right(1) == wxT("*") )
203 if ( file
.Left( m_filespec
.Length() - 1 ).Upper() != m_filespec
.Left( m_filespec
.Length() - 1 ).Upper() )
208 else if ( file
.Upper() != m_filespec
.Upper() )
220 *filename
= wxMacMakeStringFromPascal( m_name
) ;
225 // ----------------------------------------------------------------------------
227 // ----------------------------------------------------------------------------
230 bool wxDir::Exists(const wxString
& dir
)
232 return wxPathExists(dir
);
235 // ----------------------------------------------------------------------------
236 // wxDir construction/destruction
237 // ----------------------------------------------------------------------------
239 wxDir::wxDir(const wxString
& dirname
)
246 bool wxDir::Open(const wxString
& dirname
)
249 m_data
= new wxDirData(dirname
);
254 bool wxDir::IsOpened() const
256 return m_data
!= NULL
;
259 wxString
wxDir::GetName() const
264 name
= M_DIR
->GetName();
265 if ( !name
.empty() && (name
.Last() == _T('/')) )
267 // chop off the last (back)slash
268 name
.Truncate(name
.length() - 1);
283 // ----------------------------------------------------------------------------
285 // ----------------------------------------------------------------------------
287 bool wxDir::GetFirst(wxString
*filename
,
288 const wxString
& filespec
,
291 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
295 M_DIR
->SetFileSpec(filespec
);
296 M_DIR
->SetFlags(flags
);
298 return GetNext(filename
);
301 bool wxDir::GetNext(wxString
*filename
) const
303 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
305 wxCHECK_MSG( filename
, FALSE
, _T("bad pointer in wxDir::GetNext()") );
307 return M_DIR
->Read(filename
);