]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: mgl/dir.cpp | |
3 | // Purpose: wxDir implementation for MGL | |
4 | // Author: Vaclav Slavik, Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 2001/12/09 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // (c) 2001 SciTech Software, Inc. (www.scitechsoft.com) | |
10 | // Licence: wxWindows license | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | // ============================================================================ | |
14 | // declarations | |
15 | // ============================================================================ | |
16 | ||
17 | // ---------------------------------------------------------------------------- | |
18 | // headers | |
19 | // ---------------------------------------------------------------------------- | |
20 | ||
21 | #ifdef __GNUG__ | |
22 | #pragma implementation "dir.h" | |
23 | #endif | |
24 | ||
25 | // For compilers that support precompilation, includes "wx.h". | |
26 | #include "wx/wxprec.h" | |
27 | ||
28 | #ifdef __BORLANDC__ | |
29 | #pragma hdrstop | |
30 | #endif | |
31 | ||
32 | #ifndef WX_PRECOMP | |
33 | #include "wx/intl.h" | |
34 | #include "wx/log.h" | |
35 | #endif // PCH | |
36 | ||
37 | #include "wx/dir.h" | |
38 | #include "wx/filefn.h" // for wxMatchWild | |
39 | #include "wx/mgl/private.h" | |
40 | ||
41 | // ---------------------------------------------------------------------------- | |
42 | // macros | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
45 | #define M_DIR ((wxDirData *)m_data) | |
46 | ||
47 | // ---------------------------------------------------------------------------- | |
48 | // private classes | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
51 | // this class stores everything we need to enumerate the files | |
52 | class wxDirData | |
53 | { | |
54 | public: | |
55 | wxDirData(const wxString& dirname); | |
56 | ~wxDirData(); | |
57 | ||
58 | bool IsOk() const { return m_dir != NULL; } | |
59 | ||
60 | void SetFileSpec(const wxString& filespec) { m_filespec = filespec; } | |
61 | void SetFlags(int flags) { m_flags = flags; } | |
62 | ||
63 | void Rewind(); | |
64 | bool Read(wxString *filename); | |
65 | ||
66 | const wxString& GetName() const { return m_dirname; } | |
67 | ||
68 | private: | |
69 | void *m_dir; | |
70 | ||
71 | wxString m_dirname; | |
72 | wxString m_filespec; | |
73 | ||
74 | int m_flags; | |
75 | }; | |
76 | ||
77 | // ============================================================================ | |
78 | // implementation | |
79 | // ============================================================================ | |
80 | ||
81 | // ---------------------------------------------------------------------------- | |
82 | // wxDirData | |
83 | // ---------------------------------------------------------------------------- | |
84 | ||
85 | wxDirData::wxDirData(const wxString& dirname) | |
86 | : m_dirname(dirname) | |
87 | { | |
88 | m_dir = NULL; | |
89 | ||
90 | // throw away the trailing slashes | |
91 | size_t n = m_dirname.length(); | |
92 | wxCHECK_RET( n, _T("empty dir name in wxDir") ); | |
93 | ||
94 | while ( n > 0 && m_dirname[--n] == wxFILE_SEP_PATH ) {} | |
95 | ||
96 | m_dirname.Truncate(n + 1); | |
97 | } | |
98 | ||
99 | wxDirData::~wxDirData() | |
100 | { | |
101 | if ( m_dir ) | |
102 | PM_findClose(m_dir); | |
103 | } | |
104 | ||
105 | void wxDirData::Rewind() | |
106 | { | |
107 | if ( m_dir ) | |
108 | { | |
109 | PM_findClose(m_dir); | |
110 | m_dir = NULL; | |
111 | } | |
112 | } | |
113 | ||
114 | bool wxDirData::Read(wxString *filename) | |
115 | { | |
116 | PM_findData data; | |
117 | bool matches = FALSE; | |
118 | ||
119 | // speed up string concatenation in the loop a bit | |
120 | wxString path = m_dirname; | |
121 | path += wxFILE_SEP_PATH; | |
122 | path.reserve(path.length() + 255); | |
123 | ||
124 | while ( !matches ) | |
125 | { | |
126 | if ( m_dir ) | |
127 | { | |
128 | if ( !PM_findNextFile(m_dir, &data) ) | |
129 | return FALSE; | |
130 | } | |
131 | else | |
132 | { | |
133 | m_dir = PM_findFirstFile(path + m_filespec , &data); | |
134 | if ( m_dir == PM_FILE_INVALID ) | |
135 | { | |
136 | m_dir = NULL; | |
137 | return FALSE; | |
138 | } | |
139 | } | |
140 | ||
141 | // don't return "." and ".." unless asked for | |
142 | if ( data.name[0] == '.' && | |
143 | ((data.name[1] == '.' && data.name[2] == '\0') || | |
144 | (data.name[1] == '\0')) ) | |
145 | { | |
146 | if ( !(m_flags & wxDIR_DOTDOT) ) | |
147 | continue; | |
148 | ||
149 | // we found a valid match | |
150 | break; | |
151 | } | |
152 | ||
153 | // check the type now | |
154 | if ( !(m_flags & wxDIR_FILES) && !(data.attrib & PM_FILE_DIRECTORY) ) | |
155 | { | |
156 | // it's a file, but we don't want them | |
157 | continue; | |
158 | } | |
159 | else if ( !(m_flags & wxDIR_DIRS) && (data.attrib & PM_FILE_DIRECTORY) ) | |
160 | { | |
161 | // it's a dir, and we don't want it | |
162 | continue; | |
163 | } | |
164 | ||
165 | matches = m_flags & wxDIR_HIDDEN ? TRUE : !(data.attrib & PM_FILE_HIDDEN); | |
166 | } | |
167 | ||
168 | *filename = data.name; | |
169 | ||
170 | return TRUE; | |
171 | } | |
172 | ||
173 | ||
174 | // ---------------------------------------------------------------------------- | |
175 | // wxDir helpers | |
176 | // ---------------------------------------------------------------------------- | |
177 | ||
178 | /* static */ | |
179 | bool wxDir::Exists(const wxString& dir) | |
180 | { | |
181 | return wxPathExists(dir); | |
182 | } | |
183 | ||
184 | // ---------------------------------------------------------------------------- | |
185 | // wxDir construction/destruction | |
186 | // ---------------------------------------------------------------------------- | |
187 | ||
188 | wxDir::wxDir(const wxString& dirname) | |
189 | { | |
190 | m_data = NULL; | |
191 | ||
192 | (void)Open(dirname); | |
193 | } | |
194 | ||
195 | bool wxDir::Open(const wxString& dirname) | |
196 | { | |
197 | delete M_DIR; | |
198 | m_data = new wxDirData(dirname); | |
199 | ||
200 | if ( !M_DIR->IsOk() ) | |
201 | { | |
202 | wxLogSysError(_("Can not enumerate files in directory '%s'"), | |
203 | dirname.c_str()); | |
204 | ||
205 | delete M_DIR; | |
206 | m_data = NULL; | |
207 | ||
208 | return FALSE; | |
209 | } | |
210 | ||
211 | return TRUE; | |
212 | } | |
213 | ||
214 | bool wxDir::IsOpened() const | |
215 | { | |
216 | return m_data != NULL; | |
217 | } | |
218 | ||
219 | wxString wxDir::GetName() const | |
220 | { | |
221 | wxString name; | |
222 | if ( m_data ) | |
223 | { | |
224 | name = M_DIR->GetName(); | |
225 | if ( !name.empty() && (name.Last() == wxFILE_SEP_PATH) ) | |
226 | { | |
227 | // chop off the last (back)slash | |
228 | name.Truncate(name.length() - 1); | |
229 | } | |
230 | } | |
231 | ||
232 | return name; | |
233 | } | |
234 | ||
235 | wxDir::~wxDir() | |
236 | { | |
237 | delete M_DIR; | |
238 | } | |
239 | ||
240 | // ---------------------------------------------------------------------------- | |
241 | // wxDir enumerating | |
242 | // ---------------------------------------------------------------------------- | |
243 | ||
244 | bool wxDir::GetFirst(wxString *filename, | |
245 | const wxString& filespec, | |
246 | int flags) const | |
247 | { | |
248 | wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") ); | |
249 | ||
250 | M_DIR->Rewind(); | |
251 | ||
252 | M_DIR->SetFileSpec(filespec); | |
253 | M_DIR->SetFlags(flags); | |
254 | ||
255 | return GetNext(filename); | |
256 | } | |
257 | ||
258 | bool wxDir::GetNext(wxString *filename) const | |
259 | { | |
260 | wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") ); | |
261 | ||
262 | wxCHECK_MSG( filename, FALSE, _T("bad pointer in wxDir::GetNext()") ); | |
263 | ||
264 | return M_DIR->Read(filename); | |
265 | } | |
266 | ||
267 | bool wxDir::HasSubDirs(const wxString& spec) | |
268 | { | |
269 | wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") ); | |
270 | ||
271 | // just try to find first directory | |
272 | wxString s; | |
273 | return GetFirst(&s, spec, wxDIR_DIRS | wxDIR_HIDDEN); | |
274 | } | |
275 |