]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/dirmac.cpp
printing to CW console only for debug builds
[wxWidgets.git] / src / mac / classic / dirmac.cpp
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 #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 wxDirExists()
38
39 #ifndef __DARWIN__
40 #include <windows.h>
41 #endif
42
43 #include "wx/mac/private.h"
44
45 #ifdef __DARWIN__
46 # include "MoreFilesX.h"
47 #else
48 # include "MoreFiles.h"
49 # include "MoreFilesExtras.h"
50 #endif
51
52 // ----------------------------------------------------------------------------
53 // constants
54 // ----------------------------------------------------------------------------
55
56 #ifndef MAX_PATH
57 #define MAX_PATH 260 // from VC++ headers
58 #endif
59
60 // ----------------------------------------------------------------------------
61 // macros
62 // ----------------------------------------------------------------------------
63
64 #define M_DIR ((wxDirData *)m_data)
65
66 // ----------------------------------------------------------------------------
67 // private classes
68 // ----------------------------------------------------------------------------
69
70 // this class stores everything we need to enumerate the files
71 class wxDirData
72 {
73 public:
74 wxDirData(const wxString& dirname);
75 ~wxDirData();
76
77 void SetFileSpec(const wxString& filespec) { m_filespec = filespec; }
78 void SetFlags(int flags) { m_flags = flags; }
79
80 bool Read(wxString *filename); // reads the next
81 void Rewind() ;
82
83 const wxString& GetName() const { return m_dirname; }
84 bool Ok() const { return m_ok; }
85
86 private:
87 CInfoPBRec m_CPB ;
88 wxInt16 m_index ;
89 long m_dirId ;
90 Str255 m_name ;
91 Boolean m_isDir ;
92
93 wxString m_dirname;
94 wxString m_filespec;
95
96 int m_flags;
97 bool m_ok;
98 };
99
100 // ============================================================================
101 // implementation
102 // ============================================================================
103
104 // ----------------------------------------------------------------------------
105 // wxDirData
106 // ----------------------------------------------------------------------------
107
108 wxDirData::wxDirData(const wxString& dirname)
109 : m_dirname(dirname)
110 {
111 m_ok = false;
112
113 OSErr err;
114
115 // throw away the trailing slashes
116 size_t n = m_dirname.length();
117 wxCHECK_RET( n, _T("empty dir name in wxDir") );
118
119 while ( n > 0 && wxIsPathSeparator(m_dirname[--n]) )
120 ;
121
122 m_dirname.Truncate(n + 1);
123
124 #ifdef __DARWIN__
125 FSRef theRef;
126
127 // get the FSRef associated with the POSIX path
128 err = FSPathMakeRef((const UInt8 *) m_dirname.c_str(), &theRef, NULL);
129 FSGetVRefNum(&theRef, &(m_CPB.hFileInfo.ioVRefNum));
130
131 err = FSGetNodeID( &theRef , &m_dirId , &m_isDir ) ;
132 #else
133 FSSpec fsspec ;
134
135 wxMacFilename2FSSpec( m_dirname , &fsspec ) ;
136 m_CPB.hFileInfo.ioVRefNum = fsspec.vRefNum ;
137
138 err = FSpGetDirectoryID( &fsspec , &m_dirId , &m_isDir ) ;
139 #endif
140 //wxASSERT_MSG( (err == noErr) || (err == nsvErr) , wxT("Error accessing directory " + m_dirname)) ;
141 if ( (err == noErr) || (err == nsvErr))
142 m_ok = true;
143 else
144 wxLogError(wxString(wxT("Error accessing directory ")) + m_dirname);
145
146 m_CPB.hFileInfo.ioNamePtr = m_name ;
147 m_index = 0 ;
148 }
149
150 wxDirData::~wxDirData()
151 {
152 }
153
154 void wxDirData::Rewind()
155 {
156 m_index = 0 ;
157 }
158
159 bool wxDirData::Read(wxString *filename)
160 {
161 if ( !m_isDir )
162 return false ;
163
164 wxString result;
165
166 short err = noErr ;
167
168 while ( err == noErr )
169 {
170 m_index++ ;
171 m_CPB.dirInfo.ioFDirIndex = m_index;
172 m_CPB.dirInfo.ioDrDirID = m_dirId; /* we need to do this every time */
173 err = PBGetCatInfoSync((CInfoPBPtr)&m_CPB);
174 if ( err != noErr )
175 break ;
176
177 // its hidden but we don't want it
178 if ( ( m_CPB.hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible ) && !(m_flags & wxDIR_HIDDEN) )
179 continue ;
180 #ifdef __DARWIN__
181 // under X, names that start with '.' are hidden
182 if ( ( m_name[1] == '.' ) && !(m_flags & wxDIR_HIDDEN) )
183 continue;
184 #endif
185 #if TARGET_CARBON
186 // under X thats the way the mounting points look like
187 if ( ( m_CPB.dirInfo.ioDrDirID == 0 ) && ( m_flags & wxDIR_DIRS) )
188 break ;
189 #endif
190 // we have a directory
191 if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) != 0 && (m_flags & wxDIR_DIRS) )
192 break ;
193
194 // its a file but we don't want it
195 if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) == 0 && !(m_flags & wxDIR_FILES ) )
196 continue ;
197
198 wxString file = wxMacMakeStringFromPascal( m_name ) ;
199 if ( m_filespec.empty() || m_filespec == wxT("*.*") || m_filespec == wxT("*") )
200 {
201 }
202 else if ( m_filespec.Length() > 1 && m_filespec.Left(1) == wxT("*") )
203 {
204 if ( file.Right( m_filespec.Length() - 1 ).Upper() != m_filespec.Mid(1).Upper() )
205 {
206 continue ;
207 }
208 }
209 else if ( m_filespec.Length() > 1 && m_filespec.Right(1) == wxT("*") )
210 {
211 if ( file.Left( m_filespec.Length() - 1 ).Upper() != m_filespec.Left( m_filespec.Length() - 1 ).Upper() )
212 {
213 continue ;
214 }
215 }
216 else if ( file.Upper() != m_filespec.Upper() )
217 {
218 continue ;
219 }
220
221 break ;
222 }
223 if ( err != noErr )
224 {
225 return false ;
226 }
227
228 *filename = wxMacMakeStringFromPascal( m_name ) ;
229
230 return true;
231 }
232
233 // ----------------------------------------------------------------------------
234 // wxDir helpers
235 // ----------------------------------------------------------------------------
236
237 /* static */
238 bool wxDir::Exists(const wxString& dir)
239 {
240 return wxDirExists(dir);
241 }
242
243 // ----------------------------------------------------------------------------
244 // wxDir construction/destruction
245 // ----------------------------------------------------------------------------
246
247 wxDir::wxDir(const wxString& dirname)
248 {
249 m_data = NULL;
250
251 (void)Open(dirname);
252 }
253
254 bool wxDir::Open(const wxString& dirname)
255 {
256 delete M_DIR;
257 m_data = new wxDirData(dirname);
258 if (m_data->Ok())
259 return true;
260 else
261 {
262 delete m_data;
263 m_data = NULL;
264 return false;
265 }
266 }
267
268 bool wxDir::IsOpened() const
269 {
270 return m_data != NULL;
271 }
272
273 wxString wxDir::GetName() const
274 {
275 wxString name;
276 if ( m_data )
277 {
278 name = M_DIR->GetName();
279 if ( !name.empty() && (name.Last() == _T('/')) )
280 {
281 // chop off the last (back)slash
282 name.Truncate(name.length() - 1);
283 }
284 }
285
286 return name;
287 }
288
289 wxDir::~wxDir()
290 {
291 if (M_DIR != NULL) {
292 delete M_DIR;
293 m_data = NULL;
294 }
295 }
296
297 // ----------------------------------------------------------------------------
298 // wxDir enumerating
299 // ----------------------------------------------------------------------------
300
301 bool wxDir::GetFirst(wxString *filename,
302 const wxString& filespec,
303 int flags) const
304 {
305 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
306
307 M_DIR->Rewind();
308
309 M_DIR->SetFileSpec(filespec);
310 M_DIR->SetFlags(flags);
311
312 return GetNext(filename);
313 }
314
315 bool wxDir::GetNext(wxString *filename) const
316 {
317 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
318
319 wxCHECK_MSG( filename, false, _T("bad pointer in wxDir::GetNext()") );
320
321 return M_DIR->Read(filename);
322 }