]> git.saurik.com Git - wxWidgets.git/blame - src/mac/classic/dirmac.cpp
Back to previous call with -1 replacement.
[wxWidgets.git] / src / mac / classic / dirmac.cpp
CommitLineData
2646f485
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>
65571936 9// Licence: wxWindows licence
2646f485
SC
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2646f485
SC
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"
da865fdd 33#include "wx/filefn.h" // for wxDirExists()
2646f485
SC
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
da865fdd 76 bool Read(wxString *filename); // reads the next
2646f485
SC
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;
da865fdd 108
2646f485 109 OSErr err;
da865fdd 110
2646f485
SC
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);
da865fdd 119
2646f485
SC
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));
da865fdd 126
2646f485
SC
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
da865fdd 150void wxDirData::Rewind()
2646f485
SC
151{
152 m_index = 0 ;
153}
154
155bool wxDirData::Read(wxString *filename)
156{
157 if ( !m_isDir )
da865fdd
WS
158 return false ;
159
2646f485
SC
160 wxString result;
161
162 short err = noErr ;
da865fdd 163
2646f485
SC
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 ;
da865fdd 172
2646f485
SC
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 ;
da865fdd 189
2646f485
SC
190 // its a file but we don't want it
191 if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) == 0 && !(m_flags & wxDIR_FILES ) )
192 continue ;
da865fdd 193
2646f485 194 wxString file = wxMacMakeStringFromPascal( m_name ) ;
da865fdd 195 if ( m_filespec.empty() || m_filespec == wxT("*.*") || m_filespec == wxT("*") )
2646f485
SC
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 }
da865fdd 216
2646f485
SC
217 break ;
218 }
219 if ( err != noErr )
220 {
da865fdd 221 return false ;
2646f485 222 }
da865fdd 223
2646f485
SC
224 *filename = wxMacMakeStringFromPascal( m_name ) ;
225
da865fdd 226 return true;
2646f485
SC
227}
228
229// ----------------------------------------------------------------------------
230// wxDir helpers
231// ----------------------------------------------------------------------------
232
233/* static */
234bool wxDir::Exists(const wxString& dir)
235{
da865fdd 236 return wxDirExists(dir);
2646f485
SC
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())
da865fdd 255 return true;
2646f485
SC
256 else
257 {
258 delete m_data;
259 m_data = NULL;
da865fdd 260 return false;
2646f485
SC
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{
da865fdd 301 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
2646f485
SC
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{
da865fdd 313 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
2646f485 314
da865fdd 315 wxCHECK_MSG( filename, false, _T("bad pointer in wxDir::GetNext()") );
2646f485
SC
316
317 return M_DIR->Read(filename);
318}