]>
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
61 bool wxDir::HasSubDirs(const wxString
& spec
)
64 return GetFirst(&s
, spec
, wxDIR_DIRS
| wxDIR_HIDDEN
);
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 size_t wxDir::Traverse(wxDirTraverser
& sink
,
74 const wxString
& filespec
,
77 wxCHECK_MSG( IsOpened(), (size_t)-1,
78 _T("dir must be opened before traversing it") );
80 // the total number of files found
83 // the name of this dir with path delimiter at the end
84 wxString prefix
= GetName();
85 prefix
+= wxFILE_SEP_PATH
;
87 // first, recurse into subdirs
88 if ( flags
& wxDIR_DIRS
)
91 bool cont
= GetFirst(&dirname
, _T(""), wxDIR_DIRS
| wxDIR_HIDDEN
);
94 wxDirTraverseResult res
= sink
.OnDir(prefix
+ dirname
);
96 if ( res
== wxDIR_STOP
)
99 if ( res
== wxDIR_CONTINUE
)
101 wxDir
subdir(prefix
+ dirname
);
102 if ( subdir
.IsOpened() )
104 nFiles
+= subdir
.Traverse(sink
, filespec
, flags
);
109 wxASSERT_MSG( res
== wxDIR_IGNORE
,
110 _T("unexpected OnDir() return value") );
113 cont
= GetNext(&dirname
);
117 // now enum our own files
118 if ( flags
& wxDIR_FILES
)
120 flags
&= ~wxDIR_DIRS
;
123 bool cont
= GetFirst(&filename
, filespec
, flags
);
126 wxDirTraverseResult res
= sink
.OnFile(prefix
+ filename
);
127 if ( res
== wxDIR_STOP
)
130 wxASSERT_MSG( res
== wxDIR_CONTINUE
,
131 _T("unexpected OnFile() return value") );
135 cont
= GetNext(&filename
);
142 // ----------------------------------------------------------------------------
143 // wxDir::GetAllFiles()
144 // ----------------------------------------------------------------------------
146 class wxDirTraverserSimple
: public wxDirTraverser
149 wxDirTraverserSimple(wxArrayString
& files
) : m_files(files
) { }
151 virtual wxDirTraverseResult
OnFile(const wxString
& filename
)
153 m_files
.Add(filename
);
154 return wxDIR_CONTINUE
;
157 virtual wxDirTraverseResult
OnDir(const wxString
& WXUNUSED(dirname
))
159 return wxDIR_CONTINUE
;
163 wxArrayString
& m_files
;
167 size_t wxDir::GetAllFiles(const wxString
& dirname
,
168 wxArrayString
*files
,
169 const wxString
& filespec
,
172 wxCHECK_MSG( files
, (size_t)-1, _T("NULL pointer in wxDir::GetAllFiles") );
177 if ( dir
.IsOpened() )
179 wxDirTraverserSimple
traverser(*files
);
181 nFiles
+= dir
.Traverse(traverser
, filespec
, flags
);