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