]> git.saurik.com Git - wxWidgets.git/blob - src/common/wfstream.cpp
Ups, mismatch between header and source wrt InterruptWait()
[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 m_lasterror = wxStream_NOERROR;
77 if (m_file->Eof())
78 m_lasterror = wxStream_EOF;
79 if (ret == wxInvalidOffset) {
80 m_lasterror = wxStream_READ_ERR;
81 ret = 0;
82 }
83
84 return ret;
85 }
86
87 off_t wxFileInputStream::OnSysSeek(off_t pos, wxSeekMode mode)
88 {
89 return m_file->Seek(pos, mode);
90 }
91
92 off_t wxFileInputStream::OnSysTell() const
93 {
94 return m_file->Tell();
95 }
96
97 // ----------------------------------------------------------------------------
98 // wxFileOutputStream
99 // ----------------------------------------------------------------------------
100
101 wxFileOutputStream::wxFileOutputStream(const wxString& fileName)
102 {
103 m_file = new wxFile(fileName, wxFile::write);
104 m_file_destroy = TRUE;
105 }
106
107 wxFileOutputStream::wxFileOutputStream(wxFile& file)
108 {
109 m_file = &file;
110 m_file_destroy = FALSE;
111 }
112
113 wxFileOutputStream::wxFileOutputStream()
114 : wxOutputStream()
115 {
116 m_file_destroy = FALSE;
117 m_file = NULL;
118 }
119
120 wxFileOutputStream::wxFileOutputStream(int fd)
121 {
122 m_file = new wxFile(fd);
123 m_file_destroy = TRUE;
124 }
125
126 wxFileOutputStream::~wxFileOutputStream()
127 {
128 if (m_file_destroy) {
129 Sync();
130 delete m_file;
131 }
132 }
133
134 size_t wxFileOutputStream::OnSysWrite(const void *buffer, size_t size)
135 {
136 size_t ret = m_file->Write(buffer, size);
137 if (m_file->Error())
138 m_lasterror = wxStream_WRITE_ERR;
139 else
140 m_lasterror = wxStream_NOERROR;
141 return ret;
142 }
143
144 off_t wxFileOutputStream::OnSysTell() const
145 {
146 return m_file->Tell();
147 }
148
149 off_t wxFileOutputStream::OnSysSeek(off_t pos, wxSeekMode mode)
150 {
151 return m_file->Seek(pos, mode);
152 }
153
154 void wxFileOutputStream::Sync()
155 {
156 wxOutputStream::Sync();
157 m_file->Flush();
158 }
159
160 size_t wxFileOutputStream::GetSize() const
161 {
162 return m_file->Length();
163 }
164
165 // ----------------------------------------------------------------------------
166 // wxFileStream
167 // ----------------------------------------------------------------------------
168 wxFileStream::wxFileStream(const wxString& fileName)
169 : wxFileInputStream(fileName), wxFileOutputStream(*wxFileInputStream::m_file)
170 {
171 }
172
173 // ----------------------------------------------------------------------------
174 // wxFFileInputStream
175 // ----------------------------------------------------------------------------
176
177 wxFFileInputStream::wxFFileInputStream(const wxString& fileName)
178 : wxInputStream()
179 {
180 m_file = new wxFFile(fileName, "r");
181 m_file_destroy = TRUE;
182 }
183
184 wxFFileInputStream::wxFFileInputStream()
185 : wxInputStream()
186 {
187 m_file_destroy = FALSE;
188 m_file = NULL;
189 }
190
191 wxFFileInputStream::wxFFileInputStream(wxFFile& file)
192 {
193 m_file = &file;
194 m_file_destroy = FALSE;
195 }
196
197 wxFFileInputStream::wxFFileInputStream(FILE *file)
198 {
199 m_file = new wxFFile(file);
200 m_file_destroy = TRUE;
201 }
202
203 wxFFileInputStream::~wxFFileInputStream()
204 {
205 if (m_file_destroy)
206 delete m_file;
207 }
208
209 size_t wxFFileInputStream::GetSize() const
210 {
211 return m_file->Length();
212 }
213
214 size_t wxFFileInputStream::OnSysRead(void *buffer, size_t size)
215 {
216 off_t ret;
217
218 ret = m_file->Read(buffer, size);
219
220 if (m_file->Eof())
221 m_lasterror = wxStream_EOF;
222 if (ret == wxInvalidOffset) {
223 m_lasterror = wxStream_READ_ERR;
224 ret = 0;
225 }
226
227 return ret;
228 }
229
230 off_t wxFFileInputStream::OnSysSeek(off_t pos, wxSeekMode mode)
231 {
232 return m_file->Seek(pos, mode);
233 }
234
235 off_t wxFFileInputStream::OnSysTell() const
236 {
237 return m_file->Tell();
238 }
239
240 // ----------------------------------------------------------------------------
241 // wxFFileOutputStream
242 // ----------------------------------------------------------------------------
243
244 wxFFileOutputStream::wxFFileOutputStream(const wxString& fileName)
245 {
246 m_file = new wxFFile(fileName, "w+");
247 m_file_destroy = TRUE;
248 }
249
250 wxFFileOutputStream::wxFFileOutputStream(wxFFile& file)
251 {
252 m_file = &file;
253 m_file_destroy = FALSE;
254 }
255
256 wxFFileOutputStream::wxFFileOutputStream()
257 : wxOutputStream()
258 {
259 m_file_destroy = FALSE;
260 m_file = NULL;
261 }
262
263 wxFFileOutputStream::wxFFileOutputStream(FILE *file)
264 {
265 m_file = new wxFFile(file);
266 m_file_destroy = TRUE;
267 }
268
269 wxFFileOutputStream::~wxFFileOutputStream()
270 {
271 if (m_file_destroy) {
272 Sync();
273 delete m_file;
274 }
275 }
276
277 size_t wxFFileOutputStream::OnSysWrite(const void *buffer, size_t size)
278 {
279 size_t ret = m_file->Write(buffer, size);
280 if (m_file->Error())
281 m_lasterror = wxStream_WRITE_ERR;
282 else
283 m_lasterror = wxStream_NOERROR;
284 return ret;
285 }
286
287 off_t wxFFileOutputStream::OnSysTell() const
288 {
289 return m_file->Tell();
290 }
291
292 off_t wxFFileOutputStream::OnSysSeek(off_t pos, wxSeekMode mode)
293 {
294 return m_file->Seek(pos, mode);
295 }
296
297 void wxFFileOutputStream::Sync()
298 {
299 wxOutputStream::Sync();
300 m_file->Flush();
301 }
302
303 size_t wxFFileOutputStream::GetSize() const
304 {
305 return m_file->Length();
306 }
307
308 // ----------------------------------------------------------------------------
309 // wxFFileStream
310 // ----------------------------------------------------------------------------
311 wxFFileStream::wxFFileStream(const wxString& fileName)
312 : wxFFileInputStream(fileName), wxFFileOutputStream(*wxFFileInputStream::m_file)
313 {
314 }
315 #endif
316 // wxUSE_STREAMS && wxUSE_FILE
317