]> git.saurik.com Git - wxWidgets.git/blame - src/unix/dir.cpp
Replaced 'InlineHelp' property attribute with 'Hint'; Use SetHint() wxTextCtrl and...
[wxWidgets.git] / src / unix / dir.cpp
CommitLineData
1944c6bd
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: 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>
65571936 9// Licence: wxWindows licence
1944c6bd
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
1944c6bd
VZ
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
35#include <sys/types.h>
1357a7dd
VZ
36#include <sys/stat.h>
37#include <unistd.h>
1944c6bd
VZ
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
52class wxDirData
53{
54public:
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
35332784
VZ
66 const wxString& GetName() const { return m_dirname; }
67
1944c6bd
VZ
68private:
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
87wxDirData::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();
9a83f860 94 wxCHECK_RET( n, wxT("empty dir name in wxDir") );
1944c6bd
VZ
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
105wxDirData::~wxDirData()
106{
107 if ( m_dir )
108 {
109 if ( closedir(m_dir) != 0 )
110 {
9a83f860 111 wxLogLastError(wxT("closedir"));
1944c6bd
VZ
112 }
113 }
114}
115
116bool wxDirData::Read(wxString *filename)
117{
d3b9f782 118 dirent *de = NULL; // just to silence compiler warnings
da865fdd 119 bool matches = false;
1944c6bd 120
b0d36402
VZ
121 // speed up string concatenation in the loop a bit
122 wxString path = m_dirname;
9a83f860 123 path += wxT('/');
b0d36402 124 path.reserve(path.length() + 255);
da865fdd 125
e6ccaf1a 126 wxString de_d_name;
b0d36402 127
1944c6bd
VZ
128 while ( !matches )
129 {
130 de = readdir(m_dir);
131 if ( !de )
da865fdd
WS
132 return false;
133
e6ccaf1a 134#if wxUSE_UNICODE
86501081 135 de_d_name = wxString(de->d_name, *wxConvFileName);
e6ccaf1a
RR
136#else
137 de_d_name = de->d_name;
da865fdd 138#endif
1944c6bd
VZ
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;
b0d36402
VZ
147
148 // we found a valid match
149 break;
1944c6bd
VZ
150 }
151
152 // check the type now
e6ccaf1a 153 if ( !(m_flags & wxDIR_FILES) && !wxDir::Exists(path + de_d_name) )
1944c6bd
VZ
154 {
155 // it's a file, but we don't want them
156 continue;
157 }
e6ccaf1a 158 else if ( !(m_flags & wxDIR_DIRS) && wxDir::Exists(path + de_d_name) )
1944c6bd
VZ
159 {
160 // it's a dir, and we don't want it
161 continue;
162 }
163
164 // finally, check the name
b0d36402 165 if ( m_filespec.empty() )
1944c6bd 166 {
da865fdd 167 matches = m_flags & wxDIR_HIDDEN ? true : de->d_name[0] != '.';
1944c6bd
VZ
168 }
169 else
170 {
171 // test against the pattern
e6ccaf1a 172 matches = wxMatchWild(m_filespec, de_d_name,
1944c6bd
VZ
173 !(m_flags & wxDIR_HIDDEN));
174 }
175 }
176
e6ccaf1a 177 *filename = de_d_name;
1944c6bd 178
da865fdd 179 return true;
1944c6bd
VZ
180}
181
182#else // old VMS (TODO)
183
184wxDirData::wxDirData(const wxString& WXUNUSED(dirname))
185{
9a83f860 186 wxFAIL_MSG(wxT("not implemented"));
1944c6bd
VZ
187}
188
189wxDirData::~wxDirData()
190{
191}
192
193bool wxDirData::Read(wxString * WXUNUSED(filename))
194{
da865fdd 195 return false;
1944c6bd
VZ
196}
197
198#endif // not or new VMS/old VMS
199
200// ----------------------------------------------------------------------------
201// wxDir helpers
202// ----------------------------------------------------------------------------
203
204/* static */
205bool wxDir::Exists(const wxString& dir)
206{
da865fdd 207 return wxDirExists(dir);
1944c6bd
VZ
208}
209
210// ----------------------------------------------------------------------------
211// wxDir construction/destruction
212// ----------------------------------------------------------------------------
213
214wxDir::wxDir(const wxString& dirname)
215{
216 m_data = NULL;
217
218 (void)Open(dirname);
219}
220
221bool wxDir::Open(const wxString& dirname)
222{
223 delete M_DIR;
224 m_data = new wxDirData(dirname);
225
226 if ( !M_DIR->IsOk() )
227 {
1944c6bd
VZ
228 delete M_DIR;
229 m_data = NULL;
230
da865fdd 231 return false;
1944c6bd
VZ
232 }
233
da865fdd 234 return true;
1944c6bd
VZ
235}
236
237bool wxDir::IsOpened() const
238{
239 return m_data != NULL;
240}
241
35332784
VZ
242wxString wxDir::GetName() const
243{
244 wxString name;
245 if ( m_data )
246 {
247 name = M_DIR->GetName();
9a83f860 248 if ( !name.empty() && (name.Last() == wxT('/')) )
35332784
VZ
249 {
250 // chop off the last (back)slash
251 name.Truncate(name.length() - 1);
252 }
253 }
254
255 return name;
256}
257
1944c6bd
VZ
258wxDir::~wxDir()
259{
260 delete M_DIR;
261}
262
263// ----------------------------------------------------------------------------
264// wxDir enumerating
265// ----------------------------------------------------------------------------
266
267bool wxDir::GetFirst(wxString *filename,
268 const wxString& filespec,
269 int flags) const
270{
9a83f860 271 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
1944c6bd
VZ
272
273 M_DIR->Rewind();
274
275 M_DIR->SetFileSpec(filespec);
276 M_DIR->SetFlags(flags);
277
278 return GetNext(filename);
279}
280
281bool wxDir::GetNext(wxString *filename) const
282{
9a83f860 283 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
1944c6bd 284
9a83f860 285 wxCHECK_MSG( filename, false, wxT("bad pointer in wxDir::GetNext()") );
1944c6bd
VZ
286
287 return M_DIR->Read(filename);
288}
1357a7dd 289
106dcc2c 290bool wxDir::HasSubDirs(const wxString& spec) const
1357a7dd 291{
9a83f860 292 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
1357a7dd
VZ
293
294 if ( spec.empty() )
295 {
296 // faster check for presence of any subdirectory: normally each subdir
297 // has a hard link to the parent directory and so, knowing that there
298 // are at least "." and "..", we have a subdirectory if and only if
299 // links number is > 2 - this is just a guess but it works fairly well
300 // in practice
301 //
302 // note that we may guess wrongly in one direction only: i.e. we may
303 // return true when there are no subdirectories but this is ok as the
304 // caller will learn it soon enough when it calls GetFirst(wxDIR)
305 // anyhow
306 wxStructStat stBuf;
766fc092 307 if ( wxStat(M_DIR->GetName(), &stBuf) == 0 )
1357a7dd
VZ
308 {
309 switch ( stBuf.st_nlink )
310 {
311 case 2:
312 // just "." and ".."
da865fdd 313 return false;
1357a7dd
VZ
314
315 case 0:
316 case 1:
317 // weird filesystem, don't try to guess for it, use dumb
318 // method below
319 break;
320
321 default:
322 // assume we have subdirs - may turn out to be wrong if we
323 // have other hard links to this directory but it's not
324 // that bad as explained above
da865fdd 325 return true;
1357a7dd
VZ
326 }
327 }
328 }
329
330 // just try to find first directory
331 wxString s;
332 return GetFirst(&s, spec, wxDIR_DIRS | wxDIR_HIDDEN);
333}
334