]> git.saurik.com Git - wxWidgets.git/blame - src/common/wfstream.cpp
Pressing build-in joystick on WinCE phones fires wxEVT_JOY_BUTTON_DOWN event.
[wxWidgets.git] / src / common / wfstream.cpp
CommitLineData
3d4c6a21
GL
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$
f6bcfd97 8// Copyright: (c) Guilhem Lavaux
65571936 9// Licence: wxWindows licence
3d4c6a21
GL
10/////////////////////////////////////////////////////////////////////////////
11
79c3e0e1
GL
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
3d4c6a21 14
79c3e0e1 15#ifdef __BORLANDC__
ce4169a4 16 #pragma hdrstop
79c3e0e1
GL
17#endif
18
85990624 19#if wxUSE_STREAMS
ce4169a4
RR
20
21#include <stdio.h>
d1af991f
RR
22#include "wx/stream.h"
23#include "wx/wfstream.h"
ce4169a4 24
85990624
RN
25#if wxUSE_FILE
26
79c3e0e1
GL
27// ----------------------------------------------------------------------------
28// wxFileInputStream
29// ----------------------------------------------------------------------------
30
31wxFileInputStream::wxFileInputStream(const wxString& fileName)
25c70b07 32 : wxInputStream()
3d4c6a21 33{
9adf4299 34 m_file = new wxFile(fileName, wxFile::read);
cab1a605 35 m_file_destroy = true;
b9698194
VZ
36 if ( !m_file->IsOpened() )
37 m_lasterror = wxSTREAM_READ_ERROR;
3d4c6a21
GL
38}
39
25c70b07
GL
40wxFileInputStream::wxFileInputStream()
41 : wxInputStream()
42{
cab1a605 43 m_file_destroy = false;
f6bcfd97 44 m_file = NULL;
25c70b07
GL
45}
46
0aca1ded
GL
47wxFileInputStream::wxFileInputStream(wxFile& file)
48{
f6bcfd97 49 m_file = &file;
cab1a605 50 m_file_destroy = false;
0aca1ded
GL
51}
52
53wxFileInputStream::wxFileInputStream(int fd)
54{
f6bcfd97 55 m_file = new wxFile(fd);
cab1a605 56 m_file_destroy = true;
0aca1ded
GL
57}
58
79c3e0e1 59wxFileInputStream::~wxFileInputStream()
3d4c6a21 60{
f6bcfd97
BP
61 if (m_file_destroy)
62 delete m_file;
3d4c6a21
GL
63}
64
588066b7 65wxFileOffset wxFileInputStream::GetLength() const
84b46c35 66{
f6bcfd97 67 return m_file->Length();
84b46c35
GL
68}
69
75ed1d15 70size_t wxFileInputStream::OnSysRead(void *buffer, size_t size)
6d44bf31 71{
f8a586e0 72 ssize_t ret = m_file->Read(buffer, size);
fae05df5 73
e69a1ea8 74 // NB: we can't use a switch here because HP-UX CC doesn't allow
30984dea 75 // switching over long long (which size_t is in 64bit mode)
9a26db9e 76
e69a1ea8
VZ
77 if ( !ret )
78 {
79 // nothing read, so nothing more to read
80 m_lasterror = wxSTREAM_EOF;
81 }
f8a586e0 82 else if ( ret == wxInvalidOffset )
e69a1ea8
VZ
83 {
84 m_lasterror = wxSTREAM_READ_ERROR;
85 ret = 0;
86 }
87 else
88 {
89 // normal case
90 m_lasterror = wxSTREAM_NO_ERROR;
f6bcfd97 91 }
fae05df5 92
f6bcfd97 93 return ret;
6d44bf31
GL
94}
95
4004775e 96wxFileOffset wxFileInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
3d4c6a21 97{
9a26db9e 98 return m_file->Seek(pos, mode);
79c3e0e1
GL
99}
100
4004775e 101wxFileOffset wxFileInputStream::OnSysTell() const
79c3e0e1 102{
f6bcfd97 103 return m_file->Tell();
79c3e0e1
GL
104}
105
106// ----------------------------------------------------------------------------
107// wxFileOutputStream
108// ----------------------------------------------------------------------------
109
110wxFileOutputStream::wxFileOutputStream(const wxString& fileName)
25c70b07 111{
9adf4299 112 m_file = new wxFile(fileName, wxFile::write);
cab1a605 113 m_file_destroy = true;
9a26db9e 114
942bef71 115 if (!m_file->IsOpened())
942bef71 116 m_lasterror = wxSTREAM_WRITE_ERROR;
25c70b07
GL
117}
118
84b46c35
GL
119wxFileOutputStream::wxFileOutputStream(wxFile& file)
120{
f6bcfd97 121 m_file = &file;
cab1a605 122 m_file_destroy = false;
84b46c35
GL
123}
124
25c70b07 125wxFileOutputStream::wxFileOutputStream()
9a26db9e 126 : wxOutputStream()
79c3e0e1 127{
cab1a605 128 m_file_destroy = false;
f6bcfd97 129 m_file = NULL;
3d4c6a21
GL
130}
131
0aca1ded
GL
132wxFileOutputStream::wxFileOutputStream(int fd)
133{
f6bcfd97 134 m_file = new wxFile(fd);
cab1a605 135 m_file_destroy = true;
0aca1ded
GL
136}
137
79c3e0e1 138wxFileOutputStream::~wxFileOutputStream()
3d4c6a21 139{
9a26db9e 140 if (m_file_destroy)
f6bcfd97
BP
141 {
142 Sync();
143 delete m_file;
144 }
79c3e0e1 145}
3d4c6a21 146
75ed1d15 147size_t wxFileOutputStream::OnSysWrite(const void *buffer, size_t size)
79c3e0e1 148{
f6bcfd97 149 size_t ret = m_file->Write(buffer, size);
9a26db9e
VZ
150
151 m_lasterror = m_file->Error() ? wxSTREAM_WRITE_ERROR : wxSTREAM_NO_ERROR;
152
f6bcfd97 153 return ret;
79c3e0e1 154}
3d4c6a21 155
4004775e 156wxFileOffset wxFileOutputStream::OnSysTell() const
79c3e0e1 157{
f6bcfd97 158 return m_file->Tell();
3d4c6a21
GL
159}
160
4004775e 161wxFileOffset wxFileOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
3d4c6a21 162{
f6bcfd97 163 return m_file->Seek(pos, mode);
3d4c6a21
GL
164}
165
79c3e0e1 166void wxFileOutputStream::Sync()
3d4c6a21 167{
f6bcfd97
BP
168 wxOutputStream::Sync();
169 m_file->Flush();
3d4c6a21 170}
84b46c35 171
588066b7 172wxFileOffset wxFileOutputStream::GetLength() const
84b46c35 173{
f6bcfd97 174 return m_file->Length();
84b46c35
GL
175}
176
177// ----------------------------------------------------------------------------
e1265174
MW
178// wxTempFileOutputStream
179// ----------------------------------------------------------------------------
180
181wxTempFileOutputStream::wxTempFileOutputStream(const wxString& fileName)
182{
183 m_file = new wxTempFile(fileName);
184
185 if (!m_file->IsOpened())
186 m_lasterror = wxSTREAM_WRITE_ERROR;
187}
188
189wxTempFileOutputStream::~wxTempFileOutputStream()
190{
191 if (m_file->IsOpened())
192 Discard();
193 delete m_file;
194}
195
196size_t wxTempFileOutputStream::OnSysWrite(const void *buffer, size_t size)
197{
198 if (IsOk() && m_file->Write(buffer, size))
199 return size;
200 m_lasterror = wxSTREAM_WRITE_ERROR;
201 return 0;
202}
203
204// ----------------------------------------------------------------------------
84b46c35
GL
205// wxFileStream
206// ----------------------------------------------------------------------------
f6bcfd97 207
84b46c35 208wxFileStream::wxFileStream(const wxString& fileName)
f6bcfd97 209 : wxFileInputStream(fileName)
84b46c35 210{
f6bcfd97 211 wxFileOutputStream::m_file = wxFileInputStream::m_file;
84b46c35 212}
ce4169a4 213
85990624
RN
214#endif //wxUSE_FILE
215
216#if wxUSE_FFILE
217
65045edd
RR
218// ----------------------------------------------------------------------------
219// wxFFileInputStream
220// ----------------------------------------------------------------------------
221
5fec5bb6
VZ
222wxFFileInputStream::wxFFileInputStream(const wxString& fileName,
223 const wxChar *mode)
224 : wxInputStream()
65045edd 225{
5fec5bb6 226 m_file = new wxFFile(fileName, mode);
cab1a605 227 m_file_destroy = true;
ecc20148
MW
228
229 if (!m_file->IsOpened())
230 m_lasterror = wxSTREAM_WRITE_ERROR;
65045edd
RR
231}
232
233wxFFileInputStream::wxFFileInputStream()
5fec5bb6 234 : wxInputStream()
65045edd 235{
f6bcfd97 236 m_file = NULL;
5fec5bb6 237 m_file_destroy = false;
65045edd
RR
238}
239
240wxFFileInputStream::wxFFileInputStream(wxFFile& file)
241{
f6bcfd97 242 m_file = &file;
cab1a605 243 m_file_destroy = false;
65045edd
RR
244}
245
246wxFFileInputStream::wxFFileInputStream(FILE *file)
247{
f6bcfd97 248 m_file = new wxFFile(file);
cab1a605 249 m_file_destroy = true;
65045edd
RR
250}
251
252wxFFileInputStream::~wxFFileInputStream()
253{
f6bcfd97
BP
254 if (m_file_destroy)
255 delete m_file;
65045edd
RR
256}
257
588066b7 258wxFileOffset wxFFileInputStream::GetLength() const
65045edd 259{
f6bcfd97 260 return m_file->Length();
65045edd
RR
261}
262
263size_t wxFFileInputStream::OnSysRead(void *buffer, size_t size)
264{
f8a586e0 265 ssize_t ret = m_file->Read(buffer, size);
65045edd 266
b9d84e4c
DE
267 // It is not safe to call Eof() if the file is not opened.
268 if (!m_file->IsOpened() || m_file->Eof())
2b5f62a0 269 m_lasterror = wxSTREAM_EOF;
f8a586e0 270 if (ret == wxInvalidOffset)
f6bcfd97 271 {
2b5f62a0 272 m_lasterror = wxSTREAM_READ_ERROR;
f6bcfd97
BP
273 ret = 0;
274 }
65045edd 275
f6bcfd97 276 return ret;
65045edd
RR
277}
278
4004775e 279wxFileOffset wxFFileInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
65045edd 280{
70a7bd90 281 return m_file->Seek(pos, mode) ? m_file->Tell() : wxInvalidOffset;
65045edd
RR
282}
283
4004775e 284wxFileOffset wxFFileInputStream::OnSysTell() const
65045edd 285{
f6bcfd97 286 return m_file->Tell();
65045edd
RR
287}
288
289// ----------------------------------------------------------------------------
290// wxFFileOutputStream
291// ----------------------------------------------------------------------------
292
5fec5bb6
VZ
293wxFFileOutputStream::wxFFileOutputStream(const wxString& fileName,
294 const wxChar *mode)
65045edd 295{
5fec5bb6 296 m_file = new wxFFile(fileName, mode);
cab1a605 297 m_file_destroy = true;
9a26db9e 298
942bef71
RR
299 if (!m_file->IsOpened())
300 {
301 m_lasterror = wxSTREAM_WRITE_ERROR;
302 }
303 else
304 {
305 if (m_file->Error())
306 m_lasterror = wxSTREAM_WRITE_ERROR;
307 }
65045edd
RR
308}
309
310wxFFileOutputStream::wxFFileOutputStream(wxFFile& file)
311{
f6bcfd97 312 m_file = &file;
cab1a605 313 m_file_destroy = false;
65045edd
RR
314}
315
316wxFFileOutputStream::wxFFileOutputStream()
5fec5bb6 317 : wxOutputStream()
65045edd 318{
f6bcfd97 319 m_file = NULL;
5fec5bb6 320 m_file_destroy = false;
65045edd
RR
321}
322
323wxFFileOutputStream::wxFFileOutputStream(FILE *file)
324{
f6bcfd97 325 m_file = new wxFFile(file);
cab1a605 326 m_file_destroy = true;
65045edd
RR
327}
328
329wxFFileOutputStream::~wxFFileOutputStream()
330{
9a26db9e 331 if (m_file_destroy)
f6bcfd97
BP
332 {
333 Sync();
334 delete m_file;
335 }
65045edd
RR
336}
337
338size_t wxFFileOutputStream::OnSysWrite(const void *buffer, size_t size)
339{
f6bcfd97 340 size_t ret = m_file->Write(buffer, size);
b9d84e4c
DE
341 // It is not safe to call Error() if the file is not opened.
342 if (!m_file->IsOpened() || m_file->Error())
2b5f62a0 343 m_lasterror = wxSTREAM_WRITE_ERROR;
f6bcfd97 344 else
2b5f62a0 345 m_lasterror = wxSTREAM_NO_ERROR;
f6bcfd97 346 return ret;
65045edd
RR
347}
348
4004775e 349wxFileOffset wxFFileOutputStream::OnSysTell() const
65045edd 350{
f6bcfd97 351 return m_file->Tell();
65045edd
RR
352}
353
4004775e 354wxFileOffset wxFFileOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
65045edd 355{
70a7bd90 356 return m_file->Seek(pos, mode) ? m_file->Tell() : wxInvalidOffset;
65045edd
RR
357}
358
359void wxFFileOutputStream::Sync()
360{
f6bcfd97
BP
361 wxOutputStream::Sync();
362 m_file->Flush();
65045edd
RR
363}
364
588066b7 365wxFileOffset wxFFileOutputStream::GetLength() const
65045edd 366{
f6bcfd97 367 return m_file->Length();
65045edd
RR
368}
369
370// ----------------------------------------------------------------------------
371// wxFFileStream
372// ----------------------------------------------------------------------------
f6bcfd97 373
65045edd 374wxFFileStream::wxFFileStream(const wxString& fileName)
f6bcfd97 375 : wxFFileInputStream(fileName)
65045edd 376{
f6bcfd97 377 wxFFileOutputStream::m_file = wxFFileInputStream::m_file;
65045edd 378}
f6bcfd97 379
85990624
RN
380#endif //wxUSE_FFILE
381
382#endif // wxUSE_STREAMS
cc985fac 383