]>
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 | ||
20 | #ifdef __GNUG__ | |
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 | ||
f5c6eb5c | 39 | #ifndef __DARWIN__ |
03e11df5 GD |
40 | #include <windows.h> |
41 | #endif | |
0207122d | 42 | |
a2b77260 | 43 | #include "wx/filename.h" |
76a5e5d2 SC |
44 | #include "wx/mac/private.h" |
45 | ||
a2b77260 | 46 | #include "MoreFilesX.h" |
0207122d SC |
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(); | |
a2b77260 SC |
72 | |
73 | void Close() ; | |
0207122d SC |
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 | ||
f4ac0693 GD |
80 | const wxString& GetName() const { return m_dirname; } |
81 | ||
0207122d | 82 | private: |
a2b77260 | 83 | FSIterator m_iterator ; |
0207122d SC |
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); | |
a2b77260 | 110 | m_iterator = NULL ; |
0207122d SC |
111 | } |
112 | ||
113 | wxDirData::~wxDirData() | |
114 | { | |
a2b77260 SC |
115 | Close() ; |
116 | } | |
117 | ||
118 | void wxDirData::Close() | |
119 | { | |
120 | if ( m_iterator ) | |
121 | { | |
122 | FSCloseIterator( m_iterator ) ; | |
123 | m_iterator = NULL ; | |
124 | } | |
0207122d SC |
125 | } |
126 | ||
127 | void wxDirData::Rewind() | |
128 | { | |
a2b77260 | 129 | Close() ; |
0207122d SC |
130 | } |
131 | ||
132 | bool wxDirData::Read(wxString *filename) | |
a2b77260 | 133 | { |
0207122d | 134 | wxString result; |
a2b77260 SC |
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 | } | |
e40298d5 | 150 | |
a2b77260 SC |
151 | wxString name ; |
152 | ||
153 | while( noErr == err ) | |
2d4e4f80 | 154 | { |
a2b77260 SC |
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 ) ; | |
2d4e4f80 | 165 | |
a2b77260 | 166 | if ( noErr != err ) |
2d4e4f80 | 167 | break ; |
a2b77260 SC |
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 ; | |
2d4e4f80 | 179 | |
7c19f4d2 RN |
180 | // its a dir and we don't want it |
181 | if ( (catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) && !(m_flags & wxDIR_DIRS) ) | |
182 | continue ; | |
a2b77260 | 183 | |
2d4e4f80 | 184 | // its a file but we don't want it |
a2b77260 | 185 | if ( (catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) == 0 && !(m_flags & wxDIR_FILES ) ) |
2d4e4f80 | 186 | continue ; |
a2b77260 | 187 | |
427ff662 | 188 | if ( m_filespec.IsEmpty() || m_filespec == wxT("*.*") || m_filespec == wxT("*") ) |
2d4e4f80 GD |
189 | { |
190 | } | |
a2b77260 | 191 | else if ( !wxMatchWild(m_filespec, name , FALSE) ) |
2d4e4f80 GD |
192 | { |
193 | continue ; | |
194 | } | |
195 | ||
196 | break ; | |
197 | } | |
198 | if ( err != noErr ) | |
199 | { | |
200 | return FALSE ; | |
201 | } | |
202 | ||
a2b77260 | 203 | *filename = name ; |
0207122d SC |
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); | |
facd6764 SC |
232 | |
233 | return TRUE; | |
0207122d SC |
234 | } |
235 | ||
236 | bool wxDir::IsOpened() const | |
237 | { | |
238 | return m_data != NULL; | |
239 | } | |
240 | ||
f4ac0693 GD |
241 | wxString wxDir::GetName() const |
242 | { | |
243 | wxString name; | |
244 | if ( m_data ) | |
245 | { | |
e40298d5 JS |
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 | } | |
f4ac0693 GD |
252 | } |
253 | ||
254 | return name; | |
255 | } | |
256 | ||
0207122d SC |
257 | wxDir::~wxDir() |
258 | { | |
f5bb2251 GD |
259 | if (M_DIR != NULL) { |
260 | delete M_DIR; | |
261 | m_data = NULL; | |
262 | } | |
0207122d SC |
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 | } |