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 /* this is done in platform-specific files
21 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
22 #pragma implementation "dir.h"
26 // For compilers that support precompilation, includes "wx.h".
27 #include "wx/wxprec.h"
34 #include "wx/string.h"
37 #include "wx/filefn.h"
40 #include "wx/arrstr.h"
43 // ============================================================================
45 // ============================================================================
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
52 wxDirTraverser::OnOpenError(const wxString
& WXUNUSED(dirname
))
57 // ----------------------------------------------------------------------------
58 // wxDir::HasFiles() and HasSubDirs()
59 // ----------------------------------------------------------------------------
61 // dumb generic implementation
63 bool wxDir::HasFiles(const wxString
& spec
)
66 return GetFirst(&s
, spec
, wxDIR_FILES
| wxDIR_HIDDEN
);
69 // we have a (much) faster version for Unix
70 #if (defined(__CYGWIN__) && defined(__WINDOWS__)) || !defined(__UNIX_LIKE__) || defined(__WXMAC__) || defined(__EMX__) || defined(__WINE__)
72 bool wxDir::HasSubDirs(const wxString
& spec
)
75 return GetFirst(&s
, spec
, wxDIR_DIRS
| wxDIR_HIDDEN
);
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 size_t wxDir::Traverse(wxDirTraverser
& sink
,
85 const wxString
& filespec
,
88 wxCHECK_MSG( IsOpened(), (size_t)-1,
89 _T("dir must be opened before traversing it") );
91 // the total number of files found
94 // the name of this dir with path delimiter at the end
95 wxString prefix
= GetName();
96 prefix
+= wxFILE_SEP_PATH
;
98 // first, recurse into subdirs
99 if ( flags
& wxDIR_DIRS
)
102 for ( bool cont
= GetFirst(&dirname
, wxEmptyString
, wxDIR_DIRS
| (flags
& wxDIR_HIDDEN
) );
104 cont
= cont
&& GetNext(&dirname
) )
106 const wxString fulldirname
= prefix
+ dirname
;
108 switch ( sink
.OnDir(fulldirname
) )
111 wxFAIL_MSG(_T("unexpected OnDir() return value") );
122 // don't give the error messages for the directories
123 // which we can't open: there can be all sorts of good
124 // reason for this (e.g. insufficient privileges) and
125 // this shouldn't be treated as an error -- instead
126 // let the user code decide what to do
131 ok
= subdir
.Open(fulldirname
);
134 // ask the user code what to do
136 switch ( sink
.OnOpenError(fulldirname
) )
139 wxFAIL_MSG(_T("unexpected OnOpenError() return value") );
162 nFiles
+= subdir
.Traverse(sink
, filespec
, flags
);
174 // now enum our own files
175 if ( flags
& wxDIR_FILES
)
177 flags
&= ~wxDIR_DIRS
;
180 bool cont
= GetFirst(&filename
, filespec
, flags
);
183 wxDirTraverseResult res
= sink
.OnFile(prefix
+ filename
);
184 if ( res
== wxDIR_STOP
)
187 wxASSERT_MSG( res
== wxDIR_CONTINUE
,
188 _T("unexpected OnFile() return value") );
192 cont
= GetNext(&filename
);
199 // ----------------------------------------------------------------------------
200 // wxDir::GetAllFiles()
201 // ----------------------------------------------------------------------------
203 class wxDirTraverserSimple
: public wxDirTraverser
206 wxDirTraverserSimple(wxArrayString
& files
) : m_files(files
) { }
208 virtual wxDirTraverseResult
OnFile(const wxString
& filename
)
210 m_files
.push_back(filename
);
211 return wxDIR_CONTINUE
;
214 virtual wxDirTraverseResult
OnDir(const wxString
& WXUNUSED(dirname
))
216 return wxDIR_CONTINUE
;
220 wxArrayString
& m_files
;
222 DECLARE_NO_COPY_CLASS(wxDirTraverserSimple
)
226 size_t wxDir::GetAllFiles(const wxString
& dirname
,
227 wxArrayString
*files
,
228 const wxString
& filespec
,
231 wxCHECK_MSG( files
, (size_t)-1, _T("NULL pointer in wxDir::GetAllFiles") );
236 if ( dir
.IsOpened() )
238 wxDirTraverserSimple
traverser(*files
);
240 nFiles
+= dir
.Traverse(traverser
, filespec
, flags
);