1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/dir.cpp
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(wxT("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( 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
+= wxT('\\');
228 sFilespec
+= (!m_sFilespec
? wxT("*.*") : 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] == wxT('.') &&
267 ((zName
[1] == wxT('.') && zName
[2] == wxT('\0')) ||
268 (zName
[1] == wxT('\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 // ----------------------------------------------------------------------------
312 // wxDir construction/destruction
313 // ----------------------------------------------------------------------------
316 const wxString
& rsDirname
321 (void)Open(rsDirname
);
322 } // end of wxDir::wxDir
325 const wxString
& rsDirname
329 m_data
= new wxDirData(rsDirname
);
331 } // end of wxDir::Open
333 bool wxDir::IsOpened() const
335 return m_data
!= NULL
;
336 } // end of wxDir::IsOpen
338 wxString
wxDir::GetName() const
343 name
= M_DIR
->GetName();
346 // bring to canonical Windows form
347 name
.Replace(wxT("/"), wxT("\\"));
349 if ( name
.Last() == wxT('\\') )
351 // chop off the last (back)slash
352 name
.Truncate(name
.length() - 1);
363 } // end of wxDir::~wxDir
365 // ----------------------------------------------------------------------------
367 // ----------------------------------------------------------------------------
369 bool wxDir::GetFirst(
371 , const wxString
& rsFilespec
375 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
377 M_DIR
->SetFileSpec(rsFilespec
);
378 M_DIR
->SetFlags(nFlags
);
379 return GetNext(psFilename
);
380 } // end of wxDir::GetFirst
386 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
387 wxCHECK_MSG( psFilename
, false, wxT("bad pointer in wxDir::GetNext()") );
388 return M_DIR
->Read(psFilename
);
389 } // end of wxDir::GetNext