]>
git.saurik.com Git - wxWidgets.git/blob - src/mgl/dir.cpp
b325114f1cc6eb1907aed7e41a802db8c7b141d9
   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" 
  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
) { m_filespec 
= 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::Rewind() 
 114 bool wxDirData::Read(wxString 
*filename
) 
 117     bool matches 
= FALSE
; 
 119     data
.dwSize 
= sizeof(data
); 
 121     wxString path 
= m_dirname
; 
 122     path 
+= wxFILE_SEP_PATH
; 
 123     path
.reserve(path
.length() + 255); // speed up string concatenation 
 129             if ( !PM_findNextFile(m_dir
, &data
) ) 
 134             m_dir 
= PM_findFirstFile(path 
+ m_filespec 
, &data
); 
 135             if ( m_dir 
== PM_FILE_INVALID 
) 
 142         // don't return "." and ".." unless asked for 
 143         if ( data
.name
[0] == '.' && 
 144              ((data
.name
[1] == '.' && data
.name
[2] == '\0') || 
 145               (data
.name
[1] == '\0')) ) 
 147             if ( !(m_flags 
& wxDIR_DOTDOT
) ) 
 150             // we found a valid match 
 154         // check the type now 
 155         if ( !(m_flags 
& wxDIR_FILES
) && !(data
.attrib 
& PM_FILE_DIRECTORY
) ) 
 157             // it's a file, but we don't want them 
 160         else if ( !(m_flags 
& wxDIR_DIRS
) && (data
.attrib 
& PM_FILE_DIRECTORY
) ) 
 162             // it's a dir, and we don't want it 
 166         matches 
= m_flags 
& wxDIR_HIDDEN 
? TRUE 
: !(data
.attrib 
& PM_FILE_HIDDEN
); 
 169     *filename 
= data
.name
; 
 175 // ---------------------------------------------------------------------------- 
 177 // ---------------------------------------------------------------------------- 
 180 bool wxDir::Exists(const wxString
& dir
) 
 182     return wxPathExists(dir
); 
 185 // ---------------------------------------------------------------------------- 
 186 // wxDir construction/destruction 
 187 // ---------------------------------------------------------------------------- 
 189 wxDir::wxDir(const wxString
& dirname
) 
 196 bool wxDir::Open(const wxString
& dirname
) 
 201     if ( !wxDir::Exists(dirname
) ) 
 203         wxLogError(_("Directory '%s' doesn't exist!"), dirname
.c_str()); 
 207     m_data 
= new wxDirData(dirname
); 
 211 bool wxDir::IsOpened() const 
 213     return m_data 
!= NULL
; 
 216 wxString 
wxDir::GetName() const 
 221         name 
= M_DIR
->GetName(); 
 222         if ( !name
.empty() && (name
.Last() == wxFILE_SEP_PATH
) ) 
 224             // chop off the last (back)slash 
 225             name
.Truncate(name
.length() - 1); 
 237 // ---------------------------------------------------------------------------- 
 239 // ---------------------------------------------------------------------------- 
 241 bool wxDir::GetFirst(wxString 
*filename
, 
 242                      const wxString
& filespec
, 
 245     wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") ); 
 249     M_DIR
->SetFileSpec(filespec
); 
 250     M_DIR
->SetFlags(flags
); 
 252     return GetNext(filename
); 
 255 bool wxDir::GetNext(wxString 
*filename
) const 
 257     wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") ); 
 259     wxCHECK_MSG( filename
, FALSE
, _T("bad pointer in wxDir::GetNext()") ); 
 261     return M_DIR
->Read(filename
); 
 264 bool wxDir::HasSubDirs(const wxString
& spec
) 
 266     wxCHECK_MSG( IsOpened(), FALSE
, _T("must wxDir::Open() first") ); 
 268     // just try to find first directory 
 270     return GetFirst(&s
, spec
, wxDIR_DIRS 
| wxDIR_HIDDEN
);