]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dirmac.cpp
more cleanup
[wxWidgets.git] / src / mac / carbon / dirmac.cpp
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 // 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"
33 #include "wx/filefn.h" // for wxDirExists()
34
35 #ifndef __DARWIN__
36 #include <windows.h>
37 #endif
38
39 #include "wx/filename.h"
40 #include "wx/mac/private.h"
41
42 #include "MoreFilesX.h"
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
63 class wxDirData
64 {
65 public:
66 wxDirData(const wxString& dirname);
67 ~wxDirData();
68
69 void Close() ;
70 void SetFileSpec(const wxString& filespec) { m_filespec = filespec; }
71 void SetFlags(int flags) { m_flags = flags; }
72
73 bool Read(wxString *filename); // reads the next
74 void Rewind() ;
75
76 const wxString& GetName() const { return m_dirname; }
77
78 private:
79 FSIterator m_iterator ;
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
95 wxDirData::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);
106 m_iterator = NULL ;
107 }
108
109 wxDirData::~wxDirData()
110 {
111 Close() ;
112 }
113
114 void wxDirData::Close()
115 {
116 if ( m_iterator )
117 {
118 FSCloseIterator( m_iterator ) ;
119 m_iterator = NULL ;
120 }
121 }
122
123 void wxDirData::Rewind()
124 {
125 Close() ;
126 }
127
128 bool wxDirData::Read(wxString *filename)
129 {
130 wxString result;
131 OSStatus err = noErr ;
132 if ( NULL == m_iterator )
133 {
134 FSRef dirRef;
135 err = wxMacPathToFSRef( m_dirname , &dirRef ) ;
136 if ( err == noErr )
137 {
138 err = FSOpenIterator(&dirRef, kFSIterateFlat, &m_iterator);
139 }
140 if ( err )
141 {
142 Close() ;
143 return false ;
144 }
145 }
146
147 wxString name ;
148
149 while( noErr == err )
150 {
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 );
157
158 // expected error codes
159
160 if ( errFSNoMoreItems == err )
161 return false ;
162 if ( afpAccessDenied == err )
163 return false ;
164
165 if ( noErr != err )
166 break ;
167
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 ;
178
179 // its a dir and we don't want it
180 if ( (catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) && !(m_flags & wxDIR_DIRS) )
181 continue ;
182
183 // its a file but we don't want it
184 if ( (catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) == 0 && !(m_flags & wxDIR_FILES ) )
185 continue ;
186
187 if ( m_filespec.empty() || m_filespec == wxT("*.*") || m_filespec == wxT("*") )
188 {
189 }
190 else if ( !wxMatchWild(m_filespec, name , false) )
191 {
192 continue ;
193 }
194
195 break ;
196 }
197 if ( err != noErr )
198 {
199 return false ;
200 }
201
202 *filename = name ;
203 return true;
204 }
205
206 // ----------------------------------------------------------------------------
207 // wxDir helpers
208 // ----------------------------------------------------------------------------
209
210 /* static */
211 bool wxDir::Exists(const wxString& dir)
212 {
213 return wxDirExists(dir);
214 }
215
216 // ----------------------------------------------------------------------------
217 // wxDir construction/destruction
218 // ----------------------------------------------------------------------------
219
220 wxDir::wxDir(const wxString& dirname)
221 {
222 m_data = NULL;
223
224 (void)Open(dirname);
225 }
226
227 bool wxDir::Open(const wxString& dirname)
228 {
229 delete M_DIR;
230 m_data = new wxDirData(dirname);
231
232 return true;
233 }
234
235 bool wxDir::IsOpened() const
236 {
237 return m_data != NULL;
238 }
239
240 wxString wxDir::GetName() const
241 {
242 wxString name;
243 if ( m_data )
244 {
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 }
251 }
252
253 return name;
254 }
255
256 wxDir::~wxDir()
257 {
258 if (M_DIR != NULL) {
259 delete M_DIR;
260 m_data = NULL;
261 }
262 }
263
264 // ----------------------------------------------------------------------------
265 // wxDir enumerating
266 // ----------------------------------------------------------------------------
267
268 bool wxDir::GetFirst(wxString *filename,
269 const wxString& filespec,
270 int flags) const
271 {
272 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
273
274 M_DIR->Rewind();
275
276 M_DIR->SetFileSpec(filespec);
277 M_DIR->SetFlags(flags);
278
279 return GetNext(filename);
280 }
281
282 bool wxDir::GetNext(wxString *filename) const
283 {
284 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
285
286 wxCHECK_MSG( filename, false, _T("bad pointer in wxDir::GetNext()") );
287
288 return M_DIR->Read(filename);
289 }