]> git.saurik.com Git - wxWidgets.git/blame - src/os2/dir.cpp
added bitmaps to menu items
[wxWidgets.git] / src / os2 / dir.cpp
CommitLineData
6ef85b1b
SN
1/////////////////////////////////////////////////////////////////////////////
2// Name: os2/dir.cpp
3// Purpose: wxDir implementation for OS/2
4// Author: Vadim Zeitlin
5// Modified by: Stefan Neis
6// Created: 08.12.99
7// RCS-ID: $Id$
8// Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "dir.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
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 wxMatchWild
34
35#include <sys/types.h>
36
37#ifdef __EMX__
38#include <dirent.h>
6ef85b1b
SN
39#endif
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
52class wxDirData
53{
54public:
55 wxDirData(const wxString& dirname);
56 ~wxDirData();
57
58 bool IsOk() const { return m_dir != NULL; }
59
60 void SetFileSpec(const wxString& filespec) { m_filespec = filespec; }
61 void SetFlags(int flags) { m_flags = flags; }
62
63 void Rewind() { rewinddir(m_dir); }
64 bool Read(wxString *filename);
65
66private:
67 DIR *m_dir;
68
69 wxString m_dirname;
70 wxString m_filespec;
71
72 int m_flags;
73};
74
75// ============================================================================
76// implementation
77// ============================================================================
78
79// ----------------------------------------------------------------------------
80// wxDirData
81// ----------------------------------------------------------------------------
82
83wxDirData::wxDirData(const wxString& dirname)
84 : m_dirname(dirname)
85{
86 m_dir = NULL;
87
88 // throw away the trailing slashes
89 size_t n = m_dirname.length();
90 wxCHECK_RET( n, _T("empty dir name in wxDir") );
91
92 while ( n > 0 && m_dirname[--n] == '/' )
93 ;
94
95 m_dirname.Truncate(n + 1);
96
97 // do open the dir
98 m_dir = opendir(m_dirname.fn_str());
99}
100
101wxDirData::~wxDirData()
102{
103 if ( m_dir )
104 {
105 if ( closedir(m_dir) != 0 )
106 {
107 wxLogLastError(_T("closedir"));
108 }
109 }
110}
111
112bool wxDirData::Read(wxString *filename)
113{
114 dirent *de;
115 bool matches = FALSE;
116
117 while ( !matches )
118 {
119 de = readdir(m_dir);
120 if ( !de )
121 return FALSE;
122
123 // don't return "." and ".." unless asked for
124 if ( de->d_name[0] == '.' &&
125 ((de->d_name[1] == '.' && de->d_name[2] == '\0') ||
126 (de->d_name[1] == '\0')) )
127 {
128 if ( !(m_flags & wxDIR_DOTDOT) )
129 continue;
130 }
131
132 // check the type now
133 if ( !(m_flags & wxDIR_FILES) &&
134 !wxDir::Exists(m_dirname + _T('/') + de->d_name) )
135 {
136 // it's a file, but we don't want them
137 continue;
138 }
139 else if ( !(m_flags & wxDIR_DIRS) &&
140 wxDir::Exists(m_dirname + _T('/') + de->d_name) )
141 {
142 // it's a dir, and we don't want it
143 continue;
144 }
145
146 // finally, check the name
147 if ( !m_filespec )
148 {
149 matches = m_flags & wxDIR_HIDDEN ? TRUE : de->d_name[0] != '.';
150 }
151 else
152 {
153 // test against the pattern
154 matches = wxMatchWild(m_filespec, de->d_name,
155 !(m_flags & wxDIR_HIDDEN));
156 }
157 }
158
159 *filename = de->d_name;
160
161 return TRUE;
162}
163
164// ----------------------------------------------------------------------------
165// wxDir helpers
166// ----------------------------------------------------------------------------
167
168/* static */
169bool wxDir::Exists(const wxString& dir)
170{
171 return wxPathExists(dir);
172}
173
174// ----------------------------------------------------------------------------
175// wxDir construction/destruction
176// ----------------------------------------------------------------------------
177
178wxDir::wxDir(const wxString& dirname)
179{
180 m_data = NULL;
181
182 (void)Open(dirname);
183}
184
185bool wxDir::Open(const wxString& dirname)
186{
187 delete M_DIR;
188 m_data = new wxDirData(dirname);
189
190 if ( !M_DIR->IsOk() )
191 {
192 wxLogSysError(_("Can not enumerate files in directory '%s'"),
193 dirname.c_str());
194
195 delete M_DIR;
196 m_data = NULL;
197
198 return FALSE;
199 }
200
201 return TRUE;
202}
203
204bool wxDir::IsOpened() const
205{
206 return m_data != NULL;
207}
208
209wxDir::~wxDir()
210{
211 delete M_DIR;
212}
213
214// ----------------------------------------------------------------------------
215// wxDir enumerating
216// ----------------------------------------------------------------------------
217
218bool wxDir::GetFirst(wxString *filename,
219 const wxString& filespec,
220 int flags) const
221{
222 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
223
224 M_DIR->Rewind();
225
226 M_DIR->SetFileSpec(filespec);
227 M_DIR->SetFlags(flags);
228
229 return GetNext(filename);
230}
231
232bool wxDir::GetNext(wxString *filename) const
233{
234 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
235
236 wxCHECK_MSG( filename, FALSE, _T("bad pointer in wxDir::GetNext()") );
237
238 return M_DIR->Read(filename);
239}