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