// headers
// ----------------------------------------------------------------------------
-#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
- #pragma implementation "dir.h"
-#endif
-
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#include "wx/dir.h"
#include "wx/filefn.h" // for wxDirExists()
+#include "pfall.h"
+
// ----------------------------------------------------------------------------
// define the types and functions used for file searching
// ----------------------------------------------------------------------------
// private classes
// ----------------------------------------------------------------------------
+// this class stores everything we need to enumerate the files
+class wxDirData
+{
+public:
+ wxDirData(const wxString& dirname);
+ ~wxDirData();
+
+ bool IsOk() const { return m_dir != NULL; }
+
+ void SetFileSpec(const wxString& filespec) { m_filespec = filespec; }
+ void SetFlags(int flags) { m_flags = flags; }
+
+ void Close();
+ bool Read(wxString *filename);
+
+ const wxString& GetName() const { return m_dirname; }
+
+private:
+ void *m_dir;
+
+ wxString m_dirname;
+ wxString m_filespec;
+
+ int m_flags;
+};
+
// ============================================================================
// implementation
// ============================================================================
+// ----------------------------------------------------------------------------
+// wxDirData
+// ----------------------------------------------------------------------------
+
+wxDirData::wxDirData(const wxString& dirname)
+ : m_dirname(dirname)
+{
+ m_dir = NULL;
+
+ // throw away the trailing slashes
+ size_t n = m_dirname.length();
+ wxCHECK_RET( n, _T("empty dir name in wxDir") );
+
+ while ( n > 0 && m_dirname[--n] == '/' )
+ ;
+
+ m_dirname.Truncate(n + 1);
+
+ // do open the dir
+ //m_dir = opendir(m_dirname.fn_str());
+}
+
+wxDirData::~wxDirData()
+{
+ Close ();
+}
+
+void wxDirData::Close()
+{
+ if ( m_dir )
+ {
+ if ( svfs_dir_endfind (m_dir) != 0 )
+ {
+ wxLogLastError(_T("svfs_dir_endfind"));
+ }
+ m_dir = NULL;
+ }
+}
+
+bool wxDirData::Read(wxString *filename)
+{
+ //dirent *de = NULL; // just to silence compiler warnings
+ int ret;
+ char tmpbuf[300];
+ bool matches = false;
+ size_t flags_search;
+
+ // speed up string concatenation in the loop a bit
+ wxString path = m_dirname;
+ path += _T('/');
+ path.reserve(path.length() + 255);
+
+ wxString de_d_name;
+ de_d_name.reserve(500);
+ flags_search = 0;
+ if (wxDIR_DIRS & m_flags) {
+ flags_search |= SDIR_DIRS;
+ }
+ if (wxDIR_FILES & m_flags) {
+ flags_search |= SDIR_FILES;
+ }
+ if (NULL == m_dir) {
+#ifdef _PACC_VER
+// walk around the PalmOS pacc compiler bug
+ ret = svfs_dir_findfirst (m_dirname.fn_str().data(), &m_dir, tmpbuf, sizeof (tmpbuf), flags_search);
+#else
+ ret = svfs_dir_findfirst (m_dirname.fn_str(), &m_dir, tmpbuf, sizeof (tmpbuf), flags_search);
+#endif
+ } else {
+ ret = svfs_dir_findnext (m_dir, tmpbuf, sizeof (tmpbuf));
+ }
+ for (; ret > 0; ) {
+
+#if wxUSE_UNICODE
+ de_d_name = wxString(tmpbuf, *wxConvFileName);
+#else
+ de_d_name = tmpbuf;
+#endif
+
+ // don't return "." and ".." unless asked for
+ if ( tmpbuf[0] == '.' &&
+ ((tmpbuf[1] == '.' && tmpbuf[2] == '\0') ||
+ (tmpbuf[1] == '\0')) )
+ {
+ if ( !(m_flags & wxDIR_DOTDOT) )
+ continue;
+
+ // we found a valid match
+ break;
+ }
+
+ // check the name
+ if ( m_filespec.empty() )
+ {
+ matches = m_flags & wxDIR_HIDDEN ? true : tmpbuf[0] != '.';
+ }
+ else
+ {
+ // test against the pattern
+ matches = wxMatchWild(m_filespec, de_d_name,
+ !(m_flags & wxDIR_HIDDEN));
+ }
+ if (matches)
+ break;
+ ret = svfs_dir_findnext (m_dir, tmpbuf, sizeof (tmpbuf));
+ }
+
+ *filename = de_d_name;
+
+ return true;
+}
+
// ----------------------------------------------------------------------------
// wxDir helpers
// ----------------------------------------------------------------------------
/* static */
bool wxDir::Exists(const wxString& dir)
{
- return false;
+ return wxDirExists(dir);
}
// ----------------------------------------------------------------------------
wxDir::wxDir(const wxString& dirname)
{
+ m_data = NULL;
+ (void)Open(dirname);
}
bool wxDir::Open(const wxString& dirname)
{
- return false;
+ delete M_DIR;
+ m_data = new wxDirData(dirname);
+
+ return true;
}
bool wxDir::IsOpened() const
{
- return false;
+ return m_data != NULL;
}
wxString wxDir::GetName() const
{
wxString name;
+ if ( m_data )
+ {
+ name = M_DIR->GetName();
+ if ( !name.empty() && (name.Last() == _T('/')) )
+ {
+ // chop off the last (back)slash
+ name.Truncate(name.length() - 1);
+ }
+ }
return name;
}
wxDir::~wxDir()
{
+ delete M_DIR;
}
// ----------------------------------------------------------------------------
const wxString& filespec,
int flags) const
{
- return false;
+ wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
+ M_DIR->Close();
+ M_DIR->SetFileSpec(filespec);
+ M_DIR->SetFlags(flags);
+ return GetNext(filename);
}
bool wxDir::GetNext(wxString *filename) const
{
- return false;
+ wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
+ wxCHECK_MSG( filename, false, _T("bad pointer in wxDir::GetNext()") );
+ return M_DIR->Read(filename);
}