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