]>
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 licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
24 #include "wx/os2/private.h"
30 #include "wx/filefn.h" // for wxMatchWild
32 #include <sys/types.h>
34 #define INCL_DOSFILEMGR
35 #define INCL_DOSERRORS
42 // ----------------------------------------------------------------------------
43 // define the types and functions used for file searching
44 // ----------------------------------------------------------------------------
46 typedef FILEFINDBUF3 FIND_STRUCT
;
47 typedef HDIR FIND_DATA
;
48 typedef ULONG FIND_ATTR
;
50 static inline FIND_DATA
InitFindData() { return ERROR_INVALID_HANDLE
; }
52 static inline bool IsFindDataOk(
56 return vFd
!= ERROR_INVALID_HANDLE
;
59 static inline void FreeFindData(
63 if (!::DosFindClose(vFd
))
65 wxLogLastError(_T("DosFindClose"));
69 static inline FIND_DATA
FindFirst(
70 const wxString
& rsSpec
71 , FIND_STRUCT
* pFinddata
74 ULONG ulFindCount
= 1;
75 FIND_DATA hDir
= HDIR_CREATE
;
78 rc
= ::DosFindFirst( (PSZ
)rsSpec
.c_str()
80 ,0x37 // was: FILE_NORMAL
87 return InitFindData();
91 static inline bool FindNext(
93 , FIND_STRUCT
* pFinddata
96 ULONG ulFindCount
= 1;
98 return ::DosFindNext( vFd
100 ,sizeof(FILEFINDBUF3
)
105 static const wxChar
* GetNameFromFindData(
106 FIND_STRUCT
* pFinddata
109 return (wxChar
*)pFinddata
->achName
;
112 static const FIND_ATTR
GetAttrFromFindData(
113 FIND_STRUCT
* pFinddata
116 return pFinddata
->attrFile
;
119 static inline bool IsDir(
123 return (vAttr
& FILE_DIRECTORY
) != 0;
126 static inline bool IsHidden(
130 return (vAttr
& (FILE_HIDDEN
| FILE_SYSTEM
)) != 0;
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
138 #define MAX_PATH 260 // from PM++ headers
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
145 #define M_DIR ((wxDirData *)m_data)
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
151 // this class stores everything we need to enumerate the files
155 wxDirData(const wxString
& rsDirname
);
158 void SetFileSpec(const wxString
& rsFilespec
) { m_sFilespec
= rsFilespec
; }
159 void SetFlags(int nFlags
) { m_nFlags
= nFlags
; }
161 const wxString
& GetName() const { return m_sDirname
; }
164 bool Read(wxString
* rsFilename
);
167 FIND_DATA m_vFinddata
;
169 wxString m_sFilespec
;
171 }; // end of CLASS wxDirData
173 // ============================================================================
175 // ============================================================================
177 // ----------------------------------------------------------------------------
179 // ----------------------------------------------------------------------------
181 wxDirData::wxDirData(
182 const wxString
& rsDirname
184 : m_sDirname(rsDirname
)
186 m_vFinddata
= InitFindData();
187 } // end of wxDirData::wxDirData
189 wxDirData::~wxDirData()
192 } // end of wxDirData::~wxDirData
194 void wxDirData::Close()
196 if ( IsFindDataOk(m_vFinddata
) )
198 FreeFindData(m_vFinddata
);
199 m_vFinddata
= InitFindData();
201 } // end of wxDirData::Close
203 void wxDirData::Rewind()
206 } // end of wxDirData::Rewind
208 bool wxDirData::Read(
214 FILEFINDBUF3 vFinddata
;
215 #define PTR_TO_FINDDATA (&vFinddata)
217 if (!IsFindDataOk(m_vFinddata
))
222 wxString sFilespec
= m_sDirname
;
224 if ( !wxEndsWithPathSeparator(sFilespec
) )
226 sFilespec
+= _T('\\');
228 sFilespec
+= (!m_sFilespec
? _T("*.*") : m_sFilespec
.c_str());
230 m_vFinddata
= FindFirst( sFilespec
236 if ( !IsFindDataOk(m_vFinddata
) )
252 if (!FindNext( m_vFinddata
260 zName
= GetNameFromFindData(PTR_TO_FINDDATA
);
261 vAttr
= GetAttrFromFindData(PTR_TO_FINDDATA
);
264 // Don't return "." and ".." unless asked for
266 if ( zName
[0] == _T('.') &&
267 ((zName
[1] == _T('.') && zName
[2] == _T('\0')) ||
268 (zName
[1] == _T('\0'))) )
270 if (!(m_nFlags
& wxDIR_DOTDOT
))
275 // Check the type now
277 if (!(m_nFlags
& wxDIR_FILES
) && !IsDir(vAttr
))
280 // It's a file, but we don't want them
284 else if (!(m_nFlags
& wxDIR_DIRS
) && IsDir(vAttr
) )
287 // It's a dir, and we don't want it
293 // Finally, check whether it's a hidden file
295 if (!(m_nFlags
& wxDIR_HIDDEN
))
300 // It's a hidden file, skip it
309 } // end of wxDirData::Read
311 // ----------------------------------------------------------------------------
313 // ----------------------------------------------------------------------------
317 const wxString
& rsDir
320 return wxDirExists(rsDir
);
321 } // end of wxDir::Exists
323 // ----------------------------------------------------------------------------
324 // wxDir construction/destruction
325 // ----------------------------------------------------------------------------
328 const wxString
& rsDirname
333 (void)Open(rsDirname
);
334 } // end of wxDir::wxDir
337 const wxString
& rsDirname
341 m_data
= new wxDirData(rsDirname
);
343 } // end of wxDir::Open
345 bool wxDir::IsOpened() const
347 return m_data
!= NULL
;
348 } // end of wxDir::IsOpen
350 wxString
wxDir::GetName() const
355 name
= M_DIR
->GetName();
358 // bring to canonical Windows form
359 name
.Replace(_T("/"), _T("\\"));
361 if ( name
.Last() == _T('\\') )
363 // chop off the last (back)slash
364 name
.Truncate(name
.length() - 1);
375 } // end of wxDir::~wxDir
377 // ----------------------------------------------------------------------------
379 // ----------------------------------------------------------------------------
381 bool wxDir::GetFirst(
383 , const wxString
& rsFilespec
387 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
389 M_DIR
->SetFileSpec(rsFilespec
);
390 M_DIR
->SetFlags(nFlags
);
391 return GetNext(psFilename
);
392 } // end of wxDir::GetFirst
398 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
399 wxCHECK_MSG( psFilename
, false, _T("bad pointer in wxDir::GetNext()") );
400 return M_DIR
->Read(psFilename
);
401 } // end of wxDir::GetNext