]>
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
44 // ----------------------------------------------------------------------------
45 // define the types and functions used for file searching
46 // ----------------------------------------------------------------------------
48 typedef FILEFINDBUF3 FIND_STRUCT
;
49 typedef HDIR FIND_DATA
;
50 typedef ULONG FIND_ATTR
;
52 static inline FIND_DATA
InitFindData() { return ERROR_INVALID_HANDLE
; }
54 static inline bool IsFindDataOk(
58 return vFd
!= ERROR_INVALID_HANDLE
;
61 static inline void FreeFindData(
65 if (!::DosFindClose(vFd
))
67 wxLogLastError(_T("DosFindClose"));
71 static inline FIND_DATA
FindFirst(
72 const wxString
& rsSpec
73 , FIND_STRUCT
* pFinddata
76 ULONG ulFindCount
= 1;
80 rc
= ::DosFindFirst( rsSpec
.c_str()
93 static inline bool FindNext(
95 , FIND_STRUCT
* pFinddata
98 ULONG ulFindCount
= 1;
100 return ::DosFindNext( vFd
102 ,sizeof(FILEFINDBUF3
)
107 static const wxChar
* GetNameFromFindData(
108 FIND_STRUCT
* pFinddata
111 return pFinddata
->achName
;
114 static const FIND_ATTR
GetAttrFromFindData(
115 FIND_STRUCT
* pFinddata
118 return pFinddata
->attrFile
;
121 static inline bool IsDir(
125 return (vAttr
& FILE_DIRECTORY
) != 0;
128 static inline bool IsHidden(
132 return (vAttr
& (FILE_HIDDEN
| FILE_SYSTEM
)) != 0;
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
140 #define MAX_PATH 260 // from PM++ headers
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 #define M_DIR ((wxDirData *)m_data)
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 // this class stores everything we need to enumerate the files
157 wxDirData(const wxString
& rsDirname
);
160 void SetFileSpec(const wxString
& rsFilespec
) { m_sFilespec
= rsFilespec
; }
161 void SetFlags(int nFlags
) { m_nFlags
= nFlags
; }
165 bool Read(wxString
* rsFilename
);
168 FIND_DATA m_vFinddata
;
170 wxString m_sFilespec
;
172 }; // end of CLASS wxDirData
174 // ============================================================================
176 // ============================================================================
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
182 wxDirData::wxDirData(
183 const wxString
& rsDirname
185 : m_sDirname(rsDirname
)
187 m_vFinddata
= InitFindData();
188 } // end of wxDirData::wxDirData
190 wxDirData::~wxDirData()
193 } // end of wxDirData::~wxDirData
195 void wxDirData::Close()
197 if ( IsFindDataOk(m_vFinddata
) )
199 FreeFindData(m_vFinddata
);
200 m_vFinddata
= InitFindData();
202 } // end of wxDirData::Close
204 void wxDirData::Rewind()
207 } // end of wxDirData::Rewind
209 bool wxDirData::Read(
215 FILEFINDBUF3 vFinddata
;
216 #define PTR_TO_FINDDATA (&vFinddata)
218 if (!IsFindDataOk(m_vFinddata
))
223 wxString sFilespec
= m_sDirname
;
225 if ( !wxEndsWithPathSeparator(sFilespec
) )
227 sFilespec
+= _T('\\');
229 sFilespec
+= (!m_sFilespec
? _T("*.*") : m_sFilespec
.c_str());
231 m_vFinddata
= FindFirst( sFilespec
237 if ( !IsFindDataOk(m_vFinddata
) )
253 if (!FindNext( m_vFinddata
261 zName
= GetNameFromFindData(PTR_TO_FINDDATA
);
262 vAttr
= GetAttrFromFindData(PTR_TO_FINDDATA
);
265 // Don't return "." and ".." unless asked for
267 if ( zName
[0] == _T('.') &&
268 ((zName
[1] == _T('.') && zName
[2] == _T('\0')) ||
269 (zName
[1] == _T('\0'))) )
271 if (!(m_nFlags
& wxDIR_DOTDOT
))
276 // Check the type now
278 if (!(m_nFlags
& wxDIR_FILES
) && !IsDir(vAttr
))
281 // It's a file, but we don't want them
285 else if (!(m_nFlags
& wxDIR_DIRS
) && IsDir(vAttr
) )
288 // It's a dir, and we don't want it
294 // Finally, check whether it's a hidden file
296 if (!(m_nFlags
& wxDIR_HIDDEN
))
301 // It's a hidden file, skip it
310 } // end of wxDirData::Read
312 // ----------------------------------------------------------------------------
314 // ----------------------------------------------------------------------------
318 const wxString
& rsDir
321 return wxPathExists(rsDir
);
322 } // end of wxDir::Exists
324 // ----------------------------------------------------------------------------
325 // wxDir construction/destruction
326 // ----------------------------------------------------------------------------
329 const wxString
& rsDirname
334 (void)Open(rsDirname
);
335 } // end of wxDir::wxDir
338 const wxString
& rsDirname
342 m_data
= new wxDirData(rsDirname
);
344 } // end of wxDir::Open
346 bool wxDir::IsOpened() const
348 return m_data
!= NULL
;
349 } // end of wxDir::IsOpen
354 } // end of wxDir::~wxDir
356 // ----------------------------------------------------------------------------
358 // ----------------------------------------------------------------------------
360 bool wxDir::GetFirst(
362 , const wxString
& rsFilespec
366 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
368 M_DIR
->SetFileSpec(rsFilespec
);
369 M_DIR
->SetFlags(nFlags
);
370 return GetNext(psFilename
);
371 } // end of wxDir::GetFirst
377 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
378 wxCHECK_MSG( psFilename
, FALSE
, _T("bad pointer in wxDir::GetNext()") );
379 return M_DIR
->Read(psFilename
);
380 } // end of wxDir::GetNext