]> git.saurik.com Git - wxWidgets.git/blame - src/common/wfstream.cpp
Applied patch for ArtProvider.
[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
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
593d4b0d 13#pragma implementation "wfstream.h"
3d4c6a21
GL
14#endif
15
79c3e0e1
GL
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
3d4c6a21 18
79c3e0e1 19#ifdef __BORLANDC__
ce4169a4 20 #pragma hdrstop
79c3e0e1
GL
21#endif
22
85990624 23#if wxUSE_STREAMS
ce4169a4
RR
24
25#include <stdio.h>
d1af991f
RR
26#include "wx/stream.h"
27#include "wx/wfstream.h"
ce4169a4 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;
3d4c6a21
GL
40}
41
25c70b07
GL
42wxFileInputStream::wxFileInputStream()
43 : wxInputStream()
44{
cab1a605 45 m_file_destroy = false;
f6bcfd97 46 m_file = NULL;
25c70b07
GL
47}
48
0aca1ded
GL
49wxFileInputStream::wxFileInputStream(wxFile& file)
50{
f6bcfd97 51 m_file = &file;
cab1a605 52 m_file_destroy = false;
0aca1ded
GL
53}
54
55wxFileInputStream::wxFileInputStream(int fd)
56{
f6bcfd97 57 m_file = new wxFile(fd);
cab1a605 58 m_file_destroy = true;
0aca1ded
GL
59}
60
79c3e0e1 61wxFileInputStream::~wxFileInputStream()
3d4c6a21 62{
f6bcfd97
BP
63 if (m_file_destroy)
64 delete m_file;
3d4c6a21
GL
65}
66
588066b7 67wxFileOffset wxFileInputStream::GetLength() const
84b46c35 68{
f6bcfd97 69 return m_file->Length();
84b46c35
GL
70}
71
75ed1d15 72size_t wxFileInputStream::OnSysRead(void *buffer, size_t size)
6d44bf31 73{
f8a586e0 74 ssize_t ret = m_file->Read(buffer, size);
fae05df5 75
e69a1ea8 76 // NB: we can't use a switch here because HP-UX CC doesn't allow
30984dea 77 // switching over long long (which size_t is in 64bit mode)
9a26db9e 78
e69a1ea8
VZ
79 if ( !ret )
80 {
81 // nothing read, so nothing more to read
82 m_lasterror = wxSTREAM_EOF;
83 }
f8a586e0 84 else if ( ret == wxInvalidOffset )
e69a1ea8
VZ
85 {
86 m_lasterror = wxSTREAM_READ_ERROR;
87 ret = 0;
88 }
89 else
90 {
91 // normal case
92 m_lasterror = wxSTREAM_NO_ERROR;
f6bcfd97 93 }
fae05df5 94
f6bcfd97 95 return ret;
6d44bf31
GL
96}
97
4004775e 98wxFileOffset wxFileInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
3d4c6a21 99{
9a26db9e 100 return m_file->Seek(pos, mode);
79c3e0e1
GL
101}
102
4004775e 103wxFileOffset wxFileInputStream::OnSysTell() const
79c3e0e1 104{
f6bcfd97 105 return m_file->Tell();
79c3e0e1
GL
106}
107
108// ----------------------------------------------------------------------------
109// wxFileOutputStream
110// ----------------------------------------------------------------------------
111
112wxFileOutputStream::wxFileOutputStream(const wxString& fileName)
25c70b07 113{
9adf4299 114 m_file = new wxFile(fileName, wxFile::write);
cab1a605 115 m_file_destroy = true;
9a26db9e 116
942bef71
RR
117 if (!m_file->IsOpened())
118 {
119 m_lasterror = wxSTREAM_WRITE_ERROR;
120 }
121 else
122 {
123 if (m_file->Error())
124 m_lasterror = wxSTREAM_WRITE_ERROR;
125 }
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
186// ----------------------------------------------------------------------------
187// wxFileStream
188// ----------------------------------------------------------------------------
f6bcfd97 189
84b46c35 190wxFileStream::wxFileStream(const wxString& fileName)
f6bcfd97 191 : wxFileInputStream(fileName)
84b46c35 192{
f6bcfd97 193 wxFileOutputStream::m_file = wxFileInputStream::m_file;
84b46c35 194}
ce4169a4 195
85990624
RN
196#endif //wxUSE_FILE
197
198#if wxUSE_FFILE
199
65045edd
RR
200// ----------------------------------------------------------------------------
201// wxFFileInputStream
202// ----------------------------------------------------------------------------
203
5fec5bb6
VZ
204wxFFileInputStream::wxFFileInputStream(const wxString& fileName,
205 const wxChar *mode)
206 : wxInputStream()
65045edd 207{
5fec5bb6 208 m_file = new wxFFile(fileName, mode);
cab1a605 209 m_file_destroy = true;
65045edd
RR
210}
211
212wxFFileInputStream::wxFFileInputStream()
5fec5bb6 213 : wxInputStream()
65045edd 214{
f6bcfd97 215 m_file = NULL;
5fec5bb6 216 m_file_destroy = false;
65045edd
RR
217}
218
219wxFFileInputStream::wxFFileInputStream(wxFFile& file)
220{
f6bcfd97 221 m_file = &file;
cab1a605 222 m_file_destroy = false;
65045edd
RR
223}
224
225wxFFileInputStream::wxFFileInputStream(FILE *file)
226{
f6bcfd97 227 m_file = new wxFFile(file);
cab1a605 228 m_file_destroy = true;
65045edd
RR
229}
230
231wxFFileInputStream::~wxFFileInputStream()
232{
f6bcfd97
BP
233 if (m_file_destroy)
234 delete m_file;
65045edd
RR
235}
236
588066b7 237wxFileOffset wxFFileInputStream::GetLength() const
65045edd 238{
f6bcfd97 239 return m_file->Length();
65045edd
RR
240}
241
242size_t wxFFileInputStream::OnSysRead(void *buffer, size_t size)
243{
f8a586e0 244 ssize_t ret = m_file->Read(buffer, size);
65045edd 245
b9d84e4c
DE
246 // It is not safe to call Eof() if the file is not opened.
247 if (!m_file->IsOpened() || m_file->Eof())
2b5f62a0 248 m_lasterror = wxSTREAM_EOF;
f8a586e0 249 if (ret == wxInvalidOffset)
f6bcfd97 250 {
2b5f62a0 251 m_lasterror = wxSTREAM_READ_ERROR;
f6bcfd97
BP
252 ret = 0;
253 }
65045edd 254
f6bcfd97 255 return ret;
65045edd
RR
256}
257
4004775e 258wxFileOffset wxFFileInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
65045edd 259{
70a7bd90 260 return m_file->Seek(pos, mode) ? m_file->Tell() : wxInvalidOffset;
65045edd
RR
261}
262
4004775e 263wxFileOffset wxFFileInputStream::OnSysTell() const
65045edd 264{
f6bcfd97 265 return m_file->Tell();
65045edd
RR
266}
267
268// ----------------------------------------------------------------------------
269// wxFFileOutputStream
270// ----------------------------------------------------------------------------
271
5fec5bb6
VZ
272wxFFileOutputStream::wxFFileOutputStream(const wxString& fileName,
273 const wxChar *mode)
65045edd 274{
5fec5bb6 275 m_file = new wxFFile(fileName, mode);
cab1a605 276 m_file_destroy = true;
9a26db9e 277
942bef71
RR
278 if (!m_file->IsOpened())
279 {
280 m_lasterror = wxSTREAM_WRITE_ERROR;
281 }
282 else
283 {
284 if (m_file->Error())
285 m_lasterror = wxSTREAM_WRITE_ERROR;
286 }
65045edd
RR
287}
288
289wxFFileOutputStream::wxFFileOutputStream(wxFFile& file)
290{
f6bcfd97 291 m_file = &file;
cab1a605 292 m_file_destroy = false;
65045edd
RR
293}
294
295wxFFileOutputStream::wxFFileOutputStream()
5fec5bb6 296 : wxOutputStream()
65045edd 297{
f6bcfd97 298 m_file = NULL;
5fec5bb6 299 m_file_destroy = false;
65045edd
RR
300}
301
302wxFFileOutputStream::wxFFileOutputStream(FILE *file)
303{
f6bcfd97 304 m_file = new wxFFile(file);
cab1a605 305 m_file_destroy = true;
65045edd
RR
306}
307
308wxFFileOutputStream::~wxFFileOutputStream()
309{
9a26db9e 310 if (m_file_destroy)
f6bcfd97
BP
311 {
312 Sync();
313 delete m_file;
314 }
65045edd
RR
315}
316
317size_t wxFFileOutputStream::OnSysWrite(const void *buffer, size_t size)
318{
f6bcfd97 319 size_t ret = m_file->Write(buffer, size);
b9d84e4c
DE
320 // It is not safe to call Error() if the file is not opened.
321 if (!m_file->IsOpened() || m_file->Error())
2b5f62a0 322 m_lasterror = wxSTREAM_WRITE_ERROR;
f6bcfd97 323 else
2b5f62a0 324 m_lasterror = wxSTREAM_NO_ERROR;
f6bcfd97 325 return ret;
65045edd
RR
326}
327
4004775e 328wxFileOffset wxFFileOutputStream::OnSysTell() const
65045edd 329{
f6bcfd97 330 return m_file->Tell();
65045edd
RR
331}
332
4004775e 333wxFileOffset wxFFileOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
65045edd 334{
70a7bd90 335 return m_file->Seek(pos, mode) ? m_file->Tell() : wxInvalidOffset;
65045edd
RR
336}
337
338void wxFFileOutputStream::Sync()
339{
f6bcfd97
BP
340 wxOutputStream::Sync();
341 m_file->Flush();
65045edd
RR
342}
343
588066b7 344wxFileOffset wxFFileOutputStream::GetLength() const
65045edd 345{
f6bcfd97 346 return m_file->Length();
65045edd
RR
347}
348
349// ----------------------------------------------------------------------------
350// wxFFileStream
351// ----------------------------------------------------------------------------
f6bcfd97 352
65045edd 353wxFFileStream::wxFFileStream(const wxString& fileName)
f6bcfd97 354 : wxFFileInputStream(fileName)
65045edd 355{
f6bcfd97 356 wxFFileOutputStream::m_file = wxFFileInputStream::m_file;
65045edd 357}
f6bcfd97 358
85990624
RN
359#endif //wxUSE_FFILE
360
361#endif // wxUSE_STREAMS
cc985fac 362