]> git.saurik.com Git - wxWidgets.git/blame - src/common/wfstream.cpp
corrected misleading comment about DllGetVersion()
[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;
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;
65045edd
RR
232}
233
234wxFFileInputStream::wxFFileInputStream()
5fec5bb6 235 : wxInputStream()
65045edd 236{
f6bcfd97 237 m_file = NULL;
5fec5bb6 238 m_file_destroy = false;
65045edd
RR
239}
240
241wxFFileInputStream::wxFFileInputStream(wxFFile& file)
242{
f6bcfd97 243 m_file = &file;
cab1a605 244 m_file_destroy = false;
65045edd
RR
245}
246
247wxFFileInputStream::wxFFileInputStream(FILE *file)
248{
f6bcfd97 249 m_file = new wxFFile(file);
cab1a605 250 m_file_destroy = true;
65045edd
RR
251}
252
253wxFFileInputStream::~wxFFileInputStream()
254{
f6bcfd97
BP
255 if (m_file_destroy)
256 delete m_file;
65045edd
RR
257}
258
588066b7 259wxFileOffset wxFFileInputStream::GetLength() const
65045edd 260{
f6bcfd97 261 return m_file->Length();
65045edd
RR
262}
263
264size_t wxFFileInputStream::OnSysRead(void *buffer, size_t size)
265{
f8a586e0 266 ssize_t ret = m_file->Read(buffer, size);
65045edd 267
b9d84e4c
DE
268 // It is not safe to call Eof() if the file is not opened.
269 if (!m_file->IsOpened() || m_file->Eof())
2b5f62a0 270 m_lasterror = wxSTREAM_EOF;
f8a586e0 271 if (ret == wxInvalidOffset)
f6bcfd97 272 {
2b5f62a0 273 m_lasterror = wxSTREAM_READ_ERROR;
f6bcfd97
BP
274 ret = 0;
275 }
65045edd 276
f6bcfd97 277 return ret;
65045edd
RR
278}
279
4004775e 280wxFileOffset wxFFileInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
65045edd 281{
70a7bd90 282 return m_file->Seek(pos, mode) ? m_file->Tell() : wxInvalidOffset;
65045edd
RR
283}
284
4004775e 285wxFileOffset wxFFileInputStream::OnSysTell() const
65045edd 286{
f6bcfd97 287 return m_file->Tell();
65045edd
RR
288}
289
290// ----------------------------------------------------------------------------
291// wxFFileOutputStream
292// ----------------------------------------------------------------------------
293
5fec5bb6
VZ
294wxFFileOutputStream::wxFFileOutputStream(const wxString& fileName,
295 const wxChar *mode)
65045edd 296{
5fec5bb6 297 m_file = new wxFFile(fileName, mode);
cab1a605 298 m_file_destroy = true;
9a26db9e 299
942bef71
RR
300 if (!m_file->IsOpened())
301 {
302 m_lasterror = wxSTREAM_WRITE_ERROR;
303 }
304 else
305 {
306 if (m_file->Error())
307 m_lasterror = wxSTREAM_WRITE_ERROR;
308 }
65045edd
RR
309}
310
311wxFFileOutputStream::wxFFileOutputStream(wxFFile& file)
312{
f6bcfd97 313 m_file = &file;
cab1a605 314 m_file_destroy = false;
65045edd
RR
315}
316
317wxFFileOutputStream::wxFFileOutputStream()
5fec5bb6 318 : wxOutputStream()
65045edd 319{
f6bcfd97 320 m_file = NULL;
5fec5bb6 321 m_file_destroy = false;
65045edd
RR
322}
323
324wxFFileOutputStream::wxFFileOutputStream(FILE *file)
325{
f6bcfd97 326 m_file = new wxFFile(file);
cab1a605 327 m_file_destroy = true;
65045edd
RR
328}
329
330wxFFileOutputStream::~wxFFileOutputStream()
331{
9a26db9e 332 if (m_file_destroy)
f6bcfd97
BP
333 {
334 Sync();
335 delete m_file;
336 }
65045edd
RR
337}
338
339size_t wxFFileOutputStream::OnSysWrite(const void *buffer, size_t size)
340{
f6bcfd97 341 size_t ret = m_file->Write(buffer, size);
b9d84e4c
DE
342 // It is not safe to call Error() if the file is not opened.
343 if (!m_file->IsOpened() || m_file->Error())
2b5f62a0 344 m_lasterror = wxSTREAM_WRITE_ERROR;
f6bcfd97 345 else
2b5f62a0 346 m_lasterror = wxSTREAM_NO_ERROR;
f6bcfd97 347 return ret;
65045edd
RR
348}
349
4004775e 350wxFileOffset wxFFileOutputStream::OnSysTell() const
65045edd 351{
f6bcfd97 352 return m_file->Tell();
65045edd
RR
353}
354
4004775e 355wxFileOffset wxFFileOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
65045edd 356{
70a7bd90 357 return m_file->Seek(pos, mode) ? m_file->Tell() : wxInvalidOffset;
65045edd
RR
358}
359
360void wxFFileOutputStream::Sync()
361{
f6bcfd97
BP
362 wxOutputStream::Sync();
363 m_file->Flush();
65045edd
RR
364}
365
588066b7 366wxFileOffset wxFFileOutputStream::GetLength() const
65045edd 367{
f6bcfd97 368 return m_file->Length();
65045edd
RR
369}
370
371// ----------------------------------------------------------------------------
372// wxFFileStream
373// ----------------------------------------------------------------------------
f6bcfd97 374
65045edd 375wxFFileStream::wxFFileStream(const wxString& fileName)
f6bcfd97 376 : wxFFileInputStream(fileName)
65045edd 377{
f6bcfd97 378 wxFFileOutputStream::m_file = wxFFileInputStream::m_file;
65045edd 379}
f6bcfd97 380
85990624
RN
381#endif //wxUSE_FFILE
382
383#endif // wxUSE_STREAMS
cc985fac 384