]>
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 license | |
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 | ||
39 | #ifndef __DARWIN__ | |
40 | #include <windows.h> | |
41 | #endif | |
42 | ||
43 | #include "wx/mac/private.h" | |
44 | ||
45 | #include "MoreFiles.h" | |
46 | #include "MoreFilesExtras.h" | |
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(); | |
72 | ||
73 | void SetFileSpec(const wxString& filespec) { m_filespec = filespec; } | |
74 | void SetFlags(int flags) { m_flags = flags; } | |
75 | ||
76 | bool Read(wxString *filename); // reads the next | |
77 | void Rewind() ; | |
78 | ||
79 | const wxString& GetName() const { return m_dirname; } | |
80 | ||
81 | private: | |
82 | CInfoPBRec m_CPB ; | |
83 | wxInt16 m_index ; | |
84 | long m_dirId ; | |
85 | Str255 m_name ; | |
86 | Boolean m_isDir ; | |
87 | ||
88 | wxString m_dirname; | |
89 | wxString m_filespec; | |
90 | ||
91 | int m_flags; | |
92 | }; | |
93 | ||
94 | // ============================================================================ | |
95 | // implementation | |
96 | // ============================================================================ | |
97 | ||
98 | // ---------------------------------------------------------------------------- | |
99 | // wxDirData | |
100 | // ---------------------------------------------------------------------------- | |
101 | ||
102 | wxDirData::wxDirData(const wxString& dirname) | |
103 | : m_dirname(dirname) | |
104 | { | |
105 | // throw away the trailing slashes | |
106 | size_t n = m_dirname.length(); | |
107 | wxCHECK_RET( n, _T("empty dir name in wxDir") ); | |
108 | ||
109 | while ( n > 0 && wxIsPathSeparator(m_dirname[--n]) ) | |
110 | ; | |
111 | ||
112 | m_dirname.Truncate(n + 1); | |
113 | ||
114 | FSSpec fsspec ; | |
115 | ||
116 | wxMacFilename2FSSpec( m_dirname , &fsspec ) ; | |
117 | m_CPB.hFileInfo.ioVRefNum = fsspec.vRefNum ; | |
118 | m_CPB.hFileInfo.ioNamePtr = m_name ; | |
119 | m_index = 0 ; | |
120 | ||
121 | OSErr err = FSpGetDirectoryID( &fsspec , &m_dirId , &m_isDir ) ; | |
122 | wxASSERT_MSG( err == noErr , "Error accessing directory") ; | |
123 | } | |
124 | ||
125 | wxDirData::~wxDirData() | |
126 | { | |
127 | } | |
128 | ||
129 | void wxDirData::Rewind() | |
130 | { | |
131 | m_index = 0 ; | |
132 | } | |
133 | ||
134 | bool wxDirData::Read(wxString *filename) | |
135 | { | |
136 | if ( !m_isDir ) | |
137 | return FALSE ; | |
138 | ||
139 | #if TARGET_CARBON | |
140 | char c_name[256] ; | |
141 | #endif | |
142 | wxString result; | |
143 | ||
144 | short err = noErr ; | |
145 | ||
146 | while ( err == noErr ) | |
147 | { | |
148 | m_index++ ; | |
149 | m_CPB.dirInfo.ioFDirIndex = m_index; | |
150 | m_CPB.dirInfo.ioDrDirID = m_dirId; /* we need to do this every time */ | |
151 | err = PBGetCatInfoSync((CInfoPBPtr)&m_CPB); | |
152 | if ( err != noErr ) | |
153 | break ; | |
154 | ||
155 | #if TARGET_CARBON | |
156 | p2cstrcpy( c_name, m_name ) ; | |
157 | strcpy( (char *)m_name, c_name); | |
158 | #else | |
159 | p2cstr( m_name ) ; | |
160 | #endif | |
161 | #if TARGET_CARBON | |
162 | // under X thats the way the mounting points look like | |
163 | if ( ( m_CPB.dirInfo.ioDrDirID == 0 ) && ( m_flags & wxDIR_DIRS) ) | |
164 | break ; | |
165 | #endif | |
166 | if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) != 0 && (m_flags & wxDIR_DIRS) ) // we have a directory | |
167 | break ; | |
168 | ||
169 | if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) == 0 && !(m_flags & wxDIR_FILES ) ) // its a file but we don't want it | |
170 | continue ; | |
171 | ||
172 | if ( ( m_CPB.hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible ) && !(m_flags & wxDIR_HIDDEN) ) // its hidden but we don't want it | |
173 | continue ; | |
174 | ||
175 | wxString file( m_name ) ; | |
176 | if ( m_filespec.IsEmpty() || m_filespec == "*.*" || m_filespec == "*" ) | |
177 | { | |
178 | } | |
179 | else if ( m_filespec.Length() > 1 && m_filespec.Left(1) =="*" ) | |
180 | { | |
181 | if ( file.Right( m_filespec.Length() - 1 ).Upper() != m_filespec.Mid(1).Upper() ) | |
182 | { | |
183 | continue ; | |
184 | } | |
185 | } | |
186 | else if ( m_filespec.Length() > 1 && m_filespec.Right(1) == "*" ) | |
187 | { | |
188 | if ( file.Left( m_filespec.Length() - 1 ).Upper() != m_filespec.Left( m_filespec.Length() - 1 ).Upper() ) | |
189 | { | |
190 | continue ; | |
191 | } | |
192 | } | |
193 | else if ( file.Upper() != m_filespec.Upper() ) | |
194 | { | |
195 | continue ; | |
196 | } | |
197 | ||
198 | break ; | |
199 | } | |
200 | if ( err != noErr ) | |
201 | { | |
202 | return FALSE ; | |
203 | } | |
204 | ||
205 | *filename = (char*) m_name ; | |
206 | ||
207 | return TRUE; | |
208 | } | |
209 | ||
210 | // ---------------------------------------------------------------------------- | |
211 | // wxDir helpers | |
212 | // ---------------------------------------------------------------------------- | |
213 | ||
214 | /* static */ | |
215 | bool wxDir::Exists(const wxString& dir) | |
216 | { | |
217 | return wxPathExists(dir); | |
218 | } | |
219 | ||
220 | // ---------------------------------------------------------------------------- | |
221 | // wxDir construction/destruction | |
222 | // ---------------------------------------------------------------------------- | |
223 | ||
224 | wxDir::wxDir(const wxString& dirname) | |
225 | { | |
226 | m_data = NULL; | |
227 | ||
228 | (void)Open(dirname); | |
229 | } | |
230 | ||
231 | bool wxDir::Open(const wxString& dirname) | |
232 | { | |
233 | delete M_DIR; | |
234 | m_data = new wxDirData(dirname); | |
235 | ||
236 | return TRUE; | |
237 | } | |
238 | ||
239 | bool wxDir::IsOpened() const | |
240 | { | |
241 | return m_data != NULL; | |
242 | } | |
243 | ||
244 | wxString wxDir::GetName() const | |
245 | { | |
246 | wxString name; | |
247 | if ( m_data ) | |
248 | { | |
249 | name = M_DIR->GetName(); | |
250 | if ( !name.empty() && (name.Last() == _T('/')) ) | |
251 | { | |
252 | // chop off the last (back)slash | |
253 | name.Truncate(name.length() - 1); | |
254 | } | |
255 | } | |
256 | ||
257 | return name; | |
258 | } | |
259 | ||
260 | wxDir::~wxDir() | |
261 | { | |
262 | if (M_DIR != NULL) { | |
263 | delete M_DIR; | |
264 | m_data = NULL; | |
265 | } | |
266 | } | |
267 | ||
268 | // ---------------------------------------------------------------------------- | |
269 | // wxDir enumerating | |
270 | // ---------------------------------------------------------------------------- | |
271 | ||
272 | bool wxDir::GetFirst(wxString *filename, | |
273 | const wxString& filespec, | |
274 | int flags) const | |
275 | { | |
276 | wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") ); | |
277 | ||
278 | M_DIR->Rewind(); | |
279 | ||
280 | M_DIR->SetFileSpec(filespec); | |
281 | M_DIR->SetFlags(flags); | |
282 | ||
283 | return GetNext(filename); | |
284 | } | |
285 | ||
286 | bool wxDir::GetNext(wxString *filename) const | |
287 | { | |
288 | wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") ); | |
289 | ||
290 | wxCHECK_MSG( filename, FALSE, _T("bad pointer in wxDir::GetNext()") ); | |
291 | ||
292 | return M_DIR->Read(filename); | |
293 | } |