]> git.saurik.com Git - wxWidgets.git/blob - src/mgl/dirmgl.cpp
use the new wxSystemSettings API everywhere
[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 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 #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) { m_filespec = 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::Rewind()
110 {
111 if ( m_dir )
112 {
113 PM_findClose(m_dir);
114 m_dir = NULL;
115 }
116 }
117
118 bool wxDirData::Read(wxString *filename)
119 {
120 PM_findData data;
121 bool matches = FALSE;
122
123 data.dwSize = sizeof(data);
124
125 wxString path = m_dirname;
126 path += wxFILE_SEP_PATH;
127 path.reserve(path.length() + 255); // speed up string concatenation
128
129 while ( !matches )
130 {
131 if ( m_dir )
132 {
133 if ( !PM_findNextFile(m_dir, &data) )
134 return FALSE;
135 }
136 else
137 {
138 m_dir = PM_findFirstFile(path + m_filespec , &data);
139 if ( m_dir == PM_FILE_INVALID )
140 {
141 m_dir = NULL;
142 return FALSE;
143 }
144 }
145
146 // don't return "." and ".." unless asked for
147 if ( data.name[0] == '.' &&
148 ((data.name[1] == '.' && data.name[2] == '\0') ||
149 (data.name[1] == '\0')) )
150 {
151 if ( !(m_flags & wxDIR_DOTDOT) )
152 continue;
153
154 // we found a valid match
155 break;
156 }
157
158 // check the type now
159 if ( !(m_flags & wxDIR_FILES) && !(data.attrib & PM_FILE_DIRECTORY) )
160 {
161 // it's a file, but we don't want them
162 continue;
163 }
164 else if ( !(m_flags & wxDIR_DIRS) && (data.attrib & PM_FILE_DIRECTORY) )
165 {
166 // it's a dir, and we don't want it
167 continue;
168 }
169
170 matches = m_flags & wxDIR_HIDDEN ? TRUE : !(data.attrib & PM_FILE_HIDDEN);
171 }
172
173 *filename = data.name;
174
175 return TRUE;
176 }
177
178
179 // ----------------------------------------------------------------------------
180 // wxDir helpers
181 // ----------------------------------------------------------------------------
182
183 /* static */
184 bool wxDir::Exists(const wxString& dir)
185 {
186 return wxPathExists(dir);
187 }
188
189 // ----------------------------------------------------------------------------
190 // wxDir construction/destruction
191 // ----------------------------------------------------------------------------
192
193 wxDir::wxDir(const wxString& dirname)
194 {
195 m_data = NULL;
196
197 (void)Open(dirname);
198 }
199
200 bool wxDir::Open(const wxString& dirname)
201 {
202 delete M_DIR;
203 m_data = NULL;
204
205 if ( !wxDir::Exists(dirname) )
206 {
207 wxLogError(_("Directory '%s' doesn't exist!"), dirname.c_str());
208 return FALSE;
209 }
210
211 m_data = new wxDirData(dirname);
212 return TRUE;
213 }
214
215 bool wxDir::IsOpened() const
216 {
217 return m_data != NULL;
218 }
219
220 wxString wxDir::GetName() const
221 {
222 wxString name;
223 if ( m_data )
224 {
225 name = M_DIR->GetName();
226 if ( !name.empty() && (name.Last() == wxFILE_SEP_PATH) )
227 {
228 // chop off the last (back)slash
229 name.Truncate(name.length() - 1);
230 }
231 }
232
233 return name;
234 }
235
236 wxDir::~wxDir()
237 {
238 delete M_DIR;
239 }
240
241 // ----------------------------------------------------------------------------
242 // wxDir enumerating
243 // ----------------------------------------------------------------------------
244
245 bool wxDir::GetFirst(wxString *filename,
246 const wxString& filespec,
247 int flags) const
248 {
249 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
250
251 M_DIR->Rewind();
252
253 M_DIR->SetFileSpec(filespec);
254 M_DIR->SetFlags(flags);
255
256 return GetNext(filename);
257 }
258
259 bool wxDir::GetNext(wxString *filename) const
260 {
261 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
262
263 wxCHECK_MSG( filename, FALSE, _T("bad pointer in wxDir::GetNext()") );
264
265 return M_DIR->Read(filename);
266 }
267
268 #endif // !__UNIX__
269