]>
Commit | Line | Data |
---|---|---|
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 | // 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" | |
33 | #include "wx/filefn.h" // for wxDirExists() | |
34 | ||
35 | #ifndef __DARWIN__ | |
36 | #include <windows.h> | |
37 | #endif | |
38 | ||
39 | #include "wx/filename.h" | |
40 | #include "wx/mac/private.h" | |
41 | ||
42 | #include "MoreFilesX.h" | |
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(); | |
68 | ||
69 | void Close() ; | |
70 | void SetFileSpec(const wxString& filespec) { m_filespec = filespec; } | |
71 | void SetFlags(int flags) { m_flags = flags; } | |
72 | ||
73 | bool Read(wxString *filename); // reads the next | |
74 | void Rewind() ; | |
75 | ||
76 | const wxString& GetName() const { return m_dirname; } | |
77 | ||
78 | private: | |
79 | FSIterator m_iterator ; | |
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); | |
106 | m_iterator = NULL ; | |
107 | } | |
108 | ||
109 | wxDirData::~wxDirData() | |
110 | { | |
111 | Close() ; | |
112 | } | |
113 | ||
114 | void wxDirData::Close() | |
115 | { | |
116 | if ( m_iterator ) | |
117 | { | |
118 | FSCloseIterator( m_iterator ) ; | |
119 | m_iterator = NULL ; | |
120 | } | |
121 | } | |
122 | ||
123 | void wxDirData::Rewind() | |
124 | { | |
125 | Close() ; | |
126 | } | |
127 | ||
128 | bool wxDirData::Read(wxString *filename) | |
129 | { | |
130 | wxString result; | |
131 | OSStatus err = noErr ; | |
132 | if ( NULL == m_iterator ) | |
133 | { | |
134 | FSRef dirRef; | |
135 | err = wxMacPathToFSRef( m_dirname , &dirRef ) ; | |
136 | if ( err == noErr ) | |
137 | { | |
138 | err = FSOpenIterator(&dirRef, kFSIterateFlat, &m_iterator); | |
139 | } | |
140 | if ( err ) | |
141 | { | |
142 | Close() ; | |
143 | return false ; | |
144 | } | |
145 | } | |
146 | ||
147 | wxString name ; | |
148 | ||
149 | while( noErr == err ) | |
150 | { | |
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 ; | |
159 | ||
160 | wxASSERT( noErr == err ) ; | |
161 | ||
162 | if ( noErr != err ) | |
163 | break ; | |
164 | ||
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 ; | |
175 | ||
176 | // its a dir and we don't want it | |
177 | if ( (catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) && !(m_flags & wxDIR_DIRS) ) | |
178 | continue ; | |
179 | ||
180 | // its a file but we don't want it | |
181 | if ( (catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) == 0 && !(m_flags & wxDIR_FILES ) ) | |
182 | continue ; | |
183 | ||
184 | if ( m_filespec.empty() || m_filespec == wxT("*.*") || m_filespec == wxT("*") ) | |
185 | { | |
186 | } | |
187 | else if ( !wxMatchWild(m_filespec, name , false) ) | |
188 | { | |
189 | continue ; | |
190 | } | |
191 | ||
192 | break ; | |
193 | } | |
194 | if ( err != noErr ) | |
195 | { | |
196 | return false ; | |
197 | } | |
198 | ||
199 | *filename = name ; | |
200 | return true; | |
201 | } | |
202 | ||
203 | // ---------------------------------------------------------------------------- | |
204 | // wxDir helpers | |
205 | // ---------------------------------------------------------------------------- | |
206 | ||
207 | /* static */ | |
208 | bool wxDir::Exists(const wxString& dir) | |
209 | { | |
210 | return wxDirExists(dir); | |
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); | |
228 | ||
229 | return true; | |
230 | } | |
231 | ||
232 | bool wxDir::IsOpened() const | |
233 | { | |
234 | return m_data != NULL; | |
235 | } | |
236 | ||
237 | wxString wxDir::GetName() const | |
238 | { | |
239 | wxString name; | |
240 | if ( m_data ) | |
241 | { | |
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 | } | |
248 | } | |
249 | ||
250 | return name; | |
251 | } | |
252 | ||
253 | wxDir::~wxDir() | |
254 | { | |
255 | if (M_DIR != NULL) { | |
256 | delete M_DIR; | |
257 | m_data = NULL; | |
258 | } | |
259 | } | |
260 | ||
261 | // ---------------------------------------------------------------------------- | |
262 | // wxDir enumerating | |
263 | // ---------------------------------------------------------------------------- | |
264 | ||
265 | bool wxDir::GetFirst(wxString *filename, | |
266 | const wxString& filespec, | |
267 | int flags) const | |
268 | { | |
269 | wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") ); | |
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 | { | |
281 | wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") ); | |
282 | ||
283 | wxCHECK_MSG( filename, false, _T("bad pointer in wxDir::GetNext()") ); | |
284 | ||
285 | return M_DIR->Read(filename); | |
286 | } |