]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/dir.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDir implementation for Unix/POSIX systems
4 // Author: Vadim Zeitlin
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"
37 #include "wx/filefn.h" // for wxMatchWild
39 #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 #if !defined( __VMS__ ) || ( __VMS_VER >= 70000000 )
87 wxDirData::wxDirData(const wxString
& dirname
)
92 // throw away the trailing slashes
93 size_t n
= m_dirname
.length();
94 wxCHECK_RET( n
, _T("empty dir name in wxDir") );
96 while ( n
> 0 && m_dirname
[--n
] == '/' )
99 m_dirname
.Truncate(n
+ 1);
102 m_dir
= opendir(m_dirname
.fn_str());
105 wxDirData::~wxDirData()
109 if ( closedir(m_dir
) != 0 )
111 wxLogLastError(_T("closedir"));
116 bool wxDirData::Read(wxString
*filename
)
119 bool matches
= FALSE
;
127 // don't return "." and ".." unless asked for
128 if ( de
->d_name
[0] == '.' &&
129 ((de
->d_name
[1] == '.' && de
->d_name
[2] == '\0') ||
130 (de
->d_name
[1] == '\0')) )
132 if ( !(m_flags
& wxDIR_DOTDOT
) )
136 // check the type now
137 if ( !(m_flags
& wxDIR_FILES
) &&
138 !wxDir::Exists(m_dirname
+ _T('/') + de
->d_name
) )
140 // it's a file, but we don't want them
143 else if ( !(m_flags
& wxDIR_DIRS
) &&
144 wxDir::Exists(m_dirname
+ _T('/') + de
->d_name
) )
146 // it's a dir, and we don't want it
150 // finally, check the name
153 matches
= m_flags
& wxDIR_HIDDEN
? TRUE
: de
->d_name
[0] != '.';
157 // test against the pattern
158 matches
= wxMatchWild(m_filespec
, de
->d_name
,
159 !(m_flags
& wxDIR_HIDDEN
));
163 *filename
= de
->d_name
;
168 #else // old VMS (TODO)
170 wxDirData::wxDirData(const wxString
& WXUNUSED(dirname
))
172 wxFAIL_MSG(_T("not implemented"));
175 wxDirData::~wxDirData()
179 bool wxDirData::Read(wxString
* WXUNUSED(filename
))
184 #endif // not or new VMS/old VMS
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
191 bool wxDir::Exists(const wxString
& dir
)
193 return wxPathExists(dir
);
196 // ----------------------------------------------------------------------------
197 // wxDir construction/destruction
198 // ----------------------------------------------------------------------------
200 wxDir::wxDir(const wxString
& dirname
)
207 bool wxDir::Open(const wxString
& dirname
)
210 m_data
= new wxDirData(dirname
);
212 if ( !M_DIR
->IsOk() )
214 wxLogSysError(_("Can not enumerate files in directory '%s'"),
226 bool wxDir::IsOpened() const
228 return m_data
!= NULL
;
236 // ----------------------------------------------------------------------------
238 // ----------------------------------------------------------------------------
240 bool wxDir::GetFirst(wxString
*filename
,
241 const wxString
& filespec
,
244 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
248 M_DIR
->SetFileSpec(filespec
);
249 M_DIR
->SetFlags(flags
);
251 return GetNext(filename
);
254 bool wxDir::GetNext(wxString
*filename
) const
256 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
258 wxCHECK_MSG( filename
, FALSE
, _T("bad pointer in wxDir::GetNext()") );
260 return M_DIR
->Read(filename
);