]> git.saurik.com Git - wxWidgets.git/blob - src/common/wfstream.cpp
ported KO's v262 change forward
[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 licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_STREAMS
20
21 #include <stdio.h>
22 #include "wx/stream.h"
23 #include "wx/wfstream.h"
24
25 #if wxUSE_FILE
26
27 // ----------------------------------------------------------------------------
28 // wxFileInputStream
29 // ----------------------------------------------------------------------------
30
31 wxFileInputStream::wxFileInputStream(const wxString& fileName)
32 : wxInputStream()
33 {
34 m_file = new wxFile(fileName, wxFile::read);
35 m_file_destroy = true;
36 if ( !m_file->IsOpened() )
37 m_lasterror = wxSTREAM_READ_ERROR;
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 wxFileOffset wxFileInputStream::GetLength() const
66 {
67 return m_file->Length();
68 }
69
70 size_t wxFileInputStream::OnSysRead(void *buffer, size_t size)
71 {
72 ssize_t ret = m_file->Read(buffer, size);
73
74 // NB: we can't use a switch here because HP-UX CC doesn't allow
75 // switching over long long (which size_t is in 64bit mode)
76
77 if ( !ret )
78 {
79 // nothing read, so nothing more to read
80 m_lasterror = wxSTREAM_EOF;
81 }
82 else if ( ret == wxInvalidOffset )
83 {
84 m_lasterror = wxSTREAM_READ_ERROR;
85 ret = 0;
86 }
87 else
88 {
89 // normal case
90 m_lasterror = wxSTREAM_NO_ERROR;
91 }
92
93 return ret;
94 }
95
96 wxFileOffset wxFileInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
97 {
98 return m_file->Seek(pos, mode);
99 }
100
101 wxFileOffset wxFileInputStream::OnSysTell() const
102 {
103 return m_file->Tell();
104 }
105
106 // ----------------------------------------------------------------------------
107 // wxFileOutputStream
108 // ----------------------------------------------------------------------------
109
110 wxFileOutputStream::wxFileOutputStream(const wxString& fileName)
111 {
112 m_file = new wxFile(fileName, wxFile::write);
113 m_file_destroy = true;
114
115 if (!m_file->IsOpened())
116 m_lasterror = wxSTREAM_WRITE_ERROR;
117 }
118
119 wxFileOutputStream::wxFileOutputStream(wxFile& file)
120 {
121 m_file = &file;
122 m_file_destroy = false;
123 }
124
125 wxFileOutputStream::wxFileOutputStream()
126 : wxOutputStream()
127 {
128 m_file_destroy = false;
129 m_file = NULL;
130 }
131
132 wxFileOutputStream::wxFileOutputStream(int fd)
133 {
134 m_file = new wxFile(fd);
135 m_file_destroy = true;
136 }
137
138 wxFileOutputStream::~wxFileOutputStream()
139 {
140 if (m_file_destroy)
141 {
142 Sync();
143 delete m_file;
144 }
145 }
146
147 size_t wxFileOutputStream::OnSysWrite(const void *buffer, size_t size)
148 {
149 size_t ret = m_file->Write(buffer, size);
150
151 m_lasterror = m_file->Error() ? wxSTREAM_WRITE_ERROR : wxSTREAM_NO_ERROR;
152
153 return ret;
154 }
155
156 wxFileOffset wxFileOutputStream::OnSysTell() const
157 {
158 return m_file->Tell();
159 }
160
161 wxFileOffset wxFileOutputStream::OnSysSeek(wxFileOffset 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 wxFileOffset wxFileOutputStream::GetLength() const
173 {
174 return m_file->Length();
175 }
176
177 // ----------------------------------------------------------------------------
178 // wxTempFileOutputStream
179 // ----------------------------------------------------------------------------
180
181 wxTempFileOutputStream::wxTempFileOutputStream(const wxString& fileName)
182 {
183 m_file = new wxTempFile(fileName);
184
185 if (!m_file->IsOpened())
186 m_lasterror = wxSTREAM_WRITE_ERROR;
187 }
188
189 wxTempFileOutputStream::~wxTempFileOutputStream()
190 {
191 if (m_file->IsOpened())
192 Discard();
193 delete m_file;
194 }
195
196 size_t wxTempFileOutputStream::OnSysWrite(const void *buffer, size_t size)
197 {
198 if (IsOk() && m_file->Write(buffer, size))
199 return size;
200 m_lasterror = wxSTREAM_WRITE_ERROR;
201 return 0;
202 }
203
204 // ----------------------------------------------------------------------------
205 // wxFileStream
206 // ----------------------------------------------------------------------------
207
208 wxFileStream::wxFileStream(const wxString& fileName)
209 : wxFileInputStream(fileName)
210 {
211 wxFileOutputStream::m_file = wxFileInputStream::m_file;
212 }
213
214 #endif //wxUSE_FILE
215
216 #if wxUSE_FFILE
217
218 // ----------------------------------------------------------------------------
219 // wxFFileInputStream
220 // ----------------------------------------------------------------------------
221
222 wxFFileInputStream::wxFFileInputStream(const wxString& fileName,
223 const wxChar *mode)
224 : wxInputStream()
225 {
226 m_file = new wxFFile(fileName, mode);
227 m_file_destroy = true;
228
229 if (!m_file->IsOpened())
230 m_lasterror = wxSTREAM_WRITE_ERROR;
231 }
232
233 wxFFileInputStream::wxFFileInputStream()
234 : wxInputStream()
235 {
236 m_file = NULL;
237 m_file_destroy = false;
238 }
239
240 wxFFileInputStream::wxFFileInputStream(wxFFile& file)
241 {
242 m_file = &file;
243 m_file_destroy = false;
244 }
245
246 wxFFileInputStream::wxFFileInputStream(FILE *file)
247 {
248 m_file = new wxFFile(file);
249 m_file_destroy = true;
250 }
251
252 wxFFileInputStream::~wxFFileInputStream()
253 {
254 if (m_file_destroy)
255 delete m_file;
256 }
257
258 wxFileOffset wxFFileInputStream::GetLength() const
259 {
260 return m_file->Length();
261 }
262
263 size_t wxFFileInputStream::OnSysRead(void *buffer, size_t size)
264 {
265 ssize_t ret = m_file->Read(buffer, size);
266
267 // It is not safe to call Eof() if the file is not opened.
268 if (!m_file->IsOpened() || m_file->Eof())
269 m_lasterror = wxSTREAM_EOF;
270 if (ret == wxInvalidOffset)
271 {
272 m_lasterror = wxSTREAM_READ_ERROR;
273 ret = 0;
274 }
275
276 return ret;
277 }
278
279 wxFileOffset wxFFileInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
280 {
281 return m_file->Seek(pos, mode) ? m_file->Tell() : wxInvalidOffset;
282 }
283
284 wxFileOffset wxFFileInputStream::OnSysTell() const
285 {
286 return m_file->Tell();
287 }
288
289 // ----------------------------------------------------------------------------
290 // wxFFileOutputStream
291 // ----------------------------------------------------------------------------
292
293 wxFFileOutputStream::wxFFileOutputStream(const wxString& fileName,
294 const wxChar *mode)
295 {
296 m_file = new wxFFile(fileName, mode);
297 m_file_destroy = true;
298
299 if (!m_file->IsOpened())
300 {
301 m_lasterror = wxSTREAM_WRITE_ERROR;
302 }
303 else
304 {
305 if (m_file->Error())
306 m_lasterror = wxSTREAM_WRITE_ERROR;
307 }
308 }
309
310 wxFFileOutputStream::wxFFileOutputStream(wxFFile& file)
311 {
312 m_file = &file;
313 m_file_destroy = false;
314 }
315
316 wxFFileOutputStream::wxFFileOutputStream()
317 : wxOutputStream()
318 {
319 m_file = NULL;
320 m_file_destroy = false;
321 }
322
323 wxFFileOutputStream::wxFFileOutputStream(FILE *file)
324 {
325 m_file = new wxFFile(file);
326 m_file_destroy = true;
327 }
328
329 wxFFileOutputStream::~wxFFileOutputStream()
330 {
331 if (m_file_destroy)
332 {
333 Sync();
334 delete m_file;
335 }
336 }
337
338 size_t wxFFileOutputStream::OnSysWrite(const void *buffer, size_t size)
339 {
340 size_t ret = m_file->Write(buffer, size);
341 // It is not safe to call Error() if the file is not opened.
342 if (!m_file->IsOpened() || m_file->Error())
343 m_lasterror = wxSTREAM_WRITE_ERROR;
344 else
345 m_lasterror = wxSTREAM_NO_ERROR;
346 return ret;
347 }
348
349 wxFileOffset wxFFileOutputStream::OnSysTell() const
350 {
351 return m_file->Tell();
352 }
353
354 wxFileOffset wxFFileOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode)
355 {
356 return m_file->Seek(pos, mode) ? m_file->Tell() : wxInvalidOffset;
357 }
358
359 void wxFFileOutputStream::Sync()
360 {
361 wxOutputStream::Sync();
362 m_file->Flush();
363 }
364
365 wxFileOffset wxFFileOutputStream::GetLength() const
366 {
367 return m_file->Length();
368 }
369
370 // ----------------------------------------------------------------------------
371 // wxFFileStream
372 // ----------------------------------------------------------------------------
373
374 wxFFileStream::wxFFileStream(const wxString& fileName)
375 : wxFFileInputStream(fileName)
376 {
377 wxFFileOutputStream::m_file = wxFFileInputStream::m_file;
378 }
379
380 #endif //wxUSE_FFILE
381
382 #endif // wxUSE_STREAMS
383