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