]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/wfstream.cpp
Removed unnecessary casts
[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 licence
10/////////////////////////////////////////////////////////////////////////////
11
12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
24
25#include <stdio.h>
26#include "wx/stream.h"
27#include "wx/wfstream.h"
28
29#if wxUSE_FILE
30
31// ----------------------------------------------------------------------------
32// wxFileInputStream
33// ----------------------------------------------------------------------------
34
35wxFileInputStream::wxFileInputStream(const wxString& fileName)
36 : wxInputStream()
37{
38 m_file = new wxFile(fileName, wxFile::read);
39 m_file_destroy = true;
40}
41
42wxFileInputStream::wxFileInputStream()
43 : wxInputStream()
44{
45 m_file_destroy = false;
46 m_file = NULL;
47}
48
49wxFileInputStream::wxFileInputStream(wxFile& file)
50{
51 m_file = &file;
52 m_file_destroy = false;
53}
54
55wxFileInputStream::wxFileInputStream(int fd)
56{
57 m_file = new wxFile(fd);
58 m_file_destroy = true;
59}
60
61wxFileInputStream::~wxFileInputStream()
62{
63 if (m_file_destroy)
64 delete m_file;
65}
66
67wxFileOffset wxFileInputStream::GetLength() const
68{
69 return m_file->Length();
70}
71
72size_t wxFileInputStream::OnSysRead(void *buffer, size_t size)
73{
74 ssize_t ret = m_file->Read(buffer, size);
75
76 // NB: we can't use a switch here because HP-UX CC doesn't allow
77 // switching over long long (which size_t is in 64bit mode)
78
79 if ( !ret )
80 {
81 // nothing read, so nothing more to read
82 m_lasterror = wxSTREAM_EOF;
83 }
84 else if ( ret == wxInvalidOffset )
85 {
86 m_lasterror = wxSTREAM_READ_ERROR;
87 ret = 0;
88 }
89 else
90 {
91 // normal case
92 m_lasterror = wxSTREAM_NO_ERROR;
93 }
94
95 return ret;
96}
97
98wxFileOffset wxFileInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
99{
100 return m_file->Seek(pos, mode);
101}
102
103wxFileOffset wxFileInputStream::OnSysTell() const
104{
105 return m_file->Tell();
106}
107
108// ----------------------------------------------------------------------------
109// wxFileOutputStream
110// ----------------------------------------------------------------------------
111
112wxFileOutputStream::wxFileOutputStream(const wxString& fileName)
113{
114 m_file = new wxFile(fileName, wxFile::write);
115 m_file_destroy = true;
116
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 }
126}
127
128wxFileOutputStream::wxFileOutputStream(wxFile& file)
129{
130 m_file = &file;
131 m_file_destroy = false;
132}
133
134wxFileOutputStream::wxFileOutputStream()
135 : wxOutputStream()
136{
137 m_file_destroy = false;
138 m_file = NULL;
139}
140
141wxFileOutputStream::wxFileOutputStream(int fd)
142{
143 m_file = new wxFile(fd);
144 m_file_destroy = true;
145}
146
147wxFileOutputStream::~wxFileOutputStream()
148{
149 if (m_file_destroy)
150 {
151 Sync();
152 delete m_file;
153 }
154}
155
156size_t wxFileOutputStream::OnSysWrite(const void *buffer, size_t size)
157{
158 size_t ret = m_file->Write(buffer, size);
159
160 m_lasterror = m_file->Error() ? wxSTREAM_WRITE_ERROR : wxSTREAM_NO_ERROR;
161
162 return ret;
163}
164
165wxFileOffset wxFileOutputStream::OnSysTell() const
166{
167 return m_file->Tell();
168}
169
170wxFileOffset wxFileOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
171{
172 return m_file->Seek(pos, mode);
173}
174
175void wxFileOutputStream::Sync()
176{
177 wxOutputStream::Sync();
178 m_file->Flush();
179}
180
181wxFileOffset wxFileOutputStream::GetLength() const
182{
183 return m_file->Length();
184}
185
186// ----------------------------------------------------------------------------
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// ----------------------------------------------------------------------------
214// wxFileStream
215// ----------------------------------------------------------------------------
216
217wxFileStream::wxFileStream(const wxString& fileName)
218 : wxFileInputStream(fileName)
219{
220 wxFileOutputStream::m_file = wxFileInputStream::m_file;
221}
222
223#endif //wxUSE_FILE
224
225#if wxUSE_FFILE
226
227// ----------------------------------------------------------------------------
228// wxFFileInputStream
229// ----------------------------------------------------------------------------
230
231wxFFileInputStream::wxFFileInputStream(const wxString& fileName,
232 const wxChar *mode)
233 : wxInputStream()
234{
235 m_file = new wxFFile(fileName, mode);
236 m_file_destroy = true;
237}
238
239wxFFileInputStream::wxFFileInputStream()
240 : wxInputStream()
241{
242 m_file = NULL;
243 m_file_destroy = false;
244}
245
246wxFFileInputStream::wxFFileInputStream(wxFFile& file)
247{
248 m_file = &file;
249 m_file_destroy = false;
250}
251
252wxFFileInputStream::wxFFileInputStream(FILE *file)
253{
254 m_file = new wxFFile(file);
255 m_file_destroy = true;
256}
257
258wxFFileInputStream::~wxFFileInputStream()
259{
260 if (m_file_destroy)
261 delete m_file;
262}
263
264wxFileOffset wxFFileInputStream::GetLength() const
265{
266 return m_file->Length();
267}
268
269size_t wxFFileInputStream::OnSysRead(void *buffer, size_t size)
270{
271 ssize_t ret = m_file->Read(buffer, size);
272
273 // It is not safe to call Eof() if the file is not opened.
274 if (!m_file->IsOpened() || m_file->Eof())
275 m_lasterror = wxSTREAM_EOF;
276 if (ret == wxInvalidOffset)
277 {
278 m_lasterror = wxSTREAM_READ_ERROR;
279 ret = 0;
280 }
281
282 return ret;
283}
284
285wxFileOffset wxFFileInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
286{
287 return m_file->Seek(pos, mode) ? m_file->Tell() : wxInvalidOffset;
288}
289
290wxFileOffset wxFFileInputStream::OnSysTell() const
291{
292 return m_file->Tell();
293}
294
295// ----------------------------------------------------------------------------
296// wxFFileOutputStream
297// ----------------------------------------------------------------------------
298
299wxFFileOutputStream::wxFFileOutputStream(const wxString& fileName,
300 const wxChar *mode)
301{
302 m_file = new wxFFile(fileName, mode);
303 m_file_destroy = true;
304
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 }
314}
315
316wxFFileOutputStream::wxFFileOutputStream(wxFFile& file)
317{
318 m_file = &file;
319 m_file_destroy = false;
320}
321
322wxFFileOutputStream::wxFFileOutputStream()
323 : wxOutputStream()
324{
325 m_file = NULL;
326 m_file_destroy = false;
327}
328
329wxFFileOutputStream::wxFFileOutputStream(FILE *file)
330{
331 m_file = new wxFFile(file);
332 m_file_destroy = true;
333}
334
335wxFFileOutputStream::~wxFFileOutputStream()
336{
337 if (m_file_destroy)
338 {
339 Sync();
340 delete m_file;
341 }
342}
343
344size_t wxFFileOutputStream::OnSysWrite(const void *buffer, size_t size)
345{
346 size_t ret = m_file->Write(buffer, size);
347 // It is not safe to call Error() if the file is not opened.
348 if (!m_file->IsOpened() || m_file->Error())
349 m_lasterror = wxSTREAM_WRITE_ERROR;
350 else
351 m_lasterror = wxSTREAM_NO_ERROR;
352 return ret;
353}
354
355wxFileOffset wxFFileOutputStream::OnSysTell() const
356{
357 return m_file->Tell();
358}
359
360wxFileOffset wxFFileOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
361{
362 return m_file->Seek(pos, mode) ? m_file->Tell() : wxInvalidOffset;
363}
364
365void wxFFileOutputStream::Sync()
366{
367 wxOutputStream::Sync();
368 m_file->Flush();
369}
370
371wxFileOffset wxFFileOutputStream::GetLength() const
372{
373 return m_file->Length();
374}
375
376// ----------------------------------------------------------------------------
377// wxFFileStream
378// ----------------------------------------------------------------------------
379
380wxFFileStream::wxFFileStream(const wxString& fileName)
381 : wxFFileInputStream(fileName)
382{
383 wxFFileOutputStream::m_file = wxFFileInputStream::m_file;
384}
385
386#endif //wxUSE_FFILE
387
388#endif // wxUSE_STREAMS
389