]> git.saurik.com Git - wxWidgets.git/blame - src/mgl/dir.cpp
removed obsolete files and added missing ones
[wxWidgets.git] / src / mgl / dir.cpp
CommitLineData
ffd10d5d
VS
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
52class wxDirData
53{
54public:
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
68private:
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
85wxDirData::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
99wxDirData::~wxDirData()
100{
101 if ( m_dir )
102 PM_findClose(m_dir);
103}
104
105void wxDirData::Rewind()
106{
107 if ( m_dir )
108 {
109 PM_findClose(m_dir);
110 m_dir = NULL;
111 }
112}
113
114bool wxDirData::Read(wxString *filename)
115{
116 PM_findData data;
117 bool matches = FALSE;
118
fd4bc54d
VS
119 data.dwSize = sizeof(data);
120
ffd10d5d
VS
121 wxString path = m_dirname;
122 path += wxFILE_SEP_PATH;
fd4bc54d 123 path.reserve(path.length() + 255); // speed up string concatenation
ffd10d5d
VS
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 */
180bool wxDir::Exists(const wxString& dir)
181{
182 return wxPathExists(dir);
183}
184
185// ----------------------------------------------------------------------------
186// wxDir construction/destruction
187// ----------------------------------------------------------------------------
188
189wxDir::wxDir(const wxString& dirname)
190{
191 m_data = NULL;
192
193 (void)Open(dirname);
194}
195
196bool wxDir::Open(const wxString& dirname)
197{
198 delete M_DIR;
17ade085
VS
199 m_data = NULL;
200
201 if ( !wxDir::Exists(dirname) )
202 {
203 wxLogError(_("Directory '%s' doesn't exist!"), dirname.c_str());
204 return FALSE;
205 }
206
ffd10d5d 207 m_data = new wxDirData(dirname);
ffd10d5d
VS
208 return TRUE;
209}
210
211bool wxDir::IsOpened() const
212{
213 return m_data != NULL;
214}
215
216wxString wxDir::GetName() const
217{
218 wxString name;
219 if ( m_data )
220 {
221 name = M_DIR->GetName();
222 if ( !name.empty() && (name.Last() == wxFILE_SEP_PATH) )
223 {
224 // chop off the last (back)slash
225 name.Truncate(name.length() - 1);
226 }
227 }
228
229 return name;
230}
231
232wxDir::~wxDir()
233{
234 delete M_DIR;
235}
236
237// ----------------------------------------------------------------------------
238// wxDir enumerating
239// ----------------------------------------------------------------------------
240
241bool wxDir::GetFirst(wxString *filename,
242 const wxString& filespec,
243 int flags) const
244{
245 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
246
247 M_DIR->Rewind();
248
249 M_DIR->SetFileSpec(filespec);
250 M_DIR->SetFlags(flags);
251
252 return GetNext(filename);
253}
254
255bool wxDir::GetNext(wxString *filename) const
256{
257 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
258
259 wxCHECK_MSG( filename, FALSE, _T("bad pointer in wxDir::GetNext()") );
260
261 return M_DIR->Read(filename);
262}
263
264bool wxDir::HasSubDirs(const wxString& spec)
265{
266 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
267
268 // just try to find first directory
269 wxString s;
270 return GetFirst(&s, spec, wxDIR_DIRS | wxDIR_HIDDEN);
271}
272