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