]>
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
);
68 const wxString
& GetName() const { return m_dirname
; }
79 // ============================================================================
81 // ============================================================================
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 #if !defined( __VMS__ ) || ( __VMS_VER >= 70000000 )
89 wxDirData::wxDirData(const wxString
& dirname
)
94 // throw away the trailing slashes
95 size_t n
= m_dirname
.length();
96 wxCHECK_RET( n
, _T("empty dir name in wxDir") );
98 while ( n
> 0 && m_dirname
[--n
] == '/' )
101 m_dirname
.Truncate(n
+ 1);
104 m_dir
= opendir(m_dirname
.fn_str());
107 wxDirData::~wxDirData()
111 if ( closedir(m_dir
) != 0 )
113 wxLogLastError(_T("closedir"));
118 bool wxDirData::Read(wxString
*filename
)
120 dirent
*de
= (dirent
*)NULL
; // just to silent compiler warnings
121 bool matches
= FALSE
;
129 // don't return "." and ".." unless asked for
130 if ( de
->d_name
[0] == '.' &&
131 ((de
->d_name
[1] == '.' && de
->d_name
[2] == '\0') ||
132 (de
->d_name
[1] == '\0')) )
134 if ( !(m_flags
& wxDIR_DOTDOT
) )
138 // check the type now
139 if ( !(m_flags
& wxDIR_FILES
) &&
140 !wxDir::Exists(m_dirname
+ _T('/') + de
->d_name
) )
142 // it's a file, but we don't want them
145 else if ( !(m_flags
& wxDIR_DIRS
) &&
146 wxDir::Exists(m_dirname
+ _T('/') + de
->d_name
) )
148 // it's a dir, and we don't want it
152 // finally, check the name
155 matches
= m_flags
& wxDIR_HIDDEN
? TRUE
: de
->d_name
[0] != '.';
159 // test against the pattern
160 matches
= wxMatchWild(m_filespec
, de
->d_name
,
161 !(m_flags
& wxDIR_HIDDEN
));
165 *filename
= de
->d_name
;
170 #else // old VMS (TODO)
172 wxDirData::wxDirData(const wxString
& WXUNUSED(dirname
))
174 wxFAIL_MSG(_T("not implemented"));
177 wxDirData::~wxDirData()
181 bool wxDirData::Read(wxString
* WXUNUSED(filename
))
186 #endif // not or new VMS/old VMS
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
193 bool wxDir::Exists(const wxString
& dir
)
195 return wxPathExists(dir
);
198 // ----------------------------------------------------------------------------
199 // wxDir construction/destruction
200 // ----------------------------------------------------------------------------
202 wxDir::wxDir(const wxString
& dirname
)
209 bool wxDir::Open(const wxString
& dirname
)
212 m_data
= new wxDirData(dirname
);
214 if ( !M_DIR
->IsOk() )
216 wxLogSysError(_("Can not enumerate files in directory '%s'"),
228 bool wxDir::IsOpened() const
230 return m_data
!= NULL
;
233 wxString
wxDir::GetName() const
238 name
= M_DIR
->GetName();
239 if ( !name
.empty() && (name
.Last() == _T('/')) )
241 // chop off the last (back)slash
242 name
.Truncate(name
.length() - 1);
254 // ----------------------------------------------------------------------------
256 // ----------------------------------------------------------------------------
258 bool wxDir::GetFirst(wxString
*filename
,
259 const wxString
& filespec
,
262 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
266 M_DIR
->SetFileSpec(filespec
);
267 M_DIR
->SetFlags(flags
);
269 return GetNext(filename
);
272 bool wxDir::GetNext(wxString
*filename
) const
274 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
276 wxCHECK_MSG( filename
, FALSE
, _T("bad pointer in wxDir::GetNext()") );
278 return M_DIR
->Read(filename
);