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