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