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