]> git.saurik.com Git - wxWidgets.git/blob - src/os2/dir.cpp
Added includes needed if not using precompiled headers.
[wxWidgets.git] / src / os2 / dir.cpp
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>
39 #else
40 #include <direct.h>
41 #endif
42
43 // ----------------------------------------------------------------------------
44 // macros
45 // ----------------------------------------------------------------------------
46
47 #define M_DIR ((wxDirData *)m_data)
48
49 // ----------------------------------------------------------------------------
50 // private classes
51 // ----------------------------------------------------------------------------
52
53 // this class stores everything we need to enumerate the files
54 class wxDirData
55 {
56 public:
57 wxDirData(const wxString& dirname);
58 ~wxDirData();
59
60 bool IsOk() const { return m_dir != NULL; }
61
62 void SetFileSpec(const wxString& filespec) { m_filespec = filespec; }
63 void SetFlags(int flags) { m_flags = flags; }
64
65 void Rewind() { rewinddir(m_dir); }
66 bool Read(wxString *filename);
67
68 private:
69 DIR *m_dir;
70
71 wxString m_dirname;
72 wxString m_filespec;
73
74 int m_flags;
75 };
76
77 // ============================================================================
78 // implementation
79 // ============================================================================
80
81 // ----------------------------------------------------------------------------
82 // wxDirData
83 // ----------------------------------------------------------------------------
84
85 wxDirData::wxDirData(const wxString& dirname)
86 : m_dirname(dirname)
87 {
88 m_dir = NULL;
89
90 // throw away the trailing slashes
91 size_t n = m_dirname.length();
92 wxCHECK_RET( n, _T("empty dir name in wxDir") );
93
94 while ( n > 0 && m_dirname[--n] == '/' )
95 ;
96
97 m_dirname.Truncate(n + 1);
98
99 // do open the dir
100 m_dir = opendir(m_dirname.fn_str());
101 }
102
103 wxDirData::~wxDirData()
104 {
105 if ( m_dir )
106 {
107 if ( closedir(m_dir) != 0 )
108 {
109 wxLogLastError(_T("closedir"));
110 }
111 }
112 }
113
114 bool wxDirData::Read(wxString *filename)
115 {
116 dirent *de;
117 bool matches = FALSE;
118
119 while ( !matches )
120 {
121 de = readdir(m_dir);
122 if ( !de )
123 return FALSE;
124
125 // don't return "." and ".." unless asked for
126 if ( de->d_name[0] == '.' &&
127 ((de->d_name[1] == '.' && de->d_name[2] == '\0') ||
128 (de->d_name[1] == '\0')) )
129 {
130 if ( !(m_flags & wxDIR_DOTDOT) )
131 continue;
132 }
133
134 // check the type now
135 if ( !(m_flags & wxDIR_FILES) &&
136 !wxDir::Exists(m_dirname + _T('/') + de->d_name) )
137 {
138 // it's a file, but we don't want them
139 continue;
140 }
141 else if ( !(m_flags & wxDIR_DIRS) &&
142 wxDir::Exists(m_dirname + _T('/') + de->d_name) )
143 {
144 // it's a dir, and we don't want it
145 continue;
146 }
147
148 // finally, check the name
149 if ( !m_filespec )
150 {
151 matches = m_flags & wxDIR_HIDDEN ? TRUE : de->d_name[0] != '.';
152 }
153 else
154 {
155 // test against the pattern
156 matches = wxMatchWild(m_filespec, de->d_name,
157 !(m_flags & wxDIR_HIDDEN));
158 }
159 }
160
161 *filename = de->d_name;
162
163 return TRUE;
164 }
165
166 // ----------------------------------------------------------------------------
167 // wxDir helpers
168 // ----------------------------------------------------------------------------
169
170 /* static */
171 bool wxDir::Exists(const wxString& dir)
172 {
173 return wxPathExists(dir);
174 }
175
176 // ----------------------------------------------------------------------------
177 // wxDir construction/destruction
178 // ----------------------------------------------------------------------------
179
180 wxDir::wxDir(const wxString& dirname)
181 {
182 m_data = NULL;
183
184 (void)Open(dirname);
185 }
186
187 bool wxDir::Open(const wxString& dirname)
188 {
189 delete M_DIR;
190 m_data = new wxDirData(dirname);
191
192 if ( !M_DIR->IsOk() )
193 {
194 wxLogSysError(_("Can not enumerate files in directory '%s'"),
195 dirname.c_str());
196
197 delete M_DIR;
198 m_data = NULL;
199
200 return FALSE;
201 }
202
203 return TRUE;
204 }
205
206 bool wxDir::IsOpened() const
207 {
208 return m_data != NULL;
209 }
210
211 wxDir::~wxDir()
212 {
213 delete M_DIR;
214 }
215
216 // ----------------------------------------------------------------------------
217 // wxDir enumerating
218 // ----------------------------------------------------------------------------
219
220 bool wxDir::GetFirst(wxString *filename,
221 const wxString& filespec,
222 int flags) const
223 {
224 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
225
226 M_DIR->Rewind();
227
228 M_DIR->SetFileSpec(filespec);
229 M_DIR->SetFlags(flags);
230
231 return GetNext(filename);
232 }
233
234 bool wxDir::GetNext(wxString *filename) const
235 {
236 wxCHECK_MSG( IsOpened(), FALSE, _T("must wxDir::Open() first") );
237
238 wxCHECK_MSG( filename, FALSE, _T("bad pointer in wxDir::GetNext()") );
239
240 return M_DIR->Read(filename);
241 }