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