]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/dir.cpp
8364eddf5a86771765ed3b3b6d2b8bfce8aa9256
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 silence compiler warnings
121 bool matches
= FALSE
;
123 // speed up string concatenation in the loop a bit
124 wxString path
= m_dirname
;
126 path
.reserve(path
.length() + 255);
134 // don't return "." and ".." unless asked for
135 if ( de
->d_name
[0] == '.' &&
136 ((de
->d_name
[1] == '.' && de
->d_name
[2] == '\0') ||
137 (de
->d_name
[1] == '\0')) )
139 if ( !(m_flags
& wxDIR_DOTDOT
) )
142 // we found a valid match
146 // check the type now
147 if ( !(m_flags
& wxDIR_FILES
) && !wxDir::Exists(path
+ de
->d_name
) )
149 // it's a file, but we don't want them
152 else if ( !(m_flags
& wxDIR_DIRS
) && wxDir::Exists(path
+ de
->d_name
) )
154 // it's a dir, and we don't want it
158 // finally, check the name
159 if ( m_filespec
.empty() )
161 matches
= m_flags
& wxDIR_HIDDEN
? TRUE
: de
->d_name
[0] != '.';
165 // test against the pattern
166 matches
= wxMatchWild(m_filespec
, de
->d_name
,
167 !(m_flags
& wxDIR_HIDDEN
));
171 *filename
= de
->d_name
;
176 #else // old VMS (TODO)
178 wxDirData::wxDirData(const wxString
& WXUNUSED(dirname
))
180 wxFAIL_MSG(_T("not implemented"));
183 wxDirData::~wxDirData()
187 bool wxDirData::Read(wxString
* WXUNUSED(filename
))
192 #endif // not or new VMS/old VMS
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
199 bool wxDir::Exists(const wxString
& dir
)
201 return wxPathExists(dir
);
204 // ----------------------------------------------------------------------------
205 // wxDir construction/destruction
206 // ----------------------------------------------------------------------------
208 wxDir::wxDir(const wxString
& dirname
)
215 bool wxDir::Open(const wxString
& dirname
)
218 m_data
= new wxDirData(dirname
);
220 if ( !M_DIR
->IsOk() )
222 wxLogSysError(_("Can not enumerate files in directory '%s'"),
234 bool wxDir::IsOpened() const
236 return m_data
!= NULL
;
239 wxString
wxDir::GetName() const
244 name
= M_DIR
->GetName();
245 if ( !name
.empty() && (name
.Last() == _T('/')) )
247 // chop off the last (back)slash
248 name
.Truncate(name
.length() - 1);
260 // ----------------------------------------------------------------------------
262 // ----------------------------------------------------------------------------
264 bool wxDir::GetFirst(wxString
*filename
,
265 const wxString
& filespec
,
268 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
272 M_DIR
->SetFileSpec(filespec
);
273 M_DIR
->SetFlags(flags
);
275 return GetNext(filename
);
278 bool wxDir::GetNext(wxString
*filename
) const
280 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
282 wxCHECK_MSG( filename
, FALSE
, _T("bad pointer in wxDir::GetNext()") );
284 return M_DIR
->Read(filename
);