]> git.saurik.com Git - wxWidgets.git/blob - src/common/dircmn.cpp
don't try to remove -g flag from CFLAGS, it is not necessary and doesn't work correct...
[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 #include "wx/filefn.h"
38 #endif //WX_PRECOMP
39
40 #include "wx/dir.h"
41
42 // ============================================================================
43 // implementation
44 // ============================================================================
45
46 // ----------------------------------------------------------------------------
47 // wxDir::HasFiles() and HasSubDirs()
48 // ----------------------------------------------------------------------------
49
50 // dumb generic implementation
51
52 bool wxDir::HasFiles(const wxString& spec)
53 {
54 wxString s;
55 return GetFirst(&s, spec, wxDIR_FILES | wxDIR_HIDDEN);
56 }
57
58 // we have a (much) faster version for Unix
59 // MBN: Cygwin should use the Unix version, but with the current build sistem
60 // this is painful
61 #if defined(__CYGWIN__) || !defined(__UNIX_LIKE__) || defined(__WXMAC__)
62
63 bool wxDir::HasSubDirs(const wxString& spec)
64 {
65 wxString s;
66 return GetFirst(&s, spec, wxDIR_DIRS | wxDIR_HIDDEN);
67 }
68
69 #endif // !Unix
70
71 // ----------------------------------------------------------------------------
72 // wxDir::Traverse()
73 // ----------------------------------------------------------------------------
74
75 size_t wxDir::Traverse(wxDirTraverser& sink,
76 const wxString& filespec,
77 int flags) const
78 {
79 wxCHECK_MSG( IsOpened(), (size_t)-1,
80 _T("dir must be opened before traversing it") );
81
82 // the total number of files found
83 size_t nFiles = 0;
84
85 // the name of this dir with path delimiter at the end
86 wxString prefix = GetName();
87 prefix += wxFILE_SEP_PATH;
88
89 // first, recurse into subdirs
90 if ( flags & wxDIR_DIRS )
91 {
92 wxString dirname;
93 bool cont = GetFirst(&dirname, _T(""), wxDIR_DIRS | wxDIR_HIDDEN);
94 while ( cont )
95 {
96 wxDirTraverseResult res = sink.OnDir(prefix + dirname);
97
98 if ( res == wxDIR_STOP )
99 break;
100
101 if ( res == wxDIR_CONTINUE )
102 {
103 wxDir subdir(prefix + dirname);
104 if ( subdir.IsOpened() )
105 {
106 nFiles += subdir.Traverse(sink, filespec, flags);
107 }
108 }
109 else
110 {
111 wxASSERT_MSG( res == wxDIR_IGNORE,
112 _T("unexpected OnDir() return value") );
113 }
114
115 cont = GetNext(&dirname);
116 }
117 }
118
119 // now enum our own files
120 if ( flags & wxDIR_FILES )
121 {
122 flags &= ~wxDIR_DIRS;
123
124 wxString filename;
125 bool cont = GetFirst(&filename, filespec, flags);
126 while ( cont )
127 {
128 wxDirTraverseResult res = sink.OnFile(prefix + filename);
129 if ( res == wxDIR_STOP )
130 break;
131
132 wxASSERT_MSG( res == wxDIR_CONTINUE,
133 _T("unexpected OnFile() return value") );
134
135 nFiles++;
136
137 cont = GetNext(&filename);
138 }
139 }
140
141 return nFiles;
142 }
143
144 // ----------------------------------------------------------------------------
145 // wxDir::GetAllFiles()
146 // ----------------------------------------------------------------------------
147
148 class wxDirTraverserSimple : public wxDirTraverser
149 {
150 public:
151 wxDirTraverserSimple(wxArrayString& files) : m_files(files) { }
152
153 virtual wxDirTraverseResult OnFile(const wxString& filename)
154 {
155 m_files.Add(filename);
156 return wxDIR_CONTINUE;
157 }
158
159 virtual wxDirTraverseResult OnDir(const wxString& WXUNUSED(dirname))
160 {
161 return wxDIR_CONTINUE;
162 }
163
164 private:
165 wxArrayString& m_files;
166 };
167
168 /* static */
169 size_t wxDir::GetAllFiles(const wxString& dirname,
170 wxArrayString *files,
171 const wxString& filespec,
172 int flags)
173 {
174 wxCHECK_MSG( files, (size_t)-1, _T("NULL pointer in wxDir::GetAllFiles") );
175
176 size_t nFiles = 0;
177
178 wxDir dir(dirname);
179 if ( dir.IsOpened() )
180 {
181 wxDirTraverserSimple traverser(*files);
182
183 nFiles += dir.Traverse(traverser, filespec, flags);
184 }
185
186 return nFiles;
187 }
188