]> git.saurik.com Git - wxWidgets.git/blame - src/os2/dir.cpp
Suppress "Assignement in logical expression" type warnings for compilers that compain...
[wxWidgets.git] / src / os2 / dir.cpp
CommitLineData
6ef85b1b
SN
1/////////////////////////////////////////////////////////////////////////////
2// Name: os2/dir.cpp
3// Purpose: wxDir implementation for OS/2
4// Author: Vadim Zeitlin
5// Modified by: Stefan Neis
6// Created: 08.12.99
7// RCS-ID: $Id$
8// Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "dir.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifndef WX_PRECOMP
28 #include "wx/intl.h"
29 #include "wx/log.h"
30#endif // PCH
31
32#include "wx/dir.h"
33#include "wx/filefn.h" // for wxMatchWild
34
35#include <sys/types.h>
36
6f38c86f
DW
37#define INCL_DOSFILEMGR
38#include <os2.h>
39
6ef85b1b
SN
40#ifdef __EMX__
41#include <dirent.h>
6ef85b1b
SN
42#endif
43
6f38c86f
DW
44// ----------------------------------------------------------------------------
45// define the types and functions used for file searching
46// ----------------------------------------------------------------------------
47
48typedef FILEFINDBUF3 FIND_STRUCT;
49typedef HDIR FIND_DATA;
50typedef ULONG FIND_ATTR;
51
52static inline FIND_DATA InitFindData() { return ERROR_INVALID_HANDLE; }
53
54static inline bool IsFindDataOk(
55 FIND_DATA vFd
56)
57{
58 return vFd != ERROR_INVALID_HANDLE;
59}
60
61static inline void FreeFindData(
62 FIND_DATA vFd
63)
64{
65 if (!::DosFindClose(vFd))
66 {
67 wxLogLastError(_T("DosFindClose"));
68 }
69}
70
71static inline FIND_DATA FindFirst(
72 const wxString& rsSpec
73, FIND_STRUCT* pFinddata
74)
75{
76 ULONG ulFindCount = 1;
77 FIND_DATA hDir;
78 FIND_ATTR rc;
79
80 rc = ::DosFindFirst( rsSpec.c_str()
81 ,&hDir
82 ,FILE_NORMAL
83 ,pFinddata
84 ,sizeof(FILEFINDBUF3)
85 ,&ulFindCount
86 ,FIL_STANDARD
87 );
88 if (rc != 0)
89 return 0;
90 return hDir;
91}
92
93static inline bool FindNext(
94 FIND_DATA vFd
95, FIND_STRUCT* pFinddata
96)
97{
98 ULONG ulFindCount = 1;
99
100 return ::DosFindNext( vFd
101 ,pFinddata
102 ,sizeof(FILEFINDBUF3)
103 ,&ulFindCount
104 ) != 0;
105}
106
107static const wxChar* GetNameFromFindData(
108 FIND_STRUCT* pFinddata
109)
110{
111 return pFinddata->achName;
112}
113
114static const FIND_ATTR GetAttrFromFindData(
115 FIND_STRUCT* pFinddata
116)
117{
118 return pFinddata->attrFile;
119}
120
121static inline bool IsDir(
122 FIND_ATTR vAttr
123)
124{
125 return (vAttr & FILE_DIRECTORY) != 0;
126}
127
128static inline bool IsHidden(
129 FIND_ATTR vAttr
130)
131{
132 return (vAttr & (FILE_HIDDEN | FILE_SYSTEM)) != 0;
133}
134
135// ----------------------------------------------------------------------------
136// constants
137// ----------------------------------------------------------------------------
138
139#ifndef MAX_PATH
140 #define MAX_PATH 260 // from PM++ headers
141#endif
142
6ef85b1b
SN
143// ----------------------------------------------------------------------------
144// macros
145// ----------------------------------------------------------------------------
146
147#define M_DIR ((wxDirData *)m_data)
148
149// ----------------------------------------------------------------------------
150// private classes
151// ----------------------------------------------------------------------------
152
153// this class stores everything we need to enumerate the files
154class wxDirData
155{
156public:
6f38c86f 157 wxDirData(const wxString& rsDirname);
6ef85b1b
SN
158 ~wxDirData();
159
6f38c86f
DW
160 void SetFileSpec(const wxString& rsFilespec) { m_sFilespec = rsFilespec; }
161 void SetFlags(int nFlags) { m_nFlags = nFlags; }
6ef85b1b 162
6f38c86f
DW
163 void Close();
164 void Rewind();
165 bool Read(wxString* rsFilename);
6ef85b1b
SN
166
167private:
6f38c86f
DW
168 FIND_DATA m_vFinddata;
169 wxString m_sDirname;
170 wxString m_sFilespec;
171 int m_nFlags;
172}; // end of CLASS wxDirData
6ef85b1b
SN
173
174// ============================================================================
175// implementation
176// ============================================================================
177
178// ----------------------------------------------------------------------------
179// wxDirData
180// ----------------------------------------------------------------------------
181
6f38c86f
DW
182wxDirData::wxDirData(
183 const wxString& rsDirname
184)
185: m_sDirname(rsDirname)
6ef85b1b 186{
6f38c86f
DW
187 m_vFinddata = InitFindData();
188} // end of wxDirData::wxDirData
6ef85b1b 189
6f38c86f
DW
190wxDirData::~wxDirData()
191{
192 Close();
193} // end of wxDirData::~wxDirData
6ef85b1b 194
6f38c86f
DW
195void wxDirData::Close()
196{
197 if ( IsFindDataOk(m_vFinddata) )
198 {
199 FreeFindData(m_vFinddata);
200 m_vFinddata = InitFindData();
201 }
202} // end of wxDirData::Close
6ef85b1b 203
6f38c86f
DW
204void wxDirData::Rewind()
205{
206 Close();
207} // end of wxDirData::Rewind
6ef85b1b 208
6f38c86f
DW
209bool wxDirData::Read(
210 wxString* psFilename
211)
6ef85b1b 212{
6f38c86f
DW
213 bool bFirst = FALSE;
214
215 FILEFINDBUF3 vFinddata;
216 #define PTR_TO_FINDDATA (&vFinddata)
217
218 if (!IsFindDataOk(m_vFinddata))
6ef85b1b 219 {
6f38c86f
DW
220 //
221 // Open first
222 //
223 wxString sFilespec = m_sDirname;
224
225 if ( !wxEndsWithPathSeparator(sFilespec) )
6ef85b1b 226 {
6f38c86f 227 sFilespec += _T('\\');
6ef85b1b 228 }
6f38c86f
DW
229 sFilespec += (!m_sFilespec ? _T("*.*") : m_sFilespec.c_str());
230
231 m_vFinddata = FindFirst( sFilespec
232 ,PTR_TO_FINDDATA
233 );
234 bFirst = TRUE;
6ef85b1b 235 }
6ef85b1b 236
6f38c86f
DW
237 if ( !IsFindDataOk(m_vFinddata) )
238 {
239 return FALSE;
240 }
6ef85b1b 241
6f38c86f
DW
242 const wxChar* zName;
243 FIND_ATTR vAttr;
244
245 for ( ;; )
6ef85b1b 246 {
6f38c86f
DW
247 if (bFirst)
248 {
249 bFirst = FALSE;
250 }
251 else
6ef85b1b 252 {
6f38c86f
DW
253 if (!FindNext( m_vFinddata
254 ,PTR_TO_FINDDATA
255 ))
256 {
257 return FALSE;
258 }
259 }
260
261 zName = GetNameFromFindData(PTR_TO_FINDDATA);
262 vAttr = GetAttrFromFindData(PTR_TO_FINDDATA);
263
264 //
265 // Don't return "." and ".." unless asked for
266 //
267 if ( zName[0] == _T('.') &&
268 ((zName[1] == _T('.') && zName[2] == _T('\0')) ||
269 (zName[1] == _T('\0'))) )
270 {
271 if (!(m_nFlags & wxDIR_DOTDOT))
6ef85b1b
SN
272 continue;
273 }
274
6f38c86f
DW
275 //
276 // Check the type now
277 //
278 if (!(m_nFlags & wxDIR_FILES) && !IsDir(vAttr))
6ef85b1b 279 {
6f38c86f
DW
280 //
281 // It's a file, but we don't want them
282 //
6ef85b1b
SN
283 continue;
284 }
6f38c86f 285 else if (!(m_nFlags & wxDIR_DIRS) && IsDir(vAttr) )
6ef85b1b 286 {
6f38c86f
DW
287 //
288 // It's a dir, and we don't want it
289 //
6ef85b1b
SN
290 continue;
291 }
292
6f38c86f
DW
293 //
294 // Finally, check whether it's a hidden file
295 //
296 if (!(m_nFlags & wxDIR_HIDDEN))
6ef85b1b 297 {
6f38c86f
DW
298 if (IsHidden(vAttr))
299 {
300 //
301 // It's a hidden file, skip it
302 //
303 continue;
304 }
6ef85b1b 305 }
6f38c86f
DW
306 *psFilename = zName;
307 break;
6ef85b1b 308 }
6ef85b1b 309 return TRUE;
6f38c86f 310} // end of wxDirData::Read
6ef85b1b
SN
311
312// ----------------------------------------------------------------------------
313// wxDir helpers
314// ----------------------------------------------------------------------------
315
316/* static */
6f38c86f
DW
317bool wxDir::Exists(
318 const wxString& rsDir
319)
6ef85b1b 320{
6f38c86f
DW
321 return wxPathExists(rsDir);
322} // end of wxDir::Exists
6ef85b1b
SN
323
324// ----------------------------------------------------------------------------
325// wxDir construction/destruction
326// ----------------------------------------------------------------------------
327
6f38c86f
DW
328wxDir::wxDir(
329 const wxString& rsDirname
330)
6ef85b1b
SN
331{
332 m_data = NULL;
333
6f38c86f
DW
334 (void)Open(rsDirname);
335} // end of wxDir::wxDir
6ef85b1b 336
6f38c86f
DW
337bool wxDir::Open(
338 const wxString& rsDirname
339)
6ef85b1b
SN
340{
341 delete M_DIR;
6f38c86f 342 m_data = new wxDirData(rsDirname);
6ef85b1b 343 return TRUE;
6f38c86f 344} // end of wxDir::Open
6ef85b1b
SN
345
346bool wxDir::IsOpened() const
347{
348 return m_data != NULL;
6f38c86f 349} // end of wxDir::IsOpen
6ef85b1b
SN
350
351wxDir::~wxDir()
352{
353 delete M_DIR;
6f38c86f 354} // end of wxDir::~wxDir
6ef85b1b
SN
355
356// ----------------------------------------------------------------------------
357// wxDir enumerating
358// ----------------------------------------------------------------------------
359
6f38c86f
DW
360bool wxDir::GetFirst(
361 wxString* psFilename
362, const wxString& rsFilespec
363, int nFlags
364) const
6ef85b1b
SN
365{
366 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
6ef85b1b 367 M_DIR->Rewind();
6f38c86f
DW
368 M_DIR->SetFileSpec(rsFilespec);
369 M_DIR->SetFlags(nFlags);
370 return GetNext(psFilename);
371} // end of wxDir::GetFirst
372
373bool wxDir::GetNext(
374 wxString* psFilename
375) const
6ef85b1b
SN
376{
377 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
6f38c86f
DW
378 wxCHECK_MSG( psFilename, FALSE, _T("bad pointer in wxDir::GetNext()") );
379 return M_DIR->Read(psFilename);
380} // end of wxDir::GetNext
6ef85b1b 381