]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dirmac.cpp
renamed IsRefTo() to IsSameAs() (do complain if this is not more clear) and
[wxWidgets.git] / src / mac / carbon / dirmac.cpp
CommitLineData
0207122d 1/////////////////////////////////////////////////////////////////////////////
716d0327 2// Name: mac/dirmac.cpp
0207122d
SC
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
0207122d
SC
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
0207122d
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
716d0327
PC
27#include "wx/dir.h"
28
0207122d
SC
29#ifndef WX_PRECOMP
30 #include "wx/intl.h"
31 #include "wx/log.h"
32#endif // PCH
33
da865fdd 34#include "wx/filefn.h" // for wxDirExists()
a2b77260 35#include "wx/filename.h"
76a5e5d2
SC
36#include "wx/mac/private.h"
37
0207122d
SC
38// ----------------------------------------------------------------------------
39// private classes
40// ----------------------------------------------------------------------------
41
42// this class stores everything we need to enumerate the files
43class wxDirData
44{
45public:
46 wxDirData(const wxString& dirname);
47 ~wxDirData();
da865fdd 48
a2b77260 49 void Close() ;
0207122d
SC
50 void SetFileSpec(const wxString& filespec) { m_filespec = filespec; }
51 void SetFlags(int flags) { m_flags = flags; }
52
da865fdd 53 bool Read(wxString *filename); // reads the next
0207122d
SC
54 void Rewind() ;
55
f4ac0693
GD
56 const wxString& GetName() const { return m_dirname; }
57
0207122d 58private:
a2b77260 59 FSIterator m_iterator ;
0207122d
SC
60
61 wxString m_dirname;
62 wxString m_filespec;
63
64 int m_flags;
65};
66
67// ============================================================================
68// implementation
69// ============================================================================
70
71// ----------------------------------------------------------------------------
72// wxDirData
73// ----------------------------------------------------------------------------
74
75wxDirData::wxDirData(const wxString& dirname)
76 : m_dirname(dirname)
77{
78 // throw away the trailing slashes
79 size_t n = m_dirname.length();
80 wxCHECK_RET( n, _T("empty dir name in wxDir") );
81
82 while ( n > 0 && wxIsPathSeparator(m_dirname[--n]) )
83 ;
84
85 m_dirname.Truncate(n + 1);
a2b77260 86 m_iterator = NULL ;
0207122d
SC
87}
88
89wxDirData::~wxDirData()
90{
a2b77260
SC
91 Close() ;
92}
93
94void wxDirData::Close()
da865fdd 95{
a2b77260
SC
96 if ( m_iterator )
97 {
98 FSCloseIterator( m_iterator ) ;
99 m_iterator = NULL ;
100 }
0207122d
SC
101}
102
da865fdd 103void wxDirData::Rewind()
0207122d 104{
a2b77260 105 Close() ;
0207122d
SC
106}
107
108bool wxDirData::Read(wxString *filename)
da865fdd 109{
0207122d 110 wxString result;
a2b77260
SC
111 OSStatus err = noErr ;
112 if ( NULL == m_iterator )
113 {
114 FSRef dirRef;
115 err = wxMacPathToFSRef( m_dirname , &dirRef ) ;
116 if ( err == noErr )
117 {
da865fdd
WS
118 err = FSOpenIterator(&dirRef, kFSIterateFlat, &m_iterator);
119 }
120 if ( err )
121 {
122 Close() ;
123 return false ;
124 }
a2b77260 125 }
da865fdd 126
a2b77260 127 wxString name ;
da865fdd 128
a2b77260 129 while( noErr == err )
2d4e4f80 130 {
a2b77260
SC
131 HFSUniStr255 uniname ;
132 FSRef fileRef;
133 FSCatalogInfo catalogInfo;
4913272f 134 ItemCount fetched = 0;
a2b77260
SC
135
136 err = FSGetCatalogInfoBulk( m_iterator, 1, &fetched, NULL, kFSCatInfoNodeFlags | kFSCatInfoFinderInfo , &catalogInfo , &fileRef, NULL, &uniname );
3ca57155
SC
137
138 // expected error codes
139
a2b77260
SC
140 if ( errFSNoMoreItems == err )
141 return false ;
3ca57155
SC
142 if ( afpAccessDenied == err )
143 return false ;
da865fdd 144
a2b77260 145 if ( noErr != err )
2d4e4f80 146 break ;
da865fdd 147
a2b77260
SC
148 name = wxMacHFSUniStrToString( &uniname ) ;
149
150 if ( ( name == wxT(".") || name == wxT("..") ) && !(m_flags & wxDIR_DOTDOT) )
151 continue;
152
153 if ( ( name[0U] == '.' ) && !(m_flags & wxDIR_HIDDEN ) )
154 continue ;
155
156 if ( (((FileInfo*)&catalogInfo.finderInfo)->finderFlags & kIsInvisible ) && !(m_flags & wxDIR_HIDDEN ) )
157 continue ;
da865fdd 158
7c19f4d2
RN
159 // its a dir and we don't want it
160 if ( (catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) && !(m_flags & wxDIR_DIRS) )
161 continue ;
a2b77260 162
2d4e4f80 163 // its a file but we don't want it
a2b77260 164 if ( (catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) == 0 && !(m_flags & wxDIR_FILES ) )
2d4e4f80 165 continue ;
da865fdd
WS
166
167 if ( m_filespec.empty() || m_filespec == wxT("*.*") || m_filespec == wxT("*") )
2d4e4f80
GD
168 {
169 }
da865fdd 170 else if ( !wxMatchWild(m_filespec, name , false) )
2d4e4f80
GD
171 {
172 continue ;
173 }
da865fdd 174
2d4e4f80
GD
175 break ;
176 }
177 if ( err != noErr )
178 {
da865fdd 179 return false ;
2d4e4f80 180 }
da865fdd 181
a2b77260 182 *filename = name ;
da865fdd 183 return true;
0207122d
SC
184}
185
186// ----------------------------------------------------------------------------
187// wxDir helpers
188// ----------------------------------------------------------------------------
189
190/* static */
191bool wxDir::Exists(const wxString& dir)
192{
da865fdd 193 return wxDirExists(dir);
0207122d
SC
194}
195
196// ----------------------------------------------------------------------------
197// wxDir construction/destruction
198// ----------------------------------------------------------------------------
199
200wxDir::wxDir(const wxString& dirname)
201{
202 m_data = NULL;
203
204 (void)Open(dirname);
205}
206
207bool wxDir::Open(const wxString& dirname)
208{
716d0327 209 delete m_data;
0207122d 210 m_data = new wxDirData(dirname);
facd6764 211
da865fdd 212 return true;
0207122d
SC
213}
214
215bool wxDir::IsOpened() const
216{
217 return m_data != NULL;
218}
219
f4ac0693
GD
220wxString wxDir::GetName() const
221{
222 wxString name;
223 if ( m_data )
224 {
716d0327 225 name = m_data->GetName();
3dee36ae
WS
226 if ( !name.empty() && (name.Last() == _T('/')) )
227 {
228 // chop off the last (back)slash
229 name.Truncate(name.length() - 1);
230 }
f4ac0693
GD
231 }
232
233 return name;
234}
235
0207122d
SC
236wxDir::~wxDir()
237{
716d0327
PC
238 delete m_data;
239 m_data = NULL;
0207122d
SC
240}
241
242// ----------------------------------------------------------------------------
243// wxDir enumerating
244// ----------------------------------------------------------------------------
245
246bool wxDir::GetFirst(wxString *filename,
247 const wxString& filespec,
248 int flags) const
249{
da865fdd 250 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
0207122d 251
716d0327 252 m_data->Rewind();
0207122d 253
716d0327
PC
254 m_data->SetFileSpec(filespec);
255 m_data->SetFlags(flags);
0207122d
SC
256
257 return GetNext(filename);
258}
259
260bool wxDir::GetNext(wxString *filename) const
261{
da865fdd 262 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
0207122d 263
da865fdd 264 wxCHECK_MSG( filename, false, _T("bad pointer in wxDir::GetNext()") );
0207122d 265
716d0327 266 return m_data->Read(filename);
0207122d 267}