Call wxThread::Create() from Run() automatically.
[wxWidgets.git] / src / msw / dir.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/dir.cpp
3 // Purpose: wxDir implementation for Win32
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
34 #ifdef __WINDOWS__
35 #include "wx/msw/private.h"
36 #endif
37
38 // ----------------------------------------------------------------------------
39 // define the types and functions used for file searching
40 // ----------------------------------------------------------------------------
41
42 namespace
43 {
44
45 typedef WIN32_FIND_DATA FIND_STRUCT;
46 typedef HANDLE FIND_DATA;
47 typedef DWORD FIND_ATTR;
48
49 inline FIND_DATA InitFindData()
50 {
51 return INVALID_HANDLE_VALUE;
52 }
53
54 inline bool IsFindDataOk(FIND_DATA fd)
55 {
56 return fd != INVALID_HANDLE_VALUE;
57 }
58
59 inline void FreeFindData(FIND_DATA fd)
60 {
61 if ( !::FindClose(fd) )
62 {
63 wxLogLastError(wxT("FindClose"));
64 }
65 }
66
67 const wxChar *GetNameFromFindData(const FIND_STRUCT *finddata)
68 {
69 return finddata->cFileName;
70 }
71
72 // Helper function checking that the contents of the given FIND_STRUCT really
73 // match our filter. We need to do it ourselves as native Windows functions
74 // apply the filter to both the long and the short names of the file, so
75 // something like "*.bar" matches "foo.bar.baz" too and not only "foo.bar", so
76 // we have to double check that we have a real match.
77 inline bool
78 CheckFoundMatch(const FIND_STRUCT* finddata, const wxString& filter)
79 {
80 return filter.empty() ||
81 wxString(GetNameFromFindData(finddata)).Matches(filter);
82 }
83
84 inline bool
85 FindNext(FIND_DATA fd, const wxString& filter, FIND_STRUCT *finddata)
86 {
87 for ( ;; )
88 {
89 if ( !::FindNextFile(fd, finddata) )
90 return false;
91
92 // If we did find something, check that it really matches.
93 if ( CheckFoundMatch(finddata, filter) )
94 return true;
95 }
96 }
97
98 inline FIND_DATA
99 FindFirst(const wxString& spec,
100 const wxString& filter,
101 FIND_STRUCT *finddata)
102 {
103 FIND_DATA fd = ::FindFirstFile(spec.t_str(), finddata);
104
105 // As in FindNext() above, we need to check that the file name we found
106 // really matches our filter and look for the next match if it doesn't.
107 if ( IsFindDataOk(fd) && !CheckFoundMatch(finddata, filter) )
108 {
109 if ( !FindNext(fd, filter, finddata) )
110 {
111 // As we return the invalid handle from here to indicate that we
112 // didn't find anything, close the one we initially received
113 // ourselves.
114 FreeFindData(fd);
115
116 return INVALID_HANDLE_VALUE;
117 }
118 }
119
120 return fd;
121 }
122
123 inline FIND_ATTR GetAttrFromFindData(FIND_STRUCT *finddata)
124 {
125 return finddata->dwFileAttributes;
126 }
127
128 inline bool IsDir(FIND_ATTR attr)
129 {
130 return (attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
131 }
132
133 inline bool IsHidden(FIND_ATTR attr)
134 {
135 return (attr & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) != 0;
136 }
137
138 } // anonymous namespace
139
140 // ----------------------------------------------------------------------------
141 // constants
142 // ----------------------------------------------------------------------------
143
144 #ifndef MAX_PATH
145 #define MAX_PATH 260 // from VC++ headers
146 #endif
147
148 // ----------------------------------------------------------------------------
149 // macros
150 // ----------------------------------------------------------------------------
151
152 #define M_DIR ((wxDirData *)m_data)
153
154 // ----------------------------------------------------------------------------
155 // private classes
156 // ----------------------------------------------------------------------------
157
158 // this class stores everything we need to enumerate the files
159 class wxDirData
160 {
161 public:
162 wxDirData(const wxString& dirname);
163 ~wxDirData();
164
165 void SetFileSpec(const wxString& filespec) { m_filespec = filespec; }
166 void SetFlags(int flags) { m_flags = flags; }
167
168 void Close();
169 void Rewind();
170 bool Read(wxString *filename);
171
172 const wxString& GetName() const { return m_dirname; }
173
174 private:
175 FIND_DATA m_finddata;
176
177 wxString m_dirname;
178 wxString m_filespec;
179
180 int m_flags;
181
182 wxDECLARE_NO_COPY_CLASS(wxDirData);
183 };
184
185 // ============================================================================
186 // implementation
187 // ============================================================================
188
189 // ----------------------------------------------------------------------------
190 // wxDirData
191 // ----------------------------------------------------------------------------
192
193 wxDirData::wxDirData(const wxString& dirname)
194 : m_dirname(dirname)
195 {
196 m_finddata = InitFindData();
197 }
198
199 wxDirData::~wxDirData()
200 {
201 Close();
202 }
203
204 void wxDirData::Close()
205 {
206 if ( IsFindDataOk(m_finddata) )
207 {
208 FreeFindData(m_finddata);
209
210 m_finddata = InitFindData();
211 }
212 }
213
214 void wxDirData::Rewind()
215 {
216 Close();
217 }
218
219 bool wxDirData::Read(wxString *filename)
220 {
221 bool first = false;
222
223 WIN32_FIND_DATA finddata;
224 #define PTR_TO_FINDDATA (&finddata)
225
226 if ( !IsFindDataOk(m_finddata) )
227 {
228 // open first
229 wxString filespec = m_dirname;
230 if ( !wxEndsWithPathSeparator(filespec) )
231 {
232 filespec += wxT('\\');
233 }
234 if ( !m_filespec )
235 filespec += wxT("*.*");
236 else
237 filespec += m_filespec;
238
239 m_finddata = FindFirst(filespec, m_filespec, PTR_TO_FINDDATA);
240
241 first = true;
242 }
243
244 if ( !IsFindDataOk(m_finddata) )
245 {
246 #ifdef __WIN32__
247 DWORD err = ::GetLastError();
248
249 if ( err != ERROR_FILE_NOT_FOUND && err != ERROR_NO_MORE_FILES )
250 {
251 wxLogSysError(err, _("Cannot enumerate files in directory '%s'"),
252 m_dirname.c_str());
253 }
254 #endif // __WIN32__
255 //else: not an error, just no (such) files
256
257 return false;
258 }
259
260 const wxChar *name;
261 FIND_ATTR attr;
262
263 for ( ;; )
264 {
265 if ( first )
266 {
267 first = false;
268 }
269 else
270 {
271 if ( !FindNext(m_finddata, m_filespec, PTR_TO_FINDDATA) )
272 {
273 #ifdef __WIN32__
274 DWORD err = ::GetLastError();
275
276 if ( err != ERROR_NO_MORE_FILES )
277 {
278 wxLogLastError(wxT("FindNext"));
279 }
280 #endif // __WIN32__
281 //else: not an error, just no more (such) files
282
283 return false;
284 }
285 }
286
287 name = GetNameFromFindData(PTR_TO_FINDDATA);
288 attr = GetAttrFromFindData(PTR_TO_FINDDATA);
289
290 // don't return "." and ".." unless asked for
291 if ( name[0] == wxT('.') &&
292 ((name[1] == wxT('.') && name[2] == wxT('\0')) ||
293 (name[1] == wxT('\0'))) )
294 {
295 if ( !(m_flags & wxDIR_DOTDOT) )
296 continue;
297 }
298
299 // check the type now
300 if ( !(m_flags & wxDIR_FILES) && !IsDir(attr) )
301 {
302 // it's a file, but we don't want them
303 continue;
304 }
305 else if ( !(m_flags & wxDIR_DIRS) && IsDir(attr) )
306 {
307 // it's a dir, and we don't want it
308 continue;
309 }
310
311 // finally, check whether it's a hidden file
312 if ( !(m_flags & wxDIR_HIDDEN) )
313 {
314 if ( IsHidden(attr) )
315 {
316 // it's a hidden file, skip it
317 continue;
318 }
319 }
320
321 *filename = name;
322
323 break;
324 }
325
326 return true;
327 }
328
329 // ----------------------------------------------------------------------------
330 // wxDir construction/destruction
331 // ----------------------------------------------------------------------------
332
333 wxDir::wxDir(const wxString& dirname)
334 {
335 m_data = NULL;
336
337 (void)Open(dirname);
338 }
339
340 bool wxDir::Open(const wxString& dirname)
341 {
342 delete M_DIR;
343
344 // The Unix code does a similar test
345 if (wxDirExists(dirname))
346 {
347 m_data = new wxDirData(dirname);
348
349 return true;
350 }
351 else
352 {
353 m_data = NULL;
354
355 return false;
356 }
357 }
358
359 bool wxDir::IsOpened() const
360 {
361 return m_data != NULL;
362 }
363
364 wxString wxDir::GetName() const
365 {
366 wxString name;
367 if ( m_data )
368 {
369 name = M_DIR->GetName();
370 if ( !name.empty() )
371 {
372 // bring to canonical Windows form
373 name.Replace(wxT("/"), wxT("\\"));
374
375 if ( name.Last() == wxT('\\') )
376 {
377 // chop off the last (back)slash
378 name.Truncate(name.length() - 1);
379 }
380 }
381 }
382
383 return name;
384 }
385
386 void wxDir::Close()
387 {
388 if ( m_data )
389 {
390 delete m_data;
391 m_data = NULL;
392 }
393 }
394
395 // ----------------------------------------------------------------------------
396 // wxDir enumerating
397 // ----------------------------------------------------------------------------
398
399 bool wxDir::GetFirst(wxString *filename,
400 const wxString& filespec,
401 int flags) const
402 {
403 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
404
405 M_DIR->Rewind();
406
407 M_DIR->SetFileSpec(filespec);
408 M_DIR->SetFlags(flags);
409
410 return GetNext(filename);
411 }
412
413 bool wxDir::GetNext(wxString *filename) const
414 {
415 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
416
417 wxCHECK_MSG( filename, false, wxT("bad pointer in wxDir::GetNext()") );
418
419 return M_DIR->Read(filename);
420 }
421
422 // ----------------------------------------------------------------------------
423 // wxGetDirectoryTimes: used by wxFileName::GetTimes()
424 // ----------------------------------------------------------------------------
425
426 #ifdef __WIN32__
427
428 extern bool
429 wxGetDirectoryTimes(const wxString& dirname,
430 FILETIME *ftAccess, FILETIME *ftCreate, FILETIME *ftMod)
431 {
432 #ifdef __WXWINCE__
433 // FindFirst() is going to fail
434 wxASSERT_MSG( !dirname.empty(),
435 wxT("incorrect directory name format in wxGetDirectoryTimes") );
436 #else
437 // FindFirst() is going to fail
438 wxASSERT_MSG( !dirname.empty() && dirname.Last() != wxT('\\'),
439 wxT("incorrect directory name format in wxGetDirectoryTimes") );
440 #endif
441
442 FIND_STRUCT fs;
443 FIND_DATA fd = FindFirst(dirname, wxEmptyString, &fs);
444 if ( !IsFindDataOk(fd) )
445 {
446 return false;
447 }
448
449 *ftAccess = fs.ftLastAccessTime;
450 *ftCreate = fs.ftCreationTime;
451 *ftMod = fs.ftLastWriteTime;
452
453 FindClose(fd);
454
455 return true;
456 }
457
458 #endif // __WIN32__
459