]> git.saurik.com Git - wxWidgets.git/blame - src/mgl/dirmgl.cpp
don't crash when XIM is used under IRIX
[wxWidgets.git] / src / mgl / dirmgl.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>
c41c20a5 9// (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
65571936 10// Licence: wxWindows licence
ffd10d5d
VS
11/////////////////////////////////////////////////////////////////////////////
12
13// ============================================================================
14// declarations
15// ============================================================================
16
17// ----------------------------------------------------------------------------
18// headers
19// ----------------------------------------------------------------------------
20
ffd10d5d
VS
21// For compilers that support precompilation, includes "wx.h".
22#include "wx/wxprec.h"
23
24#ifdef __BORLANDC__
25 #pragma hdrstop
26#endif
27
34885a14
VS
28#include "wx/defs.h"
29
30#ifndef __UNIX__
31
ffd10d5d
VS
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
7a5df861 60 void SetFileSpec(const wxString& filespec);
ffd10d5d
VS
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
7a5df861
VS
105void wxDirData::SetFileSpec(const wxString& filespec)
106{
107#ifdef __DOS__
da865fdd 108 if ( filespec.empty() )
7a5df861
VS
109 m_filespec = _T("*.*");
110 else
111#endif
112 m_filespec = filespec;
113}
114
ffd10d5d
VS
115void wxDirData::Rewind()
116{
117 if ( m_dir )
118 {
119 PM_findClose(m_dir);
120 m_dir = NULL;
121 }
122}
123
124bool wxDirData::Read(wxString *filename)
125{
126 PM_findData data;
da865fdd 127 bool matches = false;
ffd10d5d 128
fd4bc54d 129 data.dwSize = sizeof(data);
da865fdd 130
ffd10d5d
VS
131 wxString path = m_dirname;
132 path += wxFILE_SEP_PATH;
fd4bc54d 133 path.reserve(path.length() + 255); // speed up string concatenation
ffd10d5d
VS
134
135 while ( !matches )
136 {
137 if ( m_dir )
138 {
139 if ( !PM_findNextFile(m_dir, &data) )
da865fdd 140 return false;
ffd10d5d
VS
141 }
142 else
143 {
144 m_dir = PM_findFirstFile(path + m_filespec , &data);
145 if ( m_dir == PM_FILE_INVALID )
146 {
147 m_dir = NULL;
da865fdd 148 return false;
ffd10d5d
VS
149 }
150 }
151
152 // don't return "." and ".." unless asked for
153 if ( data.name[0] == '.' &&
154 ((data.name[1] == '.' && data.name[2] == '\0') ||
155 (data.name[1] == '\0')) )
156 {
157 if ( !(m_flags & wxDIR_DOTDOT) )
158 continue;
159
160 // we found a valid match
161 break;
162 }
163
164 // check the type now
165 if ( !(m_flags & wxDIR_FILES) && !(data.attrib & PM_FILE_DIRECTORY) )
166 {
167 // it's a file, but we don't want them
168 continue;
169 }
170 else if ( !(m_flags & wxDIR_DIRS) && (data.attrib & PM_FILE_DIRECTORY) )
171 {
172 // it's a dir, and we don't want it
173 continue;
174 }
175
da865fdd 176 matches = m_flags & wxDIR_HIDDEN ? true : !(data.attrib & PM_FILE_HIDDEN);
ffd10d5d
VS
177 }
178
179 *filename = data.name;
180
da865fdd 181 return true;
ffd10d5d
VS
182}
183
184
185// ----------------------------------------------------------------------------
186// wxDir helpers
187// ----------------------------------------------------------------------------
188
189/* static */
190bool wxDir::Exists(const wxString& dir)
191{
da865fdd 192 return wxDirExists(dir);
ffd10d5d
VS
193}
194
195// ----------------------------------------------------------------------------
196// wxDir construction/destruction
197// ----------------------------------------------------------------------------
198
199wxDir::wxDir(const wxString& dirname)
200{
201 m_data = NULL;
202
203 (void)Open(dirname);
204}
205
206bool wxDir::Open(const wxString& dirname)
207{
208 delete M_DIR;
17ade085 209 m_data = NULL;
da865fdd 210
17ade085
VS
211 if ( !wxDir::Exists(dirname) )
212 {
213 wxLogError(_("Directory '%s' doesn't exist!"), dirname.c_str());
da865fdd 214 return false;
17ade085 215 }
da865fdd 216
ffd10d5d 217 m_data = new wxDirData(dirname);
da865fdd 218 return true;
ffd10d5d
VS
219}
220
221bool wxDir::IsOpened() const
222{
223 return m_data != NULL;
224}
225
226wxString wxDir::GetName() const
227{
228 wxString name;
229 if ( m_data )
230 {
231 name = M_DIR->GetName();
232 if ( !name.empty() && (name.Last() == wxFILE_SEP_PATH) )
233 {
234 // chop off the last (back)slash
235 name.Truncate(name.length() - 1);
236 }
237 }
238
239 return name;
240}
241
242wxDir::~wxDir()
243{
244 delete M_DIR;
245}
246
247// ----------------------------------------------------------------------------
248// wxDir enumerating
249// ----------------------------------------------------------------------------
250
251bool wxDir::GetFirst(wxString *filename,
252 const wxString& filespec,
253 int flags) const
254{
da865fdd 255 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
ffd10d5d
VS
256
257 M_DIR->Rewind();
258
259 M_DIR->SetFileSpec(filespec);
260 M_DIR->SetFlags(flags);
261
262 return GetNext(filename);
263}
264
265bool wxDir::GetNext(wxString *filename) const
266{
da865fdd 267 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
ffd10d5d 268
da865fdd 269 wxCHECK_MSG( filename, false, _T("bad pointer in wxDir::GetNext()") );
ffd10d5d
VS
270
271 return M_DIR->Read(filename);
272}
273
34885a14
VS
274#endif // !__UNIX__
275