]>
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 license
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 // ----------------------------------------------------------------------------
47 // wxDir::HasFiles() and HasSubDirs()
48 // ----------------------------------------------------------------------------
50 // dumb generic implementation
52 bool wxDir::HasFiles(const wxString
& spec
)
55 return GetFirst(&s
, spec
, wxDIR_FILES
| wxDIR_HIDDEN
);
58 // we have a (much) faster version for Unix
59 // MBN: Cygwin should use the Unix version, but with the current build sistem
61 #if defined(__CYGWIN__) || !defined(__UNIX_LIKE__) || defined(__WXMAC__)
63 bool wxDir::HasSubDirs(const wxString
& spec
)
66 return GetFirst(&s
, spec
, wxDIR_DIRS
| wxDIR_HIDDEN
);
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 size_t wxDir::Traverse(wxDirTraverser
& sink
,
76 const wxString
& filespec
,
79 wxCHECK_MSG( IsOpened(), (size_t)-1,
80 _T("dir must be opened before traversing it") );
82 // the total number of files found
85 // the name of this dir with path delimiter at the end
86 wxString prefix
= GetName();
87 prefix
+= wxFILE_SEP_PATH
;
89 // first, recurse into subdirs
90 if ( flags
& wxDIR_DIRS
)
93 bool cont
= GetFirst(&dirname
, _T(""), wxDIR_DIRS
| wxDIR_HIDDEN
);
96 wxDirTraverseResult res
= sink
.OnDir(prefix
+ dirname
);
98 if ( res
== wxDIR_STOP
)
101 if ( res
== wxDIR_CONTINUE
)
103 wxDir
subdir(prefix
+ dirname
);
104 if ( subdir
.IsOpened() )
106 nFiles
+= subdir
.Traverse(sink
, filespec
, flags
);
111 wxASSERT_MSG( res
== wxDIR_IGNORE
,
112 _T("unexpected OnDir() return value") );
115 cont
= GetNext(&dirname
);
119 // now enum our own files
120 if ( flags
& wxDIR_FILES
)
122 flags
&= ~wxDIR_DIRS
;
125 bool cont
= GetFirst(&filename
, filespec
, flags
);
128 wxDirTraverseResult res
= sink
.OnFile(prefix
+ filename
);
129 if ( res
== wxDIR_STOP
)
132 wxASSERT_MSG( res
== wxDIR_CONTINUE
,
133 _T("unexpected OnFile() return value") );
137 cont
= GetNext(&filename
);
144 // ----------------------------------------------------------------------------
145 // wxDir::GetAllFiles()
146 // ----------------------------------------------------------------------------
148 class wxDirTraverserSimple
: public wxDirTraverser
151 wxDirTraverserSimple(wxArrayString
& files
) : m_files(files
) { }
153 virtual wxDirTraverseResult
OnFile(const wxString
& filename
)
155 m_files
.Add(filename
);
156 return wxDIR_CONTINUE
;
159 virtual wxDirTraverseResult
OnDir(const wxString
& WXUNUSED(dirname
))
161 return wxDIR_CONTINUE
;
165 wxArrayString
& m_files
;
169 size_t wxDir::GetAllFiles(const wxString
& dirname
,
170 wxArrayString
*files
,
171 const wxString
& filespec
,
174 wxCHECK_MSG( files
, (size_t)-1, _T("NULL pointer in wxDir::GetAllFiles") );
179 if ( dir
.IsOpened() )
181 wxDirTraverserSimple
traverser(*files
);
183 nFiles
+= dir
.Traverse(traverser
, filespec
, flags
);