]> git.saurik.com Git - wxWidgets.git/blame - src/common/wfstream.cpp
Added constructor to wxGCDC from wxPrinterDC
[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
a6ac8d75
VZ
110bool wxFileInputStream::IsOk() const
111{
92bf6cc1 112 return wxInputStream::IsOk() && m_file->IsOpened();
f02f4d43
SC
113}
114
79c3e0e1
GL
115// ----------------------------------------------------------------------------
116// wxFileOutputStream
117// ----------------------------------------------------------------------------
118
119wxFileOutputStream::wxFileOutputStream(const wxString& fileName)
25c70b07 120{
9adf4299 121 m_file = new wxFile(fileName, wxFile::write);
cab1a605 122 m_file_destroy = true;
9a26db9e 123
942bef71 124 if (!m_file->IsOpened())
942bef71 125 m_lasterror = wxSTREAM_WRITE_ERROR;
25c70b07
GL
126}
127
84b46c35
GL
128wxFileOutputStream::wxFileOutputStream(wxFile& file)
129{
f6bcfd97 130 m_file = &file;
cab1a605 131 m_file_destroy = false;
84b46c35
GL
132}
133
25c70b07 134wxFileOutputStream::wxFileOutputStream()
9a26db9e 135 : wxOutputStream()
79c3e0e1 136{
cab1a605 137 m_file_destroy = false;
f6bcfd97 138 m_file = NULL;
3d4c6a21
GL
139}
140
0aca1ded
GL
141wxFileOutputStream::wxFileOutputStream(int fd)
142{
f6bcfd97 143 m_file = new wxFile(fd);
cab1a605 144 m_file_destroy = true;
0aca1ded
GL
145}
146
79c3e0e1 147wxFileOutputStream::~wxFileOutputStream()
3d4c6a21 148{
9a26db9e 149 if (m_file_destroy)
f6bcfd97
BP
150 {
151 Sync();
152 delete m_file;
153 }
79c3e0e1 154}
3d4c6a21 155
75ed1d15 156size_t wxFileOutputStream::OnSysWrite(const void *buffer, size_t size)
79c3e0e1 157{
f6bcfd97 158 size_t ret = m_file->Write(buffer, size);
9a26db9e
VZ
159
160 m_lasterror = m_file->Error() ? wxSTREAM_WRITE_ERROR : wxSTREAM_NO_ERROR;
161
f6bcfd97 162 return ret;
79c3e0e1 163}
3d4c6a21 164
4004775e 165wxFileOffset wxFileOutputStream::OnSysTell() const
79c3e0e1 166{
f6bcfd97 167 return m_file->Tell();
3d4c6a21
GL
168}
169
4004775e 170wxFileOffset wxFileOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
3d4c6a21 171{
f6bcfd97 172 return m_file->Seek(pos, mode);
3d4c6a21
GL
173}
174
79c3e0e1 175void wxFileOutputStream::Sync()
3d4c6a21 176{
f6bcfd97
BP
177 wxOutputStream::Sync();
178 m_file->Flush();
3d4c6a21 179}
84b46c35 180
588066b7 181wxFileOffset wxFileOutputStream::GetLength() const
84b46c35 182{
f6bcfd97 183 return m_file->Length();
84b46c35
GL
184}
185
a6ac8d75
VZ
186bool wxFileOutputStream::IsOk() const
187{
92bf6cc1 188 return wxOutputStream::IsOk() && m_file->IsOpened();
f02f4d43
SC
189}
190
84b46c35 191// ----------------------------------------------------------------------------
e1265174
MW
192// wxTempFileOutputStream
193// ----------------------------------------------------------------------------
194
195wxTempFileOutputStream::wxTempFileOutputStream(const wxString& fileName)
196{
197 m_file = new wxTempFile(fileName);
198
199 if (!m_file->IsOpened())
200 m_lasterror = wxSTREAM_WRITE_ERROR;
201}
202
203wxTempFileOutputStream::~wxTempFileOutputStream()
204{
205 if (m_file->IsOpened())
206 Discard();
207 delete m_file;
208}
209
210size_t wxTempFileOutputStream::OnSysWrite(const void *buffer, size_t size)
211{
212 if (IsOk() && m_file->Write(buffer, size))
213 return size;
214 m_lasterror = wxSTREAM_WRITE_ERROR;
215 return 0;
216}
217
218// ----------------------------------------------------------------------------
84b46c35
GL
219// wxFileStream
220// ----------------------------------------------------------------------------
f6bcfd97 221
84b46c35 222wxFileStream::wxFileStream(const wxString& fileName)
f6bcfd97 223 : wxFileInputStream(fileName)
84b46c35 224{
f6bcfd97 225 wxFileOutputStream::m_file = wxFileInputStream::m_file;
84b46c35 226}
ce4169a4 227
a6ac8d75
VZ
228bool wxFileStream::IsOk() const
229{
92bf6cc1 230 return wxFileOutputStream::IsOk() && wxFileInputStream::IsOk();
a6ac8d75
VZ
231}
232
92bf6cc1 233#endif // wxUSE_FILE
85990624
RN
234
235#if wxUSE_FFILE
236
65045edd
RR
237// ----------------------------------------------------------------------------
238// wxFFileInputStream
239// ----------------------------------------------------------------------------
240
5fec5bb6 241wxFFileInputStream::wxFFileInputStream(const wxString& fileName,
dc65c743 242 const wxString& mode)
5fec5bb6 243 : wxInputStream()
65045edd 244{
5fec5bb6 245 m_file = new wxFFile(fileName, mode);
cab1a605 246 m_file_destroy = true;
ecc20148
MW
247
248 if (!m_file->IsOpened())
249 m_lasterror = wxSTREAM_WRITE_ERROR;
65045edd
RR
250}
251
252wxFFileInputStream::wxFFileInputStream()
5fec5bb6 253 : wxInputStream()
65045edd 254{
f6bcfd97 255 m_file = NULL;
5fec5bb6 256 m_file_destroy = false;
65045edd
RR
257}
258
259wxFFileInputStream::wxFFileInputStream(wxFFile& file)
260{
f6bcfd97 261 m_file = &file;
cab1a605 262 m_file_destroy = false;
65045edd
RR
263}
264
265wxFFileInputStream::wxFFileInputStream(FILE *file)
266{
f6bcfd97 267 m_file = new wxFFile(file);
cab1a605 268 m_file_destroy = true;
65045edd
RR
269}
270
271wxFFileInputStream::~wxFFileInputStream()
272{
f6bcfd97
BP
273 if (m_file_destroy)
274 delete m_file;
65045edd
RR
275}
276
588066b7 277wxFileOffset wxFFileInputStream::GetLength() const
65045edd 278{
f6bcfd97 279 return m_file->Length();
65045edd
RR
280}
281
282size_t wxFFileInputStream::OnSysRead(void *buffer, size_t size)
283{
f8a586e0 284 ssize_t ret = m_file->Read(buffer, size);
65045edd 285
b9d84e4c
DE
286 // It is not safe to call Eof() if the file is not opened.
287 if (!m_file->IsOpened() || m_file->Eof())
2b5f62a0 288 m_lasterror = wxSTREAM_EOF;
f8a586e0 289 if (ret == wxInvalidOffset)
f6bcfd97 290 {
2b5f62a0 291 m_lasterror = wxSTREAM_READ_ERROR;
f6bcfd97
BP
292 ret = 0;
293 }
65045edd 294
f6bcfd97 295 return ret;
65045edd
RR
296}
297
4004775e 298wxFileOffset wxFFileInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
65045edd 299{
70a7bd90 300 return m_file->Seek(pos, mode) ? m_file->Tell() : wxInvalidOffset;
65045edd
RR
301}
302
4004775e 303wxFileOffset wxFFileInputStream::OnSysTell() const
65045edd 304{
f6bcfd97 305 return m_file->Tell();
65045edd
RR
306}
307
a6ac8d75
VZ
308bool wxFFileInputStream::IsOk() const
309{
310 return wxStreamBase::IsOk() && m_file->IsOpened();
f02f4d43
SC
311}
312
65045edd
RR
313// ----------------------------------------------------------------------------
314// wxFFileOutputStream
315// ----------------------------------------------------------------------------
316
5fec5bb6 317wxFFileOutputStream::wxFFileOutputStream(const wxString& fileName,
dc65c743 318 const wxString& mode)
65045edd 319{
5fec5bb6 320 m_file = new wxFFile(fileName, mode);
cab1a605 321 m_file_destroy = true;
9a26db9e 322
942bef71
RR
323 if (!m_file->IsOpened())
324 {
325 m_lasterror = wxSTREAM_WRITE_ERROR;
326 }
327 else
328 {
329 if (m_file->Error())
330 m_lasterror = wxSTREAM_WRITE_ERROR;
331 }
65045edd
RR
332}
333
334wxFFileOutputStream::wxFFileOutputStream(wxFFile& file)
335{
f6bcfd97 336 m_file = &file;
cab1a605 337 m_file_destroy = false;
65045edd
RR
338}
339
340wxFFileOutputStream::wxFFileOutputStream()
5fec5bb6 341 : wxOutputStream()
65045edd 342{
f6bcfd97 343 m_file = NULL;
5fec5bb6 344 m_file_destroy = false;
65045edd
RR
345}
346
347wxFFileOutputStream::wxFFileOutputStream(FILE *file)
348{
f6bcfd97 349 m_file = new wxFFile(file);
cab1a605 350 m_file_destroy = true;
65045edd
RR
351}
352
353wxFFileOutputStream::~wxFFileOutputStream()
354{
9a26db9e 355 if (m_file_destroy)
f6bcfd97
BP
356 {
357 Sync();
358 delete m_file;
359 }
65045edd
RR
360}
361
362size_t wxFFileOutputStream::OnSysWrite(const void *buffer, size_t size)
363{
f6bcfd97 364 size_t ret = m_file->Write(buffer, size);
b9d84e4c
DE
365 // It is not safe to call Error() if the file is not opened.
366 if (!m_file->IsOpened() || m_file->Error())
2b5f62a0 367 m_lasterror = wxSTREAM_WRITE_ERROR;
f6bcfd97 368 else
2b5f62a0 369 m_lasterror = wxSTREAM_NO_ERROR;
f6bcfd97 370 return ret;
65045edd
RR
371}
372
4004775e 373wxFileOffset wxFFileOutputStream::OnSysTell() const
65045edd 374{
f6bcfd97 375 return m_file->Tell();
65045edd
RR
376}
377
4004775e 378wxFileOffset wxFFileOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
65045edd 379{
70a7bd90 380 return m_file->Seek(pos, mode) ? m_file->Tell() : wxInvalidOffset;
65045edd
RR
381}
382
383void wxFFileOutputStream::Sync()
384{
f6bcfd97
BP
385 wxOutputStream::Sync();
386 m_file->Flush();
65045edd
RR
387}
388
588066b7 389wxFileOffset wxFFileOutputStream::GetLength() const
65045edd 390{
f6bcfd97 391 return m_file->Length();
65045edd
RR
392}
393
a6ac8d75
VZ
394bool wxFFileOutputStream::IsOk() const
395{
396 return wxStreamBase::IsOk() && m_file->IsOpened();
f02f4d43
SC
397}
398
65045edd
RR
399// ----------------------------------------------------------------------------
400// wxFFileStream
401// ----------------------------------------------------------------------------
f6bcfd97 402
65045edd 403wxFFileStream::wxFFileStream(const wxString& fileName)
f6bcfd97 404 : wxFFileInputStream(fileName)
65045edd 405{
f6bcfd97 406 wxFFileOutputStream::m_file = wxFFileInputStream::m_file;
65045edd 407}
f6bcfd97 408
92bf6cc1
VZ
409bool wxFFileStream::IsOk() const
410{
411 return wxFFileOutputStream::IsOk() && wxFFileInputStream::IsOk();
412}
413
85990624
RN
414#endif //wxUSE_FFILE
415
416#endif // wxUSE_STREAMS