]>
git.saurik.com Git - wxWidgets.git/blob - src/mgl/dirmgl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mgl/dir.cpp
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 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
20 // ============================================================================
22 // ============================================================================
24 // ----------------------------------------------------------------------------
26 // ----------------------------------------------------------------------------
36 #include "wx/filefn.h" // for wxMatchWild
37 #include "wx/mgl/private.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 #define M_DIR ((wxDirData *)m_data)
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // this class stores everything we need to enumerate the files
53 wxDirData(const wxString
& dirname
);
56 bool IsOk() const { return m_dir
!= NULL
; }
58 void SetFileSpec(const wxString
& filespec
);
59 void SetFlags(int flags
) { m_flags
= flags
; }
62 bool Read(wxString
*filename
);
64 const wxString
& GetName() const { return m_dirname
; }
75 // ============================================================================
77 // ============================================================================
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 wxDirData::wxDirData(const wxString
& dirname
)
88 // throw away the trailing slashes
89 size_t n
= m_dirname
.length();
90 wxCHECK_RET( n
, wxT("empty dir name in wxDir") );
92 while ( n
> 0 && m_dirname
[--n
] == wxFILE_SEP_PATH
) {}
94 m_dirname
.Truncate(n
+ 1);
97 wxDirData::~wxDirData()
103 void wxDirData::SetFileSpec(const wxString
& filespec
)
106 if ( filespec
.empty() )
107 m_filespec
= wxT("*.*");
110 m_filespec
= filespec
;
113 void wxDirData::Rewind()
122 bool wxDirData::Read(wxString
*filename
)
125 bool matches
= false;
127 data
.dwSize
= sizeof(data
);
129 wxString path
= m_dirname
;
130 path
+= wxFILE_SEP_PATH
;
131 path
.reserve(path
.length() + 255); // speed up string concatenation
137 if ( !PM_findNextFile(m_dir
, &data
) )
142 m_dir
= PM_findFirstFile(path
+ m_filespec
, &data
);
143 if ( m_dir
== PM_FILE_INVALID
)
150 // don't return "." and ".." unless asked for
151 if ( data
.name
[0] == '.' &&
152 ((data
.name
[1] == '.' && data
.name
[2] == '\0') ||
153 (data
.name
[1] == '\0')) )
155 if ( !(m_flags
& wxDIR_DOTDOT
) )
158 // we found a valid match
162 // check the type now
163 if ( !(m_flags
& wxDIR_FILES
) && !(data
.attrib
& PM_FILE_DIRECTORY
) )
165 // it's a file, but we don't want them
168 else if ( !(m_flags
& wxDIR_DIRS
) && (data
.attrib
& PM_FILE_DIRECTORY
) )
170 // it's a dir, and we don't want it
174 matches
= m_flags
& wxDIR_HIDDEN
? true : !(data
.attrib
& PM_FILE_HIDDEN
);
177 *filename
= data
.name
;
182 // ----------------------------------------------------------------------------
183 // wxDir construction/destruction
184 // ----------------------------------------------------------------------------
186 wxDir::wxDir(const wxString
& dirname
)
193 bool wxDir::Open(const wxString
& dirname
)
198 if ( !wxDir::Exists(dirname
) )
200 wxLogError(_("Directory '%s' doesn't exist!"), dirname
.c_str());
204 m_data
= new wxDirData(dirname
);
208 bool wxDir::IsOpened() const
210 return m_data
!= NULL
;
213 wxString
wxDir::GetName() const
218 name
= M_DIR
->GetName();
219 if ( !name
.empty() && (name
.Last() == wxFILE_SEP_PATH
) )
221 // chop off the last (back)slash
222 name
.Truncate(name
.length() - 1);
234 // ----------------------------------------------------------------------------
236 // ----------------------------------------------------------------------------
238 bool wxDir::GetFirst(wxString
*filename
,
239 const wxString
& filespec
,
242 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
246 M_DIR
->SetFileSpec(filespec
);
247 M_DIR
->SetFlags(flags
);
249 return GetNext(filename
);
252 bool wxDir::GetNext(wxString
*filename
) const
254 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
256 wxCHECK_MSG( filename
, false, wxT("bad pointer in wxDir::GetNext()") );
258 return M_DIR
->Read(filename
);