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