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