]>
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>
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 #define M_DIR ((wxDirData *)m_data)
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // this class stores everything we need to enumerate the files
55 wxDirData(const wxString
& dirname
);
58 bool IsOk() const { return m_dir
!= NULL
; }
60 void SetFileSpec(const wxString
& filespec
) { m_filespec
= filespec
; }
61 void SetFlags(int flags
) { m_flags
= flags
; }
63 void Rewind() { rewinddir(m_dir
); }
64 bool Read(wxString
*filename
);
75 // ============================================================================
77 // ============================================================================
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 wxDirData::wxDirData(const wxString
& dirname
)
88 // throw away the trailing slashes
89 size_t n
= m_dirname
.length();
90 wxCHECK_RET( n
, _T("empty dir name in wxDir") );
92 while ( n
> 0 && m_dirname
[--n
] == '/' )
95 m_dirname
.Truncate(n
+ 1);
98 m_dir
= opendir(m_dirname
.fn_str());
101 wxDirData::~wxDirData()
105 if ( closedir(m_dir
) != 0 )
107 wxLogLastError(_T("closedir"));
112 bool wxDirData::Read(wxString
*filename
)
115 bool matches
= FALSE
;
123 // don't return "." and ".." unless asked for
124 if ( de
->d_name
[0] == '.' &&
125 ((de
->d_name
[1] == '.' && de
->d_name
[2] == '\0') ||
126 (de
->d_name
[1] == '\0')) )
128 if ( !(m_flags
& wxDIR_DOTDOT
) )
132 // check the type now
133 if ( !(m_flags
& wxDIR_FILES
) &&
134 !wxDir::Exists(m_dirname
+ _T('/') + de
->d_name
) )
136 // it's a file, but we don't want them
139 else if ( !(m_flags
& wxDIR_DIRS
) &&
140 wxDir::Exists(m_dirname
+ _T('/') + de
->d_name
) )
142 // it's a dir, and we don't want it
146 // finally, check the name
149 matches
= m_flags
& wxDIR_HIDDEN
? TRUE
: de
->d_name
[0] != '.';
153 // test against the pattern
154 matches
= wxMatchWild(m_filespec
, de
->d_name
,
155 !(m_flags
& wxDIR_HIDDEN
));
159 *filename
= de
->d_name
;
164 // ----------------------------------------------------------------------------
166 // ----------------------------------------------------------------------------
169 bool wxDir::Exists(const wxString
& dir
)
171 return wxPathExists(dir
);
174 // ----------------------------------------------------------------------------
175 // wxDir construction/destruction
176 // ----------------------------------------------------------------------------
178 wxDir::wxDir(const wxString
& dirname
)
185 bool wxDir::Open(const wxString
& dirname
)
188 m_data
= new wxDirData(dirname
);
190 if ( !M_DIR
->IsOk() )
192 wxLogSysError(_("Can not enumerate files in directory '%s'"),
204 bool wxDir::IsOpened() const
206 return m_data
!= NULL
;
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 bool wxDir::GetFirst(wxString
*filename
,
219 const wxString
& filespec
,
222 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
226 M_DIR
->SetFileSpec(filespec
);
227 M_DIR
->SetFlags(flags
);
229 return GetNext(filename
);
232 bool wxDir::GetNext(wxString
*filename
) const
234 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
236 wxCHECK_MSG( filename
, FALSE
, _T("bad pointer in wxDir::GetNext()") );
238 return M_DIR
->Read(filename
);