]>
Commit | Line | Data |
---|---|---|
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 | |
63 | class wxDirData | |
64 | { | |
65 | public: | |
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 | 78 | private: |
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 | ||
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); | |
a2b77260 | 106 | m_iterator = NULL ; |
0207122d SC |
107 | } |
108 | ||
109 | wxDirData::~wxDirData() | |
110 | { | |
a2b77260 SC |
111 | Close() ; |
112 | } | |
113 | ||
114 | void 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 | 123 | void wxDirData::Rewind() |
0207122d | 124 | { |
a2b77260 | 125 | Close() ; |
0207122d SC |
126 | } |
127 | ||
128 | bool 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 ); | |
157 | if ( errFSNoMoreItems == err ) | |
158 | return false ; | |
da865fdd | 159 | |
a2b77260 | 160 | wxASSERT( noErr == err ) ; |
da865fdd | 161 | |
a2b77260 | 162 | if ( noErr != err ) |
2d4e4f80 | 163 | break ; |
da865fdd | 164 | |
a2b77260 SC |
165 | name = wxMacHFSUniStrToString( &uniname ) ; |
166 | ||
167 | if ( ( name == wxT(".") || name == wxT("..") ) && !(m_flags & wxDIR_DOTDOT) ) | |
168 | continue; | |
169 | ||
170 | if ( ( name[0U] == '.' ) && !(m_flags & wxDIR_HIDDEN ) ) | |
171 | continue ; | |
172 | ||
173 | if ( (((FileInfo*)&catalogInfo.finderInfo)->finderFlags & kIsInvisible ) && !(m_flags & wxDIR_HIDDEN ) ) | |
174 | continue ; | |
da865fdd | 175 | |
7c19f4d2 RN |
176 | // its a dir and we don't want it |
177 | if ( (catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) && !(m_flags & wxDIR_DIRS) ) | |
178 | continue ; | |
a2b77260 | 179 | |
2d4e4f80 | 180 | // its a file but we don't want it |
a2b77260 | 181 | if ( (catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) == 0 && !(m_flags & wxDIR_FILES ) ) |
2d4e4f80 | 182 | continue ; |
da865fdd WS |
183 | |
184 | if ( m_filespec.empty() || m_filespec == wxT("*.*") || m_filespec == wxT("*") ) | |
2d4e4f80 GD |
185 | { |
186 | } | |
da865fdd | 187 | else if ( !wxMatchWild(m_filespec, name , false) ) |
2d4e4f80 GD |
188 | { |
189 | continue ; | |
190 | } | |
da865fdd | 191 | |
2d4e4f80 GD |
192 | break ; |
193 | } | |
194 | if ( err != noErr ) | |
195 | { | |
da865fdd | 196 | return false ; |
2d4e4f80 | 197 | } |
da865fdd | 198 | |
a2b77260 | 199 | *filename = name ; |
da865fdd | 200 | return true; |
0207122d SC |
201 | } |
202 | ||
203 | // ---------------------------------------------------------------------------- | |
204 | // wxDir helpers | |
205 | // ---------------------------------------------------------------------------- | |
206 | ||
207 | /* static */ | |
208 | bool wxDir::Exists(const wxString& dir) | |
209 | { | |
da865fdd | 210 | return wxDirExists(dir); |
0207122d SC |
211 | } |
212 | ||
213 | // ---------------------------------------------------------------------------- | |
214 | // wxDir construction/destruction | |
215 | // ---------------------------------------------------------------------------- | |
216 | ||
217 | wxDir::wxDir(const wxString& dirname) | |
218 | { | |
219 | m_data = NULL; | |
220 | ||
221 | (void)Open(dirname); | |
222 | } | |
223 | ||
224 | bool wxDir::Open(const wxString& dirname) | |
225 | { | |
226 | delete M_DIR; | |
227 | m_data = new wxDirData(dirname); | |
facd6764 | 228 | |
da865fdd | 229 | return true; |
0207122d SC |
230 | } |
231 | ||
232 | bool wxDir::IsOpened() const | |
233 | { | |
234 | return m_data != NULL; | |
235 | } | |
236 | ||
f4ac0693 GD |
237 | wxString wxDir::GetName() const |
238 | { | |
239 | wxString name; | |
240 | if ( m_data ) | |
241 | { | |
3dee36ae WS |
242 | name = M_DIR->GetName(); |
243 | if ( !name.empty() && (name.Last() == _T('/')) ) | |
244 | { | |
245 | // chop off the last (back)slash | |
246 | name.Truncate(name.length() - 1); | |
247 | } | |
f4ac0693 GD |
248 | } |
249 | ||
250 | return name; | |
251 | } | |
252 | ||
0207122d SC |
253 | wxDir::~wxDir() |
254 | { | |
f5bb2251 GD |
255 | if (M_DIR != NULL) { |
256 | delete M_DIR; | |
257 | m_data = NULL; | |
258 | } | |
0207122d SC |
259 | } |
260 | ||
261 | // ---------------------------------------------------------------------------- | |
262 | // wxDir enumerating | |
263 | // ---------------------------------------------------------------------------- | |
264 | ||
265 | bool wxDir::GetFirst(wxString *filename, | |
266 | const wxString& filespec, | |
267 | int flags) const | |
268 | { | |
da865fdd | 269 | wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") ); |
0207122d SC |
270 | |
271 | M_DIR->Rewind(); | |
272 | ||
273 | M_DIR->SetFileSpec(filespec); | |
274 | M_DIR->SetFlags(flags); | |
275 | ||
276 | return GetNext(filename); | |
277 | } | |
278 | ||
279 | bool wxDir::GetNext(wxString *filename) const | |
280 | { | |
da865fdd | 281 | wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") ); |
0207122d | 282 | |
da865fdd | 283 | wxCHECK_MSG( filename, false, _T("bad pointer in wxDir::GetNext()") ); |
0207122d SC |
284 | |
285 | return M_DIR->Read(filename); | |
286 | } |