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