]> git.saurik.com Git - wxWidgets.git/blame - src/common/wfstream.cpp
added wxString ctor from std::string (inside #ifdef wxUSE_STD_STRING); removed pragma...
[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// ----------------------------------------------------------------------------
e1265174
MW
187// wxTempFileOutputStream
188// ----------------------------------------------------------------------------
189
190wxTempFileOutputStream::wxTempFileOutputStream(const wxString& fileName)
191{
192 m_file = new wxTempFile(fileName);
193
194 if (!m_file->IsOpened())
195 m_lasterror = wxSTREAM_WRITE_ERROR;
196}
197
198wxTempFileOutputStream::~wxTempFileOutputStream()
199{
200 if (m_file->IsOpened())
201 Discard();
202 delete m_file;
203}
204
205size_t wxTempFileOutputStream::OnSysWrite(const void *buffer, size_t size)
206{
207 if (IsOk() && m_file->Write(buffer, size))
208 return size;
209 m_lasterror = wxSTREAM_WRITE_ERROR;
210 return 0;
211}
212
213// ----------------------------------------------------------------------------
84b46c35
GL
214// wxFileStream
215// ----------------------------------------------------------------------------
f6bcfd97 216
84b46c35 217wxFileStream::wxFileStream(const wxString& fileName)
f6bcfd97 218 : wxFileInputStream(fileName)
84b46c35 219{
f6bcfd97 220 wxFileOutputStream::m_file = wxFileInputStream::m_file;
84b46c35 221}
ce4169a4 222
85990624
RN
223#endif //wxUSE_FILE
224
225#if wxUSE_FFILE
226
65045edd
RR
227// ----------------------------------------------------------------------------
228// wxFFileInputStream
229// ----------------------------------------------------------------------------
230
5fec5bb6
VZ
231wxFFileInputStream::wxFFileInputStream(const wxString& fileName,
232 const wxChar *mode)
233 : wxInputStream()
65045edd 234{
5fec5bb6 235 m_file = new wxFFile(fileName, mode);
cab1a605 236 m_file_destroy = true;
65045edd
RR
237}
238
239wxFFileInputStream::wxFFileInputStream()
5fec5bb6 240 : wxInputStream()
65045edd 241{
f6bcfd97 242 m_file = NULL;
5fec5bb6 243 m_file_destroy = false;
65045edd
RR
244}
245
246wxFFileInputStream::wxFFileInputStream(wxFFile& file)
247{
f6bcfd97 248 m_file = &file;
cab1a605 249 m_file_destroy = false;
65045edd
RR
250}
251
252wxFFileInputStream::wxFFileInputStream(FILE *file)
253{
f6bcfd97 254 m_file = new wxFFile(file);
cab1a605 255 m_file_destroy = true;
65045edd
RR
256}
257
258wxFFileInputStream::~wxFFileInputStream()
259{
f6bcfd97
BP
260 if (m_file_destroy)
261 delete m_file;
65045edd
RR
262}
263
588066b7 264wxFileOffset wxFFileInputStream::GetLength() const
65045edd 265{
f6bcfd97 266 return m_file->Length();
65045edd
RR
267}
268
269size_t wxFFileInputStream::OnSysRead(void *buffer, size_t size)
270{
f8a586e0 271 ssize_t ret = m_file->Read(buffer, size);
65045edd 272
b9d84e4c
DE
273 // It is not safe to call Eof() if the file is not opened.
274 if (!m_file->IsOpened() || m_file->Eof())
2b5f62a0 275 m_lasterror = wxSTREAM_EOF;
f8a586e0 276 if (ret == wxInvalidOffset)
f6bcfd97 277 {
2b5f62a0 278 m_lasterror = wxSTREAM_READ_ERROR;
f6bcfd97
BP
279 ret = 0;
280 }
65045edd 281
f6bcfd97 282 return ret;
65045edd
RR
283}
284
4004775e 285wxFileOffset wxFFileInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
65045edd 286{
70a7bd90 287 return m_file->Seek(pos, mode) ? m_file->Tell() : wxInvalidOffset;
65045edd
RR
288}
289
4004775e 290wxFileOffset wxFFileInputStream::OnSysTell() const
65045edd 291{
f6bcfd97 292 return m_file->Tell();
65045edd
RR
293}
294
295// ----------------------------------------------------------------------------
296// wxFFileOutputStream
297// ----------------------------------------------------------------------------
298
5fec5bb6
VZ
299wxFFileOutputStream::wxFFileOutputStream(const wxString& fileName,
300 const wxChar *mode)
65045edd 301{
5fec5bb6 302 m_file = new wxFFile(fileName, mode);
cab1a605 303 m_file_destroy = true;
9a26db9e 304
942bef71
RR
305 if (!m_file->IsOpened())
306 {
307 m_lasterror = wxSTREAM_WRITE_ERROR;
308 }
309 else
310 {
311 if (m_file->Error())
312 m_lasterror = wxSTREAM_WRITE_ERROR;
313 }
65045edd
RR
314}
315
316wxFFileOutputStream::wxFFileOutputStream(wxFFile& file)
317{
f6bcfd97 318 m_file = &file;
cab1a605 319 m_file_destroy = false;
65045edd
RR
320}
321
322wxFFileOutputStream::wxFFileOutputStream()
5fec5bb6 323 : wxOutputStream()
65045edd 324{
f6bcfd97 325 m_file = NULL;
5fec5bb6 326 m_file_destroy = false;
65045edd
RR
327}
328
329wxFFileOutputStream::wxFFileOutputStream(FILE *file)
330{
f6bcfd97 331 m_file = new wxFFile(file);
cab1a605 332 m_file_destroy = true;
65045edd
RR
333}
334
335wxFFileOutputStream::~wxFFileOutputStream()
336{
9a26db9e 337 if (m_file_destroy)
f6bcfd97
BP
338 {
339 Sync();
340 delete m_file;
341 }
65045edd
RR
342}
343
344size_t wxFFileOutputStream::OnSysWrite(const void *buffer, size_t size)
345{
f6bcfd97 346 size_t ret = m_file->Write(buffer, size);
b9d84e4c
DE
347 // It is not safe to call Error() if the file is not opened.
348 if (!m_file->IsOpened() || m_file->Error())
2b5f62a0 349 m_lasterror = wxSTREAM_WRITE_ERROR;
f6bcfd97 350 else
2b5f62a0 351 m_lasterror = wxSTREAM_NO_ERROR;
f6bcfd97 352 return ret;
65045edd
RR
353}
354
4004775e 355wxFileOffset wxFFileOutputStream::OnSysTell() const
65045edd 356{
f6bcfd97 357 return m_file->Tell();
65045edd
RR
358}
359
4004775e 360wxFileOffset wxFFileOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
65045edd 361{
70a7bd90 362 return m_file->Seek(pos, mode) ? m_file->Tell() : wxInvalidOffset;
65045edd
RR
363}
364
365void wxFFileOutputStream::Sync()
366{
f6bcfd97
BP
367 wxOutputStream::Sync();
368 m_file->Flush();
65045edd
RR
369}
370
588066b7 371wxFileOffset wxFFileOutputStream::GetLength() const
65045edd 372{
f6bcfd97 373 return m_file->Length();
65045edd
RR
374}
375
376// ----------------------------------------------------------------------------
377// wxFFileStream
378// ----------------------------------------------------------------------------
f6bcfd97 379
65045edd 380wxFFileStream::wxFFileStream(const wxString& fileName)
f6bcfd97 381 : wxFFileInputStream(fileName)
65045edd 382{
f6bcfd97 383 wxFFileOutputStream::m_file = wxFFileInputStream::m_file;
65045edd 384}
f6bcfd97 385
85990624
RN
386#endif //wxUSE_FFILE
387
388#endif // wxUSE_STREAMS
cc985fac 389