]>
git.saurik.com Git - wxWidgets.git/blob - src/mgl/dirmgl.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-2002 SciTech Software, Inc. (www.scitechsoft.com)
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #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
);
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::SetFileSpec(const wxString
& filespec
)
108 if ( filespec
.empty() )
109 m_filespec
= _T("*.*");
112 m_filespec
= filespec
;
115 void wxDirData::Rewind()
124 bool wxDirData::Read(wxString
*filename
)
127 bool matches
= false;
129 data
.dwSize
= sizeof(data
);
131 wxString path
= m_dirname
;
132 path
+= wxFILE_SEP_PATH
;
133 path
.reserve(path
.length() + 255); // speed up string concatenation
139 if ( !PM_findNextFile(m_dir
, &data
) )
144 m_dir
= PM_findFirstFile(path
+ m_filespec
, &data
);
145 if ( m_dir
== PM_FILE_INVALID
)
152 // don't return "." and ".." unless asked for
153 if ( data
.name
[0] == '.' &&
154 ((data
.name
[1] == '.' && data
.name
[2] == '\0') ||
155 (data
.name
[1] == '\0')) )
157 if ( !(m_flags
& wxDIR_DOTDOT
) )
160 // we found a valid match
164 // check the type now
165 if ( !(m_flags
& wxDIR_FILES
) && !(data
.attrib
& PM_FILE_DIRECTORY
) )
167 // it's a file, but we don't want them
170 else if ( !(m_flags
& wxDIR_DIRS
) && (data
.attrib
& PM_FILE_DIRECTORY
) )
172 // it's a dir, and we don't want it
176 matches
= m_flags
& wxDIR_HIDDEN
? true : !(data
.attrib
& PM_FILE_HIDDEN
);
179 *filename
= data
.name
;
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
190 bool wxDir::Exists(const wxString
& dir
)
192 return wxDirExists(dir
);
195 // ----------------------------------------------------------------------------
196 // wxDir construction/destruction
197 // ----------------------------------------------------------------------------
199 wxDir::wxDir(const wxString
& dirname
)
206 bool wxDir::Open(const wxString
& dirname
)
211 if ( !wxDir::Exists(dirname
) )
213 wxLogError(_("Directory '%s' doesn't exist!"), dirname
.c_str());
217 m_data
= new wxDirData(dirname
);
221 bool wxDir::IsOpened() const
223 return m_data
!= NULL
;
226 wxString
wxDir::GetName() const
231 name
= M_DIR
->GetName();
232 if ( !name
.empty() && (name
.Last() == wxFILE_SEP_PATH
) )
234 // chop off the last (back)slash
235 name
.Truncate(name
.length() - 1);
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
251 bool wxDir::GetFirst(wxString
*filename
,
252 const wxString
& filespec
,
255 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
259 M_DIR
->SetFileSpec(filespec
);
260 M_DIR
->SetFlags(flags
);
262 return GetNext(filename
);
265 bool wxDir::GetNext(wxString
*filename
) const
267 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
269 wxCHECK_MSG( filename
, false, _T("bad pointer in wxDir::GetNext()") );
271 return M_DIR
->Read(filename
);