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 // License: 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"
37 // ============================================================================
39 // ============================================================================
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
46 wxDirTraverser::OnOpenError(const wxString
& WXUNUSED(dirname
))
51 // ----------------------------------------------------------------------------
52 // wxDir::HasFiles() and HasSubDirs()
53 // ----------------------------------------------------------------------------
55 // dumb generic implementation
57 bool wxDir::HasFiles(const wxString
& spec
)
60 return GetFirst(&s
, spec
, wxDIR_FILES
| wxDIR_HIDDEN
);
63 // we have a (much) faster version for Unix
64 #if (defined(__CYGWIN__) && defined(__WINDOWS__)) || !defined(__UNIX_LIKE__) || defined(__WXMAC__) || defined(__EMX__) || defined(__WINE__)
66 bool wxDir::HasSubDirs(const wxString
& spec
)
69 return GetFirst(&s
, spec
, wxDIR_DIRS
| wxDIR_HIDDEN
);
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 size_t wxDir::Traverse(wxDirTraverser
& sink
,
79 const wxString
& filespec
,
82 wxCHECK_MSG( IsOpened(), (size_t)-1,
83 _T("dir must be opened before traversing it") );
85 // the total number of files found
88 // the name of this dir with path delimiter at the end
89 wxString prefix
= GetName();
90 prefix
+= wxFILE_SEP_PATH
;
92 // first, recurse into subdirs
93 if ( flags
& wxDIR_DIRS
)
96 for ( bool cont
= GetFirst(&dirname
, wxEmptyString
, wxDIR_DIRS
| (flags
& wxDIR_HIDDEN
) );
98 cont
= cont
&& GetNext(&dirname
) )
100 const wxString fulldirname
= prefix
+ dirname
;
102 switch ( sink
.OnDir(fulldirname
) )
105 wxFAIL_MSG(_T("unexpected OnDir() return value") );
116 // don't give the error messages for the directories
117 // which we can't open: there can be all sorts of good
118 // reason for this (e.g. insufficient privileges) and
119 // this shouldn't be treated as an error -- instead
120 // let the user code decide what to do
125 ok
= subdir
.Open(fulldirname
);
128 // ask the user code what to do
130 switch ( sink
.OnOpenError(fulldirname
) )
133 wxFAIL_MSG(_T("unexpected OnOpenError() return value") );
156 nFiles
+= subdir
.Traverse(sink
, filespec
, flags
);
168 // now enum our own files
169 if ( flags
& wxDIR_FILES
)
171 flags
&= ~wxDIR_DIRS
;
174 bool cont
= GetFirst(&filename
, filespec
, flags
);
177 wxDirTraverseResult res
= sink
.OnFile(prefix
+ filename
);
178 if ( res
== wxDIR_STOP
)
181 wxASSERT_MSG( res
== wxDIR_CONTINUE
,
182 _T("unexpected OnFile() return value") );
186 cont
= GetNext(&filename
);
193 // ----------------------------------------------------------------------------
194 // wxDir::GetAllFiles()
195 // ----------------------------------------------------------------------------
197 class wxDirTraverserSimple
: public wxDirTraverser
200 wxDirTraverserSimple(wxArrayString
& files
) : m_files(files
) { }
202 virtual wxDirTraverseResult
OnFile(const wxString
& filename
)
204 m_files
.push_back(filename
);
205 return wxDIR_CONTINUE
;
208 virtual wxDirTraverseResult
OnDir(const wxString
& WXUNUSED(dirname
))
210 return wxDIR_CONTINUE
;
214 wxArrayString
& m_files
;
216 DECLARE_NO_COPY_CLASS(wxDirTraverserSimple
)
220 size_t wxDir::GetAllFiles(const wxString
& dirname
,
221 wxArrayString
*files
,
222 const wxString
& filespec
,
225 wxCHECK_MSG( files
, (size_t)-1, _T("NULL pointer in wxDir::GetAllFiles") );
230 if ( dir
.IsOpened() )
232 wxDirTraverserSimple
traverser(*files
);
234 nFiles
+= dir
.Traverse(traverser
, filespec
, flags
);
240 // ----------------------------------------------------------------------------
241 // wxDir::FindFirst()
242 // ----------------------------------------------------------------------------
244 class wxDirTraverserFindFirst
: public wxDirTraverser
247 wxDirTraverserFindFirst() { }
249 virtual wxDirTraverseResult
OnFile(const wxString
& filename
)
255 virtual wxDirTraverseResult
OnDir(const wxString
& WXUNUSED(dirname
))
257 return wxDIR_CONTINUE
;
260 const wxString
& GetFile() const
268 DECLARE_NO_COPY_CLASS(wxDirTraverserFindFirst
)
272 wxString
wxDir::FindFirst(const wxString
& dirname
,
273 const wxString
& filespec
,
277 if ( dir
.IsOpened() )
279 wxDirTraverserFindFirst traverser
;
281 dir
.Traverse(traverser
, filespec
, flags
| wxDIR_FILES
);
282 return traverser
.GetFile();
285 return wxEmptyString
;