]>
Commit | Line | Data |
---|---|---|
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 may want to check the type of | |
154 | // the path itself and not whatever it points to in case of a symlink | |
155 | wxFileName fn = wxFileName::DirName(path + de_d_name); | |
156 | if ( m_flags & wxDIR_NO_FOLLOW ) | |
157 | { | |
158 | fn.DontFollowLink(); | |
159 | } | |
160 | ||
161 | if ( !(m_flags & wxDIR_FILES) && !fn.DirExists() ) | |
162 | { | |
163 | // it's a file, but we don't want them | |
164 | continue; | |
165 | } | |
166 | else if ( !(m_flags & wxDIR_DIRS) && fn.DirExists() ) | |
167 | { | |
168 | // it's a dir, and we don't want it | |
169 | continue; | |
170 | } | |
171 | ||
172 | // finally, check the name | |
173 | if ( m_filespec.empty() ) | |
174 | { | |
175 | matches = m_flags & wxDIR_HIDDEN ? true : de->d_name[0] != '.'; | |
176 | } | |
177 | else | |
178 | { | |
179 | // test against the pattern | |
180 | matches = wxMatchWild(m_filespec, de_d_name, | |
181 | !(m_flags & wxDIR_HIDDEN)); | |
182 | } | |
183 | } | |
184 | ||
185 | *filename = de_d_name; | |
186 | ||
187 | return true; | |
188 | } | |
189 | ||
190 | #else // old VMS (TODO) | |
191 | ||
192 | wxDirData::wxDirData(const wxString& WXUNUSED(dirname)) | |
193 | { | |
194 | wxFAIL_MSG(wxT("not implemented")); | |
195 | } | |
196 | ||
197 | wxDirData::~wxDirData() | |
198 | { | |
199 | } | |
200 | ||
201 | bool wxDirData::Read(wxString * WXUNUSED(filename)) | |
202 | { | |
203 | return false; | |
204 | } | |
205 | ||
206 | #endif // not or new VMS/old VMS | |
207 | ||
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 | { | |
226 | delete M_DIR; | |
227 | m_data = NULL; | |
228 | ||
229 | return false; | |
230 | } | |
231 | ||
232 | return true; | |
233 | } | |
234 | ||
235 | bool wxDir::IsOpened() const | |
236 | { | |
237 | return m_data != NULL; | |
238 | } | |
239 | ||
240 | wxString wxDir::GetName() const | |
241 | { | |
242 | wxString name; | |
243 | if ( m_data ) | |
244 | { | |
245 | name = M_DIR->GetName(); | |
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('/')) ) | |
250 | { | |
251 | // chop off the last slash | |
252 | name.RemoveLast(); | |
253 | } | |
254 | } | |
255 | ||
256 | return name; | |
257 | } | |
258 | ||
259 | void wxDir::Close() | |
260 | { | |
261 | if ( m_data ) | |
262 | { | |
263 | delete m_data; | |
264 | m_data = NULL; | |
265 | } | |
266 | } | |
267 | ||
268 | // ---------------------------------------------------------------------------- | |
269 | // wxDir enumerating | |
270 | // ---------------------------------------------------------------------------- | |
271 | ||
272 | bool wxDir::GetFirst(wxString *filename, | |
273 | const wxString& filespec, | |
274 | int flags) const | |
275 | { | |
276 | wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") ); | |
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 | { | |
288 | wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") ); | |
289 | ||
290 | wxCHECK_MSG( filename, false, wxT("bad pointer in wxDir::GetNext()") ); | |
291 | ||
292 | return M_DIR->Read(filename); | |
293 | } | |
294 | ||
295 | bool wxDir::HasSubDirs(const wxString& spec) const | |
296 | { | |
297 | wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") ); | |
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; | |
312 | if ( wxStat(M_DIR->GetName(), &stBuf) == 0 ) | |
313 | { | |
314 | switch ( stBuf.st_nlink ) | |
315 | { | |
316 | case 2: | |
317 | // just "." and ".." | |
318 | return false; | |
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 | |
330 | return true; | |
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 |