]> git.saurik.com Git - wxWidgets.git/blob - src/unix/dir.cpp
Refactor wxXRC to allow defining handlers outside of xrc library.
[wxWidgets.git] / src / unix / dir.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/dir.cpp
3 // Purpose: wxDir implementation for Unix/POSIX systems
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 08.12.99
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/intl.h"
29 #include "wx/log.h"
30 #endif // PCH
31
32 #include "wx/dir.h"
33 #include "wx/filefn.h" // for wxMatchWild
34 #include "wx/filename.h"
35
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39
40 #include <dirent.h>
41
42 // ----------------------------------------------------------------------------
43 // macros
44 // ----------------------------------------------------------------------------
45
46 #define M_DIR ((wxDirData *)m_data)
47
48 // ----------------------------------------------------------------------------
49 // private classes
50 // ----------------------------------------------------------------------------
51
52 // this class stores everything we need to enumerate the files
53 class wxDirData
54 {
55 public:
56 wxDirData(const wxString& dirname);
57 ~wxDirData();
58
59 bool IsOk() const { return m_dir != NULL; }
60
61 void SetFileSpec(const wxString& filespec) { m_filespec = filespec; }
62 void SetFlags(int flags) { m_flags = flags; }
63
64 void Rewind() { rewinddir(m_dir); }
65 bool Read(wxString *filename);
66
67 const wxString& GetName() const { return m_dirname; }
68
69 private:
70 DIR *m_dir;
71
72 wxString m_dirname;
73 wxString m_filespec;
74
75 int m_flags;
76 };
77
78 // ============================================================================
79 // implementation
80 // ============================================================================
81
82 // ----------------------------------------------------------------------------
83 // wxDirData
84 // ----------------------------------------------------------------------------
85
86 #if !defined( __VMS__ ) || ( __VMS_VER >= 70000000 )
87
88 wxDirData::wxDirData(const wxString& dirname)
89 : m_dirname(dirname)
90 {
91 m_dir = NULL;
92
93 // throw away the trailing slashes
94 size_t n = m_dirname.length();
95 wxCHECK_RET( n, wxT("empty dir name in wxDir") );
96
97 while ( n > 0 && m_dirname[--n] == '/' )
98 ;
99
100 m_dirname.Truncate(n + 1);
101
102 // do open the dir
103 m_dir = opendir(m_dirname.fn_str());
104 }
105
106 wxDirData::~wxDirData()
107 {
108 if ( m_dir )
109 {
110 if ( closedir(m_dir) != 0 )
111 {
112 wxLogLastError(wxT("closedir"));
113 }
114 }
115 }
116
117 bool wxDirData::Read(wxString *filename)
118 {
119 dirent *de = NULL; // just to silence compiler warnings
120 bool matches = false;
121
122 // speed up string concatenation in the loop a bit
123 wxString path = m_dirname;
124 path += wxT('/');
125 path.reserve(path.length() + 255);
126
127 wxString de_d_name;
128
129 while ( !matches )
130 {
131 de = readdir(m_dir);
132 if ( !de )
133 return false;
134
135 #if wxUSE_UNICODE
136 de_d_name = wxString(de->d_name, *wxConvFileName);
137 #else
138 de_d_name = de->d_name;
139 #endif
140
141 // don't return "." and ".." unless asked for
142 if ( de->d_name[0] == '.' &&
143 ((de->d_name[1] == '.' && de->d_name[2] == '\0') ||
144 (de->d_name[1] == '\0')) )
145 {
146 if ( !(m_flags & wxDIR_DOTDOT) )
147 continue;
148
149 // we found a valid match
150 break;
151 }
152
153 // check the type now: notice that we want to check the type of this
154 // path itself and not whatever it points to in case of a symlink
155 wxFileName fn = wxFileName::DirName(path + de_d_name);
156 fn.DontFollowLink();
157
158 if ( !(m_flags & wxDIR_FILES) && !fn.DirExists() )
159 {
160 // it's a file, but we don't want them
161 continue;
162 }
163 else if ( !(m_flags & wxDIR_DIRS) && fn.DirExists() )
164 {
165 // it's a dir, and we don't want it
166 continue;
167 }
168
169 // finally, check the name
170 if ( m_filespec.empty() )
171 {
172 matches = m_flags & wxDIR_HIDDEN ? true : de->d_name[0] != '.';
173 }
174 else
175 {
176 // test against the pattern
177 matches = wxMatchWild(m_filespec, de_d_name,
178 !(m_flags & wxDIR_HIDDEN));
179 }
180 }
181
182 *filename = de_d_name;
183
184 return true;
185 }
186
187 #else // old VMS (TODO)
188
189 wxDirData::wxDirData(const wxString& WXUNUSED(dirname))
190 {
191 wxFAIL_MSG(wxT("not implemented"));
192 }
193
194 wxDirData::~wxDirData()
195 {
196 }
197
198 bool wxDirData::Read(wxString * WXUNUSED(filename))
199 {
200 return false;
201 }
202
203 #endif // not or new VMS/old VMS
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 if ( !M_DIR->IsOk() )
222 {
223 delete M_DIR;
224 m_data = NULL;
225
226 return false;
227 }
228
229 return true;
230 }
231
232 bool wxDir::IsOpened() const
233 {
234 return m_data != NULL;
235 }
236
237 wxString wxDir::GetName() const
238 {
239 wxString name;
240 if ( m_data )
241 {
242 name = M_DIR->GetName();
243
244 // Notice that we need to check for length > 1 as we shouldn't remove
245 // the last slash from the root directory!
246 if ( name.length() > 1 && (name.Last() == wxT('/')) )
247 {
248 // chop off the last slash
249 name.RemoveLast();
250 }
251 }
252
253 return name;
254 }
255
256 void wxDir::Close()
257 {
258 if ( m_data )
259 {
260 delete m_data;
261 m_data = NULL;
262 }
263 }
264
265 // ----------------------------------------------------------------------------
266 // wxDir enumerating
267 // ----------------------------------------------------------------------------
268
269 bool wxDir::GetFirst(wxString *filename,
270 const wxString& filespec,
271 int flags) const
272 {
273 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
274
275 M_DIR->Rewind();
276
277 M_DIR->SetFileSpec(filespec);
278 M_DIR->SetFlags(flags);
279
280 return GetNext(filename);
281 }
282
283 bool wxDir::GetNext(wxString *filename) const
284 {
285 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
286
287 wxCHECK_MSG( filename, false, wxT("bad pointer in wxDir::GetNext()") );
288
289 return M_DIR->Read(filename);
290 }
291
292 bool wxDir::HasSubDirs(const wxString& spec) const
293 {
294 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
295
296 if ( spec.empty() )
297 {
298 // faster check for presence of any subdirectory: normally each subdir
299 // has a hard link to the parent directory and so, knowing that there
300 // are at least "." and "..", we have a subdirectory if and only if
301 // links number is > 2 - this is just a guess but it works fairly well
302 // in practice
303 //
304 // note that we may guess wrongly in one direction only: i.e. we may
305 // return true when there are no subdirectories but this is ok as the
306 // caller will learn it soon enough when it calls GetFirst(wxDIR)
307 // anyhow
308 wxStructStat stBuf;
309 if ( wxStat(M_DIR->GetName(), &stBuf) == 0 )
310 {
311 switch ( stBuf.st_nlink )
312 {
313 case 2:
314 // just "." and ".."
315 return false;
316
317 case 0:
318 case 1:
319 // weird filesystem, don't try to guess for it, use dumb
320 // method below
321 break;
322
323 default:
324 // assume we have subdirs - may turn out to be wrong if we
325 // have other hard links to this directory but it's not
326 // that bad as explained above
327 return true;
328 }
329 }
330 }
331
332 // just try to find first directory
333 wxString s;
334 return GetFirst(&s, spec, wxDIR_DIRS | wxDIR_HIDDEN);
335 }
336