]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/dir.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/dir.cpp
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 licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
33 #include "wx/filefn.h" // for wxMatchWild
34 #include "wx/filename.h"
36 #include <sys/types.h>
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 #define M_DIR ((wxDirData *)m_data)
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // this class stores everything we need to enumerate the files
56 wxDirData(const wxString
& dirname
);
59 bool IsOk() const { return m_dir
!= NULL
; }
61 void SetFileSpec(const wxString
& filespec
) { m_filespec
= filespec
; }
62 void SetFlags(int flags
) { m_flags
= flags
; }
64 void Rewind() { rewinddir(m_dir
); }
65 bool Read(wxString
*filename
);
67 const wxString
& GetName() const { return m_dirname
; }
78 // ============================================================================
80 // ============================================================================
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 #if !defined( __VMS__ ) || ( __VMS_VER >= 70000000 )
88 wxDirData::wxDirData(const wxString
& dirname
)
93 // throw away the trailing slashes
94 size_t n
= m_dirname
.length();
95 wxCHECK_RET( n
, wxT("empty dir name in wxDir") );
97 while ( n
> 0 && m_dirname
[--n
] == '/' )
100 m_dirname
.Truncate(n
+ 1);
103 m_dir
= opendir(m_dirname
.fn_str());
106 wxDirData::~wxDirData()
110 if ( closedir(m_dir
) != 0 )
112 wxLogLastError(wxT("closedir"));
117 bool wxDirData::Read(wxString
*filename
)
119 dirent
*de
= NULL
; // just to silence compiler warnings
120 bool matches
= false;
122 // speed up string concatenation in the loop a bit
123 wxString path
= m_dirname
;
125 path
.reserve(path
.length() + 255);
136 de_d_name
= wxString(de
->d_name
, *wxConvFileName
);
138 de_d_name
= de
->d_name
;
141 // don't return "." and ".." unless asked for
142 if ( de
->d_name
[0] == '.' &&
143 ((de
->d_name
[1] == '.' && de
->d_name
[2] == '\0') ||
144 (de
->d_name
[1] == '\0')) )
146 if ( !(m_flags
& wxDIR_DOTDOT
) )
149 // we found a valid match
153 // check the type now: notice that we want to check the type of this
154 // path itself and not whatever it points to in case of a symlink
155 wxFileName fn
= wxFileName::DirName(path
+ de_d_name
);
158 if ( !(m_flags
& wxDIR_FILES
) && !fn
.DirExists() )
160 // it's a file, but we don't want them
163 else if ( !(m_flags
& wxDIR_DIRS
) && fn
.DirExists() )
165 // it's a dir, and we don't want it
169 // finally, check the name
170 if ( m_filespec
.empty() )
172 matches
= m_flags
& wxDIR_HIDDEN
? true : de
->d_name
[0] != '.';
176 // test against the pattern
177 matches
= wxMatchWild(m_filespec
, de_d_name
,
178 !(m_flags
& wxDIR_HIDDEN
));
182 *filename
= de_d_name
;
187 #else // old VMS (TODO)
189 wxDirData::wxDirData(const wxString
& WXUNUSED(dirname
))
191 wxFAIL_MSG(wxT("not implemented"));
194 wxDirData::~wxDirData()
198 bool wxDirData::Read(wxString
* WXUNUSED(filename
))
203 #endif // not or new VMS/old VMS
205 // ----------------------------------------------------------------------------
206 // wxDir construction/destruction
207 // ----------------------------------------------------------------------------
209 wxDir::wxDir(const wxString
& dirname
)
216 bool wxDir::Open(const wxString
& dirname
)
219 m_data
= new wxDirData(dirname
);
221 if ( !M_DIR
->IsOk() )
232 bool wxDir::IsOpened() const
234 return m_data
!= NULL
;
237 wxString
wxDir::GetName() const
242 name
= M_DIR
->GetName();
244 // Notice that we need to check for length > 1 as we shouldn't remove
245 // the last slash from the root directory!
246 if ( name
.length() > 1 && (name
.Last() == wxT('/')) )
248 // chop off the last slash
265 // ----------------------------------------------------------------------------
267 // ----------------------------------------------------------------------------
269 bool wxDir::GetFirst(wxString
*filename
,
270 const wxString
& filespec
,
273 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
277 M_DIR
->SetFileSpec(filespec
);
278 M_DIR
->SetFlags(flags
);
280 return GetNext(filename
);
283 bool wxDir::GetNext(wxString
*filename
) const
285 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
287 wxCHECK_MSG( filename
, false, wxT("bad pointer in wxDir::GetNext()") );
289 return M_DIR
->Read(filename
);
292 bool wxDir::HasSubDirs(const wxString
& spec
) const
294 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
298 // faster check for presence of any subdirectory: normally each subdir
299 // has a hard link to the parent directory and so, knowing that there
300 // are at least "." and "..", we have a subdirectory if and only if
301 // links number is > 2 - this is just a guess but it works fairly well
304 // note that we may guess wrongly in one direction only: i.e. we may
305 // return true when there are no subdirectories but this is ok as the
306 // caller will learn it soon enough when it calls GetFirst(wxDIR)
309 if ( wxStat(M_DIR
->GetName(), &stBuf
) == 0 )
311 switch ( stBuf
.st_nlink
)
319 // weird filesystem, don't try to guess for it, use dumb
324 // assume we have subdirs - may turn out to be wrong if we
325 // have other hard links to this directory but it's not
326 // that bad as explained above
332 // just try to find first directory
334 return GetFirst(&s
, spec
, wxDIR_DIRS
| wxDIR_HIDDEN
);