]> git.saurik.com Git - wxWidgets.git/blame - src/common/wfstream.cpp
* Changed behaviour of wxTextStreams::operator(wxUint8/wxInt8). Now it writes
[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$
8// Copyright: (c) Guilhem Lavaux
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{
25c70b07
GL
36 m_file = new wxFile(fileName, wxFile::read);
37 m_file_destroy = TRUE;
3d4c6a21
GL
38}
39
25c70b07
GL
40wxFileInputStream::wxFileInputStream()
41 : wxInputStream()
42{
43 m_file_destroy = FALSE;
44 m_file = NULL;
45}
46
0aca1ded
GL
47wxFileInputStream::wxFileInputStream(wxFile& file)
48{
49 m_file = &file;
50 m_file_destroy = FALSE;
0aca1ded
GL
51}
52
53wxFileInputStream::wxFileInputStream(int fd)
54{
55 m_file = new wxFile(fd);
56 m_file_destroy = TRUE;
0aca1ded
GL
57}
58
79c3e0e1 59wxFileInputStream::~wxFileInputStream()
3d4c6a21 60{
25c70b07
GL
61 if (m_file_destroy)
62 delete m_file;
3d4c6a21
GL
63}
64
6d44bf31 65char wxFileInputStream::Peek()
3d4c6a21 66{
6d44bf31 67 return 0;
3d4c6a21
GL
68}
69
84b46c35
GL
70size_t wxFileInputStream::StreamSize() const
71{
72 return m_file->Length();
73}
74
75ed1d15 75size_t wxFileInputStream::OnSysRead(void *buffer, size_t size)
6d44bf31 76{
fae05df5
GL
77 off_t ret;
78
79 ret = m_file->Read(buffer, size);
80
81 if (m_file->Eof())
82 m_lasterror = wxStream_EOF;
83 if (ret == wxInvalidOffset) {
84 m_lasterror = wxStream_READ_ERR;
85 ret = 0;
86 }
87
88 return ret;
6d44bf31
GL
89}
90
75ed1d15 91off_t wxFileInputStream::OnSysSeek(off_t pos, wxSeekMode mode)
3d4c6a21 92{
25c70b07 93 return m_file->Seek(pos, mode);
79c3e0e1
GL
94}
95
75ed1d15 96off_t wxFileInputStream::OnSysTell() const
79c3e0e1 97{
25c70b07 98 return m_file->Tell();
79c3e0e1
GL
99}
100
101// ----------------------------------------------------------------------------
102// wxFileOutputStream
103// ----------------------------------------------------------------------------
104
105wxFileOutputStream::wxFileOutputStream(const wxString& fileName)
25c70b07
GL
106{
107 m_file = new wxFile(fileName, wxFile::write);
108 m_file_destroy = TRUE;
25c70b07
GL
109}
110
84b46c35
GL
111wxFileOutputStream::wxFileOutputStream(wxFile& file)
112{
113 m_file = &file;
114 m_file_destroy = FALSE;
84b46c35
GL
115}
116
25c70b07
GL
117wxFileOutputStream::wxFileOutputStream()
118 : wxOutputStream()
79c3e0e1 119{
25c70b07
GL
120 m_file_destroy = FALSE;
121 m_file = NULL;
3d4c6a21
GL
122}
123
0aca1ded
GL
124wxFileOutputStream::wxFileOutputStream(int fd)
125{
126 m_file = new wxFile(fd);
127 m_file_destroy = TRUE;
0aca1ded
GL
128}
129
79c3e0e1 130wxFileOutputStream::~wxFileOutputStream()
3d4c6a21 131{
25c70b07
GL
132 if (m_file_destroy) {
133 Sync();
134 delete m_file;
135 }
79c3e0e1 136}
3d4c6a21 137
75ed1d15 138size_t wxFileOutputStream::OnSysWrite(const void *buffer, size_t size)
79c3e0e1 139{
25c70b07 140 size_t ret = m_file->Write(buffer, size);
75ed1d15 141 m_lasterror = wxStream_EOF; // TODO
6d44bf31 142 return ret;
79c3e0e1 143}
3d4c6a21 144
75ed1d15 145off_t wxFileOutputStream::OnSysTell() const
79c3e0e1 146{
25c70b07 147 return m_file->Tell();
3d4c6a21
GL
148}
149
75ed1d15 150off_t wxFileOutputStream::OnSysSeek(off_t pos, wxSeekMode mode)
3d4c6a21 151{
25c70b07 152 return m_file->Seek(pos, mode);
3d4c6a21
GL
153}
154
79c3e0e1 155void wxFileOutputStream::Sync()
3d4c6a21 156{
6d44bf31 157 wxOutputStream::Sync();
25c70b07 158 m_file->Flush();
3d4c6a21 159}
84b46c35
GL
160
161size_t wxFileOutputStream::StreamSize() const
162{
163 return m_file->Length();
164}
165
166// ----------------------------------------------------------------------------
167// wxFileStream
168// ----------------------------------------------------------------------------
169wxFileStream::wxFileStream(const wxString& fileName)
170 : wxFileInputStream(fileName), wxFileOutputStream(*wxFileInputStream::m_file)
171{
172}
ce4169a4
RR
173
174#endif
175 // wxUSE_STREAMS && wxUSE_FILE
cc985fac 176