]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dirmac.cpp
corrected typo in conditional compilation for Darwin/Mac OS X
[wxWidgets.git] / src / mac / carbon / 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 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 #ifndef __DARWIN__
44 #include "MoreFiles.h"
45 #include "MoreFilesExtras.h"
46 #endif
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 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
114 wxMacFilename2FSSpec( m_dirname , &fsspec ) ;
115 m_CPB.hFileInfo.ioVRefNum = fsspec.vRefNum ;
116 m_CPB.hFileInfo.ioNamePtr = m_name ;
117 m_index = 0 ;
118
119 #ifdef __DARWIN__
120 // TODO: what are we supposed to do for Mac OS X
121 #else
122 FSpGetDirectoryID( &fsspec , &m_dirId , &m_isDir ) ;
123 #endif
124 }
125
126 wxDirData::~wxDirData()
127 {
128 }
129
130 void wxDirData::Rewind()
131 {
132 m_index = 0 ;
133 }
134
135 bool wxDirData::Read(wxString *filename)
136 {
137 if ( !m_isDir )
138 return FALSE ;
139
140 #if TARGET_CARBON
141 char c_name[256] ;
142 #endif
143 wxString result;
144
145 short err = noErr ;
146
147 while ( err == noErr )
148 {
149 m_index++ ;
150 m_CPB.dirInfo.ioFDirIndex = m_index;
151 m_CPB.dirInfo.ioDrDirID = m_dirId; /* we need to do this every time */
152 err = PBGetCatInfoSync((CInfoPBPtr)&m_CPB);
153 if ( err != noErr )
154 break ;
155
156 #if TARGET_CARBON
157 p2cstrcpy( c_name, m_name ) ;
158 strcpy( (char *)m_name, c_name);
159 #else
160 p2cstr( m_name ) ;
161 #endif
162 if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) != 0 && (m_flags & wxDIR_DIRS) ) // we have a directory
163 break ;
164
165 if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) == 0 && !(m_flags & wxDIR_FILES ) ) // its a file but we don't want it
166 continue ;
167
168 if ( ( m_CPB.hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible ) && !(m_flags & wxDIR_HIDDEN) ) // its hidden but we don't want it
169 continue ;
170
171 wxString file( m_name ) ;
172 if ( m_filespec.IsEmpty() || m_filespec == "*.*" )
173 {
174 }
175 else if ( m_filespec.Length() > 1 && m_filespec.Left(1) =="*" )
176 {
177 if ( file.Right( m_filespec.Length() - 1 ).Upper() != m_filespec.Mid(1).Upper() )
178 {
179 continue ;
180 }
181 }
182 else if ( m_filespec.Length() > 1 && m_filespec.Right(1) == "*" )
183 {
184 if ( file.Left( m_filespec.Length() - 1 ).Upper() != m_filespec.Left( m_filespec.Length() - 1 ).Upper() )
185 {
186 continue ;
187 }
188 }
189 else if ( file.Upper() != m_filespec.Upper() )
190 {
191 continue ;
192 }
193
194 break ;
195 }
196 if ( err != noErr )
197 {
198 return FALSE ;
199 }
200
201 *filename = (char*) m_name ;
202
203 return TRUE;
204 }
205
206 // ----------------------------------------------------------------------------
207 // wxDir helpers
208 // ----------------------------------------------------------------------------
209
210 /* static */
211 bool wxDir::Exists(const wxString& dir)
212 {
213 return wxPathExists(dir);
214 }
215
216 // ----------------------------------------------------------------------------
217 // wxDir construction/destruction
218 // ----------------------------------------------------------------------------
219
220 wxDir::wxDir(const wxString& dirname)
221 {
222 m_data = NULL;
223
224 (void)Open(dirname);
225 }
226
227 bool wxDir::Open(const wxString& dirname)
228 {
229 delete M_DIR;
230 m_data = new wxDirData(dirname);
231
232 return TRUE;
233 }
234
235 bool wxDir::IsOpened() const
236 {
237 return m_data != NULL;
238 }
239
240 wxDir::~wxDir()
241 {
242 delete M_DIR;
243 }
244
245 // ----------------------------------------------------------------------------
246 // wxDir enumerating
247 // ----------------------------------------------------------------------------
248
249 bool wxDir::GetFirst(wxString *filename,
250 const wxString& filespec,
251 int flags) const
252 {
253 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
254
255 M_DIR->Rewind();
256
257 M_DIR->SetFileSpec(filespec);
258 M_DIR->SetFlags(flags);
259
260 return GetNext(filename);
261 }
262
263 bool wxDir::GetNext(wxString *filename) const
264 {
265 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
266
267 wxCHECK_MSG( filename, FALSE, _T("bad pointer in wxDir::GetNext()") );
268
269 return M_DIR->Read(filename);
270 }