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