]>
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 // ----------------------------------------------------------------------------
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
; }
84 bool Ok() const { return m_ok
; }
100 // ============================================================================
102 // ============================================================================
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 wxDirData::wxDirData(const wxString
& dirname
)
115 // throw away the trailing slashes
116 size_t n
= m_dirname
.length();
117 wxCHECK_RET( n
, _T("empty dir name in wxDir") );
119 while ( n
> 0 && wxIsPathSeparator(m_dirname
[--n
]) )
122 m_dirname
.Truncate(n
+ 1);
127 // get the FSRef associated with the POSIX path
128 err
= FSPathMakeRef((const UInt8
*) m_dirname
.c_str(), &theRef
, NULL
);
129 FSGetVRefNum(&theRef
, &(m_CPB
.hFileInfo
.ioVRefNum
));
131 err
= FSGetNodeID( &theRef
, &m_dirId
, &m_isDir
) ;
135 wxMacFilename2FSSpec( m_dirname
, &fsspec
) ;
136 m_CPB
.hFileInfo
.ioVRefNum
= fsspec
.vRefNum
;
138 err
= FSpGetDirectoryID( &fsspec
, &m_dirId
, &m_isDir
) ;
140 //wxASSERT_MSG( (err == noErr) || (err == nsvErr) , wxT("Error accessing directory " + m_dirname)) ;
141 if ( (err
== noErr
) || (err
== nsvErr
))
144 wxLogError(wxString(wxT("Error accessing directory ")) + m_dirname
);
146 m_CPB
.hFileInfo
.ioNamePtr
= m_name
;
150 wxDirData::~wxDirData()
154 void wxDirData::Rewind()
159 bool wxDirData::Read(wxString
*filename
)
168 while ( err
== noErr
)
171 m_CPB
.dirInfo
.ioFDirIndex
= m_index
;
172 m_CPB
.dirInfo
.ioDrDirID
= m_dirId
; /* we need to do this every time */
173 err
= PBGetCatInfoSync((CInfoPBPtr
)&m_CPB
);
177 // its hidden but we don't want it
178 if ( ( m_CPB
.hFileInfo
.ioFlFndrInfo
.fdFlags
& kIsInvisible
) && !(m_flags
& wxDIR_HIDDEN
) )
181 // under X, names that start with '.' are hidden
182 if ( ( m_name
[1] == '.' ) && !(m_flags
& wxDIR_HIDDEN
) )
186 // under X thats the way the mounting points look like
187 if ( ( m_CPB
.dirInfo
.ioDrDirID
== 0 ) && ( m_flags
& wxDIR_DIRS
) )
190 // we have a directory
191 if ( ( m_CPB
.dirInfo
.ioFlAttrib
& ioDirMask
) != 0 && (m_flags
& wxDIR_DIRS
) )
194 // its a file but we don't want it
195 if ( ( m_CPB
.dirInfo
.ioFlAttrib
& ioDirMask
) == 0 && !(m_flags
& wxDIR_FILES
) )
198 wxString file
= wxMacMakeStringFromPascal( m_name
) ;
199 if ( m_filespec
.IsEmpty() || m_filespec
== wxT("*.*") || m_filespec
== wxT("*") )
202 else if ( m_filespec
.Length() > 1 && m_filespec
.Left(1) == wxT("*") )
204 if ( file
.Right( m_filespec
.Length() - 1 ).Upper() != m_filespec
.Mid(1).Upper() )
209 else if ( m_filespec
.Length() > 1 && m_filespec
.Right(1) == wxT("*") )
211 if ( file
.Left( m_filespec
.Length() - 1 ).Upper() != m_filespec
.Left( m_filespec
.Length() - 1 ).Upper() )
216 else if ( file
.Upper() != m_filespec
.Upper() )
228 *filename
= wxMacMakeStringFromPascal( m_name
) ;
233 // ----------------------------------------------------------------------------
235 // ----------------------------------------------------------------------------
238 bool wxDir::Exists(const wxString
& dir
)
240 return wxPathExists(dir
);
243 // ----------------------------------------------------------------------------
244 // wxDir construction/destruction
245 // ----------------------------------------------------------------------------
247 wxDir::wxDir(const wxString
& dirname
)
254 bool wxDir::Open(const wxString
& dirname
)
257 m_data
= new wxDirData(dirname
);
268 bool wxDir::IsOpened() const
270 return m_data
!= NULL
;
273 wxString
wxDir::GetName() const
278 name
= M_DIR
->GetName();
279 if ( !name
.empty() && (name
.Last() == _T('/')) )
281 // chop off the last (back)slash
282 name
.Truncate(name
.length() - 1);
297 // ----------------------------------------------------------------------------
299 // ----------------------------------------------------------------------------
301 bool wxDir::GetFirst(wxString
*filename
,
302 const wxString
& filespec
,
305 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
309 M_DIR
->SetFileSpec(filespec
);
310 M_DIR
->SetFlags(flags
);
312 return GetNext(filename
);
315 bool wxDir::GetNext(wxString
*filename
) const
317 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
319 wxCHECK_MSG( filename
, FALSE
, _T("bad pointer in wxDir::GetNext()") );
321 return M_DIR
->Read(filename
);