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