]>
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"
28 #include "wx/os2/private.h"
34 #include "wx/filefn.h" // for wxMatchWild
36 #include <sys/types.h>
38 #define INCL_DOSFILEMGR
39 #define INCL_DOSERRORS
46 // ----------------------------------------------------------------------------
47 // define the types and functions used for file searching
48 // ----------------------------------------------------------------------------
50 typedef FILEFINDBUF3 FIND_STRUCT
;
51 typedef HDIR FIND_DATA
;
52 typedef ULONG FIND_ATTR
;
54 static inline FIND_DATA
InitFindData() { return ERROR_INVALID_HANDLE
; }
56 static inline bool IsFindDataOk(
60 return vFd
!= ERROR_INVALID_HANDLE
;
63 static inline void FreeFindData(
67 if (!::DosFindClose(vFd
))
69 wxLogLastError(_T("DosFindClose"));
73 static inline FIND_DATA
FindFirst(
74 const wxString
& rsSpec
75 , FIND_STRUCT
* pFinddata
78 ULONG ulFindCount
= 1;
82 rc
= ::DosFindFirst( rsSpec
.c_str()
95 static inline bool FindNext(
97 , FIND_STRUCT
* pFinddata
100 ULONG ulFindCount
= 1;
102 return ::DosFindNext( vFd
104 ,sizeof(FILEFINDBUF3
)
109 static const wxChar
* GetNameFromFindData(
110 FIND_STRUCT
* pFinddata
113 return pFinddata
->achName
;
116 static const FIND_ATTR
GetAttrFromFindData(
117 FIND_STRUCT
* pFinddata
120 return pFinddata
->attrFile
;
123 static inline bool IsDir(
127 return (vAttr
& FILE_DIRECTORY
) != 0;
130 static inline bool IsHidden(
134 return (vAttr
& (FILE_HIDDEN
| FILE_SYSTEM
)) != 0;
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
142 #define MAX_PATH 260 // from PM++ headers
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
149 #define M_DIR ((wxDirData *)m_data)
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
155 // this class stores everything we need to enumerate the files
159 wxDirData(const wxString
& rsDirname
);
162 void SetFileSpec(const wxString
& rsFilespec
) { m_sFilespec
= rsFilespec
; }
163 void SetFlags(int nFlags
) { m_nFlags
= nFlags
; }
165 const wxString
& GetName() const { return m_sDirname
; }
168 bool Read(wxString
* rsFilename
);
171 FIND_DATA m_vFinddata
;
173 wxString m_sFilespec
;
175 }; // end of CLASS wxDirData
177 // ============================================================================
179 // ============================================================================
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
185 wxDirData::wxDirData(
186 const wxString
& rsDirname
188 : m_sDirname(rsDirname
)
190 m_vFinddata
= InitFindData();
191 } // end of wxDirData::wxDirData
193 wxDirData::~wxDirData()
196 } // end of wxDirData::~wxDirData
198 void wxDirData::Close()
200 if ( IsFindDataOk(m_vFinddata
) )
202 FreeFindData(m_vFinddata
);
203 m_vFinddata
= InitFindData();
205 } // end of wxDirData::Close
207 void wxDirData::Rewind()
210 } // end of wxDirData::Rewind
212 bool wxDirData::Read(
218 FILEFINDBUF3 vFinddata
;
219 #define PTR_TO_FINDDATA (&vFinddata)
221 if (!IsFindDataOk(m_vFinddata
))
226 wxString sFilespec
= m_sDirname
;
228 if ( !wxEndsWithPathSeparator(sFilespec
) )
230 sFilespec
+= _T('\\');
232 sFilespec
+= (!m_sFilespec
? _T("*.*") : m_sFilespec
.c_str());
234 m_vFinddata
= FindFirst( sFilespec
240 if ( !IsFindDataOk(m_vFinddata
) )
256 if (!FindNext( m_vFinddata
264 zName
= GetNameFromFindData(PTR_TO_FINDDATA
);
265 vAttr
= GetAttrFromFindData(PTR_TO_FINDDATA
);
268 // Don't return "." and ".." unless asked for
270 if ( zName
[0] == _T('.') &&
271 ((zName
[1] == _T('.') && zName
[2] == _T('\0')) ||
272 (zName
[1] == _T('\0'))) )
274 if (!(m_nFlags
& wxDIR_DOTDOT
))
279 // Check the type now
281 if (!(m_nFlags
& wxDIR_FILES
) && !IsDir(vAttr
))
284 // It's a file, but we don't want them
288 else if (!(m_nFlags
& wxDIR_DIRS
) && IsDir(vAttr
) )
291 // It's a dir, and we don't want it
297 // Finally, check whether it's a hidden file
299 if (!(m_nFlags
& wxDIR_HIDDEN
))
304 // It's a hidden file, skip it
313 } // end of wxDirData::Read
315 // ----------------------------------------------------------------------------
317 // ----------------------------------------------------------------------------
321 const wxString
& rsDir
324 return wxPathExists(rsDir
);
325 } // end of wxDir::Exists
327 // ----------------------------------------------------------------------------
328 // wxDir construction/destruction
329 // ----------------------------------------------------------------------------
332 const wxString
& rsDirname
337 (void)Open(rsDirname
);
338 } // end of wxDir::wxDir
341 const wxString
& rsDirname
345 m_data
= new wxDirData(rsDirname
);
347 } // end of wxDir::Open
349 bool wxDir::IsOpened() const
351 return m_data
!= NULL
;
352 } // end of wxDir::IsOpen
354 wxString
wxDir::GetName() const
359 name
= M_DIR
->GetName();
362 // bring to canonical Windows form
363 name
.Replace(_T("/"), _T("\\"));
365 if ( name
.Last() == _T('\\') )
367 // chop off the last (back)slash
368 name
.Truncate(name
.length() - 1);
379 } // end of wxDir::~wxDir
381 // ----------------------------------------------------------------------------
383 // ----------------------------------------------------------------------------
385 bool wxDir::GetFirst(
387 , const wxString
& rsFilespec
391 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
393 M_DIR
->SetFileSpec(rsFilespec
);
394 M_DIR
->SetFlags(nFlags
);
395 return GetNext(psFilename
);
396 } // end of wxDir::GetFirst
402 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
403 wxCHECK_MSG( psFilename
, FALSE
, _T("bad pointer in wxDir::GetNext()") );
404 return M_DIR
->Read(psFilename
);
405 } // end of wxDir::GetNext