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