]> git.saurik.com Git - wxWidgets.git/blame - src/common/wfstream.cpp
Added a file
[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
3d4c6a21
GL
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
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
ce4169a4
RR
23#if wxUSE_STREAMS && wxUSE_FILE
24
25#include <stdio.h>
d1af991f
RR
26#include "wx/stream.h"
27#include "wx/wfstream.h"
ce4169a4 28
79c3e0e1
GL
29// ----------------------------------------------------------------------------
30// wxFileInputStream
31// ----------------------------------------------------------------------------
32
33wxFileInputStream::wxFileInputStream(const wxString& fileName)
25c70b07 34 : wxInputStream()
3d4c6a21 35{
f6bcfd97
BP
36 m_file = new wxFile(fileName, wxFile::read);
37 m_file_destroy = TRUE;
3d4c6a21
GL
38}
39
25c70b07
GL
40wxFileInputStream::wxFileInputStream()
41 : wxInputStream()
42{
f6bcfd97
BP
43 m_file_destroy = FALSE;
44 m_file = NULL;
25c70b07
GL
45}
46
0aca1ded
GL
47wxFileInputStream::wxFileInputStream(wxFile& file)
48{
f6bcfd97
BP
49 m_file = &file;
50 m_file_destroy = FALSE;
0aca1ded
GL
51}
52
53wxFileInputStream::wxFileInputStream(int fd)
54{
f6bcfd97
BP
55 m_file = new wxFile(fd);
56 m_file_destroy = TRUE;
0aca1ded
GL
57}
58
79c3e0e1 59wxFileInputStream::~wxFileInputStream()
3d4c6a21 60{
f6bcfd97
BP
61 if (m_file_destroy)
62 delete m_file;
3d4c6a21
GL
63}
64
cd25b18c 65size_t wxFileInputStream::GetSize() const
84b46c35 66{
f6bcfd97 67 return m_file->Length();
84b46c35
GL
68}
69
75ed1d15 70size_t wxFileInputStream::OnSysRead(void *buffer, size_t size)
6d44bf31 71{
f6bcfd97 72 off_t ret;
fae05df5 73
f6bcfd97 74 ret = m_file->Read(buffer, size);
fae05df5 75
f6bcfd97
BP
76 m_lasterror = wxStream_NOERROR;
77 if (m_file->Eof())
78 m_lasterror = wxStream_EOF;
79 if (ret == wxInvalidOffset)
80 {
81 m_lasterror = wxStream_READ_ERR;
82 ret = 0;
83 }
fae05df5 84
f6bcfd97 85 return ret;
6d44bf31
GL
86}
87
75ed1d15 88off_t wxFileInputStream::OnSysSeek(off_t pos, wxSeekMode mode)
3d4c6a21 89{
f6bcfd97 90 return m_file->Seek(pos, mode);
79c3e0e1
GL
91}
92
75ed1d15 93off_t wxFileInputStream::OnSysTell() const
79c3e0e1 94{
f6bcfd97 95 return m_file->Tell();
79c3e0e1
GL
96}
97
98// ----------------------------------------------------------------------------
99// wxFileOutputStream
100// ----------------------------------------------------------------------------
101
102wxFileOutputStream::wxFileOutputStream(const wxString& fileName)
25c70b07 103{
f6bcfd97
BP
104 m_file = new wxFile(fileName, wxFile::write);
105 m_file_destroy = TRUE;
942bef71
RR
106
107 if (!m_file->IsOpened())
108 {
109 m_lasterror = wxSTREAM_WRITE_ERROR;
110 }
111 else
112 {
113 if (m_file->Error())
114 m_lasterror = wxSTREAM_WRITE_ERROR;
115 }
25c70b07
GL
116}
117
84b46c35
GL
118wxFileOutputStream::wxFileOutputStream(wxFile& file)
119{
f6bcfd97
BP
120 m_file = &file;
121 m_file_destroy = FALSE;
84b46c35
GL
122}
123
25c70b07
GL
124wxFileOutputStream::wxFileOutputStream()
125 : wxOutputStream()
79c3e0e1 126{
f6bcfd97
BP
127 m_file_destroy = FALSE;
128 m_file = NULL;
3d4c6a21
GL
129}
130
0aca1ded
GL
131wxFileOutputStream::wxFileOutputStream(int fd)
132{
f6bcfd97
BP
133 m_file = new wxFile(fd);
134 m_file_destroy = TRUE;
0aca1ded
GL
135}
136
79c3e0e1 137wxFileOutputStream::~wxFileOutputStream()
3d4c6a21 138{
f6bcfd97
BP
139 if (m_file_destroy)
140 {
141 Sync();
142 delete m_file;
143 }
79c3e0e1 144}
3d4c6a21 145
75ed1d15 146size_t wxFileOutputStream::OnSysWrite(const void *buffer, size_t size)
79c3e0e1 147{
f6bcfd97
BP
148 size_t ret = m_file->Write(buffer, size);
149 if (m_file->Error())
150 m_lasterror = wxStream_WRITE_ERR;
151 else
152 m_lasterror = wxStream_NOERROR;
153 return ret;
79c3e0e1 154}
3d4c6a21 155
75ed1d15 156off_t wxFileOutputStream::OnSysTell() const
79c3e0e1 157{
f6bcfd97 158 return m_file->Tell();
3d4c6a21
GL
159}
160
75ed1d15 161off_t wxFileOutputStream::OnSysSeek(off_t pos, wxSeekMode mode)
3d4c6a21 162{
f6bcfd97 163 return m_file->Seek(pos, mode);
3d4c6a21
GL
164}
165
79c3e0e1 166void wxFileOutputStream::Sync()
3d4c6a21 167{
f6bcfd97
BP
168 wxOutputStream::Sync();
169 m_file->Flush();
3d4c6a21 170}
84b46c35 171
cd25b18c 172size_t wxFileOutputStream::GetSize() const
84b46c35 173{
f6bcfd97 174 return m_file->Length();
84b46c35
GL
175}
176
177// ----------------------------------------------------------------------------
178// wxFileStream
179// ----------------------------------------------------------------------------
f6bcfd97 180
84b46c35 181wxFileStream::wxFileStream(const wxString& fileName)
f6bcfd97 182 : wxFileInputStream(fileName)
84b46c35 183{
f6bcfd97 184 wxFileOutputStream::m_file = wxFileInputStream::m_file;
84b46c35 185}
ce4169a4 186
65045edd
RR
187// ----------------------------------------------------------------------------
188// wxFFileInputStream
189// ----------------------------------------------------------------------------
190
191wxFFileInputStream::wxFFileInputStream(const wxString& fileName)
192 : wxInputStream()
193{
f6bcfd97
BP
194 m_file = new wxFFile(fileName, "rb");
195 m_file_destroy = TRUE;
65045edd
RR
196}
197
198wxFFileInputStream::wxFFileInputStream()
199 : wxInputStream()
200{
f6bcfd97
BP
201 m_file_destroy = FALSE;
202 m_file = NULL;
65045edd
RR
203}
204
205wxFFileInputStream::wxFFileInputStream(wxFFile& file)
206{
f6bcfd97
BP
207 m_file = &file;
208 m_file_destroy = FALSE;
65045edd
RR
209}
210
211wxFFileInputStream::wxFFileInputStream(FILE *file)
212{
f6bcfd97
BP
213 m_file = new wxFFile(file);
214 m_file_destroy = TRUE;
65045edd
RR
215}
216
217wxFFileInputStream::~wxFFileInputStream()
218{
f6bcfd97
BP
219 if (m_file_destroy)
220 delete m_file;
65045edd
RR
221}
222
223size_t wxFFileInputStream::GetSize() const
224{
f6bcfd97 225 return m_file->Length();
65045edd
RR
226}
227
228size_t wxFFileInputStream::OnSysRead(void *buffer, size_t size)
229{
f6bcfd97 230 off_t ret;
65045edd 231
f6bcfd97 232 ret = m_file->Read(buffer, size);
65045edd 233
f6bcfd97
BP
234 if (m_file->Eof())
235 m_lasterror = wxStream_EOF;
236 if (ret == wxInvalidOffset)
237 {
238 m_lasterror = wxStream_READ_ERR;
239 ret = 0;
240 }
65045edd 241
f6bcfd97 242 return ret;
65045edd
RR
243}
244
245off_t wxFFileInputStream::OnSysSeek(off_t pos, wxSeekMode mode)
246{
f6bcfd97 247 return m_file->Seek(pos, mode);
65045edd
RR
248}
249
250off_t wxFFileInputStream::OnSysTell() const
251{
f6bcfd97 252 return m_file->Tell();
65045edd
RR
253}
254
255// ----------------------------------------------------------------------------
256// wxFFileOutputStream
257// ----------------------------------------------------------------------------
258
259wxFFileOutputStream::wxFFileOutputStream(const wxString& fileName)
260{
f6bcfd97
BP
261 m_file = new wxFFile(fileName, "w+b");
262 m_file_destroy = TRUE;
942bef71
RR
263
264 if (!m_file->IsOpened())
265 {
266 m_lasterror = wxSTREAM_WRITE_ERROR;
267 }
268 else
269 {
270 if (m_file->Error())
271 m_lasterror = wxSTREAM_WRITE_ERROR;
272 }
65045edd
RR
273}
274
275wxFFileOutputStream::wxFFileOutputStream(wxFFile& file)
276{
f6bcfd97
BP
277 m_file = &file;
278 m_file_destroy = FALSE;
65045edd
RR
279}
280
281wxFFileOutputStream::wxFFileOutputStream()
282 : wxOutputStream()
283{
f6bcfd97
BP
284 m_file_destroy = FALSE;
285 m_file = NULL;
65045edd
RR
286}
287
288wxFFileOutputStream::wxFFileOutputStream(FILE *file)
289{
f6bcfd97
BP
290 m_file = new wxFFile(file);
291 m_file_destroy = TRUE;
65045edd
RR
292}
293
294wxFFileOutputStream::~wxFFileOutputStream()
295{
f6bcfd97
BP
296 if (m_file_destroy)
297 {
298 Sync();
299 delete m_file;
300 }
65045edd
RR
301}
302
303size_t wxFFileOutputStream::OnSysWrite(const void *buffer, size_t size)
304{
f6bcfd97
BP
305 size_t ret = m_file->Write(buffer, size);
306 if (m_file->Error())
307 m_lasterror = wxStream_WRITE_ERR;
308 else
309 m_lasterror = wxStream_NOERROR;
310 return ret;
65045edd
RR
311}
312
313off_t wxFFileOutputStream::OnSysTell() const
314{
f6bcfd97 315 return m_file->Tell();
65045edd
RR
316}
317
318off_t wxFFileOutputStream::OnSysSeek(off_t pos, wxSeekMode mode)
319{
f6bcfd97 320 return m_file->Seek(pos, mode);
65045edd
RR
321}
322
323void wxFFileOutputStream::Sync()
324{
f6bcfd97
BP
325 wxOutputStream::Sync();
326 m_file->Flush();
65045edd
RR
327}
328
329size_t wxFFileOutputStream::GetSize() const
330{
f6bcfd97 331 return m_file->Length();
65045edd
RR
332}
333
334// ----------------------------------------------------------------------------
335// wxFFileStream
336// ----------------------------------------------------------------------------
f6bcfd97 337
65045edd 338wxFFileStream::wxFFileStream(const wxString& fileName)
f6bcfd97 339 : wxFFileInputStream(fileName)
65045edd 340{
f6bcfd97 341 wxFFileOutputStream::m_file = wxFFileInputStream::m_file;
65045edd 342}
f6bcfd97 343
ce4169a4
RR
344#endif
345 // wxUSE_STREAMS && wxUSE_FILE
cc985fac 346