]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/dirmac.cpp
xti change
[wxWidgets.git] / src / mac / 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#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
39#ifndef __DARWIN__
40 #include <windows.h>
41#endif
42
43#include "wx/mac/private.h"
44
45#ifdef __DARWIN__
46# include "MoreFilesX.h"
47#else
48# include "MoreFiles.h"
49# include "MoreFilesExtras.h"
50#endif
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
83 const wxString& GetName() const { return m_dirname; }
84
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{
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
138 m_CPB.hFileInfo.ioNamePtr = m_name ;
139 m_index = 0 ;
140}
141
142wxDirData::~wxDirData()
143{
144}
145
146void wxDirData::Rewind()
147{
148 m_index = 0 ;
149}
150
151bool wxDirData::Read(wxString *filename)
152{
153 if ( !m_isDir )
154 return FALSE ;
155
156 wxString result;
157
158 short err = noErr ;
159
160 while ( err == noErr )
161 {
162 m_index++ ;
163 m_CPB.dirInfo.ioFDirIndex = m_index;
164 m_CPB.dirInfo.ioDrDirID = m_dirId; /* we need to do this every time */
165 err = PBGetCatInfoSync((CInfoPBPtr)&m_CPB);
166 if ( err != noErr )
167 break ;
168
169 // its hidden but we don't want it
170 if ( ( m_CPB.hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible ) && !(m_flags & wxDIR_HIDDEN) )
171 continue ;
172#ifdef __DARWIN__
173 // under X, names that start with '.' are hidden
174 if ( ( m_name[1] == '.' ) && !(m_flags & wxDIR_HIDDEN) )
175 continue;
176#endif
177#if TARGET_CARBON
178 // under X thats the way the mounting points look like
179 if ( ( m_CPB.dirInfo.ioDrDirID == 0 ) && ( m_flags & wxDIR_DIRS) )
180 break ;
181#endif
182 // we have a directory
183 if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) != 0 && (m_flags & wxDIR_DIRS) )
184 break ;
185
186 // its a file but we don't want it
187 if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) == 0 && !(m_flags & wxDIR_FILES ) )
188 continue ;
189
190 wxString file = wxMacMakeStringFromPascal( m_name ) ;
191 if ( m_filespec.IsEmpty() || m_filespec == wxT("*.*") || m_filespec == wxT("*") )
192 {
193 }
194 else if ( m_filespec.Length() > 1 && m_filespec.Left(1) == wxT("*") )
195 {
196 if ( file.Right( m_filespec.Length() - 1 ).Upper() != m_filespec.Mid(1).Upper() )
197 {
198 continue ;
199 }
200 }
201 else if ( m_filespec.Length() > 1 && m_filespec.Right(1) == wxT("*") )
202 {
203 if ( file.Left( m_filespec.Length() - 1 ).Upper() != m_filespec.Left( m_filespec.Length() - 1 ).Upper() )
204 {
205 continue ;
206 }
207 }
208 else if ( file.Upper() != m_filespec.Upper() )
209 {
210 continue ;
211 }
212
213 break ;
214 }
215 if ( err != noErr )
216 {
217 return FALSE ;
218 }
219
220 *filename = wxMacMakeStringFromPascal( m_name ) ;
221
222 return TRUE;
223}
224
225// ----------------------------------------------------------------------------
226// wxDir helpers
227// ----------------------------------------------------------------------------
228
229/* static */
230bool wxDir::Exists(const wxString& dir)
231{
232 return wxPathExists(dir);
233}
234
235// ----------------------------------------------------------------------------
236// wxDir construction/destruction
237// ----------------------------------------------------------------------------
238
239wxDir::wxDir(const wxString& dirname)
240{
241 m_data = NULL;
242
243 (void)Open(dirname);
244}
245
246bool wxDir::Open(const wxString& dirname)
247{
248 delete M_DIR;
249 m_data = new wxDirData(dirname);
250
251 return TRUE;
252}
253
254bool wxDir::IsOpened() const
255{
256 return m_data != NULL;
257}
258
259wxString wxDir::GetName() const
260{
261 wxString name;
262 if ( m_data )
263 {
264 name = M_DIR->GetName();
265 if ( !name.empty() && (name.Last() == _T('/')) )
266 {
267 // chop off the last (back)slash
268 name.Truncate(name.length() - 1);
269 }
270 }
271
272 return name;
273}
274
275wxDir::~wxDir()
276{
277 if (M_DIR != NULL) {
278 delete M_DIR;
279 m_data = NULL;
280 }
281}
282
283// ----------------------------------------------------------------------------
284// wxDir enumerating
285// ----------------------------------------------------------------------------
286
287bool wxDir::GetFirst(wxString *filename,
288 const wxString& filespec,
289 int flags) const
290{
291 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
292
293 M_DIR->Rewind();
294
295 M_DIR->SetFileSpec(filespec);
296 M_DIR->SetFlags(flags);
297
298 return GetNext(filename);
299}
300
301bool wxDir::GetNext(wxString *filename) const
302{
303 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
304
305 wxCHECK_MSG( filename, FALSE, _T("bad pointer in wxDir::GetNext()") );
306
307 return M_DIR->Read(filename);
308}