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