]>
Commit | Line | Data |
---|---|---|
3d4c6a21 GL |
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$ | |
f6bcfd97 | 8 | // Copyright: (c) Guilhem Lavaux |
65571936 | 9 | // Licence: wxWindows licence |
3d4c6a21 GL |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
14f355c2 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
593d4b0d | 13 | #pragma implementation "wfstream.h" |
3d4c6a21 GL |
14 | #endif |
15 | ||
79c3e0e1 GL |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
3d4c6a21 | 18 | |
79c3e0e1 | 19 | #ifdef __BORLANDC__ |
ce4169a4 | 20 | #pragma hdrstop |
79c3e0e1 GL |
21 | #endif |
22 | ||
85990624 | 23 | #if wxUSE_STREAMS |
ce4169a4 RR |
24 | |
25 | #include <stdio.h> | |
d1af991f RR |
26 | #include "wx/stream.h" |
27 | #include "wx/wfstream.h" | |
ce4169a4 | 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 | ||
110 | // ---------------------------------------------------------------------------- | |
111 | // wxFileOutputStream | |
112 | // ---------------------------------------------------------------------------- | |
113 | ||
114 | wxFileOutputStream::wxFileOutputStream(const wxString& fileName) | |
25c70b07 | 115 | { |
9adf4299 | 116 | m_file = new wxFile(fileName, wxFile::write); |
cab1a605 | 117 | m_file_destroy = true; |
9a26db9e | 118 | |
942bef71 | 119 | if (!m_file->IsOpened()) |
942bef71 | 120 | m_lasterror = wxSTREAM_WRITE_ERROR; |
25c70b07 GL |
121 | } |
122 | ||
84b46c35 GL |
123 | wxFileOutputStream::wxFileOutputStream(wxFile& file) |
124 | { | |
f6bcfd97 | 125 | m_file = &file; |
cab1a605 | 126 | m_file_destroy = false; |
84b46c35 GL |
127 | } |
128 | ||
25c70b07 | 129 | wxFileOutputStream::wxFileOutputStream() |
9a26db9e | 130 | : wxOutputStream() |
79c3e0e1 | 131 | { |
cab1a605 | 132 | m_file_destroy = false; |
f6bcfd97 | 133 | m_file = NULL; |
3d4c6a21 GL |
134 | } |
135 | ||
0aca1ded GL |
136 | wxFileOutputStream::wxFileOutputStream(int fd) |
137 | { | |
f6bcfd97 | 138 | m_file = new wxFile(fd); |
cab1a605 | 139 | m_file_destroy = true; |
0aca1ded GL |
140 | } |
141 | ||
79c3e0e1 | 142 | wxFileOutputStream::~wxFileOutputStream() |
3d4c6a21 | 143 | { |
9a26db9e | 144 | if (m_file_destroy) |
f6bcfd97 BP |
145 | { |
146 | Sync(); | |
147 | delete m_file; | |
148 | } | |
79c3e0e1 | 149 | } |
3d4c6a21 | 150 | |
75ed1d15 | 151 | size_t wxFileOutputStream::OnSysWrite(const void *buffer, size_t size) |
79c3e0e1 | 152 | { |
f6bcfd97 | 153 | size_t ret = m_file->Write(buffer, size); |
9a26db9e VZ |
154 | |
155 | m_lasterror = m_file->Error() ? wxSTREAM_WRITE_ERROR : wxSTREAM_NO_ERROR; | |
156 | ||
f6bcfd97 | 157 | return ret; |
79c3e0e1 | 158 | } |
3d4c6a21 | 159 | |
4004775e | 160 | wxFileOffset wxFileOutputStream::OnSysTell() const |
79c3e0e1 | 161 | { |
f6bcfd97 | 162 | return m_file->Tell(); |
3d4c6a21 GL |
163 | } |
164 | ||
4004775e | 165 | wxFileOffset wxFileOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) |
3d4c6a21 | 166 | { |
f6bcfd97 | 167 | return m_file->Seek(pos, mode); |
3d4c6a21 GL |
168 | } |
169 | ||
79c3e0e1 | 170 | void wxFileOutputStream::Sync() |
3d4c6a21 | 171 | { |
f6bcfd97 BP |
172 | wxOutputStream::Sync(); |
173 | m_file->Flush(); | |
3d4c6a21 | 174 | } |
84b46c35 | 175 | |
588066b7 | 176 | wxFileOffset wxFileOutputStream::GetLength() const |
84b46c35 | 177 | { |
f6bcfd97 | 178 | return m_file->Length(); |
84b46c35 GL |
179 | } |
180 | ||
181 | // ---------------------------------------------------------------------------- | |
e1265174 MW |
182 | // wxTempFileOutputStream |
183 | // ---------------------------------------------------------------------------- | |
184 | ||
185 | wxTempFileOutputStream::wxTempFileOutputStream(const wxString& fileName) | |
186 | { | |
187 | m_file = new wxTempFile(fileName); | |
188 | ||
189 | if (!m_file->IsOpened()) | |
190 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
191 | } | |
192 | ||
193 | wxTempFileOutputStream::~wxTempFileOutputStream() | |
194 | { | |
195 | if (m_file->IsOpened()) | |
196 | Discard(); | |
197 | delete m_file; | |
198 | } | |
199 | ||
200 | size_t wxTempFileOutputStream::OnSysWrite(const void *buffer, size_t size) | |
201 | { | |
202 | if (IsOk() && m_file->Write(buffer, size)) | |
203 | return size; | |
204 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
205 | return 0; | |
206 | } | |
207 | ||
208 | // ---------------------------------------------------------------------------- | |
84b46c35 GL |
209 | // wxFileStream |
210 | // ---------------------------------------------------------------------------- | |
f6bcfd97 | 211 | |
84b46c35 | 212 | wxFileStream::wxFileStream(const wxString& fileName) |
f6bcfd97 | 213 | : wxFileInputStream(fileName) |
84b46c35 | 214 | { |
f6bcfd97 | 215 | wxFileOutputStream::m_file = wxFileInputStream::m_file; |
84b46c35 | 216 | } |
ce4169a4 | 217 | |
85990624 RN |
218 | #endif //wxUSE_FILE |
219 | ||
220 | #if wxUSE_FFILE | |
221 | ||
65045edd RR |
222 | // ---------------------------------------------------------------------------- |
223 | // wxFFileInputStream | |
224 | // ---------------------------------------------------------------------------- | |
225 | ||
5fec5bb6 VZ |
226 | wxFFileInputStream::wxFFileInputStream(const wxString& fileName, |
227 | const wxChar *mode) | |
228 | : wxInputStream() | |
65045edd | 229 | { |
5fec5bb6 | 230 | m_file = new wxFFile(fileName, mode); |
cab1a605 | 231 | m_file_destroy = true; |
65045edd RR |
232 | } |
233 | ||
234 | wxFFileInputStream::wxFFileInputStream() | |
5fec5bb6 | 235 | : wxInputStream() |
65045edd | 236 | { |
f6bcfd97 | 237 | m_file = NULL; |
5fec5bb6 | 238 | m_file_destroy = false; |
65045edd RR |
239 | } |
240 | ||
241 | wxFFileInputStream::wxFFileInputStream(wxFFile& file) | |
242 | { | |
f6bcfd97 | 243 | m_file = &file; |
cab1a605 | 244 | m_file_destroy = false; |
65045edd RR |
245 | } |
246 | ||
247 | wxFFileInputStream::wxFFileInputStream(FILE *file) | |
248 | { | |
f6bcfd97 | 249 | m_file = new wxFFile(file); |
cab1a605 | 250 | m_file_destroy = true; |
65045edd RR |
251 | } |
252 | ||
253 | wxFFileInputStream::~wxFFileInputStream() | |
254 | { | |
f6bcfd97 BP |
255 | if (m_file_destroy) |
256 | delete m_file; | |
65045edd RR |
257 | } |
258 | ||
588066b7 | 259 | wxFileOffset wxFFileInputStream::GetLength() const |
65045edd | 260 | { |
f6bcfd97 | 261 | return m_file->Length(); |
65045edd RR |
262 | } |
263 | ||
264 | size_t wxFFileInputStream::OnSysRead(void *buffer, size_t size) | |
265 | { | |
f8a586e0 | 266 | ssize_t ret = m_file->Read(buffer, size); |
65045edd | 267 | |
b9d84e4c DE |
268 | // It is not safe to call Eof() if the file is not opened. |
269 | if (!m_file->IsOpened() || m_file->Eof()) | |
2b5f62a0 | 270 | m_lasterror = wxSTREAM_EOF; |
f8a586e0 | 271 | if (ret == wxInvalidOffset) |
f6bcfd97 | 272 | { |
2b5f62a0 | 273 | m_lasterror = wxSTREAM_READ_ERROR; |
f6bcfd97 BP |
274 | ret = 0; |
275 | } | |
65045edd | 276 | |
f6bcfd97 | 277 | return ret; |
65045edd RR |
278 | } |
279 | ||
4004775e | 280 | wxFileOffset wxFFileInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) |
65045edd | 281 | { |
70a7bd90 | 282 | return m_file->Seek(pos, mode) ? m_file->Tell() : wxInvalidOffset; |
65045edd RR |
283 | } |
284 | ||
4004775e | 285 | wxFileOffset wxFFileInputStream::OnSysTell() const |
65045edd | 286 | { |
f6bcfd97 | 287 | return m_file->Tell(); |
65045edd RR |
288 | } |
289 | ||
290 | // ---------------------------------------------------------------------------- | |
291 | // wxFFileOutputStream | |
292 | // ---------------------------------------------------------------------------- | |
293 | ||
5fec5bb6 VZ |
294 | wxFFileOutputStream::wxFFileOutputStream(const wxString& fileName, |
295 | const wxChar *mode) | |
65045edd | 296 | { |
5fec5bb6 | 297 | m_file = new wxFFile(fileName, mode); |
cab1a605 | 298 | m_file_destroy = true; |
9a26db9e | 299 | |
942bef71 RR |
300 | if (!m_file->IsOpened()) |
301 | { | |
302 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
303 | } | |
304 | else | |
305 | { | |
306 | if (m_file->Error()) | |
307 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
308 | } | |
65045edd RR |
309 | } |
310 | ||
311 | wxFFileOutputStream::wxFFileOutputStream(wxFFile& file) | |
312 | { | |
f6bcfd97 | 313 | m_file = &file; |
cab1a605 | 314 | m_file_destroy = false; |
65045edd RR |
315 | } |
316 | ||
317 | wxFFileOutputStream::wxFFileOutputStream() | |
5fec5bb6 | 318 | : wxOutputStream() |
65045edd | 319 | { |
f6bcfd97 | 320 | m_file = NULL; |
5fec5bb6 | 321 | m_file_destroy = false; |
65045edd RR |
322 | } |
323 | ||
324 | wxFFileOutputStream::wxFFileOutputStream(FILE *file) | |
325 | { | |
f6bcfd97 | 326 | m_file = new wxFFile(file); |
cab1a605 | 327 | m_file_destroy = true; |
65045edd RR |
328 | } |
329 | ||
330 | wxFFileOutputStream::~wxFFileOutputStream() | |
331 | { | |
9a26db9e | 332 | if (m_file_destroy) |
f6bcfd97 BP |
333 | { |
334 | Sync(); | |
335 | delete m_file; | |
336 | } | |
65045edd RR |
337 | } |
338 | ||
339 | size_t wxFFileOutputStream::OnSysWrite(const void *buffer, size_t size) | |
340 | { | |
f6bcfd97 | 341 | size_t ret = m_file->Write(buffer, size); |
b9d84e4c DE |
342 | // It is not safe to call Error() if the file is not opened. |
343 | if (!m_file->IsOpened() || m_file->Error()) | |
2b5f62a0 | 344 | m_lasterror = wxSTREAM_WRITE_ERROR; |
f6bcfd97 | 345 | else |
2b5f62a0 | 346 | m_lasterror = wxSTREAM_NO_ERROR; |
f6bcfd97 | 347 | return ret; |
65045edd RR |
348 | } |
349 | ||
4004775e | 350 | wxFileOffset wxFFileOutputStream::OnSysTell() const |
65045edd | 351 | { |
f6bcfd97 | 352 | return m_file->Tell(); |
65045edd RR |
353 | } |
354 | ||
4004775e | 355 | wxFileOffset wxFFileOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) |
65045edd | 356 | { |
70a7bd90 | 357 | return m_file->Seek(pos, mode) ? m_file->Tell() : wxInvalidOffset; |
65045edd RR |
358 | } |
359 | ||
360 | void wxFFileOutputStream::Sync() | |
361 | { | |
f6bcfd97 BP |
362 | wxOutputStream::Sync(); |
363 | m_file->Flush(); | |
65045edd RR |
364 | } |
365 | ||
588066b7 | 366 | wxFileOffset wxFFileOutputStream::GetLength() const |
65045edd | 367 | { |
f6bcfd97 | 368 | return m_file->Length(); |
65045edd RR |
369 | } |
370 | ||
371 | // ---------------------------------------------------------------------------- | |
372 | // wxFFileStream | |
373 | // ---------------------------------------------------------------------------- | |
f6bcfd97 | 374 | |
65045edd | 375 | wxFFileStream::wxFFileStream(const wxString& fileName) |
f6bcfd97 | 376 | : wxFFileInputStream(fileName) |
65045edd | 377 | { |
f6bcfd97 | 378 | wxFFileOutputStream::m_file = wxFFileInputStream::m_file; |
65045edd | 379 | } |
f6bcfd97 | 380 | |
85990624 RN |
381 | #endif //wxUSE_FFILE |
382 | ||
383 | #endif // wxUSE_STREAMS | |
cc985fac | 384 |