]> git.saurik.com Git - wxWidgets.git/blame - src/common/dircmn.cpp
added and documented wxDir::HasFiles/SubDirs(), use the latter in wxDirDialog - it...
[wxWidgets.git] / src / common / dircmn.cpp
CommitLineData
35332784
VZ
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 license
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20/* this is done in platform-specific files
21#ifdef __GNUG__
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"
bdfeadca 37 #include "wx/filefn.h"
35332784
VZ
38#endif //WX_PRECOMP
39
40#include "wx/dir.h"
41
42// ============================================================================
43// implementation
44// ============================================================================
45
46// ----------------------------------------------------------------------------
47// wxDir::Traverse()
48// ----------------------------------------------------------------------------
49
50size_t wxDir::Traverse(wxDirTraverser& sink,
51 const wxString& filespec,
52 int flags) const
53{
54 wxCHECK_MSG( IsOpened(), (size_t)-1,
55 _T("dir must be opened before traversing it") );
56
57 // the total number of files found
58 size_t nFiles = 0;
59
60 // the name of this dir with path delimiter at the end
61 wxString prefix = GetName();
62 prefix += wxFILE_SEP_PATH;
63
64 // first, recurse into subdirs
65 if ( flags & wxDIR_DIRS )
66 {
67 wxString dirname;
68 bool cont = GetFirst(&dirname, _T(""), wxDIR_DIRS | wxDIR_HIDDEN);
69 while ( cont )
70 {
71 wxDirTraverseResult res = sink.OnDir(prefix + dirname);
72
73 if ( res == wxDIR_STOP )
74 break;
75
76 if ( res == wxDIR_CONTINUE )
77 {
78 wxDir subdir(prefix + dirname);
79 if ( subdir.IsOpened() )
80 {
81 nFiles += subdir.Traverse(sink, filespec, flags);
82 }
83 }
84 else
85 {
86 wxASSERT_MSG( res == wxDIR_IGNORE,
87 _T("unexpected OnDir() return value") );
88 }
89
90 cont = GetNext(&dirname);
91 }
92 }
93
94 // now enum our own files
95 if ( flags & wxDIR_FILES )
96 {
97 flags &= ~wxDIR_DIRS;
98
99 wxString filename;
100 bool cont = GetFirst(&filename, filespec, flags);
101 while ( cont )
102 {
103 wxDirTraverseResult res = sink.OnFile(prefix + filename);
104 if ( res == wxDIR_STOP )
105 break;
106
107 wxASSERT_MSG( res == wxDIR_CONTINUE,
108 _T("unexpected OnFile() return value") );
109
110 nFiles++;
111
112 cont = GetNext(&filename);
113 }
114 }
115
116 return nFiles;
117}
118
119// ----------------------------------------------------------------------------
120// wxDir::GetAllFiles()
121// ----------------------------------------------------------------------------
122
123class wxDirTraverserSimple : public wxDirTraverser
124{
125public:
126 wxDirTraverserSimple(wxArrayString& files) : m_files(files) { }
127
128 virtual wxDirTraverseResult OnFile(const wxString& filename)
129 {
130 m_files.Add(filename);
131 return wxDIR_CONTINUE;
132 }
133
134 virtual wxDirTraverseResult OnDir(const wxString& WXUNUSED(dirname))
135 {
136 return wxDIR_CONTINUE;
137 }
138
139private:
140 wxArrayString& m_files;
141};
142
143/* static */
144size_t wxDir::GetAllFiles(const wxString& dirname,
145 wxArrayString *files,
146 const wxString& filespec,
147 int flags)
148{
149 wxCHECK_MSG( files, (size_t)-1, _T("NULL pointer in wxDir::GetAllFiles") );
150
151 size_t nFiles = 0;
152
153 wxDir dir(dirname);
154 if ( dir.IsOpened() )
155 {
156 wxDirTraverserSimple traverser(*files);
157
158 nFiles += dir.Traverse(traverser, filespec, flags);
159 }
160
161 return nFiles;
162}
163