]>
Commit | Line | Data |
---|---|---|
489468fe | 1 | ///////////////////////////////////////////////////////////////////////////// |
80fdcdb9 | 2 | // Name: src/osx/carbon/dirmac.cpp |
489468fe SC |
3 | // Purpose: wxDir implementation for Mac |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 08.12.99 | |
489468fe SC |
7 | // Copyright: (c) 1999 Stefan Csomor <csomor@advanced.ch> |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // For compilers that support precompilation, includes "wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #include "wx/dir.h" | |
27 | ||
28 | #ifndef WX_PRECOMP | |
29 | #include "wx/intl.h" | |
30 | #include "wx/log.h" | |
31 | #endif // PCH | |
32 | ||
489468fe | 33 | #include "wx/filename.h" |
1f0c8f31 | 34 | #include "wx/osx/private.h" |
489468fe SC |
35 | |
36 | // ---------------------------------------------------------------------------- | |
37 | // private classes | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
40 | // this class stores everything we need to enumerate the files | |
41 | class wxDirData | |
42 | { | |
43 | public: | |
44 | wxDirData(const wxString& dirname); | |
45 | ~wxDirData(); | |
46 | ||
47 | void Close() ; | |
48 | void SetFileSpec(const wxString& filespec) { m_filespec = filespec; } | |
49 | void SetFlags(int flags) { m_flags = flags; } | |
50 | ||
51 | bool Read(wxString *filename); // reads the next | |
52 | void Rewind() ; | |
53 | ||
54 | const wxString& GetName() const { return m_dirname; } | |
55 | ||
56 | private: | |
57 | FSIterator m_iterator ; | |
58 | ||
59 | wxString m_dirname; | |
60 | wxString m_filespec; | |
61 | ||
62 | int m_flags; | |
63 | }; | |
64 | ||
65 | // ============================================================================ | |
66 | // implementation | |
67 | // ============================================================================ | |
68 | ||
69 | // ---------------------------------------------------------------------------- | |
70 | // wxDirData | |
71 | // ---------------------------------------------------------------------------- | |
72 | ||
73 | wxDirData::wxDirData(const wxString& dirname) | |
74 | : m_dirname(dirname) | |
75 | { | |
76 | // throw away the trailing slashes | |
77 | size_t n = m_dirname.length(); | |
9a83f860 | 78 | wxCHECK_RET( n, wxT("empty dir name in wxDir") ); |
489468fe SC |
79 | |
80 | while ( n > 0 && wxIsPathSeparator(m_dirname[--n]) ) | |
81 | ; | |
82 | ||
83 | m_dirname.Truncate(n + 1); | |
84 | m_iterator = NULL ; | |
85 | } | |
86 | ||
87 | wxDirData::~wxDirData() | |
88 | { | |
89 | Close() ; | |
90 | } | |
91 | ||
92 | void wxDirData::Close() | |
93 | { | |
94 | if ( m_iterator ) | |
95 | { | |
96 | FSCloseIterator( m_iterator ) ; | |
97 | m_iterator = NULL ; | |
98 | } | |
99 | } | |
100 | ||
101 | void wxDirData::Rewind() | |
102 | { | |
103 | Close() ; | |
104 | } | |
105 | ||
106 | bool wxDirData::Read(wxString *filename) | |
107 | { | |
108 | wxString result; | |
109 | OSStatus err = noErr ; | |
110 | if ( NULL == m_iterator ) | |
111 | { | |
112 | FSRef dirRef; | |
113 | err = wxMacPathToFSRef( m_dirname , &dirRef ) ; | |
114 | if ( err == noErr ) | |
115 | { | |
116 | err = FSOpenIterator(&dirRef, kFSIterateFlat, &m_iterator); | |
117 | } | |
118 | if ( err ) | |
119 | { | |
120 | Close() ; | |
121 | return false ; | |
122 | } | |
123 | } | |
124 | ||
125 | wxString name ; | |
126 | wxString lowerfilespec = m_filespec.Lower(); | |
127 | ||
128 | while( noErr == err ) | |
129 | { | |
130 | HFSUniStr255 uniname ; | |
131 | FSRef fileRef; | |
132 | FSCatalogInfo catalogInfo; | |
133 | ItemCount fetched = 0; | |
134 | ||
135 | err = FSGetCatalogInfoBulk( m_iterator, 1, &fetched, NULL, kFSCatInfoNodeFlags | kFSCatInfoFinderInfo , &catalogInfo , &fileRef, NULL, &uniname ); | |
03647350 VZ |
136 | |
137 | // expected error codes | |
138 | ||
489468fe SC |
139 | if ( errFSNoMoreItems == err ) |
140 | return false ; | |
141 | if ( afpAccessDenied == err ) | |
142 | return false ; | |
143 | ||
144 | if ( noErr != err ) | |
145 | break ; | |
146 | ||
147 | name = wxMacHFSUniStrToString( &uniname ) ; | |
148 | wxString lowername = name.Lower(); | |
149 | ||
150 | if ( ( name == wxT(".") || name == wxT("..") ) && !(m_flags & wxDIR_DOTDOT) ) | |
151 | continue; | |
152 | ||
153 | if ( ( name[0U] == '.' ) && !(m_flags & wxDIR_HIDDEN ) ) | |
154 | continue ; | |
155 | ||
156 | if ( (((FileInfo*)&catalogInfo.finderInfo)->finderFlags & kIsInvisible ) && !(m_flags & wxDIR_HIDDEN ) ) | |
157 | continue ; | |
158 | ||
159 | // its a dir and we don't want it | |
160 | if ( (catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) && !(m_flags & wxDIR_DIRS) ) | |
161 | continue ; | |
162 | ||
163 | // its a file but we don't want it | |
164 | if ( (catalogInfo.nodeFlags & kFSNodeIsDirectoryMask) == 0 && !(m_flags & wxDIR_FILES ) ) | |
165 | continue ; | |
166 | ||
167 | if ( m_filespec.empty() || m_filespec == wxT("*.*") || m_filespec == wxT("*") ) | |
168 | { | |
169 | } | |
170 | else if ( !wxMatchWild(lowerfilespec, lowername , false) ) | |
171 | { | |
172 | continue ; | |
173 | } | |
174 | ||
175 | break ; | |
176 | } | |
177 | if ( err != noErr ) | |
178 | { | |
179 | return false ; | |
180 | } | |
181 | ||
182 | *filename = name ; | |
183 | return true; | |
184 | } | |
185 | ||
489468fe SC |
186 | // ---------------------------------------------------------------------------- |
187 | // wxDir construction/destruction | |
188 | // ---------------------------------------------------------------------------- | |
189 | ||
190 | wxDir::wxDir(const wxString& dirname) | |
191 | { | |
192 | m_data = NULL; | |
193 | ||
194 | (void)Open(dirname); | |
195 | } | |
196 | ||
197 | bool wxDir::Open(const wxString& dirname) | |
198 | { | |
199 | delete m_data; | |
200 | m_data = new wxDirData(dirname); | |
201 | ||
202 | return true; | |
203 | } | |
204 | ||
205 | bool wxDir::IsOpened() const | |
206 | { | |
207 | return m_data != NULL; | |
208 | } | |
209 | ||
210 | wxString wxDir::GetName() const | |
211 | { | |
212 | wxString name; | |
213 | if ( m_data ) | |
214 | { | |
215 | name = m_data->GetName(); | |
9a83f860 | 216 | if ( !name.empty() && (name.Last() == wxT('/')) ) |
489468fe SC |
217 | { |
218 | // chop off the last (back)slash | |
219 | name.Truncate(name.length() - 1); | |
220 | } | |
221 | } | |
222 | ||
223 | return name; | |
224 | } | |
225 | ||
226 | wxDir::~wxDir() | |
227 | { | |
5276b0a5 | 228 | wxDELETE(m_data); |
489468fe SC |
229 | } |
230 | ||
231 | // ---------------------------------------------------------------------------- | |
232 | // wxDir enumerating | |
233 | // ---------------------------------------------------------------------------- | |
234 | ||
235 | bool wxDir::GetFirst(wxString *filename, | |
236 | const wxString& filespec, | |
237 | int flags) const | |
238 | { | |
9a83f860 | 239 | wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") ); |
489468fe SC |
240 | |
241 | m_data->Rewind(); | |
242 | ||
243 | m_data->SetFileSpec(filespec); | |
244 | m_data->SetFlags(flags); | |
245 | ||
246 | return GetNext(filename); | |
247 | } | |
248 | ||
249 | bool wxDir::GetNext(wxString *filename) const | |
250 | { | |
9a83f860 | 251 | wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") ); |
489468fe | 252 | |
9a83f860 | 253 | wxCHECK_MSG( filename, false, wxT("bad pointer in wxDir::GetNext()") ); |
489468fe SC |
254 | |
255 | return m_data->Read(filename); | |
256 | } |