| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/common/dircmn.cpp |
| 3 | // Purpose: wxDir methods common to all implementations |
| 4 | // Author: Vadim Zeitlin |
| 5 | // Modified by: |
| 6 | // Created: 19.05.01 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> |
| 9 | // License: wxWindows licence |
| 10 | /////////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 20 | /* this is done in platform-specific files |
| 21 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
| 22 | #pragma implementation "dir.h" |
| 23 | #endif |
| 24 | */ |
| 25 | |
| 26 | // For compilers that support precompilation, includes "wx.h". |
| 27 | #include "wx/wxprec.h" |
| 28 | |
| 29 | #ifdef __BORLANDC__ |
| 30 | #pragma hdrstop |
| 31 | #endif |
| 32 | |
| 33 | #ifndef WX_PRECOMP |
| 34 | #include "wx/string.h" |
| 35 | #include "wx/log.h" |
| 36 | #include "wx/intl.h" |
| 37 | #include "wx/filefn.h" |
| 38 | #endif //WX_PRECOMP |
| 39 | |
| 40 | #include "wx/dir.h" |
| 41 | |
| 42 | // ============================================================================ |
| 43 | // implementation |
| 44 | // ============================================================================ |
| 45 | |
| 46 | // ---------------------------------------------------------------------------- |
| 47 | // wxDirTraverser |
| 48 | // ---------------------------------------------------------------------------- |
| 49 | |
| 50 | wxDirTraverseResult |
| 51 | wxDirTraverser::OnOpenError(const wxString& WXUNUSED(dirname)) |
| 52 | { |
| 53 | return wxDIR_IGNORE; |
| 54 | } |
| 55 | |
| 56 | // ---------------------------------------------------------------------------- |
| 57 | // wxDir::HasFiles() and HasSubDirs() |
| 58 | // ---------------------------------------------------------------------------- |
| 59 | |
| 60 | // dumb generic implementation |
| 61 | |
| 62 | bool wxDir::HasFiles(const wxString& spec) |
| 63 | { |
| 64 | wxString s; |
| 65 | return GetFirst(&s, spec, wxDIR_FILES | wxDIR_HIDDEN); |
| 66 | } |
| 67 | |
| 68 | // we have a (much) faster version for Unix |
| 69 | #if (defined(__CYGWIN__) && defined(__WINDOWS__)) || !defined(__UNIX_LIKE__) || defined(__WXMAC__) || defined(__EMX__) |
| 70 | |
| 71 | bool wxDir::HasSubDirs(const wxString& spec) |
| 72 | { |
| 73 | wxString s; |
| 74 | return GetFirst(&s, spec, wxDIR_DIRS | wxDIR_HIDDEN); |
| 75 | } |
| 76 | |
| 77 | #endif // !Unix |
| 78 | |
| 79 | // ---------------------------------------------------------------------------- |
| 80 | // wxDir::Traverse() |
| 81 | // ---------------------------------------------------------------------------- |
| 82 | |
| 83 | size_t wxDir::Traverse(wxDirTraverser& sink, |
| 84 | const wxString& filespec, |
| 85 | int flags) const |
| 86 | { |
| 87 | wxCHECK_MSG( IsOpened(), (size_t)-1, |
| 88 | _T("dir must be opened before traversing it") ); |
| 89 | |
| 90 | // the total number of files found |
| 91 | size_t nFiles = 0; |
| 92 | |
| 93 | // the name of this dir with path delimiter at the end |
| 94 | wxString prefix = GetName(); |
| 95 | prefix += wxFILE_SEP_PATH; |
| 96 | |
| 97 | // first, recurse into subdirs |
| 98 | if ( flags & wxDIR_DIRS ) |
| 99 | { |
| 100 | wxString dirname; |
| 101 | for ( bool cont = GetFirst(&dirname, _T(""), wxDIR_DIRS | wxDIR_HIDDEN); |
| 102 | cont; |
| 103 | cont = cont && GetNext(&dirname) ) |
| 104 | { |
| 105 | const wxString fulldirname = prefix + dirname; |
| 106 | |
| 107 | switch ( sink.OnDir(fulldirname) ) |
| 108 | { |
| 109 | default: |
| 110 | wxFAIL_MSG(_T("unexpected OnDir() return value") ); |
| 111 | // fall through |
| 112 | |
| 113 | case wxDIR_STOP: |
| 114 | cont = false; |
| 115 | break; |
| 116 | |
| 117 | case wxDIR_CONTINUE: |
| 118 | { |
| 119 | wxDir subdir; |
| 120 | |
| 121 | // don't give the error messages for the directories |
| 122 | // which we can't open: there can be all sorts of good |
| 123 | // reason for this (e.g. insufficient privileges) and |
| 124 | // this shouldn't be treated as an error -- instead |
| 125 | // let the user code decide what to do |
| 126 | bool ok; |
| 127 | do |
| 128 | { |
| 129 | wxLogNull noLog; |
| 130 | ok = subdir.Open(fulldirname); |
| 131 | if ( !ok ) |
| 132 | { |
| 133 | // ask the user code what to do |
| 134 | bool tryagain; |
| 135 | switch ( sink.OnOpenError(fulldirname) ) |
| 136 | { |
| 137 | default: |
| 138 | wxFAIL_MSG(_T("unexpected OnOpenError() return value") ); |
| 139 | // fall through |
| 140 | |
| 141 | case wxDIR_STOP: |
| 142 | cont = false; |
| 143 | // fall through |
| 144 | |
| 145 | case wxDIR_IGNORE: |
| 146 | tryagain = false; |
| 147 | break; |
| 148 | |
| 149 | case wxDIR_CONTINUE: |
| 150 | tryagain = true; |
| 151 | } |
| 152 | |
| 153 | if ( !tryagain ) |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | while ( !ok ); |
| 158 | |
| 159 | if ( ok ) |
| 160 | { |
| 161 | nFiles += subdir.Traverse(sink, filespec, flags); |
| 162 | } |
| 163 | } |
| 164 | break; |
| 165 | |
| 166 | case wxDIR_IGNORE: |
| 167 | // nothing to do |
| 168 | ; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // now enum our own files |
| 174 | if ( flags & wxDIR_FILES ) |
| 175 | { |
| 176 | flags &= ~wxDIR_DIRS; |
| 177 | |
| 178 | wxString filename; |
| 179 | bool cont = GetFirst(&filename, filespec, flags); |
| 180 | while ( cont ) |
| 181 | { |
| 182 | wxDirTraverseResult res = sink.OnFile(prefix + filename); |
| 183 | if ( res == wxDIR_STOP ) |
| 184 | break; |
| 185 | |
| 186 | wxASSERT_MSG( res == wxDIR_CONTINUE, |
| 187 | _T("unexpected OnFile() return value") ); |
| 188 | |
| 189 | nFiles++; |
| 190 | |
| 191 | cont = GetNext(&filename); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | return nFiles; |
| 196 | } |
| 197 | |
| 198 | // ---------------------------------------------------------------------------- |
| 199 | // wxDir::GetAllFiles() |
| 200 | // ---------------------------------------------------------------------------- |
| 201 | |
| 202 | class wxDirTraverserSimple : public wxDirTraverser |
| 203 | { |
| 204 | public: |
| 205 | wxDirTraverserSimple(wxArrayString& files) : m_files(files) { } |
| 206 | |
| 207 | virtual wxDirTraverseResult OnFile(const wxString& filename) |
| 208 | { |
| 209 | m_files.push_back(filename); |
| 210 | return wxDIR_CONTINUE; |
| 211 | } |
| 212 | |
| 213 | virtual wxDirTraverseResult OnDir(const wxString& WXUNUSED(dirname)) |
| 214 | { |
| 215 | return wxDIR_CONTINUE; |
| 216 | } |
| 217 | |
| 218 | private: |
| 219 | wxArrayString& m_files; |
| 220 | |
| 221 | DECLARE_NO_COPY_CLASS(wxDirTraverserSimple) |
| 222 | }; |
| 223 | |
| 224 | /* static */ |
| 225 | size_t wxDir::GetAllFiles(const wxString& dirname, |
| 226 | wxArrayString *files, |
| 227 | const wxString& filespec, |
| 228 | int flags) |
| 229 | { |
| 230 | wxCHECK_MSG( files, (size_t)-1, _T("NULL pointer in wxDir::GetAllFiles") ); |
| 231 | |
| 232 | size_t nFiles = 0; |
| 233 | |
| 234 | wxDir dir(dirname); |
| 235 | if ( dir.IsOpened() ) |
| 236 | { |
| 237 | wxDirTraverserSimple traverser(*files); |
| 238 | |
| 239 | nFiles += dir.Traverse(traverser, filespec, flags); |
| 240 | } |
| 241 | |
| 242 | return nFiles; |
| 243 | } |
| 244 | |