]>
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>
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 #define M_DIR ((wxDirData *)m_data)
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 // this class stores everything we need to enumerate the files
57 wxDirData(const wxString
& dirname
);
60 bool IsOk() const { return m_dir
!= NULL
; }
62 void SetFileSpec(const wxString
& filespec
) { m_filespec
= filespec
; }
63 void SetFlags(int flags
) { m_flags
= flags
; }
65 void Rewind() { rewinddir(m_dir
); }
66 bool Read(wxString
*filename
);
77 // ============================================================================
79 // ============================================================================
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 wxDirData::wxDirData(const wxString
& dirname
)
90 // throw away the trailing slashes
91 size_t n
= m_dirname
.length();
92 wxCHECK_RET( n
, _T("empty dir name in wxDir") );
94 while ( n
> 0 && m_dirname
[--n
] == '/' )
97 m_dirname
.Truncate(n
+ 1);
100 m_dir
= opendir(m_dirname
.fn_str());
103 wxDirData::~wxDirData()
107 if ( closedir(m_dir
) != 0 )
109 wxLogLastError(_T("closedir"));
114 bool wxDirData::Read(wxString
*filename
)
117 bool matches
= FALSE
;
125 // don't return "." and ".." unless asked for
126 if ( de
->d_name
[0] == '.' &&
127 ((de
->d_name
[1] == '.' && de
->d_name
[2] == '\0') ||
128 (de
->d_name
[1] == '\0')) )
130 if ( !(m_flags
& wxDIR_DOTDOT
) )
134 // check the type now
135 if ( !(m_flags
& wxDIR_FILES
) &&
136 !wxDir::Exists(m_dirname
+ _T('/') + de
->d_name
) )
138 // it's a file, but we don't want them
141 else if ( !(m_flags
& wxDIR_DIRS
) &&
142 wxDir::Exists(m_dirname
+ _T('/') + de
->d_name
) )
144 // it's a dir, and we don't want it
148 // finally, check the name
151 matches
= m_flags
& wxDIR_HIDDEN
? TRUE
: de
->d_name
[0] != '.';
155 // test against the pattern
156 matches
= wxMatchWild(m_filespec
, de
->d_name
,
157 !(m_flags
& wxDIR_HIDDEN
));
161 *filename
= de
->d_name
;
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
171 bool wxDir::Exists(const wxString
& dir
)
173 return wxPathExists(dir
);
176 // ----------------------------------------------------------------------------
177 // wxDir construction/destruction
178 // ----------------------------------------------------------------------------
180 wxDir::wxDir(const wxString
& dirname
)
187 bool wxDir::Open(const wxString
& dirname
)
190 m_data
= new wxDirData(dirname
);
192 if ( !M_DIR
->IsOk() )
194 wxLogSysError(_("Can not enumerate files in directory '%s'"),
206 bool wxDir::IsOpened() const
208 return m_data
!= NULL
;
216 // ----------------------------------------------------------------------------
218 // ----------------------------------------------------------------------------
220 bool wxDir::GetFirst(wxString
*filename
,
221 const wxString
& filespec
,
224 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
228 M_DIR
->SetFileSpec(filespec
);
229 M_DIR
->SetFlags(flags
);
231 return GetNext(filename
);
234 bool wxDir::GetNext(wxString
*filename
) const
236 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
238 wxCHECK_MSG( filename
, FALSE
, _T("bad pointer in wxDir::GetNext()") );
240 return M_DIR
->Read(filename
);