]>
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 | ||
ce4169a4 RR |
23 | #if wxUSE_STREAMS && wxUSE_FILE |
24 | ||
25 | #include <stdio.h> | |
d1af991f RR |
26 | #include "wx/stream.h" |
27 | #include "wx/wfstream.h" | |
ce4169a4 | 28 | |
79c3e0e1 GL |
29 | // ---------------------------------------------------------------------------- |
30 | // wxFileInputStream | |
31 | // ---------------------------------------------------------------------------- | |
32 | ||
33 | wxFileInputStream::wxFileInputStream(const wxString& fileName) | |
25c70b07 | 34 | : wxInputStream() |
3d4c6a21 | 35 | { |
f6bcfd97 | 36 | m_file = new wxFile(fileName, wxFile::read); |
cab1a605 | 37 | m_file_destroy = true; |
3d4c6a21 GL |
38 | } |
39 | ||
25c70b07 GL |
40 | wxFileInputStream::wxFileInputStream() |
41 | : wxInputStream() | |
42 | { | |
cab1a605 | 43 | m_file_destroy = false; |
f6bcfd97 | 44 | m_file = NULL; |
25c70b07 GL |
45 | } |
46 | ||
0aca1ded GL |
47 | wxFileInputStream::wxFileInputStream(wxFile& file) |
48 | { | |
f6bcfd97 | 49 | m_file = &file; |
cab1a605 | 50 | m_file_destroy = false; |
0aca1ded GL |
51 | } |
52 | ||
53 | wxFileInputStream::wxFileInputStream(int fd) | |
54 | { | |
f6bcfd97 | 55 | m_file = new wxFile(fd); |
cab1a605 | 56 | m_file_destroy = true; |
0aca1ded GL |
57 | } |
58 | ||
79c3e0e1 | 59 | wxFileInputStream::~wxFileInputStream() |
3d4c6a21 | 60 | { |
f6bcfd97 BP |
61 | if (m_file_destroy) |
62 | delete m_file; | |
3d4c6a21 GL |
63 | } |
64 | ||
588066b7 | 65 | wxFileOffset wxFileInputStream::GetLength() const |
84b46c35 | 66 | { |
f6bcfd97 | 67 | return m_file->Length(); |
84b46c35 GL |
68 | } |
69 | ||
75ed1d15 | 70 | size_t wxFileInputStream::OnSysRead(void *buffer, size_t size) |
6d44bf31 | 71 | { |
f8a586e0 | 72 | ssize_t ret = m_file->Read(buffer, size); |
fae05df5 | 73 | |
e69a1ea8 | 74 | // NB: we can't use a switch here because HP-UX CC doesn't allow |
30984dea | 75 | // switching over long long (which size_t is in 64bit mode) |
9a26db9e | 76 | |
e69a1ea8 VZ |
77 | if ( !ret ) |
78 | { | |
79 | // nothing read, so nothing more to read | |
80 | m_lasterror = wxSTREAM_EOF; | |
81 | } | |
f8a586e0 | 82 | else if ( ret == wxInvalidOffset ) |
e69a1ea8 VZ |
83 | { |
84 | m_lasterror = wxSTREAM_READ_ERROR; | |
85 | ret = 0; | |
86 | } | |
87 | else | |
88 | { | |
89 | // normal case | |
90 | m_lasterror = wxSTREAM_NO_ERROR; | |
f6bcfd97 | 91 | } |
fae05df5 | 92 | |
f6bcfd97 | 93 | return ret; |
6d44bf31 GL |
94 | } |
95 | ||
4004775e | 96 | wxFileOffset wxFileInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) |
3d4c6a21 | 97 | { |
9a26db9e | 98 | return m_file->Seek(pos, mode); |
79c3e0e1 GL |
99 | } |
100 | ||
4004775e | 101 | wxFileOffset wxFileInputStream::OnSysTell() const |
79c3e0e1 | 102 | { |
f6bcfd97 | 103 | return m_file->Tell(); |
79c3e0e1 GL |
104 | } |
105 | ||
106 | // ---------------------------------------------------------------------------- | |
107 | // wxFileOutputStream | |
108 | // ---------------------------------------------------------------------------- | |
109 | ||
110 | wxFileOutputStream::wxFileOutputStream(const wxString& fileName) | |
25c70b07 | 111 | { |
f6bcfd97 | 112 | m_file = new wxFile(fileName, wxFile::write); |
cab1a605 | 113 | m_file_destroy = true; |
9a26db9e | 114 | |
942bef71 RR |
115 | if (!m_file->IsOpened()) |
116 | { | |
117 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
118 | } | |
119 | else | |
120 | { | |
121 | if (m_file->Error()) | |
122 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
123 | } | |
25c70b07 GL |
124 | } |
125 | ||
84b46c35 GL |
126 | wxFileOutputStream::wxFileOutputStream(wxFile& file) |
127 | { | |
f6bcfd97 | 128 | m_file = &file; |
cab1a605 | 129 | m_file_destroy = false; |
84b46c35 GL |
130 | } |
131 | ||
25c70b07 | 132 | wxFileOutputStream::wxFileOutputStream() |
9a26db9e | 133 | : wxOutputStream() |
79c3e0e1 | 134 | { |
cab1a605 | 135 | m_file_destroy = false; |
f6bcfd97 | 136 | m_file = NULL; |
3d4c6a21 GL |
137 | } |
138 | ||
0aca1ded GL |
139 | wxFileOutputStream::wxFileOutputStream(int fd) |
140 | { | |
f6bcfd97 | 141 | m_file = new wxFile(fd); |
cab1a605 | 142 | m_file_destroy = true; |
0aca1ded GL |
143 | } |
144 | ||
79c3e0e1 | 145 | wxFileOutputStream::~wxFileOutputStream() |
3d4c6a21 | 146 | { |
9a26db9e | 147 | if (m_file_destroy) |
f6bcfd97 BP |
148 | { |
149 | Sync(); | |
150 | delete m_file; | |
151 | } | |
79c3e0e1 | 152 | } |
3d4c6a21 | 153 | |
75ed1d15 | 154 | size_t wxFileOutputStream::OnSysWrite(const void *buffer, size_t size) |
79c3e0e1 | 155 | { |
f6bcfd97 | 156 | size_t ret = m_file->Write(buffer, size); |
9a26db9e VZ |
157 | |
158 | m_lasterror = m_file->Error() ? wxSTREAM_WRITE_ERROR : wxSTREAM_NO_ERROR; | |
159 | ||
f6bcfd97 | 160 | return ret; |
79c3e0e1 | 161 | } |
3d4c6a21 | 162 | |
4004775e | 163 | wxFileOffset wxFileOutputStream::OnSysTell() const |
79c3e0e1 | 164 | { |
f6bcfd97 | 165 | return m_file->Tell(); |
3d4c6a21 GL |
166 | } |
167 | ||
4004775e | 168 | wxFileOffset wxFileOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) |
3d4c6a21 | 169 | { |
f6bcfd97 | 170 | return m_file->Seek(pos, mode); |
3d4c6a21 GL |
171 | } |
172 | ||
79c3e0e1 | 173 | void wxFileOutputStream::Sync() |
3d4c6a21 | 174 | { |
f6bcfd97 BP |
175 | wxOutputStream::Sync(); |
176 | m_file->Flush(); | |
3d4c6a21 | 177 | } |
84b46c35 | 178 | |
588066b7 | 179 | wxFileOffset wxFileOutputStream::GetLength() const |
84b46c35 | 180 | { |
f6bcfd97 | 181 | return m_file->Length(); |
84b46c35 GL |
182 | } |
183 | ||
184 | // ---------------------------------------------------------------------------- | |
185 | // wxFileStream | |
186 | // ---------------------------------------------------------------------------- | |
f6bcfd97 | 187 | |
84b46c35 | 188 | wxFileStream::wxFileStream(const wxString& fileName) |
f6bcfd97 | 189 | : wxFileInputStream(fileName) |
84b46c35 | 190 | { |
f6bcfd97 | 191 | wxFileOutputStream::m_file = wxFileInputStream::m_file; |
84b46c35 | 192 | } |
ce4169a4 | 193 | |
65045edd RR |
194 | // ---------------------------------------------------------------------------- |
195 | // wxFFileInputStream | |
196 | // ---------------------------------------------------------------------------- | |
197 | ||
198 | wxFFileInputStream::wxFFileInputStream(const wxString& fileName) | |
199 | : wxInputStream() | |
200 | { | |
f68c6b52 | 201 | m_file = new wxFFile(fileName, _T("rb")); |
cab1a605 | 202 | m_file_destroy = true; |
65045edd RR |
203 | } |
204 | ||
205 | wxFFileInputStream::wxFFileInputStream() | |
206 | : wxInputStream() | |
207 | { | |
cab1a605 | 208 | m_file_destroy = false; |
f6bcfd97 | 209 | m_file = NULL; |
65045edd RR |
210 | } |
211 | ||
212 | wxFFileInputStream::wxFFileInputStream(wxFFile& file) | |
213 | { | |
f6bcfd97 | 214 | m_file = &file; |
cab1a605 | 215 | m_file_destroy = false; |
65045edd RR |
216 | } |
217 | ||
218 | wxFFileInputStream::wxFFileInputStream(FILE *file) | |
219 | { | |
f6bcfd97 | 220 | m_file = new wxFFile(file); |
cab1a605 | 221 | m_file_destroy = true; |
65045edd RR |
222 | } |
223 | ||
224 | wxFFileInputStream::~wxFFileInputStream() | |
225 | { | |
f6bcfd97 BP |
226 | if (m_file_destroy) |
227 | delete m_file; | |
65045edd RR |
228 | } |
229 | ||
588066b7 | 230 | wxFileOffset wxFFileInputStream::GetLength() const |
65045edd | 231 | { |
f6bcfd97 | 232 | return m_file->Length(); |
65045edd RR |
233 | } |
234 | ||
235 | size_t wxFFileInputStream::OnSysRead(void *buffer, size_t size) | |
236 | { | |
f8a586e0 | 237 | ssize_t ret = m_file->Read(buffer, size); |
65045edd | 238 | |
b9d84e4c DE |
239 | // It is not safe to call Eof() if the file is not opened. |
240 | if (!m_file->IsOpened() || m_file->Eof()) | |
2b5f62a0 | 241 | m_lasterror = wxSTREAM_EOF; |
f8a586e0 | 242 | if (ret == wxInvalidOffset) |
f6bcfd97 | 243 | { |
2b5f62a0 | 244 | m_lasterror = wxSTREAM_READ_ERROR; |
f6bcfd97 BP |
245 | ret = 0; |
246 | } | |
65045edd | 247 | |
f6bcfd97 | 248 | return ret; |
65045edd RR |
249 | } |
250 | ||
4004775e | 251 | wxFileOffset wxFFileInputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) |
65045edd | 252 | { |
b7fafe6a JJ |
253 | #ifdef __VMS |
254 | #pragma message disable intsignchange | |
255 | #endif | |
0a1390c7 | 256 | wxASSERT_MSG( pos >= LONG_MIN && pos <= LONG_MAX, _T("no huge wxFFile support") ); |
f9df3f05 | 257 | return ( m_file->Seek((long)pos, mode) ? (wxFileOffset)m_file->Tell() : wxInvalidOffset ); |
b7fafe6a JJ |
258 | #ifdef __VMS |
259 | #pragma message enable intsignchange | |
260 | #endif | |
65045edd RR |
261 | } |
262 | ||
4004775e | 263 | wxFileOffset wxFFileInputStream::OnSysTell() const |
65045edd | 264 | { |
f6bcfd97 | 265 | return m_file->Tell(); |
65045edd RR |
266 | } |
267 | ||
268 | // ---------------------------------------------------------------------------- | |
269 | // wxFFileOutputStream | |
270 | // ---------------------------------------------------------------------------- | |
271 | ||
272 | wxFFileOutputStream::wxFFileOutputStream(const wxString& fileName) | |
273 | { | |
f68c6b52 | 274 | m_file = new wxFFile(fileName, _T("w+b")); |
cab1a605 | 275 | m_file_destroy = true; |
9a26db9e | 276 | |
942bef71 RR |
277 | if (!m_file->IsOpened()) |
278 | { | |
279 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
280 | } | |
281 | else | |
282 | { | |
283 | if (m_file->Error()) | |
284 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
285 | } | |
65045edd RR |
286 | } |
287 | ||
288 | wxFFileOutputStream::wxFFileOutputStream(wxFFile& file) | |
289 | { | |
f6bcfd97 | 290 | m_file = &file; |
cab1a605 | 291 | m_file_destroy = false; |
65045edd RR |
292 | } |
293 | ||
294 | wxFFileOutputStream::wxFFileOutputStream() | |
295 | : wxOutputStream() | |
296 | { | |
cab1a605 | 297 | m_file_destroy = false; |
f6bcfd97 | 298 | m_file = NULL; |
65045edd RR |
299 | } |
300 | ||
301 | wxFFileOutputStream::wxFFileOutputStream(FILE *file) | |
302 | { | |
f6bcfd97 | 303 | m_file = new wxFFile(file); |
cab1a605 | 304 | m_file_destroy = true; |
65045edd RR |
305 | } |
306 | ||
307 | wxFFileOutputStream::~wxFFileOutputStream() | |
308 | { | |
9a26db9e | 309 | if (m_file_destroy) |
f6bcfd97 BP |
310 | { |
311 | Sync(); | |
312 | delete m_file; | |
313 | } | |
65045edd RR |
314 | } |
315 | ||
316 | size_t wxFFileOutputStream::OnSysWrite(const void *buffer, size_t size) | |
317 | { | |
f6bcfd97 | 318 | size_t ret = m_file->Write(buffer, size); |
b9d84e4c DE |
319 | // It is not safe to call Error() if the file is not opened. |
320 | if (!m_file->IsOpened() || m_file->Error()) | |
2b5f62a0 | 321 | m_lasterror = wxSTREAM_WRITE_ERROR; |
f6bcfd97 | 322 | else |
2b5f62a0 | 323 | m_lasterror = wxSTREAM_NO_ERROR; |
f6bcfd97 | 324 | return ret; |
65045edd RR |
325 | } |
326 | ||
4004775e | 327 | wxFileOffset wxFFileOutputStream::OnSysTell() const |
65045edd | 328 | { |
f6bcfd97 | 329 | return m_file->Tell(); |
65045edd RR |
330 | } |
331 | ||
4004775e | 332 | wxFileOffset wxFFileOutputStream::OnSysSeek(wxFileOffset pos, wxSeekMode mode) |
65045edd | 333 | { |
b7fafe6a JJ |
334 | #ifdef __VMS |
335 | #pragma message disable intsignchange | |
336 | #endif | |
0a1390c7 | 337 | wxASSERT_MSG( pos >= LONG_MIN && pos <= LONG_MAX, _T("no huge wxFFile support") ); |
f9df3f05 | 338 | return ( m_file->Seek((long)pos, mode) ? (wxFileOffset)m_file->Tell() : wxInvalidOffset ); |
b7fafe6a JJ |
339 | #ifdef __VMS |
340 | #pragma message enable intsignchange | |
341 | #endif | |
65045edd RR |
342 | } |
343 | ||
344 | void wxFFileOutputStream::Sync() | |
345 | { | |
f6bcfd97 BP |
346 | wxOutputStream::Sync(); |
347 | m_file->Flush(); | |
65045edd RR |
348 | } |
349 | ||
588066b7 | 350 | wxFileOffset wxFFileOutputStream::GetLength() const |
65045edd | 351 | { |
f6bcfd97 | 352 | return m_file->Length(); |
65045edd RR |
353 | } |
354 | ||
355 | // ---------------------------------------------------------------------------- | |
356 | // wxFFileStream | |
357 | // ---------------------------------------------------------------------------- | |
f6bcfd97 | 358 | |
65045edd | 359 | wxFFileStream::wxFFileStream(const wxString& fileName) |
f6bcfd97 | 360 | : wxFFileInputStream(fileName) |
65045edd | 361 | { |
f6bcfd97 | 362 | wxFFileOutputStream::m_file = wxFFileInputStream::m_file; |
65045edd | 363 | } |
f6bcfd97 | 364 | |
ce4169a4 RR |
365 | #endif |
366 | // wxUSE_STREAMS && wxUSE_FILE | |
cc985fac | 367 |