]> git.saurik.com Git - wxWidgets.git/blob - src/mgl/dirmgl.cpp
Move some OLE utilities to oleutils.cpp and simplified date conversion
[wxWidgets.git] / src / mgl / dirmgl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mgl/dir.cpp
3 // Purpose: wxDir implementation for MGL
4 // Author: Vaclav Slavik, Vadim Zeitlin
5 // Modified by:
6 // Created: 2001/12/09
7 // RCS-ID: $Id$
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 /////////////////////////////////////////////////////////////////////////////
12
13 // ============================================================================
14 // declarations
15 // ============================================================================
16
17 // ----------------------------------------------------------------------------
18 // headers
19 // ----------------------------------------------------------------------------
20
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
23
24 #ifdef __BORLANDC__
25 #pragma hdrstop
26 #endif
27
28 #include "wx/defs.h"
29
30 #ifndef __UNIX__
31
32 #ifndef WX_PRECOMP
33 #include "wx/intl.h"
34 #include "wx/log.h"
35 #endif // PCH
36
37 #include "wx/dir.h"
38 #include "wx/filefn.h" // for wxMatchWild
39 #include "wx/mgl/private.h"
40
41 // ----------------------------------------------------------------------------
42 // macros
43 // ----------------------------------------------------------------------------
44
45 #define M_DIR ((wxDirData *)m_data)
46
47 // ----------------------------------------------------------------------------
48 // private classes
49 // ----------------------------------------------------------------------------
50
51 // this class stores everything we need to enumerate the files
52 class wxDirData
53 {
54 public:
55 wxDirData(const wxString& dirname);
56 ~wxDirData();
57
58 bool IsOk() const { return m_dir != NULL; }
59
60 void SetFileSpec(const wxString& filespec);
61 void SetFlags(int flags) { m_flags = flags; }
62
63 void Rewind();
64 bool Read(wxString *filename);
65
66 const wxString& GetName() const { return m_dirname; }
67
68 private:
69 void *m_dir;
70
71 wxString m_dirname;
72 wxString m_filespec;
73
74 int m_flags;
75 };
76
77 // ============================================================================
78 // implementation
79 // ============================================================================
80
81 // ----------------------------------------------------------------------------
82 // wxDirData
83 // ----------------------------------------------------------------------------
84
85 wxDirData::wxDirData(const wxString& dirname)
86 : m_dirname(dirname)
87 {
88 m_dir = NULL;
89
90 // throw away the trailing slashes
91 size_t n = m_dirname.length();
92 wxCHECK_RET( n, _T("empty dir name in wxDir") );
93
94 while ( n > 0 && m_dirname[--n] == wxFILE_SEP_PATH ) {}
95
96 m_dirname.Truncate(n + 1);
97 }
98
99 wxDirData::~wxDirData()
100 {
101 if ( m_dir )
102 PM_findClose(m_dir);
103 }
104
105 void wxDirData::SetFileSpec(const wxString& filespec)
106 {
107 #ifdef __DOS__
108 if ( filespec.empty() )
109 m_filespec = _T("*.*");
110 else
111 #endif
112 m_filespec = filespec;
113 }
114
115 void wxDirData::Rewind()
116 {
117 if ( m_dir )
118 {
119 PM_findClose(m_dir);
120 m_dir = NULL;
121 }
122 }
123
124 bool wxDirData::Read(wxString *filename)
125 {
126 PM_findData data;
127 bool matches = false;
128
129 data.dwSize = sizeof(data);
130
131 wxString path = m_dirname;
132 path += wxFILE_SEP_PATH;
133 path.reserve(path.length() + 255); // speed up string concatenation
134
135 while ( !matches )
136 {
137 if ( m_dir )
138 {
139 if ( !PM_findNextFile(m_dir, &data) )
140 return false;
141 }
142 else
143 {
144 m_dir = PM_findFirstFile(path + m_filespec , &data);
145 if ( m_dir == PM_FILE_INVALID )
146 {
147 m_dir = NULL;
148 return false;
149 }
150 }
151
152 // don't return "." and ".." unless asked for
153 if ( data.name[0] == '.' &&
154 ((data.name[1] == '.' && data.name[2] == '\0') ||
155 (data.name[1] == '\0')) )
156 {
157 if ( !(m_flags & wxDIR_DOTDOT) )
158 continue;
159
160 // we found a valid match
161 break;
162 }
163
164 // check the type now
165 if ( !(m_flags & wxDIR_FILES) && !(data.attrib & PM_FILE_DIRECTORY) )
166 {
167 // it's a file, but we don't want them
168 continue;
169 }
170 else if ( !(m_flags & wxDIR_DIRS) && (data.attrib & PM_FILE_DIRECTORY) )
171 {
172 // it's a dir, and we don't want it
173 continue;
174 }
175
176 matches = m_flags & wxDIR_HIDDEN ? true : !(data.attrib & PM_FILE_HIDDEN);
177 }
178
179 *filename = data.name;
180
181 return true;
182 }
183
184
185 // ----------------------------------------------------------------------------
186 // wxDir helpers
187 // ----------------------------------------------------------------------------
188
189 /* static */
190 bool wxDir::Exists(const wxString& dir)
191 {
192 return wxDirExists(dir);
193 }
194
195 // ----------------------------------------------------------------------------
196 // wxDir construction/destruction
197 // ----------------------------------------------------------------------------
198
199 wxDir::wxDir(const wxString& dirname)
200 {
201 m_data = NULL;
202
203 (void)Open(dirname);
204 }
205
206 bool wxDir::Open(const wxString& dirname)
207 {
208 delete M_DIR;
209 m_data = NULL;
210
211 if ( !wxDir::Exists(dirname) )
212 {
213 wxLogError(_("Directory '%s' doesn't exist!"), dirname.c_str());
214 return false;
215 }
216
217 m_data = new wxDirData(dirname);
218 return true;
219 }
220
221 bool wxDir::IsOpened() const
222 {
223 return m_data != NULL;
224 }
225
226 wxString wxDir::GetName() const
227 {
228 wxString name;
229 if ( m_data )
230 {
231 name = M_DIR->GetName();
232 if ( !name.empty() && (name.Last() == wxFILE_SEP_PATH) )
233 {
234 // chop off the last (back)slash
235 name.Truncate(name.length() - 1);
236 }
237 }
238
239 return name;
240 }
241
242 wxDir::~wxDir()
243 {
244 delete M_DIR;
245 }
246
247 // ----------------------------------------------------------------------------
248 // wxDir enumerating
249 // ----------------------------------------------------------------------------
250
251 bool wxDir::GetFirst(wxString *filename,
252 const wxString& filespec,
253 int flags) const
254 {
255 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
256
257 M_DIR->Rewind();
258
259 M_DIR->SetFileSpec(filespec);
260 M_DIR->SetFlags(flags);
261
262 return GetNext(filename);
263 }
264
265 bool wxDir::GetNext(wxString *filename) const
266 {
267 wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") );
268
269 wxCHECK_MSG( filename, false, _T("bad pointer in wxDir::GetNext()") );
270
271 return M_DIR->Read(filename);
272 }
273
274 #endif // !__UNIX__
275