]>
Commit | Line | Data |
---|---|---|
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 | |
43 | class wxDirData | |
44 | { | |
45 | public: | |
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 | 58 | private: |
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 | ||
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); | |
a2b77260 | 86 | m_iterator = NULL ; |
0207122d SC |
87 | } |
88 | ||
89 | wxDirData::~wxDirData() | |
90 | { | |
a2b77260 SC |
91 | Close() ; |
92 | } | |
93 | ||
94 | void 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 | 103 | void wxDirData::Rewind() |
0207122d | 104 | { |
a2b77260 | 105 | Close() ; |
0207122d SC |
106 | } |
107 | ||
108 | bool 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 ; |
bf31b1d6 | 128 | wxString lowerfilespec = m_filespec.Lower(); |
da865fdd | 129 | |
a2b77260 | 130 | while( noErr == err ) |
2d4e4f80 | 131 | { |
a2b77260 SC |
132 | HFSUniStr255 uniname ; |
133 | FSRef fileRef; | |
134 | FSCatalogInfo catalogInfo; | |
4913272f | 135 | ItemCount fetched = 0; |
a2b77260 SC |
136 | |
137 | err = FSGetCatalogInfoBulk( m_iterator, 1, &fetched, NULL, kFSCatInfoNodeFlags | kFSCatInfoFinderInfo , &catalogInfo , &fileRef, NULL, &uniname ); | |
3ca57155 SC |
138 | |
139 | // expected error codes | |
140 | ||
a2b77260 SC |
141 | if ( errFSNoMoreItems == err ) |
142 | return false ; | |
3ca57155 SC |
143 | if ( afpAccessDenied == err ) |
144 | return false ; | |
da865fdd | 145 | |
a2b77260 | 146 | if ( noErr != err ) |
2d4e4f80 | 147 | break ; |
da865fdd | 148 | |
a2b77260 | 149 | name = wxMacHFSUniStrToString( &uniname ) ; |
bf31b1d6 | 150 | wxString lowername = name.Lower(); |
a2b77260 SC |
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 ; | |
da865fdd | 160 | |
7c19f4d2 RN |
161 | // its a dir and we don't want it |
162 | if ( (catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) && !(m_flags & wxDIR_DIRS) ) | |
163 | continue ; | |
a2b77260 | 164 | |
2d4e4f80 | 165 | // its a file but we don't want it |
a2b77260 | 166 | if ( (catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) == 0 && !(m_flags & wxDIR_FILES ) ) |
2d4e4f80 | 167 | continue ; |
da865fdd WS |
168 | |
169 | if ( m_filespec.empty() || m_filespec == wxT("*.*") || m_filespec == wxT("*") ) | |
2d4e4f80 GD |
170 | { |
171 | } | |
bf31b1d6 | 172 | else if ( !wxMatchWild(lowerfilespec, lowername , false) ) |
2d4e4f80 GD |
173 | { |
174 | continue ; | |
175 | } | |
da865fdd | 176 | |
2d4e4f80 GD |
177 | break ; |
178 | } | |
179 | if ( err != noErr ) | |
180 | { | |
da865fdd | 181 | return false ; |
2d4e4f80 | 182 | } |
da865fdd | 183 | |
a2b77260 | 184 | *filename = name ; |
da865fdd | 185 | return true; |
0207122d SC |
186 | } |
187 | ||
188 | // ---------------------------------------------------------------------------- | |
189 | // wxDir helpers | |
190 | // ---------------------------------------------------------------------------- | |
191 | ||
192 | /* static */ | |
193 | bool wxDir::Exists(const wxString& dir) | |
194 | { | |
da865fdd | 195 | return wxDirExists(dir); |
0207122d SC |
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 | { | |
716d0327 | 211 | delete m_data; |
0207122d | 212 | m_data = new wxDirData(dirname); |
facd6764 | 213 | |
da865fdd | 214 | return true; |
0207122d SC |
215 | } |
216 | ||
217 | bool wxDir::IsOpened() const | |
218 | { | |
219 | return m_data != NULL; | |
220 | } | |
221 | ||
f4ac0693 GD |
222 | wxString wxDir::GetName() const |
223 | { | |
224 | wxString name; | |
225 | if ( m_data ) | |
226 | { | |
716d0327 | 227 | name = m_data->GetName(); |
3dee36ae WS |
228 | if ( !name.empty() && (name.Last() == _T('/')) ) |
229 | { | |
230 | // chop off the last (back)slash | |
231 | name.Truncate(name.length() - 1); | |
232 | } | |
f4ac0693 GD |
233 | } |
234 | ||
235 | return name; | |
236 | } | |
237 | ||
0207122d SC |
238 | wxDir::~wxDir() |
239 | { | |
716d0327 PC |
240 | delete m_data; |
241 | m_data = NULL; | |
0207122d SC |
242 | } |
243 | ||
244 | // ---------------------------------------------------------------------------- | |
245 | // wxDir enumerating | |
246 | // ---------------------------------------------------------------------------- | |
247 | ||
248 | bool wxDir::GetFirst(wxString *filename, | |
249 | const wxString& filespec, | |
250 | int flags) const | |
251 | { | |
da865fdd | 252 | wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") ); |
0207122d | 253 | |
716d0327 | 254 | m_data->Rewind(); |
0207122d | 255 | |
716d0327 PC |
256 | m_data->SetFileSpec(filespec); |
257 | m_data->SetFlags(flags); | |
0207122d SC |
258 | |
259 | return GetNext(filename); | |
260 | } | |
261 | ||
262 | bool wxDir::GetNext(wxString *filename) const | |
263 | { | |
da865fdd | 264 | wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") ); |
0207122d | 265 | |
da865fdd | 266 | wxCHECK_MSG( filename, false, _T("bad pointer in wxDir::GetNext()") ); |
0207122d | 267 | |
716d0327 | 268 | return m_data->Read(filename); |
0207122d | 269 | } |