]> git.saurik.com Git - wxWidgets.git/blame - src/palmos/dir.cpp
No changes, just slightly simplify the tool insertion code.
[wxWidgets.git] / src / palmos / dir.cpp
CommitLineData
ffecfa5a 1/////////////////////////////////////////////////////////////////////////////
e2731512 2// Name: src/palmos/dir.cpp
4055ed82 3// Purpose: wxDir implementation for PalmOS
e2731512 4// Author: William Osborne - minimal working wxPalmOS port
ffecfa5a
JS
5// Modified by:
6// Created: 10.13.04
e2731512 7// RCS-ID: $Id$
ffecfa5a
JS
8// Copyright: (c) William Osborne
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
ffecfa5a
JS
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27#ifndef WX_PRECOMP
28 #include "wx/intl.h"
29 #include "wx/log.h"
30#endif // PCH
31
32#include "wx/dir.h"
da865fdd 33#include "wx/filefn.h" // for wxDirExists()
ffecfa5a 34
6afc1b46
VZ
35#include "pfall.h"
36
ffecfa5a
JS
37// ----------------------------------------------------------------------------
38// define the types and functions used for file searching
39// ----------------------------------------------------------------------------
40
41// ----------------------------------------------------------------------------
42// constants
43// ----------------------------------------------------------------------------
44
45#ifndef MAX_PATH
46 #define MAX_PATH 260 // from VC++ headers
47#endif
48
49// ----------------------------------------------------------------------------
50// macros
51// ----------------------------------------------------------------------------
52
53#define M_DIR ((wxDirData *)m_data)
54
55// ----------------------------------------------------------------------------
56// private classes
57// ----------------------------------------------------------------------------
58
6afc1b46
VZ
59// this class stores everything we need to enumerate the files
60class wxDirData
61{
62public:
63 wxDirData(const wxString& dirname);
64 ~wxDirData();
65
66 bool IsOk() const { return m_dir != NULL; }
67
68 void SetFileSpec(const wxString& filespec) { m_filespec = filespec; }
69 void SetFlags(int flags) { m_flags = flags; }
70
71 void Close();
72 bool Read(wxString *filename);
73
74 const wxString& GetName() const { return m_dirname; }
75
76private:
77 void *m_dir;
78
79 wxString m_dirname;
80 wxString m_filespec;
81
82 int m_flags;
83};
84
ffecfa5a
JS
85// ============================================================================
86// implementation
87// ============================================================================
88
6afc1b46
VZ
89// ----------------------------------------------------------------------------
90// wxDirData
91// ----------------------------------------------------------------------------
92
93wxDirData::wxDirData(const wxString& dirname)
94 : m_dirname(dirname)
95{
96 m_dir = NULL;
97
98 // throw away the trailing slashes
99 size_t n = m_dirname.length();
9a83f860 100 wxCHECK_RET( n, wxT("empty dir name in wxDir") );
6afc1b46
VZ
101
102 while ( n > 0 && m_dirname[--n] == '/' )
103 ;
104
105 m_dirname.Truncate(n + 1);
106
107 // do open the dir
108 //m_dir = opendir(m_dirname.fn_str());
109}
110
111wxDirData::~wxDirData()
112{
113 Close ();
114}
115
116void wxDirData::Close()
117{
118 if ( m_dir )
119 {
120 if ( svfs_dir_endfind (m_dir) != 0 )
121 {
9a83f860 122 wxLogLastError(wxT("svfs_dir_endfind"));
6afc1b46
VZ
123 }
124 m_dir = NULL;
125 }
126}
127
128bool wxDirData::Read(wxString *filename)
129{
d3b9f782 130 //dirent *de = NULL; // just to silence compiler warnings
6afc1b46
VZ
131 int ret;
132 char tmpbuf[300];
133 bool matches = false;
134 size_t flags_search;
135
136 // speed up string concatenation in the loop a bit
137 wxString path = m_dirname;
9a83f860 138 path += wxT('/');
6afc1b46
VZ
139 path.reserve(path.length() + 255);
140
141 wxString de_d_name;
142 de_d_name.reserve(500);
143 flags_search = 0;
144 if (wxDIR_DIRS & m_flags) {
145 flags_search |= SDIR_DIRS;
146 }
147 if (wxDIR_FILES & m_flags) {
148 flags_search |= SDIR_FILES;
149 }
150 if (NULL == m_dir) {
151#ifdef _PACC_VER
152// walk around the PalmOS pacc compiler bug
153 ret = svfs_dir_findfirst (m_dirname.fn_str().data(), &m_dir, tmpbuf, sizeof (tmpbuf), flags_search);
154#else
155 ret = svfs_dir_findfirst (m_dirname.fn_str(), &m_dir, tmpbuf, sizeof (tmpbuf), flags_search);
156#endif
157 } else {
158 ret = svfs_dir_findnext (m_dir, tmpbuf, sizeof (tmpbuf));
159 }
160 for (; ret > 0; ) {
161
162#if wxUSE_UNICODE
163 de_d_name = wxString(tmpbuf, *wxConvFileName);
164#else
165 de_d_name = tmpbuf;
166#endif
167
168 // don't return "." and ".." unless asked for
169 if ( tmpbuf[0] == '.' &&
170 ((tmpbuf[1] == '.' && tmpbuf[2] == '\0') ||
171 (tmpbuf[1] == '\0')) )
172 {
173 if ( !(m_flags & wxDIR_DOTDOT) )
174 continue;
175
176 // we found a valid match
177 break;
178 }
179
180 // check the name
181 if ( m_filespec.empty() )
182 {
183 matches = m_flags & wxDIR_HIDDEN ? true : tmpbuf[0] != '.';
184 }
185 else
186 {
187 // test against the pattern
188 matches = wxMatchWild(m_filespec, de_d_name,
189 !(m_flags & wxDIR_HIDDEN));
190 }
191 if (matches)
192 break;
193 ret = svfs_dir_findnext (m_dir, tmpbuf, sizeof (tmpbuf));
194 }
195
196 *filename = de_d_name;
197
198 return true;
199}
200
ffecfa5a
JS
201// ----------------------------------------------------------------------------
202// wxDir helpers
203// ----------------------------------------------------------------------------
204
205/* static */
206bool wxDir::Exists(const wxString& dir)
207{
6afc1b46 208 return wxDirExists(dir);
ffecfa5a
JS
209}
210
211// ----------------------------------------------------------------------------
212// wxDir construction/destruction
213// ----------------------------------------------------------------------------
214
215wxDir::wxDir(const wxString& dirname)
216{
6afc1b46
VZ
217 m_data = NULL;
218 (void)Open(dirname);
ffecfa5a
JS
219}
220
221bool wxDir::Open(const wxString& dirname)
222{
6afc1b46
VZ
223 delete M_DIR;
224 m_data = new wxDirData(dirname);
225
226 return true;
ffecfa5a
JS
227}
228
229bool wxDir::IsOpened() const
230{
6afc1b46 231 return m_data != NULL;
ffecfa5a
JS
232}
233
234wxString wxDir::GetName() const
235{
236 wxString name;
6afc1b46
VZ
237 if ( m_data )
238 {
239 name = M_DIR->GetName();
9a83f860 240 if ( !name.empty() && (name.Last() == wxT('/')) )
6afc1b46
VZ
241 {
242 // chop off the last (back)slash
243 name.Truncate(name.length() - 1);
244 }
245 }
ffecfa5a
JS
246
247 return name;
248}
249
250wxDir::~wxDir()
251{
6afc1b46 252 delete M_DIR;
ffecfa5a
JS
253}
254
255// ----------------------------------------------------------------------------
256// wxDir enumerating
257// ----------------------------------------------------------------------------
258
259bool wxDir::GetFirst(wxString *filename,
260 const wxString& filespec,
261 int flags) const
262{
9a83f860 263 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
6afc1b46
VZ
264 M_DIR->Close();
265 M_DIR->SetFileSpec(filespec);
266 M_DIR->SetFlags(flags);
267 return GetNext(filename);
ffecfa5a
JS
268}
269
270bool wxDir::GetNext(wxString *filename) const
271{
9a83f860
VZ
272 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
273 wxCHECK_MSG( filename, false, wxT("bad pointer in wxDir::GetNext()") );
6afc1b46 274 return M_DIR->Read(filename);
ffecfa5a
JS
275}
276