]>
git.saurik.com Git - wxWidgets.git/blob - src/mgl/dir.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDir implementation for MGL
4 // Author: Vaclav Slavik, Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
22 #pragma implementation "dir.h"
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
38 #include "wx/filefn.h" // for wxMatchWild
39 #include "wx/mgl/private.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 #define M_DIR ((wxDirData *)m_data)
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // this class stores everything we need to enumerate the files
55 wxDirData(const wxString
& dirname
);
58 bool IsOk() const { return m_dir
!= NULL
; }
60 void SetFileSpec(const wxString
& filespec
) { m_filespec
= filespec
; }
61 void SetFlags(int flags
) { m_flags
= flags
; }
64 bool Read(wxString
*filename
);
66 const wxString
& GetName() const { return m_dirname
; }
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
] == wxFILE_SEP_PATH
) {}
96 m_dirname
.Truncate(n
+ 1);
99 wxDirData::~wxDirData()
105 void wxDirData::Rewind()
114 bool wxDirData::Read(wxString
*filename
)
117 bool matches
= FALSE
;
119 // speed up string concatenation in the loop a bit
120 wxString path
= m_dirname
;
121 path
+= wxFILE_SEP_PATH
;
122 path
.reserve(path
.length() + 255);
128 if ( !PM_findNextFile(m_dir
, &data
) )
133 m_dir
= PM_findFirstFile(path
+ m_filespec
, &data
);
134 if ( m_dir
== PM_FILE_INVALID
)
141 // don't return "." and ".." unless asked for
142 if ( data
.name
[0] == '.' &&
143 ((data
.name
[1] == '.' && data
.name
[2] == '\0') ||
144 (data
.name
[1] == '\0')) )
146 if ( !(m_flags
& wxDIR_DOTDOT
) )
149 // we found a valid match
153 // check the type now
154 if ( !(m_flags
& wxDIR_FILES
) && !(data
.attrib
& PM_FILE_DIRECTORY
) )
156 // it's a file, but we don't want them
159 else if ( !(m_flags
& wxDIR_DIRS
) && (data
.attrib
& PM_FILE_DIRECTORY
) )
161 // it's a dir, and we don't want it
165 matches
= m_flags
& wxDIR_HIDDEN
? TRUE
: !(data
.attrib
& PM_FILE_HIDDEN
);
168 *filename
= data
.name
;
174 // ----------------------------------------------------------------------------
176 // ----------------------------------------------------------------------------
179 bool wxDir::Exists(const wxString
& dir
)
181 return wxPathExists(dir
);
184 // ----------------------------------------------------------------------------
185 // wxDir construction/destruction
186 // ----------------------------------------------------------------------------
188 wxDir::wxDir(const wxString
& dirname
)
195 bool wxDir::Open(const wxString
& dirname
)
198 m_data
= new wxDirData(dirname
);
200 if ( !M_DIR
->IsOk() )
202 wxLogSysError(_("Can not enumerate files in directory '%s'"),
214 bool wxDir::IsOpened() const
216 return m_data
!= NULL
;
219 wxString
wxDir::GetName() const
224 name
= M_DIR
->GetName();
225 if ( !name
.empty() && (name
.Last() == wxFILE_SEP_PATH
) )
227 // chop off the last (back)slash
228 name
.Truncate(name
.length() - 1);
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
244 bool wxDir::GetFirst(wxString
*filename
,
245 const wxString
& filespec
,
248 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
252 M_DIR
->SetFileSpec(filespec
);
253 M_DIR
->SetFlags(flags
);
255 return GetNext(filename
);
258 bool wxDir::GetNext(wxString
*filename
) const
260 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
262 wxCHECK_MSG( filename
, FALSE
, _T("bad pointer in wxDir::GetNext()") );
264 return M_DIR
->Read(filename
);
267 bool wxDir::HasSubDirs(const wxString
& spec
)
269 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
271 // just try to find first directory
273 return GetFirst(&s
, spec
, wxDIR_DIRS
| wxDIR_HIDDEN
);