]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dir.cpp
adapted to mac
[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 if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) != 0 && (m_flags & wxDIR_DIRS) ) // we have a directory
151 break ;
152
153 if ( ( m_CPB.dirInfo.ioFlAttrib & ioDirMask) == 0 && !(m_flags & wxDIR_FILES ) ) // its a file but we don't want it
154 continue ;
155
156 if ( ( m_CPB.hFileInfo.ioFlFndrInfo.fdFlags & kIsInvisible ) && !(m_flags & wxDIR_HIDDEN) ) // its hidden but we don't want it
157 continue ;
158
159 break ;
160 }
161 if ( err != noErr )
162 {
163 return FALSE ;
164 }
165 FSSpec spec ;
166
167 FSMakeFSSpecCompat(m_CPB.hFileInfo.ioVRefNum, m_dirId, m_name,&spec) ;
168
169 *filename = wxMacFSSpec2UnixFilename( &spec ) ;
170
171 return TRUE;
172 }
173
174 // ----------------------------------------------------------------------------
175 // wxDir helpers
176 // ----------------------------------------------------------------------------
177
178 /* static */
179 bool wxDir::Exists(const wxString& dir)
180 {
181 return wxPathExists(dir);
182 }
183
184 // ----------------------------------------------------------------------------
185 // wxDir construction/destruction
186 // ----------------------------------------------------------------------------
187
188 wxDir::wxDir(const wxString& dirname)
189 {
190 m_data = NULL;
191
192 (void)Open(dirname);
193 }
194
195 bool wxDir::Open(const wxString& dirname)
196 {
197 delete M_DIR;
198 m_data = new wxDirData(dirname);
199
200 return TRUE;
201 }
202
203 bool wxDir::IsOpened() const
204 {
205 return m_data != NULL;
206 }
207
208 wxDir::~wxDir()
209 {
210 delete M_DIR;
211 }
212
213 // ----------------------------------------------------------------------------
214 // wxDir enumerating
215 // ----------------------------------------------------------------------------
216
217 bool wxDir::GetFirst(wxString *filename,
218 const wxString& filespec,
219 int flags) const
220 {
221 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
222
223 M_DIR->Rewind();
224
225 M_DIR->SetFileSpec(filespec);
226 M_DIR->SetFlags(flags);
227
228 return GetNext(filename);
229 }
230
231 bool wxDir::GetNext(wxString *filename) const
232 {
233 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
234
235 wxCHECK_MSG( filename, FALSE, _T("bad pointer in wxDir::GetNext()") );
236
237 return M_DIR->Read(filename);
238 }