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