]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/classic/dirmac.cpp
[ 1579793 ] numerous bug fixes when mask is used
[wxWidgets.git] / src / mac / classic / dirmac.cpp
... / ...
CommitLineData
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 licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27#ifndef WX_PRECOMP
28 #include "wx/intl.h"
29 #include "wx/log.h"
30#endif // PCH
31
32#include "wx/dir.h"
33#include "wx/filefn.h" // for wxDirExists()
34
35#ifndef __DARWIN__
36 #include <windows.h>
37#endif
38
39#include "wx/mac/private.h"
40
41#ifdef __DARWIN__
42# include "MoreFilesX.h"
43#else
44# include "MoreFiles.h"
45# include "MoreFilesExtras.h"
46#endif
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
79 const wxString& GetName() const { return m_dirname; }
80 bool Ok() const { return m_ok; }
81
82private:
83 CInfoPBRec m_CPB ;
84 wxInt16 m_index ;
85 long m_dirId ;
86 Str255 m_name ;
87 Boolean m_isDir ;
88
89 wxString m_dirname;
90 wxString m_filespec;
91
92 int m_flags;
93 bool m_ok;
94};
95
96// ============================================================================
97// implementation
98// ============================================================================
99
100// ----------------------------------------------------------------------------
101// wxDirData
102// ----------------------------------------------------------------------------
103
104wxDirData::wxDirData(const wxString& dirname)
105 : m_dirname(dirname)
106{
107 m_ok = false;
108
109 OSErr err;
110
111 // throw away the trailing slashes
112 size_t n = m_dirname.length();
113 wxCHECK_RET( n, _T("empty dir name in wxDir") );
114
115 while ( n > 0 && wxIsPathSeparator(m_dirname[--n]) )
116 ;
117
118 m_dirname.Truncate(n + 1);
119
120#ifdef __DARWIN__
121 FSRef theRef;
122
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));
126
127 err = FSGetNodeID( &theRef , &m_dirId , &m_isDir ) ;
128#else
129 FSSpec fsspec ;
130
131 wxMacFilename2FSSpec( m_dirname , &fsspec ) ;
132 m_CPB.hFileInfo.ioVRefNum = fsspec.vRefNum ;
133
134 err = FSpGetDirectoryID( &fsspec , &m_dirId , &m_isDir ) ;
135#endif
136 //wxASSERT_MSG( (err == noErr) || (err == nsvErr) , wxT("Error accessing directory " + m_dirname)) ;
137 if ( (err == noErr) || (err == nsvErr))
138 m_ok = true;
139 else
140 wxLogError(wxString(wxT("Error accessing directory ")) + m_dirname);
141
142 m_CPB.hFileInfo.ioNamePtr = m_name ;
143 m_index = 0 ;
144}
145
146wxDirData::~wxDirData()
147{
148}
149
150void wxDirData::Rewind()
151{
152 m_index = 0 ;
153}
154
155bool wxDirData::Read(wxString *filename)
156{
157 if ( !m_isDir )
158 return false ;
159
160 wxString result;
161
162 short err = noErr ;
163
164 while ( err == noErr )
165 {
166 m_index++ ;
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);
170 if ( err != noErr )
171 break ;
172
173 // its hidden but we don't want it
174 if ( ( m_CPB.hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible ) && !(m_flags & wxDIR_HIDDEN) )
175 continue ;
176#ifdef __DARWIN__
177 // under X, names that start with '.' are hidden
178 if ( ( m_name[1] == '.' ) && !(m_flags & wxDIR_HIDDEN) )
179 continue;
180#endif
181#if TARGET_CARBON
182 // under X thats the way the mounting points look like
183 if ( ( m_CPB.dirInfo.ioDrDirID == 0 ) && ( m_flags & wxDIR_DIRS) )
184 break ;
185#endif
186 // we have a directory
187 if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) != 0 && (m_flags & wxDIR_DIRS) )
188 break ;
189
190 // its a file but we don't want it
191 if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) == 0 && !(m_flags & wxDIR_FILES ) )
192 continue ;
193
194 wxString file = wxMacMakeStringFromPascal( m_name ) ;
195 if ( m_filespec.empty() || m_filespec == wxT("*.*") || m_filespec == wxT("*") )
196 {
197 }
198 else if ( m_filespec.Length() > 1 && m_filespec.Left(1) == wxT("*") )
199 {
200 if ( file.Right( m_filespec.Length() - 1 ).Upper() != m_filespec.Mid(1).Upper() )
201 {
202 continue ;
203 }
204 }
205 else if ( m_filespec.Length() > 1 && m_filespec.Right(1) == wxT("*") )
206 {
207 if ( file.Left( m_filespec.Length() - 1 ).Upper() != m_filespec.Left( m_filespec.Length() - 1 ).Upper() )
208 {
209 continue ;
210 }
211 }
212 else if ( file.Upper() != m_filespec.Upper() )
213 {
214 continue ;
215 }
216
217 break ;
218 }
219 if ( err != noErr )
220 {
221 return false ;
222 }
223
224 *filename = wxMacMakeStringFromPascal( m_name ) ;
225
226 return true;
227}
228
229// ----------------------------------------------------------------------------
230// wxDir helpers
231// ----------------------------------------------------------------------------
232
233/* static */
234bool wxDir::Exists(const wxString& dir)
235{
236 return wxDirExists(dir);
237}
238
239// ----------------------------------------------------------------------------
240// wxDir construction/destruction
241// ----------------------------------------------------------------------------
242
243wxDir::wxDir(const wxString& dirname)
244{
245 m_data = NULL;
246
247 (void)Open(dirname);
248}
249
250bool wxDir::Open(const wxString& dirname)
251{
252 delete M_DIR;
253 m_data = new wxDirData(dirname);
254 if (m_data->Ok())
255 return true;
256 else
257 {
258 delete m_data;
259 m_data = NULL;
260 return false;
261 }
262}
263
264bool wxDir::IsOpened() const
265{
266 return m_data != NULL;
267}
268
269wxString wxDir::GetName() const
270{
271 wxString name;
272 if ( m_data )
273 {
274 name = M_DIR->GetName();
275 if ( !name.empty() && (name.Last() == _T('/')) )
276 {
277 // chop off the last (back)slash
278 name.Truncate(name.length() - 1);
279 }
280 }
281
282 return name;
283}
284
285wxDir::~wxDir()
286{
287 if (M_DIR != NULL) {
288 delete M_DIR;
289 m_data = NULL;
290 }
291}
292
293// ----------------------------------------------------------------------------
294// wxDir enumerating
295// ----------------------------------------------------------------------------
296
297bool wxDir::GetFirst(wxString *filename,
298 const wxString& filespec,
299 int flags) const
300{
301 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
302
303 M_DIR->Rewind();
304
305 M_DIR->SetFileSpec(filespec);
306 M_DIR->SetFlags(flags);
307
308 return GetNext(filename);
309}
310
311bool wxDir::GetNext(wxString *filename) const
312{
313 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
314
315 wxCHECK_MSG( filename, false, _T("bad pointer in wxDir::GetNext()") );
316
317 return M_DIR->Read(filename);
318}