]>
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 |
3d4c6a21 GL |
9 | // Licence: wxWindows license |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
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 BP |
36 | m_file = new wxFile(fileName, wxFile::read); |
37 | m_file_destroy = TRUE; | |
3d4c6a21 GL |
38 | } |
39 | ||
25c70b07 GL |
40 | wxFileInputStream::wxFileInputStream() |
41 | : wxInputStream() | |
42 | { | |
f6bcfd97 BP |
43 | m_file_destroy = FALSE; |
44 | m_file = NULL; | |
25c70b07 GL |
45 | } |
46 | ||
0aca1ded GL |
47 | wxFileInputStream::wxFileInputStream(wxFile& file) |
48 | { | |
f6bcfd97 BP |
49 | m_file = &file; |
50 | m_file_destroy = FALSE; | |
0aca1ded GL |
51 | } |
52 | ||
53 | wxFileInputStream::wxFileInputStream(int fd) | |
54 | { | |
f6bcfd97 BP |
55 | m_file = new wxFile(fd); |
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 | ||
cd25b18c | 65 | size_t wxFileInputStream::GetSize() 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 | { |
9a26db9e | 72 | off_t ret = m_file->Read(buffer, size); |
fae05df5 | 73 | |
9a26db9e | 74 | switch ( ret ) |
f6bcfd97 | 75 | { |
9a26db9e VZ |
76 | case 0: |
77 | m_lasterror = wxSTREAM_EOF; | |
78 | break; | |
79 | ||
80 | case wxInvalidOffset: | |
81 | m_lasterror = wxSTREAM_READ_ERROR; | |
82 | ret = 0; | |
83 | break; | |
84 | ||
85 | default: | |
86 | m_lasterror = wxSTREAM_NO_ERROR; | |
f6bcfd97 | 87 | } |
fae05df5 | 88 | |
f6bcfd97 | 89 | return ret; |
6d44bf31 GL |
90 | } |
91 | ||
75ed1d15 | 92 | off_t wxFileInputStream::OnSysSeek(off_t pos, wxSeekMode mode) |
3d4c6a21 | 93 | { |
9a26db9e | 94 | return m_file->Seek(pos, mode); |
79c3e0e1 GL |
95 | } |
96 | ||
75ed1d15 | 97 | off_t wxFileInputStream::OnSysTell() const |
79c3e0e1 | 98 | { |
f6bcfd97 | 99 | return m_file->Tell(); |
79c3e0e1 GL |
100 | } |
101 | ||
102 | // ---------------------------------------------------------------------------- | |
103 | // wxFileOutputStream | |
104 | // ---------------------------------------------------------------------------- | |
105 | ||
106 | wxFileOutputStream::wxFileOutputStream(const wxString& fileName) | |
25c70b07 | 107 | { |
f6bcfd97 BP |
108 | m_file = new wxFile(fileName, wxFile::write); |
109 | m_file_destroy = TRUE; | |
9a26db9e | 110 | |
942bef71 RR |
111 | if (!m_file->IsOpened()) |
112 | { | |
113 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
114 | } | |
115 | else | |
116 | { | |
117 | if (m_file->Error()) | |
118 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
119 | } | |
25c70b07 GL |
120 | } |
121 | ||
84b46c35 GL |
122 | wxFileOutputStream::wxFileOutputStream(wxFile& file) |
123 | { | |
f6bcfd97 BP |
124 | m_file = &file; |
125 | m_file_destroy = FALSE; | |
84b46c35 GL |
126 | } |
127 | ||
25c70b07 | 128 | wxFileOutputStream::wxFileOutputStream() |
9a26db9e | 129 | : wxOutputStream() |
79c3e0e1 | 130 | { |
f6bcfd97 BP |
131 | m_file_destroy = FALSE; |
132 | m_file = NULL; | |
3d4c6a21 GL |
133 | } |
134 | ||
0aca1ded GL |
135 | wxFileOutputStream::wxFileOutputStream(int fd) |
136 | { | |
f6bcfd97 BP |
137 | m_file = new wxFile(fd); |
138 | m_file_destroy = TRUE; | |
0aca1ded GL |
139 | } |
140 | ||
79c3e0e1 | 141 | wxFileOutputStream::~wxFileOutputStream() |
3d4c6a21 | 142 | { |
9a26db9e | 143 | if (m_file_destroy) |
f6bcfd97 BP |
144 | { |
145 | Sync(); | |
146 | delete m_file; | |
147 | } | |
79c3e0e1 | 148 | } |
3d4c6a21 | 149 | |
75ed1d15 | 150 | size_t wxFileOutputStream::OnSysWrite(const void *buffer, size_t size) |
79c3e0e1 | 151 | { |
f6bcfd97 | 152 | size_t ret = m_file->Write(buffer, size); |
9a26db9e VZ |
153 | |
154 | m_lasterror = m_file->Error() ? wxSTREAM_WRITE_ERROR : wxSTREAM_NO_ERROR; | |
155 | ||
f6bcfd97 | 156 | return ret; |
79c3e0e1 | 157 | } |
3d4c6a21 | 158 | |
75ed1d15 | 159 | off_t wxFileOutputStream::OnSysTell() const |
79c3e0e1 | 160 | { |
f6bcfd97 | 161 | return m_file->Tell(); |
3d4c6a21 GL |
162 | } |
163 | ||
75ed1d15 | 164 | off_t wxFileOutputStream::OnSysSeek(off_t pos, wxSeekMode mode) |
3d4c6a21 | 165 | { |
f6bcfd97 | 166 | return m_file->Seek(pos, mode); |
3d4c6a21 GL |
167 | } |
168 | ||
79c3e0e1 | 169 | void wxFileOutputStream::Sync() |
3d4c6a21 | 170 | { |
f6bcfd97 BP |
171 | wxOutputStream::Sync(); |
172 | m_file->Flush(); | |
3d4c6a21 | 173 | } |
84b46c35 | 174 | |
cd25b18c | 175 | size_t wxFileOutputStream::GetSize() const |
84b46c35 | 176 | { |
f6bcfd97 | 177 | return m_file->Length(); |
84b46c35 GL |
178 | } |
179 | ||
180 | // ---------------------------------------------------------------------------- | |
181 | // wxFileStream | |
182 | // ---------------------------------------------------------------------------- | |
f6bcfd97 | 183 | |
84b46c35 | 184 | wxFileStream::wxFileStream(const wxString& fileName) |
f6bcfd97 | 185 | : wxFileInputStream(fileName) |
84b46c35 | 186 | { |
f6bcfd97 | 187 | wxFileOutputStream::m_file = wxFileInputStream::m_file; |
84b46c35 | 188 | } |
ce4169a4 | 189 | |
65045edd RR |
190 | // ---------------------------------------------------------------------------- |
191 | // wxFFileInputStream | |
192 | // ---------------------------------------------------------------------------- | |
193 | ||
194 | wxFFileInputStream::wxFFileInputStream(const wxString& fileName) | |
195 | : wxInputStream() | |
196 | { | |
f6bcfd97 BP |
197 | m_file = new wxFFile(fileName, "rb"); |
198 | m_file_destroy = TRUE; | |
65045edd RR |
199 | } |
200 | ||
201 | wxFFileInputStream::wxFFileInputStream() | |
202 | : wxInputStream() | |
203 | { | |
f6bcfd97 BP |
204 | m_file_destroy = FALSE; |
205 | m_file = NULL; | |
65045edd RR |
206 | } |
207 | ||
208 | wxFFileInputStream::wxFFileInputStream(wxFFile& file) | |
209 | { | |
f6bcfd97 BP |
210 | m_file = &file; |
211 | m_file_destroy = FALSE; | |
65045edd RR |
212 | } |
213 | ||
214 | wxFFileInputStream::wxFFileInputStream(FILE *file) | |
215 | { | |
f6bcfd97 BP |
216 | m_file = new wxFFile(file); |
217 | m_file_destroy = TRUE; | |
65045edd RR |
218 | } |
219 | ||
220 | wxFFileInputStream::~wxFFileInputStream() | |
221 | { | |
f6bcfd97 BP |
222 | if (m_file_destroy) |
223 | delete m_file; | |
65045edd RR |
224 | } |
225 | ||
226 | size_t wxFFileInputStream::GetSize() const | |
227 | { | |
f6bcfd97 | 228 | return m_file->Length(); |
65045edd RR |
229 | } |
230 | ||
231 | size_t wxFFileInputStream::OnSysRead(void *buffer, size_t size) | |
232 | { | |
f6bcfd97 | 233 | off_t ret; |
65045edd | 234 | |
f6bcfd97 | 235 | ret = m_file->Read(buffer, size); |
65045edd | 236 | |
f6bcfd97 | 237 | if (m_file->Eof()) |
2b5f62a0 | 238 | m_lasterror = wxSTREAM_EOF; |
9a26db9e | 239 | if (ret == wxInvalidOffset) |
f6bcfd97 | 240 | { |
2b5f62a0 | 241 | m_lasterror = wxSTREAM_READ_ERROR; |
f6bcfd97 BP |
242 | ret = 0; |
243 | } | |
65045edd | 244 | |
f6bcfd97 | 245 | return ret; |
65045edd RR |
246 | } |
247 | ||
248 | off_t wxFFileInputStream::OnSysSeek(off_t pos, wxSeekMode mode) | |
249 | { | |
06acc7d9 | 250 | return ( m_file->Seek(pos, mode) ? pos : wxInvalidOffset ); |
65045edd RR |
251 | } |
252 | ||
253 | off_t wxFFileInputStream::OnSysTell() const | |
254 | { | |
f6bcfd97 | 255 | return m_file->Tell(); |
65045edd RR |
256 | } |
257 | ||
258 | // ---------------------------------------------------------------------------- | |
259 | // wxFFileOutputStream | |
260 | // ---------------------------------------------------------------------------- | |
261 | ||
262 | wxFFileOutputStream::wxFFileOutputStream(const wxString& fileName) | |
263 | { | |
f6bcfd97 BP |
264 | m_file = new wxFFile(fileName, "w+b"); |
265 | m_file_destroy = TRUE; | |
9a26db9e | 266 | |
942bef71 RR |
267 | if (!m_file->IsOpened()) |
268 | { | |
269 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
270 | } | |
271 | else | |
272 | { | |
273 | if (m_file->Error()) | |
274 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
275 | } | |
65045edd RR |
276 | } |
277 | ||
278 | wxFFileOutputStream::wxFFileOutputStream(wxFFile& file) | |
279 | { | |
f6bcfd97 BP |
280 | m_file = &file; |
281 | m_file_destroy = FALSE; | |
65045edd RR |
282 | } |
283 | ||
284 | wxFFileOutputStream::wxFFileOutputStream() | |
285 | : wxOutputStream() | |
286 | { | |
f6bcfd97 BP |
287 | m_file_destroy = FALSE; |
288 | m_file = NULL; | |
65045edd RR |
289 | } |
290 | ||
291 | wxFFileOutputStream::wxFFileOutputStream(FILE *file) | |
292 | { | |
f6bcfd97 BP |
293 | m_file = new wxFFile(file); |
294 | m_file_destroy = TRUE; | |
65045edd RR |
295 | } |
296 | ||
297 | wxFFileOutputStream::~wxFFileOutputStream() | |
298 | { | |
9a26db9e | 299 | if (m_file_destroy) |
f6bcfd97 BP |
300 | { |
301 | Sync(); | |
302 | delete m_file; | |
303 | } | |
65045edd RR |
304 | } |
305 | ||
306 | size_t wxFFileOutputStream::OnSysWrite(const void *buffer, size_t size) | |
307 | { | |
f6bcfd97 BP |
308 | size_t ret = m_file->Write(buffer, size); |
309 | if (m_file->Error()) | |
2b5f62a0 | 310 | m_lasterror = wxSTREAM_WRITE_ERROR; |
f6bcfd97 | 311 | else |
2b5f62a0 | 312 | m_lasterror = wxSTREAM_NO_ERROR; |
f6bcfd97 | 313 | return ret; |
65045edd RR |
314 | } |
315 | ||
316 | off_t wxFFileOutputStream::OnSysTell() const | |
317 | { | |
f6bcfd97 | 318 | return m_file->Tell(); |
65045edd RR |
319 | } |
320 | ||
321 | off_t wxFFileOutputStream::OnSysSeek(off_t pos, wxSeekMode mode) | |
322 | { | |
06acc7d9 | 323 | return ( m_file->Seek(pos, mode) ? pos : wxInvalidOffset ); |
65045edd RR |
324 | } |
325 | ||
326 | void wxFFileOutputStream::Sync() | |
327 | { | |
f6bcfd97 BP |
328 | wxOutputStream::Sync(); |
329 | m_file->Flush(); | |
65045edd RR |
330 | } |
331 | ||
332 | size_t wxFFileOutputStream::GetSize() const | |
333 | { | |
f6bcfd97 | 334 | return m_file->Length(); |
65045edd RR |
335 | } |
336 | ||
337 | // ---------------------------------------------------------------------------- | |
338 | // wxFFileStream | |
339 | // ---------------------------------------------------------------------------- | |
f6bcfd97 | 340 | |
65045edd | 341 | wxFFileStream::wxFFileStream(const wxString& fileName) |
f6bcfd97 | 342 | : wxFFileInputStream(fileName) |
65045edd | 343 | { |
f6bcfd97 | 344 | wxFFileOutputStream::m_file = wxFFileInputStream::m_file; |
65045edd | 345 | } |
f6bcfd97 | 346 | |
ce4169a4 RR |
347 | #endif |
348 | // wxUSE_STREAMS && wxUSE_FILE | |
cc985fac | 349 |