]>
git.saurik.com Git - wxWidgets.git/blob - src/os2/dir.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/dir.cpp
3 // Purpose: wxDir implementation for OS/2
4 // Author: Vadim Zeitlin
5 // Modified by: Stefan Neis
7 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
23 #include "wx/os2/private.h"
29 #include "wx/filefn.h" // for wxMatchWild
31 #include <sys/types.h>
33 #define INCL_DOSFILEMGR
34 #define INCL_DOSERRORS
41 // ----------------------------------------------------------------------------
42 // define the types and functions used for file searching
43 // ----------------------------------------------------------------------------
45 typedef FILEFINDBUF3 FIND_STRUCT
;
46 typedef HDIR FIND_DATA
;
47 typedef ULONG FIND_ATTR
;
49 static inline FIND_DATA
InitFindData() { return ERROR_INVALID_HANDLE
; }
51 static inline bool IsFindDataOk(
55 return vFd
!= ERROR_INVALID_HANDLE
;
58 static inline void FreeFindData(
62 if (!::DosFindClose(vFd
))
64 wxLogLastError(wxT("DosFindClose"));
68 static inline FIND_DATA
FindFirst(
69 const wxString
& rsSpec
70 , FIND_STRUCT
* pFinddata
73 ULONG ulFindCount
= 1;
74 FIND_DATA hDir
= HDIR_CREATE
;
77 rc
= ::DosFindFirst( rsSpec
.c_str()
79 ,0x37 // was: FILE_NORMAL
86 return InitFindData();
90 static inline bool FindNext(
92 , FIND_STRUCT
* pFinddata
95 ULONG ulFindCount
= 1;
97 return ::DosFindNext( vFd
104 static const wxChar
* GetNameFromFindData(
105 FIND_STRUCT
* pFinddata
108 return (wxChar
*)pFinddata
->achName
;
111 static const FIND_ATTR
GetAttrFromFindData(
112 FIND_STRUCT
* pFinddata
115 return pFinddata
->attrFile
;
118 static inline bool IsDir(
122 return (vAttr
& FILE_DIRECTORY
) != 0;
125 static inline bool IsHidden(
129 return (vAttr
& (FILE_HIDDEN
| FILE_SYSTEM
)) != 0;
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
137 #define MAX_PATH 260 // from PM++ headers
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 #define M_DIR ((wxDirData *)m_data)
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
150 // this class stores everything we need to enumerate the files
154 wxDirData(const wxString
& rsDirname
);
157 void SetFileSpec(const wxString
& rsFilespec
) { m_sFilespec
= rsFilespec
; }
158 void SetFlags(int nFlags
) { m_nFlags
= nFlags
; }
160 const wxString
& GetName() const { return m_sDirname
; }
163 bool Read(wxString
* rsFilename
);
166 FIND_DATA m_vFinddata
;
168 wxString m_sFilespec
;
170 }; // end of CLASS wxDirData
172 // ============================================================================
174 // ============================================================================
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 wxDirData::wxDirData(
181 const wxString
& rsDirname
183 : m_sDirname(rsDirname
)
185 m_vFinddata
= InitFindData();
186 } // end of wxDirData::wxDirData
188 wxDirData::~wxDirData()
191 } // end of wxDirData::~wxDirData
193 void wxDirData::Close()
195 if ( IsFindDataOk(m_vFinddata
) )
197 FreeFindData(m_vFinddata
);
198 m_vFinddata
= InitFindData();
200 } // end of wxDirData::Close
202 void wxDirData::Rewind()
205 } // end of wxDirData::Rewind
207 bool wxDirData::Read(
213 FILEFINDBUF3 vFinddata
;
214 #define PTR_TO_FINDDATA (&vFinddata)
216 if (!IsFindDataOk(m_vFinddata
))
221 wxString sFilespec
= m_sDirname
;
223 if ( !wxEndsWithPathSeparator(sFilespec
) )
225 sFilespec
+= wxT('\\');
227 sFilespec
+= (!m_sFilespec
? wxT("*.*") : m_sFilespec
.c_str());
229 m_vFinddata
= FindFirst( sFilespec
235 if ( !IsFindDataOk(m_vFinddata
) )
251 if (!FindNext( m_vFinddata
259 zName
= GetNameFromFindData(PTR_TO_FINDDATA
);
260 vAttr
= GetAttrFromFindData(PTR_TO_FINDDATA
);
263 // Don't return "." and ".." unless asked for
265 if ( zName
[0] == wxT('.') &&
266 ((zName
[1] == wxT('.') && zName
[2] == wxT('\0')) ||
267 (zName
[1] == wxT('\0'))) )
269 if (!(m_nFlags
& wxDIR_DOTDOT
))
274 // Check the type now
276 if (!(m_nFlags
& wxDIR_FILES
) && !IsDir(vAttr
))
279 // It's a file, but we don't want them
283 else if (!(m_nFlags
& wxDIR_DIRS
) && IsDir(vAttr
) )
286 // It's a dir, and we don't want it
292 // Finally, check whether it's a hidden file
294 if (!(m_nFlags
& wxDIR_HIDDEN
))
299 // It's a hidden file, skip it
308 } // end of wxDirData::Read
310 // ----------------------------------------------------------------------------
311 // wxDir construction/destruction
312 // ----------------------------------------------------------------------------
315 const wxString
& rsDirname
320 (void)Open(rsDirname
);
321 } // end of wxDir::wxDir
324 const wxString
& rsDirname
328 m_data
= new wxDirData(rsDirname
);
330 } // end of wxDir::Open
332 bool wxDir::IsOpened() const
334 return m_data
!= NULL
;
335 } // end of wxDir::IsOpen
337 wxString
wxDir::GetName() const
342 name
= M_DIR
->GetName();
345 // bring to canonical Windows form
346 name
.Replace(wxT("/"), wxT("\\"));
348 if ( name
.Last() == wxT('\\') )
350 // chop off the last (back)slash
351 name
.Truncate(name
.length() - 1);
362 } // end of wxDir::~wxDir
364 // ----------------------------------------------------------------------------
366 // ----------------------------------------------------------------------------
368 bool wxDir::GetFirst(
370 , const wxString
& rsFilespec
374 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
376 M_DIR
->SetFileSpec(rsFilespec
);
377 M_DIR
->SetFlags(nFlags
);
378 return GetNext(psFilename
);
379 } // end of wxDir::GetFirst
385 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
386 wxCHECK_MSG( psFilename
, false, wxT("bad pointer in wxDir::GetNext()") );
387 return M_DIR
->Read(psFilename
);
388 } // end of wxDir::GetNext