]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/palmos/dir.cpp | |
3 | // Purpose: wxDir implementation for PalmOS | |
4 | // Author: William Osborne - minimal working wxPalmOS port | |
5 | // Modified by: | |
6 | // Created: 10.13.04 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) William Osborne | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
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" | |
33 | #include "wx/filefn.h" // for wxDirExists() | |
34 | ||
35 | #include "pfall.h" | |
36 | ||
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 | ||
59 | // this class stores everything we need to enumerate the files | |
60 | class wxDirData | |
61 | { | |
62 | public: | |
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 | ||
76 | private: | |
77 | void *m_dir; | |
78 | ||
79 | wxString m_dirname; | |
80 | wxString m_filespec; | |
81 | ||
82 | int m_flags; | |
83 | }; | |
84 | ||
85 | // ============================================================================ | |
86 | // implementation | |
87 | // ============================================================================ | |
88 | ||
89 | // ---------------------------------------------------------------------------- | |
90 | // wxDirData | |
91 | // ---------------------------------------------------------------------------- | |
92 | ||
93 | wxDirData::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(); | |
100 | wxCHECK_RET( n, _T("empty dir name in wxDir") ); | |
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 | ||
111 | wxDirData::~wxDirData() | |
112 | { | |
113 | Close (); | |
114 | } | |
115 | ||
116 | void wxDirData::Close() | |
117 | { | |
118 | if ( m_dir ) | |
119 | { | |
120 | if ( svfs_dir_endfind (m_dir) != 0 ) | |
121 | { | |
122 | wxLogLastError(_T("svfs_dir_endfind")); | |
123 | } | |
124 | m_dir = NULL; | |
125 | } | |
126 | } | |
127 | ||
128 | bool wxDirData::Read(wxString *filename) | |
129 | { | |
130 | //dirent *de = (dirent *)NULL; // just to silence compiler warnings | |
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; | |
138 | path += _T('/'); | |
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 | ||
201 | // ---------------------------------------------------------------------------- | |
202 | // wxDir helpers | |
203 | // ---------------------------------------------------------------------------- | |
204 | ||
205 | /* static */ | |
206 | bool wxDir::Exists(const wxString& dir) | |
207 | { | |
208 | return wxDirExists(dir); | |
209 | } | |
210 | ||
211 | // ---------------------------------------------------------------------------- | |
212 | // wxDir construction/destruction | |
213 | // ---------------------------------------------------------------------------- | |
214 | ||
215 | wxDir::wxDir(const wxString& dirname) | |
216 | { | |
217 | m_data = NULL; | |
218 | (void)Open(dirname); | |
219 | } | |
220 | ||
221 | bool wxDir::Open(const wxString& dirname) | |
222 | { | |
223 | delete M_DIR; | |
224 | m_data = new wxDirData(dirname); | |
225 | ||
226 | return true; | |
227 | } | |
228 | ||
229 | bool wxDir::IsOpened() const | |
230 | { | |
231 | return m_data != NULL; | |
232 | } | |
233 | ||
234 | wxString wxDir::GetName() const | |
235 | { | |
236 | wxString name; | |
237 | if ( m_data ) | |
238 | { | |
239 | name = M_DIR->GetName(); | |
240 | if ( !name.empty() && (name.Last() == _T('/')) ) | |
241 | { | |
242 | // chop off the last (back)slash | |
243 | name.Truncate(name.length() - 1); | |
244 | } | |
245 | } | |
246 | ||
247 | return name; | |
248 | } | |
249 | ||
250 | wxDir::~wxDir() | |
251 | { | |
252 | delete M_DIR; | |
253 | } | |
254 | ||
255 | // ---------------------------------------------------------------------------- | |
256 | // wxDir enumerating | |
257 | // ---------------------------------------------------------------------------- | |
258 | ||
259 | bool wxDir::GetFirst(wxString *filename, | |
260 | const wxString& filespec, | |
261 | int flags) const | |
262 | { | |
263 | wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") ); | |
264 | M_DIR->Close(); | |
265 | M_DIR->SetFileSpec(filespec); | |
266 | M_DIR->SetFlags(flags); | |
267 | return GetNext(filename); | |
268 | } | |
269 | ||
270 | bool wxDir::GetNext(wxString *filename) const | |
271 | { | |
272 | wxCHECK_MSG( IsOpened(), false, _T("must wxDir::Open() first") ); | |
273 | wxCHECK_MSG( filename, false, _T("bad pointer in wxDir::GetNext()") ); | |
274 | return M_DIR->Read(filename); | |
275 | } | |
276 |