1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/dircmn.cpp
3 // Purpose: wxDir methods common to all implementations
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/string.h"
31 #include "wx/filefn.h"
32 #include "wx/arrstr.h"
36 #include "wx/filename.h"
38 // ============================================================================
40 // ============================================================================
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
47 wxDirTraverser::OnOpenError(const wxString
& WXUNUSED(dirname
))
52 // ----------------------------------------------------------------------------
53 // wxDir::HasFiles() and HasSubDirs()
54 // ----------------------------------------------------------------------------
56 // dumb generic implementation
58 bool wxDir::HasFiles(const wxString
& spec
) const
61 return GetFirst(&s
, spec
, wxDIR_FILES
| wxDIR_HIDDEN
);
64 // we have a (much) faster version for Unix
65 #if (defined(__CYGWIN__) && defined(__WINDOWS__)) || !defined(__UNIX_LIKE__) || defined(__EMX__) || defined(__WINE__)
67 bool wxDir::HasSubDirs(const wxString
& spec
) const
70 return GetFirst(&s
, spec
, wxDIR_DIRS
| wxDIR_HIDDEN
);
75 // ----------------------------------------------------------------------------
76 // wxDir::GetNameWithSep()
77 // ----------------------------------------------------------------------------
79 wxString
wxDir::GetNameWithSep() const
81 // Note that for historical reasons (i.e. because GetName() was there
82 // first) we implement this one in terms of GetName() even though it might
83 // actually make more sense to reverse this logic.
85 wxString name
= GetName();
88 // Notice that even though GetName() isn't supposed to return the
89 // separator, it can still be present for the root directory name.
90 if ( name
.Last() != wxFILE_SEP_PATH
)
91 name
+= wxFILE_SEP_PATH
;
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 size_t wxDir::Traverse(wxDirTraverser
& sink
,
102 const wxString
& filespec
,
105 wxCHECK_MSG( IsOpened(), (size_t)-1,
106 wxT("dir must be opened before traversing it") );
108 // the total number of files found
111 // the name of this dir with path delimiter at the end
112 const wxString prefix
= GetNameWithSep();
114 // first, recurse into subdirs
115 if ( flags
& wxDIR_DIRS
)
118 for ( bool cont
= GetFirst(&dirname
, wxEmptyString
, wxDIR_DIRS
| (flags
& wxDIR_HIDDEN
) );
120 cont
= cont
&& GetNext(&dirname
) )
122 const wxString fulldirname
= prefix
+ dirname
;
124 switch ( sink
.OnDir(fulldirname
) )
127 wxFAIL_MSG(wxT("unexpected OnDir() return value") );
138 // don't give the error messages for the directories
139 // which we can't open: there can be all sorts of good
140 // reason for this (e.g. insufficient privileges) and
141 // this shouldn't be treated as an error -- instead
142 // let the user code decide what to do
147 ok
= subdir
.Open(fulldirname
);
150 // ask the user code what to do
152 switch ( sink
.OnOpenError(fulldirname
) )
155 wxFAIL_MSG(wxT("unexpected OnOpenError() return value") );
178 nFiles
+= subdir
.Traverse(sink
, filespec
, flags
);
190 // now enum our own files
191 if ( flags
& wxDIR_FILES
)
193 flags
&= ~wxDIR_DIRS
;
196 bool cont
= GetFirst(&filename
, filespec
, flags
);
199 wxDirTraverseResult res
= sink
.OnFile(prefix
+ filename
);
200 if ( res
== wxDIR_STOP
)
203 wxASSERT_MSG( res
== wxDIR_CONTINUE
,
204 wxT("unexpected OnFile() return value") );
208 cont
= GetNext(&filename
);
215 // ----------------------------------------------------------------------------
216 // wxDir::GetAllFiles()
217 // ----------------------------------------------------------------------------
219 class wxDirTraverserSimple
: public wxDirTraverser
222 wxDirTraverserSimple(wxArrayString
& files
) : m_files(files
) { }
224 virtual wxDirTraverseResult
OnFile(const wxString
& filename
)
226 m_files
.push_back(filename
);
227 return wxDIR_CONTINUE
;
230 virtual wxDirTraverseResult
OnDir(const wxString
& WXUNUSED(dirname
))
232 return wxDIR_CONTINUE
;
236 wxArrayString
& m_files
;
238 wxDECLARE_NO_COPY_CLASS(wxDirTraverserSimple
);
242 size_t wxDir::GetAllFiles(const wxString
& dirname
,
243 wxArrayString
*files
,
244 const wxString
& filespec
,
247 wxCHECK_MSG( files
, (size_t)-1, wxT("NULL pointer in wxDir::GetAllFiles") );
252 if ( dir
.IsOpened() )
254 wxDirTraverserSimple
traverser(*files
);
256 nFiles
+= dir
.Traverse(traverser
, filespec
, flags
);
262 // ----------------------------------------------------------------------------
263 // wxDir::FindFirst()
264 // ----------------------------------------------------------------------------
266 class wxDirTraverserFindFirst
: public wxDirTraverser
269 wxDirTraverserFindFirst() { }
271 virtual wxDirTraverseResult
OnFile(const wxString
& filename
)
277 virtual wxDirTraverseResult
OnDir(const wxString
& WXUNUSED(dirname
))
279 return wxDIR_CONTINUE
;
282 const wxString
& GetFile() const
290 wxDECLARE_NO_COPY_CLASS(wxDirTraverserFindFirst
);
294 wxString
wxDir::FindFirst(const wxString
& dirname
,
295 const wxString
& filespec
,
299 if ( dir
.IsOpened() )
301 wxDirTraverserFindFirst traverser
;
303 dir
.Traverse(traverser
, filespec
, flags
| wxDIR_FILES
);
304 return traverser
.GetFile();
307 return wxEmptyString
;
311 // ----------------------------------------------------------------------------
312 // wxDir::GetTotalSize()
313 // ----------------------------------------------------------------------------
317 class wxDirTraverserSumSize
: public wxDirTraverser
320 wxDirTraverserSumSize() { }
322 virtual wxDirTraverseResult
OnFile(const wxString
& filename
)
324 // wxFileName::GetSize won't use this class again as
325 // we're passing it a file and not a directory;
326 // thus we are sure to avoid an endless loop
327 wxULongLong sz
= wxFileName::GetSize(filename
);
329 if (sz
== wxInvalidSize
)
331 // if the GetSize() failed (this can happen because e.g. a
332 // file is locked by another process), we can proceed but
333 // we need to at least warn the user that the resulting
334 // final size could be not reliable (if e.g. the locked
335 // file is very big).
336 m_skippedFiles
.Add(filename
);
337 return wxDIR_CONTINUE
;
341 return wxDIR_CONTINUE
;
344 virtual wxDirTraverseResult
OnDir(const wxString
& WXUNUSED(dirname
))
346 return wxDIR_CONTINUE
;
349 wxULongLong
GetTotalSize() const
351 const wxArrayString
& GetSkippedFiles() const
352 { return m_skippedFiles
; }
356 wxArrayString m_skippedFiles
;
359 wxULongLong
wxDir::GetTotalSize(const wxString
&dirname
, wxArrayString
*filesSkipped
)
361 if (!wxDirExists(dirname
))
362 return wxInvalidSize
;
364 // to get the size of this directory and its contents we need
365 // to recursively walk it...
367 if ( !dir
.IsOpened() )
368 return wxInvalidSize
;
370 wxDirTraverserSumSize traverser
;
371 if (dir
.Traverse(traverser
) == (size_t)-1 )
372 return wxInvalidSize
;
375 *filesSkipped
= traverser
.GetSkippedFiles();
377 return traverser
.GetTotalSize();
380 #endif // wxUSE_LONGLONG
382 // ----------------------------------------------------------------------------
384 // ----------------------------------------------------------------------------
387 bool wxDir::Exists(const wxString
& dir
)
389 return wxFileName::DirExists(dir
);
393 bool wxDir::Make(const wxString
&dir
, int perm
, int flags
)
395 return wxFileName::Mkdir(dir
, perm
, flags
);
399 bool wxDir::Remove(const wxString
&dir
, int flags
)
401 return wxFileName::Rmdir(dir
, flags
);