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