]> git.saurik.com Git - wxWidgets.git/blob - src/common/wfstream.cpp
Adjusted filelist.txt and wxUniv.dsp to use generic
[wxWidgets.git] / src / common / wfstream.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: fstream.cpp
3 // Purpose: "File stream" classes
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 11/07/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "wfstream.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_STREAMS && wxUSE_FILE
24
25 #include <stdio.h>
26 #include "wx/stream.h"
27 #include "wx/wfstream.h"
28
29 // ----------------------------------------------------------------------------
30 // wxFileInputStream
31 // ----------------------------------------------------------------------------
32
33 wxFileInputStream::wxFileInputStream(const wxString& fileName)
34 : wxInputStream()
35 {
36 m_file = new wxFile(fileName, wxFile::read);
37 m_file_destroy = TRUE;
38 }
39
40 wxFileInputStream::wxFileInputStream()
41 : wxInputStream()
42 {
43 m_file_destroy = FALSE;
44 m_file = NULL;
45 }
46
47 wxFileInputStream::wxFileInputStream(wxFile& file)
48 {
49 m_file = &file;
50 m_file_destroy = FALSE;
51 }
52
53 wxFileInputStream::wxFileInputStream(int fd)
54 {
55 m_file = new wxFile(fd);
56 m_file_destroy = TRUE;
57 }
58
59 wxFileInputStream::~wxFileInputStream()
60 {
61 if (m_file_destroy)
62 delete m_file;
63 }
64
65 size_t wxFileInputStream::GetSize() const
66 {
67 return m_file->Length();
68 }
69
70 size_t wxFileInputStream::OnSysRead(void *buffer, size_t size)
71 {
72 off_t ret = m_file->Read(buffer, size);
73
74 switch ( ret )
75 {
76 case 0:
77 m_lasterror = wxSTREAM_EOF;
78 break;
79
80 case wxInvalidOffset:
81 m_lasterror = wxSTREAM_READ_ERROR;
82 ret = 0;
83 break;
84
85 default:
86 m_lasterror = wxSTREAM_NO_ERROR;
87 }
88
89 return ret;
90 }
91
92 off_t wxFileInputStream::OnSysSeek(off_t pos, wxSeekMode mode)
93 {
94 return m_file->Seek(pos, mode);
95 }
96
97 off_t wxFileInputStream::OnSysTell() const
98 {
99 return m_file->Tell();
100 }
101
102 // ----------------------------------------------------------------------------
103 // wxFileOutputStream
104 // ----------------------------------------------------------------------------
105
106 wxFileOutputStream::wxFileOutputStream(const wxString& fileName)
107 {
108 m_file = new wxFile(fileName, wxFile::write);
109 m_file_destroy = TRUE;
110
111 if (!m_file->IsOpened())
112 {
113 m_lasterror = wxSTREAM_WRITE_ERROR;
114 }
115 else
116 {
117 if (m_file->Error())
118 m_lasterror = wxSTREAM_WRITE_ERROR;
119 }
120 }
121
122 wxFileOutputStream::wxFileOutputStream(wxFile& file)
123 {
124 m_file = &file;
125 m_file_destroy = FALSE;
126 }
127
128 wxFileOutputStream::wxFileOutputStream()
129 : wxOutputStream()
130 {
131 m_file_destroy = FALSE;
132 m_file = NULL;
133 }
134
135 wxFileOutputStream::wxFileOutputStream(int fd)
136 {
137 m_file = new wxFile(fd);
138 m_file_destroy = TRUE;
139 }
140
141 wxFileOutputStream::~wxFileOutputStream()
142 {
143 if (m_file_destroy)
144 {
145 Sync();
146 delete m_file;
147 }
148 }
149
150 size_t wxFileOutputStream::OnSysWrite(const void *buffer, size_t size)
151 {
152 size_t ret = m_file->Write(buffer, size);
153
154 m_lasterror = m_file->Error() ? wxSTREAM_WRITE_ERROR : wxSTREAM_NO_ERROR;
155
156 return ret;
157 }
158
159 off_t wxFileOutputStream::OnSysTell() const
160 {
161 return m_file->Tell();
162 }
163
164 off_t wxFileOutputStream::OnSysSeek(off_t pos, wxSeekMode mode)
165 {
166 return m_file->Seek(pos, mode);
167 }
168
169 void wxFileOutputStream::Sync()
170 {
171 wxOutputStream::Sync();
172 m_file->Flush();
173 }
174
175 size_t wxFileOutputStream::GetSize() const
176 {
177 return m_file->Length();
178 }
179
180 // ----------------------------------------------------------------------------
181 // wxFileStream
182 // ----------------------------------------------------------------------------
183
184 wxFileStream::wxFileStream(const wxString& fileName)
185 : wxFileInputStream(fileName)
186 {
187 wxFileOutputStream::m_file = wxFileInputStream::m_file;
188 }
189
190 // ----------------------------------------------------------------------------
191 // wxFFileInputStream
192 // ----------------------------------------------------------------------------
193
194 wxFFileInputStream::wxFFileInputStream(const wxString& fileName)
195 : wxInputStream()
196 {
197 m_file = new wxFFile(fileName, "rb");
198 m_file_destroy = TRUE;
199 }
200
201 wxFFileInputStream::wxFFileInputStream()
202 : wxInputStream()
203 {
204 m_file_destroy = FALSE;
205 m_file = NULL;
206 }
207
208 wxFFileInputStream::wxFFileInputStream(wxFFile& file)
209 {
210 m_file = &file;
211 m_file_destroy = FALSE;
212 }
213
214 wxFFileInputStream::wxFFileInputStream(FILE *file)
215 {
216 m_file = new wxFFile(file);
217 m_file_destroy = TRUE;
218 }
219
220 wxFFileInputStream::~wxFFileInputStream()
221 {
222 if (m_file_destroy)
223 delete m_file;
224 }
225
226 size_t wxFFileInputStream::GetSize() const
227 {
228 return m_file->Length();
229 }
230
231 size_t wxFFileInputStream::OnSysRead(void *buffer, size_t size)
232 {
233 off_t ret;
234
235 ret = m_file->Read(buffer, size);
236
237 if (m_file->Eof())
238 m_lasterror = wxStream_EOF;
239 if (ret == wxInvalidOffset)
240 {
241 m_lasterror = wxStream_READ_ERR;
242 ret = 0;
243 }
244
245 return ret;
246 }
247
248 off_t wxFFileInputStream::OnSysSeek(off_t pos, wxSeekMode mode)
249 {
250 return ( m_file->Seek(pos, mode) ? pos : wxInvalidOffset );
251 }
252
253 off_t wxFFileInputStream::OnSysTell() const
254 {
255 return m_file->Tell();
256 }
257
258 // ----------------------------------------------------------------------------
259 // wxFFileOutputStream
260 // ----------------------------------------------------------------------------
261
262 wxFFileOutputStream::wxFFileOutputStream(const wxString& fileName)
263 {
264 m_file = new wxFFile(fileName, "w+b");
265 m_file_destroy = TRUE;
266
267 if (!m_file->IsOpened())
268 {
269 m_lasterror = wxSTREAM_WRITE_ERROR;
270 }
271 else
272 {
273 if (m_file->Error())
274 m_lasterror = wxSTREAM_WRITE_ERROR;
275 }
276 }
277
278 wxFFileOutputStream::wxFFileOutputStream(wxFFile& file)
279 {
280 m_file = &file;
281 m_file_destroy = FALSE;
282 }
283
284 wxFFileOutputStream::wxFFileOutputStream()
285 : wxOutputStream()
286 {
287 m_file_destroy = FALSE;
288 m_file = NULL;
289 }
290
291 wxFFileOutputStream::wxFFileOutputStream(FILE *file)
292 {
293 m_file = new wxFFile(file);
294 m_file_destroy = TRUE;
295 }
296
297 wxFFileOutputStream::~wxFFileOutputStream()
298 {
299 if (m_file_destroy)
300 {
301 Sync();
302 delete m_file;
303 }
304 }
305
306 size_t wxFFileOutputStream::OnSysWrite(const void *buffer, size_t size)
307 {
308 size_t ret = m_file->Write(buffer, size);
309 if (m_file->Error())
310 m_lasterror = wxStream_WRITE_ERR;
311 else
312 m_lasterror = wxStream_NOERROR;
313 return ret;
314 }
315
316 off_t wxFFileOutputStream::OnSysTell() const
317 {
318 return m_file->Tell();
319 }
320
321 off_t wxFFileOutputStream::OnSysSeek(off_t pos, wxSeekMode mode)
322 {
323 return ( m_file->Seek(pos, mode) ? pos : wxInvalidOffset );
324 }
325
326 void wxFFileOutputStream::Sync()
327 {
328 wxOutputStream::Sync();
329 m_file->Flush();
330 }
331
332 size_t wxFFileOutputStream::GetSize() const
333 {
334 return m_file->Length();
335 }
336
337 // ----------------------------------------------------------------------------
338 // wxFFileStream
339 // ----------------------------------------------------------------------------
340
341 wxFFileStream::wxFFileStream(const wxString& fileName)
342 : wxFFileInputStream(fileName)
343 {
344 wxFFileOutputStream::m_file = wxFFileInputStream::m_file;
345 }
346
347 #endif
348 // wxUSE_STREAMS && wxUSE_FILE
349