]>
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 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"
42 #include "wx/filefn.h" // for wxMatchWild
43 #include "wx/mgl/private.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 #define M_DIR ((wxDirData *)m_data)
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // this class stores everything we need to enumerate the files
59 wxDirData(const wxString
& dirname
);
62 bool IsOk() const { return m_dir
!= NULL
; }
64 void SetFileSpec(const wxString
& filespec
) { m_filespec
= filespec
; }
65 void SetFlags(int flags
) { m_flags
= flags
; }
68 bool Read(wxString
*filename
);
70 const wxString
& GetName() const { return m_dirname
; }
81 // ============================================================================
83 // ============================================================================
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
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
] == wxFILE_SEP_PATH
) {}
100 m_dirname
.Truncate(n
+ 1);
103 wxDirData::~wxDirData()
109 void wxDirData::Rewind()
118 bool wxDirData::Read(wxString
*filename
)
121 bool matches
= FALSE
;
123 data
.dwSize
= sizeof(data
);
125 wxString path
= m_dirname
;
126 path
+= wxFILE_SEP_PATH
;
127 path
.reserve(path
.length() + 255); // speed up string concatenation
133 if ( !PM_findNextFile(m_dir
, &data
) )
138 m_dir
= PM_findFirstFile(path
+ m_filespec
, &data
);
139 if ( m_dir
== PM_FILE_INVALID
)
146 // don't return "." and ".." unless asked for
147 if ( data
.name
[0] == '.' &&
148 ((data
.name
[1] == '.' && data
.name
[2] == '\0') ||
149 (data
.name
[1] == '\0')) )
151 if ( !(m_flags
& wxDIR_DOTDOT
) )
154 // we found a valid match
158 // check the type now
159 if ( !(m_flags
& wxDIR_FILES
) && !(data
.attrib
& PM_FILE_DIRECTORY
) )
161 // it's a file, but we don't want them
164 else if ( !(m_flags
& wxDIR_DIRS
) && (data
.attrib
& PM_FILE_DIRECTORY
) )
166 // it's a dir, and we don't want it
170 matches
= m_flags
& wxDIR_HIDDEN
? TRUE
: !(data
.attrib
& PM_FILE_HIDDEN
);
173 *filename
= data
.name
;
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
184 bool wxDir::Exists(const wxString
& dir
)
186 return wxPathExists(dir
);
189 // ----------------------------------------------------------------------------
190 // wxDir construction/destruction
191 // ----------------------------------------------------------------------------
193 wxDir::wxDir(const wxString
& dirname
)
200 bool wxDir::Open(const wxString
& dirname
)
205 if ( !wxDir::Exists(dirname
) )
207 wxLogError(_("Directory '%s' doesn't exist!"), dirname
.c_str());
211 m_data
= new wxDirData(dirname
);
215 bool wxDir::IsOpened() const
217 return m_data
!= NULL
;
220 wxString
wxDir::GetName() const
225 name
= M_DIR
->GetName();
226 if ( !name
.empty() && (name
.Last() == wxFILE_SEP_PATH
) )
228 // chop off the last (back)slash
229 name
.Truncate(name
.length() - 1);
241 // ----------------------------------------------------------------------------
243 // ----------------------------------------------------------------------------
245 bool wxDir::GetFirst(wxString
*filename
,
246 const wxString
& filespec
,
249 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
253 M_DIR
->SetFileSpec(filespec
);
254 M_DIR
->SetFlags(flags
);
256 return GetNext(filename
);
259 bool wxDir::GetNext(wxString
*filename
) const
261 wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") );
263 wxCHECK_MSG( filename
, FALSE
, _T("bad pointer in wxDir::GetNext()") );
265 return M_DIR
->Read(filename
);