]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/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 license
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
) , "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
)
163 while ( err
== noErr
)
166 m_CPB
.dirInfo
.ioFDirIndex
= m_index
;
167 m_CPB
.dirInfo
.ioDrDirID
= m_dirId
; /* we need to do this every time */
168 err
= PBGetCatInfoSync((CInfoPBPtr
)&m_CPB
);
173 p2cstrcpy( c_name
, m_name
) ;
174 strcpy( (char *)m_name
, c_name
);
178 // its hidden but we don't want it
179 if ( ( m_CPB
.hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
) && !(m_flags
& wxDIR_HIDDEN
) )
182 // under X, names that start with '.' are hidden
183 if ( ( m_name
[0] == '.' ) && !(m_flags
& wxDIR_HIDDEN
) )
187 // under X thats the way the mounting points look like
188 if ( ( m_CPB
.dirInfo
.ioDrDirID
== 0 ) && ( m_flags
& wxDIR_DIRS
) )
191 // we have a directory
192 if ( ( m_CPB
.dirInfo
.ioFlAttrib
& ioDirMask
) != 0 && (m_flags
& wxDIR_DIRS
) )
195 // its a file but we don't want it
196 if ( ( m_CPB
.dirInfo
.ioFlAttrib
& ioDirMask
) == 0 && !(m_flags
& wxDIR_FILES
) )
199 wxString
file( m_name
) ;
200 if ( m_filespec
.IsEmpty() || m_filespec
== "*.*" || m_filespec
== "*" )
203 else if ( m_filespec
.Length() > 1 && m_filespec
.Left(1) =="*" )
205 if ( file
.Right( m_filespec
.Length() - 1 ).Upper() != m_filespec
.Mid(1).Upper() )
210 else if ( m_filespec
.Length() > 1 && m_filespec
.Right(1) == "*" )
212 if ( file
.Left( m_filespec
.Length() - 1 ).Upper() != m_filespec
.Left( m_filespec
.Length() - 1 ).Upper() )
217 else if ( file
.Upper() != m_filespec
.Upper() )
229 *filename
= (char*) m_name
;
234 // ----------------------------------------------------------------------------
236 // ----------------------------------------------------------------------------
239 bool wxDir::Exists(const wxString
& dir
)
241 return wxPathExists(dir
);
244 // ----------------------------------------------------------------------------
245 // wxDir construction/destruction
246 // ----------------------------------------------------------------------------
248 wxDir::wxDir(const wxString
& dirname
)
255 bool wxDir::Open(const wxString
& dirname
)
258 m_data
= new wxDirData(dirname
);
263 bool wxDir::IsOpened() const
265 return m_data
!= NULL
;
268 wxString
wxDir::GetName() const
273 name
= M_DIR
->GetName();
274 if ( !name
.empty() && (name
.Last() == _T('/')) )
276 // chop off the last (back)slash
277 name
.Truncate(name
.length() - 1);
292 // ----------------------------------------------------------------------------
294 // ----------------------------------------------------------------------------
296 bool wxDir::GetFirst(wxString
*filename
,
297 const wxString
& filespec
,
300 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
304 M_DIR
->SetFileSpec(filespec
);
305 M_DIR
->SetFlags(flags
);
307 return GetNext(filename
);
310 bool wxDir::GetNext(wxString
*filename
) const
312 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
314 wxCHECK_MSG( filename
, FALSE
, _T("bad pointer in wxDir::GetNext()") );
316 return M_DIR
->Read(filename
);