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