]> git.saurik.com Git - wxWidgets.git/blame - src/mac/dirmac.cpp
added wxArray::SetCount()
[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
2d4e4f80
GD
45#ifdef __DARWIN__
46# include "MoreFilesX.h"
47#else
48# include "MoreFiles.h"
49# include "MoreFilesExtras.h"
50#endif
0207122d
SC
51
52// ----------------------------------------------------------------------------
53// constants
54// ----------------------------------------------------------------------------
55
56#ifndef MAX_PATH
57 #define MAX_PATH 260 // from VC++ headers
58#endif
59
60// ----------------------------------------------------------------------------
61// macros
62// ----------------------------------------------------------------------------
63
64#define M_DIR ((wxDirData *)m_data)
65
66// ----------------------------------------------------------------------------
67// private classes
68// ----------------------------------------------------------------------------
69
70// this class stores everything we need to enumerate the files
71class wxDirData
72{
73public:
74 wxDirData(const wxString& dirname);
75 ~wxDirData();
76
77 void SetFileSpec(const wxString& filespec) { m_filespec = filespec; }
78 void SetFlags(int flags) { m_flags = flags; }
79
80 bool Read(wxString *filename); // reads the next
81 void Rewind() ;
82
f4ac0693
GD
83 const wxString& GetName() const { return m_dirname; }
84
0207122d
SC
85private:
86 CInfoPBRec m_CPB ;
87 wxInt16 m_index ;
88 long m_dirId ;
89 Str255 m_name ;
90 Boolean m_isDir ;
91
92 wxString m_dirname;
93 wxString m_filespec;
94
95 int m_flags;
96};
97
98// ============================================================================
99// implementation
100// ============================================================================
101
102// ----------------------------------------------------------------------------
103// wxDirData
104// ----------------------------------------------------------------------------
105
106wxDirData::wxDirData(const wxString& dirname)
107 : m_dirname(dirname)
108{
2d4e4f80
GD
109 OSErr err;
110
0207122d
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);
2d4e4f80
GD
119
120#ifdef __DARWIN__
121 FSRef theRef;
122
123 // get the FSRef associated with the POSIX path
75633395 124 err = FSPathMakeRef((const UInt8 *) m_dirname.c_str(), &theRef, NULL);
2d4e4f80
GD
125 FSGetVRefNum(&theRef, &(m_CPB.hFileInfo.ioVRefNum));
126
127 err = FSGetNodeID( &theRef , &m_dirId , &m_isDir ) ;
128#else
129 FSSpec fsspec ;
0207122d 130
2d4e4f80
GD
131 wxMacFilename2FSSpec( m_dirname , &fsspec ) ;
132 m_CPB.hFileInfo.ioVRefNum = fsspec.vRefNum ;
0207122d 133
2d4e4f80
GD
134 err = FSpGetDirectoryID( &fsspec , &m_dirId , &m_isDir ) ;
135#endif
136 wxASSERT_MSG( (err == noErr) || (err == nsvErr) , "Error accessing directory " + m_dirname) ;
03e11df5 137
2d4e4f80
GD
138 m_CPB.hFileInfo.ioNamePtr = m_name ;
139 m_index = 0 ;
0207122d
SC
140}
141
142wxDirData::~wxDirData()
143{
144}
145
146void wxDirData::Rewind()
147{
148 m_index = 0 ;
149}
150
151bool wxDirData::Read(wxString *filename)
152{
03e11df5
GD
153 if ( !m_isDir )
154 return FALSE ;
0207122d 155
03e11df5
GD
156#if TARGET_CARBON
157 char c_name[256] ;
158#endif
0207122d
SC
159 wxString result;
160
2d4e4f80 161 short err = noErr ;
0207122d 162
2d4e4f80
GD
163 while ( err == noErr )
164 {
165 m_index++ ;
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);
169 if ( err != noErr )
170 break ;
171
03e11df5 172#if TARGET_CARBON
2d4e4f80
GD
173 p2cstrcpy( c_name, m_name ) ;
174 strcpy( (char *)m_name, c_name);
03e11df5 175#else
2d4e4f80
GD
176 p2cstr( m_name ) ;
177#endif
178 // its hidden but we don't want it
179 if ( ( m_CPB.hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible ) && !(m_flags & wxDIR_HIDDEN) )
180 continue ;
181#ifdef __DARWIN__
182 // under X, names that start with '.' are hidden
183 if ( ( m_name[0] == '.' ) && !(m_flags & wxDIR_HIDDEN) )
184 continue;
fac9b0cf
SC
185#endif
186#if TARGET_CARBON
2d4e4f80
GD
187 // under X thats the way the mounting points look like
188 if ( ( m_CPB.dirInfo.ioDrDirID == 0 ) && ( m_flags & wxDIR_DIRS) )
189 break ;
03e11df5 190#endif
2d4e4f80
GD
191 // we have a directory
192 if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) != 0 && (m_flags & wxDIR_DIRS) )
193 break ;
194
195 // its a file but we don't want it
196 if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) == 0 && !(m_flags & wxDIR_FILES ) )
197 continue ;
198
199 wxString file( m_name ) ;
200 if ( m_filespec.IsEmpty() || m_filespec == "*.*" || m_filespec == "*" )
201 {
202 }
203 else if ( m_filespec.Length() > 1 && m_filespec.Left(1) =="*" )
204 {
205 if ( file.Right( m_filespec.Length() - 1 ).Upper() != m_filespec.Mid(1).Upper() )
206 {
207 continue ;
208 }
209 }
210 else if ( m_filespec.Length() > 1 && m_filespec.Right(1) == "*" )
211 {
212 if ( file.Left( m_filespec.Length() - 1 ).Upper() != m_filespec.Left( m_filespec.Length() - 1 ).Upper() )
213 {
214 continue ;
215 }
216 }
217 else if ( file.Upper() != m_filespec.Upper() )
218 {
219 continue ;
220 }
221
222 break ;
223 }
224 if ( err != noErr )
225 {
226 return FALSE ;
227 }
228
229 *filename = (char*) m_name ;
0207122d
SC
230
231 return TRUE;
232}
233
234// ----------------------------------------------------------------------------
235// wxDir helpers
236// ----------------------------------------------------------------------------
237
238/* static */
239bool wxDir::Exists(const wxString& dir)
240{
241 return wxPathExists(dir);
242}
243
244// ----------------------------------------------------------------------------
245// wxDir construction/destruction
246// ----------------------------------------------------------------------------
247
248wxDir::wxDir(const wxString& dirname)
249{
250 m_data = NULL;
251
252 (void)Open(dirname);
253}
254
255bool wxDir::Open(const wxString& dirname)
256{
257 delete M_DIR;
258 m_data = new wxDirData(dirname);
259
260 return TRUE;
261}
262
263bool wxDir::IsOpened() const
264{
265 return m_data != NULL;
266}
267
f4ac0693
GD
268wxString wxDir::GetName() const
269{
270 wxString name;
271 if ( m_data )
272 {
273 name = M_DIR->GetName();
274 if ( !name.empty() && (name.Last() == _T('/')) )
275 {
276 // chop off the last (back)slash
277 name.Truncate(name.length() - 1);
278 }
279 }
280
281 return name;
282}
283
0207122d
SC
284wxDir::~wxDir()
285{
f5bb2251
GD
286 if (M_DIR != NULL) {
287 delete M_DIR;
288 m_data = NULL;
289 }
0207122d
SC
290}
291
292// ----------------------------------------------------------------------------
293// wxDir enumerating
294// ----------------------------------------------------------------------------
295
296bool wxDir::GetFirst(wxString *filename,
297 const wxString& filespec,
298 int flags) const
299{
300 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
301
302 M_DIR->Rewind();
303
304 M_DIR->SetFileSpec(filespec);
305 M_DIR->SetFlags(flags);
306
307 return GetNext(filename);
308}
309
310bool wxDir::GetNext(wxString *filename) const
311{
312 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
313
314 wxCHECK_MSG( filename, FALSE, _T("bad pointer in wxDir::GetNext()") );
315
316 return M_DIR->Read(filename);
317}