]>
git.saurik.com Git - wxWidgets.git/blob - src/palmos/dir.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/palmos/dir.cpp
3 // Purpose: wxDir implementation for PalmOS
4 // Author: William Osborne - minimal working wxPalmOS port
8 // Copyright: (c) William Osborne
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
33 #include "wx/filefn.h" // for wxDirExists()
37 // ----------------------------------------------------------------------------
38 // define the types and functions used for file searching
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
46 #define MAX_PATH 260 // from VC++ headers
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 #define M_DIR ((wxDirData *)m_data)
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // this class stores everything we need to enumerate the files
63 wxDirData(const wxString
& dirname
);
66 bool IsOk() const { return m_dir
!= NULL
; }
68 void SetFileSpec(const wxString
& filespec
) { m_filespec
= filespec
; }
69 void SetFlags(int flags
) { m_flags
= flags
; }
72 bool Read(wxString
*filename
);
74 const wxString
& GetName() const { return m_dirname
; }
85 // ============================================================================
87 // ============================================================================
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 wxDirData::wxDirData(const wxString
& dirname
)
98 // throw away the trailing slashes
99 size_t n
= m_dirname
.length();
100 wxCHECK_RET( n
, wxT("empty dir name in wxDir") );
102 while ( n
> 0 && m_dirname
[--n
] == '/' )
105 m_dirname
.Truncate(n
+ 1);
108 //m_dir = opendir(m_dirname.fn_str());
111 wxDirData::~wxDirData()
116 void wxDirData::Close()
120 if ( svfs_dir_endfind (m_dir
) != 0 )
122 wxLogLastError(wxT("svfs_dir_endfind"));
128 bool wxDirData::Read(wxString
*filename
)
130 //dirent *de = NULL; // just to silence compiler warnings
133 bool matches
= false;
136 // speed up string concatenation in the loop a bit
137 wxString path
= m_dirname
;
139 path
.reserve(path
.length() + 255);
142 de_d_name
.reserve(500);
144 if (wxDIR_DIRS
& m_flags
) {
145 flags_search
|= SDIR_DIRS
;
147 if (wxDIR_FILES
& m_flags
) {
148 flags_search
|= SDIR_FILES
;
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
);
155 ret
= svfs_dir_findfirst (m_dirname
.fn_str(), &m_dir
, tmpbuf
, sizeof (tmpbuf
), flags_search
);
158 ret
= svfs_dir_findnext (m_dir
, tmpbuf
, sizeof (tmpbuf
));
163 de_d_name
= wxString(tmpbuf
, *wxConvFileName
);
168 // don't return "." and ".." unless asked for
169 if ( tmpbuf
[0] == '.' &&
170 ((tmpbuf
[1] == '.' && tmpbuf
[2] == '\0') ||
171 (tmpbuf
[1] == '\0')) )
173 if ( !(m_flags
& wxDIR_DOTDOT
) )
176 // we found a valid match
181 if ( m_filespec
.empty() )
183 matches
= m_flags
& wxDIR_HIDDEN
? true : tmpbuf
[0] != '.';
187 // test against the pattern
188 matches
= wxMatchWild(m_filespec
, de_d_name
,
189 !(m_flags
& wxDIR_HIDDEN
));
193 ret
= svfs_dir_findnext (m_dir
, tmpbuf
, sizeof (tmpbuf
));
196 *filename
= de_d_name
;
201 // ----------------------------------------------------------------------------
203 // ----------------------------------------------------------------------------
206 bool wxDir::Exists(const wxString
& dir
)
208 return wxDirExists(dir
);
211 // ----------------------------------------------------------------------------
212 // wxDir construction/destruction
213 // ----------------------------------------------------------------------------
215 wxDir::wxDir(const wxString
& dirname
)
221 bool wxDir::Open(const wxString
& dirname
)
224 m_data
= new wxDirData(dirname
);
229 bool wxDir::IsOpened() const
231 return m_data
!= NULL
;
234 wxString
wxDir::GetName() const
239 name
= M_DIR
->GetName();
240 if ( !name
.empty() && (name
.Last() == wxT('/')) )
242 // chop off the last (back)slash
243 name
.Truncate(name
.length() - 1);
255 // ----------------------------------------------------------------------------
257 // ----------------------------------------------------------------------------
259 bool wxDir::GetFirst(wxString
*filename
,
260 const wxString
& filespec
,
263 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
265 M_DIR
->SetFileSpec(filespec
);
266 M_DIR
->SetFlags(flags
);
267 return GetNext(filename
);
270 bool wxDir::GetNext(wxString
*filename
) const
272 wxCHECK_MSG( IsOpened(), false, wxT("must wxDir::Open() first") );
273 wxCHECK_MSG( filename
, false, wxT("bad pointer in wxDir::GetNext()") );
274 return M_DIR
->Read(filename
);