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