1 |
william |
31 |
///////////////////////////////////////////////////////////////////////////// |
2 |
|
|
// Name: src/common/fstream.cpp |
3 |
|
|
// Purpose: "File stream" classes |
4 |
|
|
// Author: Julian Smart |
5 |
|
|
// Modified by: |
6 |
|
|
// Created: 11/07/98 |
7 |
|
|
// RCS-ID: $Id: wfstream.cpp 54418 2008-06-29 01:28:43Z VZ $ |
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 "wx/wfstream.h" |
22 |
|
|
|
23 |
|
|
#ifndef WX_PRECOMP |
24 |
|
|
#include "wx/stream.h" |
25 |
|
|
#endif |
26 |
|
|
|
27 |
|
|
#include <stdio.h> |
28 |
|
|
|
29 |
|
|
#if wxUSE_FILE |
30 |
|
|
|
31 |
|
|
// ---------------------------------------------------------------------------- |
32 |
|
|
// wxFileInputStream |
33 |
|
|
// ---------------------------------------------------------------------------- |
34 |
|
|
|
35 |
|
|
wxFileInputStream::wxFileInputStream(const wxString& fileName) |
36 |
|
|
: wxInputStream() |
37 |
|
|
{ |
38 |
|
|
m_file = new wxFile(fileName, wxFile::read); |
39 |
|
|
m_file_destroy = true; |
40 |
|
|
if ( !m_file->IsOpened() ) |
41 |
|
|
m_lasterror = wxSTREAM_READ_ERROR; |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
wxFileInputStream::wxFileInputStream() |
45 |
|
|
: wxInputStream() |
46 |
|
|
{ |
47 |
|
|
m_file_destroy = false; |
48 |
|
|
m_file = NULL; |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
wxFileInputStream::wxFileInputStream(wxFile& file) |
52 |
|
|
{ |
53 |
|
|
m_file = &file; |
54 |
|
|
m_file_destroy = false; |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
wxFileInputStream::wxFileInputStream(int fd) |
58 |
|
|
{ |
59 |
|
|
m_file = new wxFile(fd); |
60 |
|
|
m_file_destroy = true; |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
wxFileInputStream::~wxFileInputStream() |
64 |
|
|
{ |
65 |
|
|
if (m_file_destroy) |
66 |
|
|
delete m_file; |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
wxFileOffset wxFileInputStream::GetLength() const |
70 |
|
|
{ |
71 |
|
|
return m_file->Length(); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
size_t wxFileInputStream::OnSysRead(void *buffer, size_t size) |
75 |
|
|
{ |
76 |
|
|
ssize_t ret = m_file->Read(buffer, size); |
77 |
|
|
|
78 |
|
|
// NB: we can't use a switch here because HP-UX CC doesn't allow |
79 |
|
|
// switching over long long (which size_t is in 64bit mode) |
80 |
|
|
|
81 |
|
|
if ( !ret ) |
82 |
|
|
{ |
83 |
|
|
// nothing read, so nothing more to read |
84 |
|
|
m_lasterror = wxSTREAM_EOF; |
85 |
|
|
} |
86 |
|
|
else if ( ret == wxInvalidOffset ) |
87 |
|
|
{ |
88 |
|
|
m_lasterror = wxSTREAM_READ_ERROR; |
89 |
|
|
ret = 0; |
90 |
|
|
} |
91 |
|
|
else |
92 |
|
|
{ |
93 |
|
|
// normal case |
94 |
|
|
m_lasterror = wxSTREAM_NO_ERROR; |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
return ret; |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
wxFileOffset wxFileInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) |
101 |
|
|
{ |
102 |
|
|
return m_file->Seek(pos, mode); |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
wxFileOffset wxFileInputStream::OnSysTell() const |
106 |
|
|
{ |
107 |
|
|
return m_file->Tell(); |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
bool wxFileInputStream::IsOk() const |
111 |
|
|
{ |
112 |
|
|
return (wxStreamBase::IsOk() && m_file->IsOpened()); |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
// ---------------------------------------------------------------------------- |
116 |
|
|
// wxFileOutputStream |
117 |
|
|
// ---------------------------------------------------------------------------- |
118 |
|
|
|
119 |
|
|
wxFileOutputStream::wxFileOutputStream(const wxString& fileName) |
120 |
|
|
{ |
121 |
|
|
m_file = new wxFile(fileName, wxFile::write); |
122 |
|
|
m_file_destroy = true; |
123 |
|
|
|
124 |
|
|
if (!m_file->IsOpened()) |
125 |
|
|
m_lasterror = wxSTREAM_WRITE_ERROR; |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
wxFileOutputStream::wxFileOutputStream(wxFile& file) |
129 |
|
|
{ |
130 |
|
|
m_file = &file; |
131 |
|
|
m_file_destroy = false; |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
wxFileOutputStream::wxFileOutputStream() |
135 |
|
|
: wxOutputStream() |
136 |
|
|
{ |
137 |
|
|
m_file_destroy = false; |
138 |
|
|
m_file = NULL; |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
wxFileOutputStream::wxFileOutputStream(int fd) |
142 |
|
|
{ |
143 |
|
|
m_file = new wxFile(fd); |
144 |
|
|
m_file_destroy = true; |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
wxFileOutputStream::~wxFileOutputStream() |
148 |
|
|
{ |
149 |
|
|
if (m_file_destroy) |
150 |
|
|
{ |
151 |
|
|
Sync(); |
152 |
|
|
delete m_file; |
153 |
|
|
} |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
size_t wxFileOutputStream::OnSysWrite(const void *buffer, size_t size) |
157 |
|
|
{ |
158 |
|
|
size_t ret = m_file->Write(buffer, size); |
159 |
|
|
|
160 |
|
|
m_lasterror = m_file->Error() ? wxSTREAM_WRITE_ERROR : wxSTREAM_NO_ERROR; |
161 |
|
|
|
162 |
|
|
return ret; |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
wxFileOffset wxFileOutputStream::OnSysTell() const |
166 |
|
|
{ |
167 |
|
|
return m_file->Tell(); |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
wxFileOffset wxFileOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) |
171 |
|
|
{ |
172 |
|
|
return m_file->Seek(pos, mode); |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
void wxFileOutputStream::Sync() |
176 |
|
|
{ |
177 |
|
|
wxOutputStream::Sync(); |
178 |
|
|
m_file->Flush(); |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
wxFileOffset wxFileOutputStream::GetLength() const |
182 |
|
|
{ |
183 |
|
|
return m_file->Length(); |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
bool wxFileOutputStream::IsOk() const |
187 |
|
|
{ |
188 |
|
|
return (wxStreamBase::IsOk() && m_file->IsOpened()); |
189 |
|
|
} |
190 |
|
|
|
191 |
|
|
// ---------------------------------------------------------------------------- |
192 |
|
|
// wxTempFileOutputStream |
193 |
|
|
// ---------------------------------------------------------------------------- |
194 |
|
|
|
195 |
|
|
wxTempFileOutputStream::wxTempFileOutputStream(const wxString& fileName) |
196 |
|
|
{ |
197 |
|
|
m_file = new wxTempFile(fileName); |
198 |
|
|
|
199 |
|
|
if (!m_file->IsOpened()) |
200 |
|
|
m_lasterror = wxSTREAM_WRITE_ERROR; |
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
wxTempFileOutputStream::~wxTempFileOutputStream() |
204 |
|
|
{ |
205 |
|
|
if (m_file->IsOpened()) |
206 |
|
|
Discard(); |
207 |
|
|
delete m_file; |
208 |
|
|
} |
209 |
|
|
|
210 |
|
|
size_t wxTempFileOutputStream::OnSysWrite(const void *buffer, size_t size) |
211 |
|
|
{ |
212 |
|
|
if (IsOk() && m_file->Write(buffer, size)) |
213 |
|
|
return size; |
214 |
|
|
m_lasterror = wxSTREAM_WRITE_ERROR; |
215 |
|
|
return 0; |
216 |
|
|
} |
217 |
|
|
|
218 |
|
|
// ---------------------------------------------------------------------------- |
219 |
|
|
// wxFileStream |
220 |
|
|
// ---------------------------------------------------------------------------- |
221 |
|
|
|
222 |
|
|
wxFileStream::wxFileStream(const wxString& fileName) |
223 |
|
|
: wxFileInputStream(), |
224 |
|
|
wxFileOutputStream() |
225 |
|
|
{ |
226 |
|
|
wxFileOutputStream::m_file = |
227 |
|
|
wxFileInputStream::m_file = new wxFile(fileName, wxFile::read_write); |
228 |
|
|
|
229 |
|
|
// this is a bit ugly as streams are symmetric but we still have to delete |
230 |
|
|
// the file we created above exactly once so we decide to (arbitrarily) do |
231 |
|
|
// it in wxFileInputStream |
232 |
|
|
wxFileInputStream::m_file_destroy = true; |
233 |
|
|
} |
234 |
|
|
|
235 |
|
|
#endif //wxUSE_FILE |
236 |
|
|
|
237 |
|
|
#if wxUSE_FFILE |
238 |
|
|
|
239 |
|
|
// ---------------------------------------------------------------------------- |
240 |
|
|
// wxFFileInputStream |
241 |
|
|
// ---------------------------------------------------------------------------- |
242 |
|
|
|
243 |
|
|
wxFFileInputStream::wxFFileInputStream(const wxString& fileName, |
244 |
|
|
const wxChar *mode) |
245 |
|
|
: wxInputStream() |
246 |
|
|
{ |
247 |
|
|
m_file = new wxFFile(fileName, mode); |
248 |
|
|
m_file_destroy = true; |
249 |
|
|
|
250 |
|
|
if (!m_file->IsOpened()) |
251 |
|
|
m_lasterror = wxSTREAM_WRITE_ERROR; |
252 |
|
|
} |
253 |
|
|
|
254 |
|
|
wxFFileInputStream::wxFFileInputStream() |
255 |
|
|
: wxInputStream() |
256 |
|
|
{ |
257 |
|
|
m_file = NULL; |
258 |
|
|
m_file_destroy = false; |
259 |
|
|
} |
260 |
|
|
|
261 |
|
|
wxFFileInputStream::wxFFileInputStream(wxFFile& file) |
262 |
|
|
{ |
263 |
|
|
m_file = &file; |
264 |
|
|
m_file_destroy = false; |
265 |
|
|
} |
266 |
|
|
|
267 |
|
|
wxFFileInputStream::wxFFileInputStream(FILE *file) |
268 |
|
|
{ |
269 |
|
|
m_file = new wxFFile(file); |
270 |
|
|
m_file_destroy = true; |
271 |
|
|
} |
272 |
|
|
|
273 |
|
|
wxFFileInputStream::~wxFFileInputStream() |
274 |
|
|
{ |
275 |
|
|
if (m_file_destroy) |
276 |
|
|
delete m_file; |
277 |
|
|
} |
278 |
|
|
|
279 |
|
|
wxFileOffset wxFFileInputStream::GetLength() const |
280 |
|
|
{ |
281 |
|
|
return m_file->Length(); |
282 |
|
|
} |
283 |
|
|
|
284 |
|
|
size_t wxFFileInputStream::OnSysRead(void *buffer, size_t size) |
285 |
|
|
{ |
286 |
|
|
ssize_t ret = m_file->Read(buffer, size); |
287 |
|
|
|
288 |
|
|
// It is not safe to call Eof() if the file is not opened. |
289 |
|
|
if (!m_file->IsOpened() || m_file->Eof()) |
290 |
|
|
m_lasterror = wxSTREAM_EOF; |
291 |
|
|
if (ret == wxInvalidOffset) |
292 |
|
|
{ |
293 |
|
|
m_lasterror = wxSTREAM_READ_ERROR; |
294 |
|
|
ret = 0; |
295 |
|
|
} |
296 |
|
|
|
297 |
|
|
return ret; |
298 |
|
|
} |
299 |
|
|
|
300 |
|
|
wxFileOffset wxFFileInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) |
301 |
|
|
{ |
302 |
|
|
return m_file->Seek(pos, mode) ? m_file->Tell() : wxInvalidOffset; |
303 |
|
|
} |
304 |
|
|
|
305 |
|
|
wxFileOffset wxFFileInputStream::OnSysTell() const |
306 |
|
|
{ |
307 |
|
|
return m_file->Tell(); |
308 |
|
|
} |
309 |
|
|
|
310 |
|
|
bool wxFFileInputStream::IsOk() const |
311 |
|
|
{ |
312 |
|
|
return (wxStreamBase::IsOk() && m_file->IsOpened()); |
313 |
|
|
} |
314 |
|
|
|
315 |
|
|
// ---------------------------------------------------------------------------- |
316 |
|
|
// wxFFileOutputStream |
317 |
|
|
// ---------------------------------------------------------------------------- |
318 |
|
|
|
319 |
|
|
wxFFileOutputStream::wxFFileOutputStream(const wxString& fileName, |
320 |
|
|
const wxChar *mode) |
321 |
|
|
{ |
322 |
|
|
m_file = new wxFFile(fileName, mode); |
323 |
|
|
m_file_destroy = true; |
324 |
|
|
|
325 |
|
|
if (!m_file->IsOpened()) |
326 |
|
|
{ |
327 |
|
|
m_lasterror = wxSTREAM_WRITE_ERROR; |
328 |
|
|
} |
329 |
|
|
else |
330 |
|
|
{ |
331 |
|
|
if (m_file->Error()) |
332 |
|
|
m_lasterror = wxSTREAM_WRITE_ERROR; |
333 |
|
|
} |
334 |
|
|
} |
335 |
|
|
|
336 |
|
|
wxFFileOutputStream::wxFFileOutputStream(wxFFile& file) |
337 |
|
|
{ |
338 |
|
|
m_file = &file; |
339 |
|
|
m_file_destroy = false; |
340 |
|
|
} |
341 |
|
|
|
342 |
|
|
wxFFileOutputStream::wxFFileOutputStream() |
343 |
|
|
: wxOutputStream() |
344 |
|
|
{ |
345 |
|
|
m_file = NULL; |
346 |
|
|
m_file_destroy = false; |
347 |
|
|
} |
348 |
|
|
|
349 |
|
|
wxFFileOutputStream::wxFFileOutputStream(FILE *file) |
350 |
|
|
{ |
351 |
|
|
m_file = new wxFFile(file); |
352 |
|
|
m_file_destroy = true; |
353 |
|
|
} |
354 |
|
|
|
355 |
|
|
wxFFileOutputStream::~wxFFileOutputStream() |
356 |
|
|
{ |
357 |
|
|
if (m_file_destroy) |
358 |
|
|
{ |
359 |
|
|
Sync(); |
360 |
|
|
delete m_file; |
361 |
|
|
} |
362 |
|
|
} |
363 |
|
|
|
364 |
|
|
size_t wxFFileOutputStream::OnSysWrite(const void *buffer, size_t size) |
365 |
|
|
{ |
366 |
|
|
size_t ret = m_file->Write(buffer, size); |
367 |
|
|
// It is not safe to call Error() if the file is not opened. |
368 |
|
|
if (!m_file->IsOpened() || m_file->Error()) |
369 |
|
|
m_lasterror = wxSTREAM_WRITE_ERROR; |
370 |
|
|
else |
371 |
|
|
m_lasterror = wxSTREAM_NO_ERROR; |
372 |
|
|
return ret; |
373 |
|
|
} |
374 |
|
|
|
375 |
|
|
wxFileOffset wxFFileOutputStream::OnSysTell() const |
376 |
|
|
{ |
377 |
|
|
return m_file->Tell(); |
378 |
|
|
} |
379 |
|
|
|
380 |
|
|
wxFileOffset wxFFileOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) |
381 |
|
|
{ |
382 |
|
|
return m_file->Seek(pos, mode) ? m_file->Tell() : wxInvalidOffset; |
383 |
|
|
} |
384 |
|
|
|
385 |
|
|
void wxFFileOutputStream::Sync() |
386 |
|
|
{ |
387 |
|
|
wxOutputStream::Sync(); |
388 |
|
|
m_file->Flush(); |
389 |
|
|
} |
390 |
|
|
|
391 |
|
|
wxFileOffset wxFFileOutputStream::GetLength() const |
392 |
|
|
{ |
393 |
|
|
return m_file->Length(); |
394 |
|
|
} |
395 |
|
|
|
396 |
|
|
bool wxFFileOutputStream::IsOk() const |
397 |
|
|
{ |
398 |
|
|
return (wxStreamBase::IsOk() && m_file->IsOpened()); |
399 |
|
|
} |
400 |
|
|
|
401 |
|
|
// ---------------------------------------------------------------------------- |
402 |
|
|
// wxFFileStream |
403 |
|
|
// ---------------------------------------------------------------------------- |
404 |
|
|
|
405 |
|
|
wxFFileStream::wxFFileStream(const wxString& fileName) |
406 |
|
|
: wxFFileInputStream(), |
407 |
|
|
wxFFileOutputStream() |
408 |
|
|
{ |
409 |
|
|
wxFFileOutputStream::m_file = |
410 |
|
|
wxFFileInputStream::m_file = new wxFFile(fileName, _T("w+b")); |
411 |
|
|
|
412 |
|
|
// see comment in wxFileStream ctor |
413 |
|
|
wxFFileInputStream::m_file_destroy = true; |
414 |
|
|
} |
415 |
|
|
|
416 |
|
|
#endif //wxUSE_FFILE |
417 |
|
|
|
418 |
|
|
#endif // wxUSE_STREAMS |