]>
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) 
  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
); 
  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::SetFileSpec(const wxString
& filespec
) 
 112     if ( filespec
.IsEmpty() ) 
 113         m_filespec 
= _T("*.*"); 
 116     m_filespec 
= filespec
; 
 119 void wxDirData::Rewind() 
 128 bool wxDirData::Read(wxString 
*filename
) 
 131     bool matches 
= FALSE
; 
 133     data
.dwSize 
= sizeof(data
); 
 135     wxString path 
= m_dirname
; 
 136     path 
+= wxFILE_SEP_PATH
; 
 137     path
.reserve(path
.length() + 255); // speed up string concatenation 
 143             if ( !PM_findNextFile(m_dir
, &data
) ) 
 148             m_dir 
= PM_findFirstFile(path 
+ m_filespec 
, &data
); 
 149             if ( m_dir 
== PM_FILE_INVALID 
) 
 156         // don't return "." and ".." unless asked for 
 157         if ( data
.name
[0] == '.' && 
 158              ((data
.name
[1] == '.' && data
.name
[2] == '\0') || 
 159               (data
.name
[1] == '\0')) ) 
 161             if ( !(m_flags 
& wxDIR_DOTDOT
) ) 
 164             // we found a valid match 
 168         // check the type now 
 169         if ( !(m_flags 
& wxDIR_FILES
) && !(data
.attrib 
& PM_FILE_DIRECTORY
) ) 
 171             // it's a file, but we don't want them 
 174         else if ( !(m_flags 
& wxDIR_DIRS
) && (data
.attrib 
& PM_FILE_DIRECTORY
) ) 
 176             // it's a dir, and we don't want it 
 180         matches 
= m_flags 
& wxDIR_HIDDEN 
? TRUE 
: !(data
.attrib 
& PM_FILE_HIDDEN
); 
 183     *filename 
= data
.name
; 
 189 // ---------------------------------------------------------------------------- 
 191 // ---------------------------------------------------------------------------- 
 194 bool wxDir::Exists(const wxString
& dir
) 
 196     return wxPathExists(dir
); 
 199 // ---------------------------------------------------------------------------- 
 200 // wxDir construction/destruction 
 201 // ---------------------------------------------------------------------------- 
 203 wxDir::wxDir(const wxString
& dirname
) 
 210 bool wxDir::Open(const wxString
& dirname
) 
 215     if ( !wxDir::Exists(dirname
) ) 
 217         wxLogError(_("Directory '%s' doesn't exist!"), dirname
.c_str()); 
 221     m_data 
= new wxDirData(dirname
); 
 225 bool wxDir::IsOpened() const 
 227     return m_data 
!= NULL
; 
 230 wxString 
wxDir::GetName() const 
 235         name 
= M_DIR
->GetName(); 
 236         if ( !name
.empty() && (name
.Last() == wxFILE_SEP_PATH
) ) 
 238             // chop off the last (back)slash 
 239             name
.Truncate(name
.length() - 1); 
 251 // ---------------------------------------------------------------------------- 
 253 // ---------------------------------------------------------------------------- 
 255 bool wxDir::GetFirst(wxString 
*filename
, 
 256                      const wxString
& filespec
, 
 259     wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") ); 
 263     M_DIR
->SetFileSpec(filespec
); 
 264     M_DIR
->SetFlags(flags
); 
 266     return GetNext(filename
); 
 269 bool wxDir::GetNext(wxString 
*filename
) const 
 271     wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") ); 
 273     wxCHECK_MSG( filename
, FALSE
, _T("bad pointer in wxDir::GetNext()") ); 
 275     return M_DIR
->Read(filename
);