]> git.saurik.com Git - wxWidgets.git/blob - src/common/dircmn.cpp
ensure minimal mime support in wxHTML even without wxMimeTypesMananger
[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 #if !defined(__UNIX_LIKE__) || defined(__WXMAC__)
60
61 bool wxDir::HasSubDirs(const wxString& spec)
62 {
63 wxString s;
64 return GetFirst(&s, spec, wxDIR_DIRS | wxDIR_HIDDEN);
65 }
66
67 #endif // !Unix
68
69 // ----------------------------------------------------------------------------
70 // wxDir::Traverse()
71 // ----------------------------------------------------------------------------
72
73 size_t wxDir::Traverse(wxDirTraverser& sink,
74 const wxString& filespec,
75 int flags) const
76 {
77 wxCHECK_MSG( IsOpened(), (size_t)-1,
78 _T("dir must be opened before traversing it") );
79
80 // the total number of files found
81 size_t nFiles = 0;
82
83 // the name of this dir with path delimiter at the end
84 wxString prefix = GetName();
85 prefix += wxFILE_SEP_PATH;
86
87 // first, recurse into subdirs
88 if ( flags & wxDIR_DIRS )
89 {
90 wxString dirname;
91 bool cont = GetFirst(&dirname, _T(""), wxDIR_DIRS | wxDIR_HIDDEN);
92 while ( cont )
93 {
94 wxDirTraverseResult res = sink.OnDir(prefix + dirname);
95
96 if ( res == wxDIR_STOP )
97 break;
98
99 if ( res == wxDIR_CONTINUE )
100 {
101 wxDir subdir(prefix + dirname);
102 if ( subdir.IsOpened() )
103 {
104 nFiles += subdir.Traverse(sink, filespec, flags);
105 }
106 }
107 else
108 {
109 wxASSERT_MSG( res == wxDIR_IGNORE,
110 _T("unexpected OnDir() return value") );
111 }
112
113 cont = GetNext(&dirname);
114 }
115 }
116
117 // now enum our own files
118 if ( flags & wxDIR_FILES )
119 {
120 flags &= ~wxDIR_DIRS;
121
122 wxString filename;
123 bool cont = GetFirst(&filename, filespec, flags);
124 while ( cont )
125 {
126 wxDirTraverseResult res = sink.OnFile(prefix + filename);
127 if ( res == wxDIR_STOP )
128 break;
129
130 wxASSERT_MSG( res == wxDIR_CONTINUE,
131 _T("unexpected OnFile() return value") );
132
133 nFiles++;
134
135 cont = GetNext(&filename);
136 }
137 }
138
139 return nFiles;
140 }
141
142 // ----------------------------------------------------------------------------
143 // wxDir::GetAllFiles()
144 // ----------------------------------------------------------------------------
145
146 class wxDirTraverserSimple : public wxDirTraverser
147 {
148 public:
149 wxDirTraverserSimple(wxArrayString& files) : m_files(files) { }
150
151 virtual wxDirTraverseResult OnFile(const wxString& filename)
152 {
153 m_files.Add(filename);
154 return wxDIR_CONTINUE;
155 }
156
157 virtual wxDirTraverseResult OnDir(const wxString& WXUNUSED(dirname))
158 {
159 return wxDIR_CONTINUE;
160 }
161
162 private:
163 wxArrayString& m_files;
164 };
165
166 /* static */
167 size_t wxDir::GetAllFiles(const wxString& dirname,
168 wxArrayString *files,
169 const wxString& filespec,
170 int flags)
171 {
172 wxCHECK_MSG( files, (size_t)-1, _T("NULL pointer in wxDir::GetAllFiles") );
173
174 size_t nFiles = 0;
175
176 wxDir dir(dirname);
177 if ( dir.IsOpened() )
178 {
179 wxDirTraverserSimple traverser(*files);
180
181 nFiles += dir.Traverse(traverser, filespec, flags);
182 }
183
184 return nFiles;
185 }
186