]> git.saurik.com Git - wxWidgets.git/blame - src/mac/dirmac.cpp
fixed (yet another?) crash in wxStrtok
[wxWidgets.git] / src / mac / dirmac.cpp
CommitLineData
0207122d
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: msw/dir.cpp
3// Purpose: wxDir implementation for Mac
4// Author: Stefan Csomor
5// Modified by:
6// Created: 08.12.99
7// RCS-ID: $Id$
8// Copyright: (c) 1999 Stefan Csomor <csomor@advanced.ch>
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "dir.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
32 #include "wx/intl.h"
33 #include "wx/log.h"
34#endif // PCH
35
36#include "wx/dir.h"
37#include "wx/filefn.h" // for wxPathExists()
38
f5c6eb5c 39#ifndef __DARWIN__
03e11df5
GD
40 #include <windows.h>
41#endif
0207122d 42
76a5e5d2
SC
43#include "wx/mac/private.h"
44
f4ac0693
GD
45#include "MoreFiles.h"
46#include "MoreFilesExtras.h"
0207122d
SC
47
48// ----------------------------------------------------------------------------
49// constants
50// ----------------------------------------------------------------------------
51
52#ifndef MAX_PATH
53 #define MAX_PATH 260 // from VC++ headers
54#endif
55
56// ----------------------------------------------------------------------------
57// macros
58// ----------------------------------------------------------------------------
59
60#define M_DIR ((wxDirData *)m_data)
61
62// ----------------------------------------------------------------------------
63// private classes
64// ----------------------------------------------------------------------------
65
66// this class stores everything we need to enumerate the files
67class wxDirData
68{
69public:
70 wxDirData(const wxString& dirname);
71 ~wxDirData();
72
73 void SetFileSpec(const wxString& filespec) { m_filespec = filespec; }
74 void SetFlags(int flags) { m_flags = flags; }
75
76 bool Read(wxString *filename); // reads the next
77 void Rewind() ;
78
f4ac0693
GD
79 const wxString& GetName() const { return m_dirname; }
80
0207122d
SC
81private:
82 CInfoPBRec m_CPB ;
83 wxInt16 m_index ;
84 long m_dirId ;
85 Str255 m_name ;
86 Boolean m_isDir ;
87
88 wxString m_dirname;
89 wxString m_filespec;
90
91 int m_flags;
92};
93
94// ============================================================================
95// implementation
96// ============================================================================
97
98// ----------------------------------------------------------------------------
99// wxDirData
100// ----------------------------------------------------------------------------
101
102wxDirData::wxDirData(const wxString& dirname)
103 : m_dirname(dirname)
104{
105 // throw away the trailing slashes
106 size_t n = m_dirname.length();
107 wxCHECK_RET( n, _T("empty dir name in wxDir") );
108
109 while ( n > 0 && wxIsPathSeparator(m_dirname[--n]) )
110 ;
111
112 m_dirname.Truncate(n + 1);
113
114 FSSpec fsspec ;
115
bedaf53e 116 wxMacFilename2FSSpec( m_dirname , &fsspec ) ;
0207122d
SC
117 m_CPB.hFileInfo.ioVRefNum = fsspec.vRefNum ;
118 m_CPB.hFileInfo.ioNamePtr = m_name ;
119 m_index = 0 ;
03e11df5 120
0e7fa783 121 OSErr err = FSpGetDirectoryID( &fsspec , &m_dirId , &m_isDir ) ;
3340066a 122 wxASSERT_MSG( err == noErr , "Error accessing directory") ;
0207122d
SC
123}
124
125wxDirData::~wxDirData()
126{
127}
128
129void wxDirData::Rewind()
130{
131 m_index = 0 ;
132}
133
134bool wxDirData::Read(wxString *filename)
135{
03e11df5
GD
136 if ( !m_isDir )
137 return FALSE ;
0207122d 138
03e11df5
GD
139#if TARGET_CARBON
140 char c_name[256] ;
141#endif
0207122d
SC
142 wxString result;
143
144 short err = noErr ;
145
146 while ( err == noErr )
147 {
148 m_index++ ;
149 m_CPB.dirInfo.ioFDirIndex = m_index;
150 m_CPB.dirInfo.ioDrDirID = m_dirId; /* we need to do this every time */
151 err = PBGetCatInfoSync((CInfoPBPtr)&m_CPB);
152 if ( err != noErr )
153 break ;
03e11df5
GD
154
155#if TARGET_CARBON
156 p2cstrcpy( c_name, m_name ) ;
157 strcpy( (char *)m_name, c_name);
158#else
2f1ae414 159 p2cstr( m_name ) ;
fac9b0cf
SC
160#endif
161#if TARGET_CARBON
162 // under X thats the way the mounting points look like
163 if ( ( m_CPB.dirInfo.ioDrDirID == 0 ) && ( m_flags & wxDIR_DIRS) )
164 break ;
03e11df5 165#endif
0207122d
SC
166 if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) != 0 && (m_flags & wxDIR_DIRS) ) // we have a directory
167 break ;
168
169 if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) == 0 && !(m_flags & wxDIR_FILES ) ) // its a file but we don't want it
170 continue ;
171
172 if ( ( m_CPB.hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible ) && !(m_flags & wxDIR_HIDDEN) ) // its hidden but we don't want it
173 continue ;
174
2f1ae414 175 wxString file( m_name ) ;
1c66f293 176 if ( m_filespec.IsEmpty() || m_filespec == "*.*" || m_filespec == "*" )
2f1ae414
SC
177 {
178 }
179 else if ( m_filespec.Length() > 1 && m_filespec.Left(1) =="*" )
180 {
181 if ( file.Right( m_filespec.Length() - 1 ).Upper() != m_filespec.Mid(1).Upper() )
182 {
183 continue ;
184 }
185 }
186 else if ( m_filespec.Length() > 1 && m_filespec.Right(1) == "*" )
187 {
188 if ( file.Left( m_filespec.Length() - 1 ).Upper() != m_filespec.Left( m_filespec.Length() - 1 ).Upper() )
189 {
190 continue ;
191 }
192 }
193 else if ( file.Upper() != m_filespec.Upper() )
194 {
195 continue ;
196 }
197
0207122d
SC
198 break ;
199 }
200 if ( err != noErr )
201 {
202 return FALSE ;
203 }
2f1ae414
SC
204
205 *filename = (char*) m_name ;
0207122d
SC
206
207 return TRUE;
208}
209
210// ----------------------------------------------------------------------------
211// wxDir helpers
212// ----------------------------------------------------------------------------
213
214/* static */
215bool wxDir::Exists(const wxString& dir)
216{
217 return wxPathExists(dir);
218}
219
220// ----------------------------------------------------------------------------
221// wxDir construction/destruction
222// ----------------------------------------------------------------------------
223
224wxDir::wxDir(const wxString& dirname)
225{
226 m_data = NULL;
227
228 (void)Open(dirname);
229}
230
231bool wxDir::Open(const wxString& dirname)
232{
233 delete M_DIR;
234 m_data = new wxDirData(dirname);
235
236 return TRUE;
237}
238
239bool wxDir::IsOpened() const
240{
241 return m_data != NULL;
242}
243
f4ac0693
GD
244wxString wxDir::GetName() const
245{
246 wxString name;
247 if ( m_data )
248 {
249 name = M_DIR->GetName();
250 if ( !name.empty() && (name.Last() == _T('/')) )
251 {
252 // chop off the last (back)slash
253 name.Truncate(name.length() - 1);
254 }
255 }
256
257 return name;
258}
259
0207122d
SC
260wxDir::~wxDir()
261{
f5bb2251
GD
262 if (M_DIR != NULL) {
263 delete M_DIR;
264 m_data = NULL;
265 }
0207122d
SC
266}
267
268// ----------------------------------------------------------------------------
269// wxDir enumerating
270// ----------------------------------------------------------------------------
271
272bool wxDir::GetFirst(wxString *filename,
273 const wxString& filespec,
274 int flags) const
275{
276 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
277
278 M_DIR->Rewind();
279
280 M_DIR->SetFileSpec(filespec);
281 M_DIR->SetFlags(flags);
282
283 return GetNext(filename);
284}
285
286bool wxDir::GetNext(wxString *filename) const
287{
288 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
289
290 wxCHECK_MSG( filename, FALSE, _T("bad pointer in wxDir::GetNext()") );
291
292 return M_DIR->Read(filename);
293}