]>
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
; }
163 const wxString
& GetName() const { return m_sDirname
; }
166 bool Read(wxString
* rsFilename
);
169 FIND_DATA m_vFinddata
;
171 wxString m_sFilespec
;
173 }; // end of CLASS wxDirData
175 // ============================================================================
177 // ============================================================================
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
183 wxDirData::wxDirData(
184 const wxString
& rsDirname
186 : m_sDirname(rsDirname
)
188 m_vFinddata
= InitFindData();
189 } // end of wxDirData::wxDirData
191 wxDirData::~wxDirData()
194 } // end of wxDirData::~wxDirData
196 void wxDirData::Close()
198 if ( IsFindDataOk(m_vFinddata
) )
200 FreeFindData(m_vFinddata
);
201 m_vFinddata
= InitFindData();
203 } // end of wxDirData::Close
205 void wxDirData::Rewind()
208 } // end of wxDirData::Rewind
210 bool wxDirData::Read(
216 FILEFINDBUF3 vFinddata
;
217 #define PTR_TO_FINDDATA (&vFinddata)
219 if (!IsFindDataOk(m_vFinddata
))
224 wxString sFilespec
= m_sDirname
;
226 if ( !wxEndsWithPathSeparator(sFilespec
) )
228 sFilespec
+= _T('\\');
230 sFilespec
+= (!m_sFilespec
? _T("*.*") : m_sFilespec
.c_str());
232 m_vFinddata
= FindFirst( sFilespec
238 if ( !IsFindDataOk(m_vFinddata
) )
254 if (!FindNext( m_vFinddata
262 zName
= GetNameFromFindData(PTR_TO_FINDDATA
);
263 vAttr
= GetAttrFromFindData(PTR_TO_FINDDATA
);
266 // Don't return "." and ".." unless asked for
268 if ( zName
[0] == _T('.') &&
269 ((zName
[1] == _T('.') && zName
[2] == _T('\0')) ||
270 (zName
[1] == _T('\0'))) )
272 if (!(m_nFlags
& wxDIR_DOTDOT
))
277 // Check the type now
279 if (!(m_nFlags
& wxDIR_FILES
) && !IsDir(vAttr
))
282 // It's a file, but we don't want them
286 else if (!(m_nFlags
& wxDIR_DIRS
) && IsDir(vAttr
) )
289 // It's a dir, and we don't want it
295 // Finally, check whether it's a hidden file
297 if (!(m_nFlags
& wxDIR_HIDDEN
))
302 // It's a hidden file, skip it
311 } // end of wxDirData::Read
313 // ----------------------------------------------------------------------------
315 // ----------------------------------------------------------------------------
319 const wxString
& rsDir
322 return wxPathExists(rsDir
);
323 } // end of wxDir::Exists
325 // ----------------------------------------------------------------------------
326 // wxDir construction/destruction
327 // ----------------------------------------------------------------------------
330 const wxString
& rsDirname
335 (void)Open(rsDirname
);
336 } // end of wxDir::wxDir
339 const wxString
& rsDirname
343 m_data
= new wxDirData(rsDirname
);
345 } // end of wxDir::Open
347 bool wxDir::IsOpened() const
349 return m_data
!= NULL
;
350 } // end of wxDir::IsOpen
352 wxString
wxDir::GetName() const
357 name
= M_DIR
->GetName();
360 // bring to canonical Windows form
361 name
.Replace(_T("/"), _T("\\"));
363 if ( name
.Last() == _T('\\') )
365 // chop off the last (back)slash
366 name
.Truncate(name
.length() - 1);
377 } // end of wxDir::~wxDir
379 // ----------------------------------------------------------------------------
381 // ----------------------------------------------------------------------------
383 bool wxDir::GetFirst(
385 , const wxString
& rsFilespec
389 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
391 M_DIR
->SetFileSpec(rsFilespec
);
392 M_DIR
->SetFlags(nFlags
);
393 return GetNext(psFilename
);
394 } // end of wxDir::GetFirst
400 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
401 wxCHECK_MSG( psFilename
, FALSE
, _T("bad pointer in wxDir::GetNext()") );
402 return M_DIR
->Read(psFilename
);
403 } // end of wxDir::GetNext