]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/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 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
33 #include "wx/filefn.h" // for wxDirExists()
39 #include "wx/mac/private.h"
42 # include "MoreFilesX.h"
44 # include "MoreFiles.h"
45 # include "MoreFilesExtras.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
);
73 void SetFileSpec(const wxString
& filespec
) { m_filespec
= filespec
; }
74 void SetFlags(int flags
) { m_flags
= flags
; }
76 bool Read(wxString
*filename
); // reads the next
79 const wxString
& GetName() const { return m_dirname
; }
80 bool Ok() const { return m_ok
; }
96 // ============================================================================
98 // ============================================================================
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 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)) ;
137 if ( (err
== noErr
) || (err
== nsvErr
))
140 wxLogError(wxString(wxT("Error accessing directory ")) + m_dirname
);
142 m_CPB
.hFileInfo
.ioNamePtr
= m_name
;
146 wxDirData::~wxDirData()
150 void wxDirData::Rewind()
155 bool wxDirData::Read(wxString
*filename
)
164 while ( err
== noErr
)
167 m_CPB
.dirInfo
.ioFDirIndex
= m_index
;
168 m_CPB
.dirInfo
.ioDrDirID
= m_dirId
; /* we need to do this every time */
169 err
= PBGetCatInfoSync((CInfoPBPtr
)&m_CPB
);
173 // its hidden but we don't want it
174 if ( ( m_CPB
.hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
) && !(m_flags
& wxDIR_HIDDEN
) )
177 // under X, names that start with '.' are hidden
178 if ( ( m_name
[1] == '.' ) && !(m_flags
& wxDIR_HIDDEN
) )
182 // under X thats the way the mounting points look like
183 if ( ( m_CPB
.dirInfo
.ioDrDirID
== 0 ) && ( m_flags
& wxDIR_DIRS
) )
186 // we have a directory
187 if ( ( m_CPB
.dirInfo
.ioFlAttrib
& ioDirMask
) != 0 && (m_flags
& wxDIR_DIRS
) )
190 // its a file but we don't want it
191 if ( ( m_CPB
.dirInfo
.ioFlAttrib
& ioDirMask
) == 0 && !(m_flags
& wxDIR_FILES
) )
194 wxString file
= wxMacMakeStringFromPascal( m_name
) ;
195 if ( m_filespec
.empty() || m_filespec
== wxT("*.*") || m_filespec
== wxT("*") )
198 else if ( m_filespec
.Length() > 1 && m_filespec
.Left(1) == wxT("*") )
200 if ( file
.Right( m_filespec
.Length() - 1 ).Upper() != m_filespec
.Mid(1).Upper() )
205 else if ( m_filespec
.Length() > 1 && m_filespec
.Right(1) == wxT("*") )
207 if ( file
.Left( m_filespec
.Length() - 1 ).Upper() != m_filespec
.Left( m_filespec
.Length() - 1 ).Upper() )
212 else if ( file
.Upper() != m_filespec
.Upper() )
224 *filename
= wxMacMakeStringFromPascal( m_name
) ;
229 // ----------------------------------------------------------------------------
231 // ----------------------------------------------------------------------------
234 bool wxDir::Exists(const wxString
& dir
)
236 return wxDirExists(dir
);
239 // ----------------------------------------------------------------------------
240 // wxDir construction/destruction
241 // ----------------------------------------------------------------------------
243 wxDir::wxDir(const wxString
& dirname
)
250 bool wxDir::Open(const wxString
& dirname
)
253 m_data
= new wxDirData(dirname
);
264 bool wxDir::IsOpened() const
266 return m_data
!= NULL
;
269 wxString
wxDir::GetName() const
274 name
= M_DIR
->GetName();
275 if ( !name
.empty() && (name
.Last() == _T('/')) )
277 // chop off the last (back)slash
278 name
.Truncate(name
.length() - 1);
293 // ----------------------------------------------------------------------------
295 // ----------------------------------------------------------------------------
297 bool wxDir::GetFirst(wxString
*filename
,
298 const wxString
& filespec
,
301 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
305 M_DIR
->SetFileSpec(filespec
);
306 M_DIR
->SetFlags(flags
);
308 return GetNext(filename
);
311 bool wxDir::GetNext(wxString
*filename
) const
313 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
315 wxCHECK_MSG( filename
, false, _T("bad pointer in wxDir::GetNext()") );
317 return M_DIR
->Read(filename
);