]>
git.saurik.com Git - wxWidgets.git/blob - src/common/dircmn.cpp
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
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"
42 // ============================================================================
44 // ============================================================================
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
51 wxDirTraverser::OnOpenError(const wxString
& WXUNUSED(dirname
))
56 // ----------------------------------------------------------------------------
57 // wxDir::HasFiles() and HasSubDirs()
58 // ----------------------------------------------------------------------------
60 // dumb generic implementation
62 bool wxDir::HasFiles(const wxString
& spec
)
65 return GetFirst(&s
, spec
, wxDIR_FILES
| wxDIR_HIDDEN
);
68 // we have a (much) faster version for Unix
69 #if (defined(__CYGWIN__) && defined(__WINDOWS__)) || !defined(__UNIX_LIKE__) || defined(__WXMAC__)
71 bool wxDir::HasSubDirs(const wxString
& spec
)
74 return GetFirst(&s
, spec
, wxDIR_DIRS
| wxDIR_HIDDEN
);
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 size_t wxDir::Traverse(wxDirTraverser
& sink
,
84 const wxString
& filespec
,
87 wxCHECK_MSG( IsOpened(), (size_t)-1,
88 _T("dir must be opened before traversing it") );
90 // the total number of files found
93 // the name of this dir with path delimiter at the end
94 wxString prefix
= GetName();
95 prefix
+= wxFILE_SEP_PATH
;
97 // first, recurse into subdirs
98 if ( flags
& wxDIR_DIRS
)
101 for ( bool cont
= GetFirst(&dirname
, _T(""), wxDIR_DIRS
| wxDIR_HIDDEN
);
103 cont
= GetNext(&dirname
) )
105 const wxString fulldirname
= prefix
+ dirname
;
107 switch ( sink
.OnDir(fulldirname
) )
110 wxFAIL_MSG(_T("unexpected OnDir() return value") );
121 // don't give the error messages for the directories
122 // which we can't open: there can be all sorts of good
123 // reason for this (e.g. insufficient privileges) and
124 // this shouldn't be treated as an error -- instead
125 // let the user code decide what to do
130 ok
= subdir
.Open(fulldirname
);
133 // ask the user code what to do
135 switch ( sink
.OnOpenError(fulldirname
) )
138 wxFAIL_MSG(_T("unexpected OnOpenError() return value") );
161 nFiles
+= subdir
.Traverse(sink
, filespec
, flags
);
173 // now enum our own files
174 if ( flags
& wxDIR_FILES
)
176 flags
&= ~wxDIR_DIRS
;
179 bool cont
= GetFirst(&filename
, filespec
, flags
);
182 wxDirTraverseResult res
= sink
.OnFile(prefix
+ filename
);
183 if ( res
== wxDIR_STOP
)
186 wxASSERT_MSG( res
== wxDIR_CONTINUE
,
187 _T("unexpected OnFile() return value") );
191 cont
= GetNext(&filename
);
198 // ----------------------------------------------------------------------------
199 // wxDir::GetAllFiles()
200 // ----------------------------------------------------------------------------
202 class wxDirTraverserSimple
: public wxDirTraverser
205 wxDirTraverserSimple(wxArrayString
& files
) : m_files(files
) { }
207 virtual wxDirTraverseResult
OnFile(const wxString
& filename
)
209 m_files
.push_back(filename
);
210 return wxDIR_CONTINUE
;
213 virtual wxDirTraverseResult
OnDir(const wxString
& WXUNUSED(dirname
))
215 return wxDIR_CONTINUE
;
219 wxArrayString
& m_files
;
223 size_t wxDir::GetAllFiles(const wxString
& dirname
,
224 wxArrayString
*files
,
225 const wxString
& filespec
,
228 wxCHECK_MSG( files
, (size_t)-1, _T("NULL pointer in wxDir::GetAllFiles") );
233 if ( dir
.IsOpened() )
235 wxDirTraverserSimple
traverser(*files
);
237 nFiles
+= dir
.Traverse(traverser
, filespec
, flags
);