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