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