added wxDir::Traverse
[wxWidgets.git] / src / common / dircmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/dircmn.cpp
3 // Purpose: wxDir methods common to all implementations
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 19.05.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 /* this is done in platform-specific files
21 #ifdef __GNUG__
22 #pragma implementation "dir.h"
23 #endif
24 */
25
26 // For compilers that support precompilation, includes "wx.h".
27 #include "wx/wxprec.h"
28
29 #ifdef __BORLANDC__
30 #pragma hdrstop
31 #endif
32
33 #ifndef WX_PRECOMP
34 #include "wx/string.h"
35 #include "wx/log.h"
36 #include "wx/intl.h"
37 #endif //WX_PRECOMP
38
39 #include "wx/dir.h"
40
41 // ============================================================================
42 // implementation
43 // ============================================================================
44
45 // ----------------------------------------------------------------------------
46 // wxDir::Traverse()
47 // ----------------------------------------------------------------------------
48
49 size_t wxDir::Traverse(wxDirTraverser& sink,
50 const wxString& filespec,
51 int flags) const
52 {
53 wxCHECK_MSG( IsOpened(), (size_t)-1,
54 _T("dir must be opened before traversing it") );
55
56 // the total number of files found
57 size_t nFiles = 0;
58
59 // the name of this dir with path delimiter at the end
60 wxString prefix = GetName();
61 prefix += wxFILE_SEP_PATH;
62
63 // first, recurse into subdirs
64 if ( flags & wxDIR_DIRS )
65 {
66 wxString dirname;
67 bool cont = GetFirst(&dirname, _T(""), wxDIR_DIRS | wxDIR_HIDDEN);
68 while ( cont )
69 {
70 wxDirTraverseResult res = sink.OnDir(prefix + dirname);
71
72 if ( res == wxDIR_STOP )
73 break;
74
75 if ( res == wxDIR_CONTINUE )
76 {
77 wxDir subdir(prefix + dirname);
78 if ( subdir.IsOpened() )
79 {
80 nFiles += subdir.Traverse(sink, filespec, flags);
81 }
82 }
83 else
84 {
85 wxASSERT_MSG( res == wxDIR_IGNORE,
86 _T("unexpected OnDir() return value") );
87 }
88
89 cont = GetNext(&dirname);
90 }
91 }
92
93 // now enum our own files
94 if ( flags & wxDIR_FILES )
95 {
96 flags &= ~wxDIR_DIRS;
97
98 wxString filename;
99 bool cont = GetFirst(&filename, filespec, flags);
100 while ( cont )
101 {
102 wxDirTraverseResult res = sink.OnFile(prefix + filename);
103 if ( res == wxDIR_STOP )
104 break;
105
106 wxASSERT_MSG( res == wxDIR_CONTINUE,
107 _T("unexpected OnFile() return value") );
108
109 nFiles++;
110
111 cont = GetNext(&filename);
112 }
113 }
114
115 return nFiles;
116 }
117
118 // ----------------------------------------------------------------------------
119 // wxDir::GetAllFiles()
120 // ----------------------------------------------------------------------------
121
122 class wxDirTraverserSimple : public wxDirTraverser
123 {
124 public:
125 wxDirTraverserSimple(wxArrayString& files) : m_files(files) { }
126
127 virtual wxDirTraverseResult OnFile(const wxString& filename)
128 {
129 m_files.Add(filename);
130 return wxDIR_CONTINUE;
131 }
132
133 virtual wxDirTraverseResult OnDir(const wxString& WXUNUSED(dirname))
134 {
135 return wxDIR_CONTINUE;
136 }
137
138 private:
139 wxArrayString& m_files;
140 };
141
142 /* static */
143 size_t wxDir::GetAllFiles(const wxString& dirname,
144 wxArrayString *files,
145 const wxString& filespec,
146 int flags)
147 {
148 wxCHECK_MSG( files, (size_t)-1, _T("NULL pointer in wxDir::GetAllFiles") );
149
150 size_t nFiles = 0;
151
152 wxDir dir(dirname);
153 if ( dir.IsOpened() )
154 {
155 wxDirTraverserSimple traverser(*files);
156
157 nFiles += dir.Traverse(traverser, filespec, flags);
158 }
159
160 return nFiles;
161 }
162