]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/dir.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDir implementation for OS/2
4 // Author: Vadim Zeitlin
5 // Modified by: Stefan Neis
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "dir.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/filefn.h" // for wxMatchWild
35 #include <sys/types.h>
37 #define INCL_DOSFILEMGR
38 #define INCL_DOSERRORS
45 // ----------------------------------------------------------------------------
46 // define the types and functions used for file searching
47 // ----------------------------------------------------------------------------
49 typedef FILEFINDBUF3 FIND_STRUCT
;
50 typedef HDIR FIND_DATA
;
51 typedef ULONG FIND_ATTR
;
53 static inline FIND_DATA
InitFindData() { return ERROR_INVALID_HANDLE
; }
55 static inline bool IsFindDataOk(
59 return vFd
!= ERROR_INVALID_HANDLE
;
62 static inline void FreeFindData(
66 if (!::DosFindClose(vFd
))
68 wxLogLastError(_T("DosFindClose"));
72 static inline FIND_DATA
FindFirst(
73 const wxString
& rsSpec
74 , FIND_STRUCT
* pFinddata
77 ULONG ulFindCount
= 1;
81 rc
= ::DosFindFirst( rsSpec
.c_str()
94 static inline bool FindNext(
96 , FIND_STRUCT
* pFinddata
99 ULONG ulFindCount
= 1;
101 return ::DosFindNext( vFd
103 ,sizeof(FILEFINDBUF3
)
108 static const wxChar
* GetNameFromFindData(
109 FIND_STRUCT
* pFinddata
112 return pFinddata
->achName
;
115 static const FIND_ATTR
GetAttrFromFindData(
116 FIND_STRUCT
* pFinddata
119 return pFinddata
->attrFile
;
122 static inline bool IsDir(
126 return (vAttr
& FILE_DIRECTORY
) != 0;
129 static inline bool IsHidden(
133 return (vAttr
& (FILE_HIDDEN
| FILE_SYSTEM
)) != 0;
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
141 #define MAX_PATH 260 // from PM++ headers
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 #define M_DIR ((wxDirData *)m_data)
150 // ----------------------------------------------------------------------------
152 // ----------------------------------------------------------------------------
154 // this class stores everything we need to enumerate the files
158 wxDirData(const wxString
& rsDirname
);
161 void SetFileSpec(const wxString
& rsFilespec
) { m_sFilespec
= rsFilespec
; }
162 void SetFlags(int nFlags
) { m_nFlags
= nFlags
; }
164 const wxString
& GetName() const { return m_sDirname
; }
167 bool Read(wxString
* rsFilename
);
170 FIND_DATA m_vFinddata
;
172 wxString m_sFilespec
;
174 }; // end of CLASS wxDirData
176 // ============================================================================
178 // ============================================================================
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
184 wxDirData::wxDirData(
185 const wxString
& rsDirname
187 : m_sDirname(rsDirname
)
189 m_vFinddata
= InitFindData();
190 } // end of wxDirData::wxDirData
192 wxDirData::~wxDirData()
195 } // end of wxDirData::~wxDirData
197 void wxDirData::Close()
199 if ( IsFindDataOk(m_vFinddata
) )
201 FreeFindData(m_vFinddata
);
202 m_vFinddata
= InitFindData();
204 } // end of wxDirData::Close
206 void wxDirData::Rewind()
209 } // end of wxDirData::Rewind
211 bool wxDirData::Read(
217 FILEFINDBUF3 vFinddata
;
218 #define PTR_TO_FINDDATA (&vFinddata)
220 if (!IsFindDataOk(m_vFinddata
))
225 wxString sFilespec
= m_sDirname
;
227 if ( !wxEndsWithPathSeparator(sFilespec
) )
229 sFilespec
+= _T('\\');
231 sFilespec
+= (!m_sFilespec
? _T("*.*") : m_sFilespec
.c_str());
233 m_vFinddata
= FindFirst( sFilespec
239 if ( !IsFindDataOk(m_vFinddata
) )
255 if (!FindNext( m_vFinddata
263 zName
= GetNameFromFindData(PTR_TO_FINDDATA
);
264 vAttr
= GetAttrFromFindData(PTR_TO_FINDDATA
);
267 // Don't return "." and ".." unless asked for
269 if ( zName
[0] == _T('.') &&
270 ((zName
[1] == _T('.') && zName
[2] == _T('\0')) ||
271 (zName
[1] == _T('\0'))) )
273 if (!(m_nFlags
& wxDIR_DOTDOT
))
278 // Check the type now
280 if (!(m_nFlags
& wxDIR_FILES
) && !IsDir(vAttr
))
283 // It's a file, but we don't want them
287 else if (!(m_nFlags
& wxDIR_DIRS
) && IsDir(vAttr
) )
290 // It's a dir, and we don't want it
296 // Finally, check whether it's a hidden file
298 if (!(m_nFlags
& wxDIR_HIDDEN
))
303 // It's a hidden file, skip it
312 } // end of wxDirData::Read
314 // ----------------------------------------------------------------------------
316 // ----------------------------------------------------------------------------
320 const wxString
& rsDir
323 return wxPathExists(rsDir
);
324 } // end of wxDir::Exists
326 // ----------------------------------------------------------------------------
327 // wxDir construction/destruction
328 // ----------------------------------------------------------------------------
331 const wxString
& rsDirname
336 (void)Open(rsDirname
);
337 } // end of wxDir::wxDir
340 const wxString
& rsDirname
344 m_data
= new wxDirData(rsDirname
);
346 } // end of wxDir::Open
348 bool wxDir::IsOpened() const
350 return m_data
!= NULL
;
351 } // end of wxDir::IsOpen
353 wxString
wxDir::GetName() const
358 name
= M_DIR
->GetName();
361 // bring to canonical Windows form
362 name
.Replace(_T("/"), _T("\\"));
364 if ( name
.Last() == _T('\\') )
366 // chop off the last (back)slash
367 name
.Truncate(name
.length() - 1);
378 } // end of wxDir::~wxDir
380 // ----------------------------------------------------------------------------
382 // ----------------------------------------------------------------------------
384 bool wxDir::GetFirst(
386 , const wxString
& rsFilespec
390 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
392 M_DIR
->SetFileSpec(rsFilespec
);
393 M_DIR
->SetFlags(nFlags
);
394 return GetNext(psFilename
);
395 } // end of wxDir::GetFirst
401 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
402 wxCHECK_MSG( psFilename
, FALSE
, _T("bad pointer in wxDir::GetNext()") );
403 return M_DIR
->Read(psFilename
);
404 } // end of wxDir::GetNext