]>
Commit | Line | Data |
---|---|---|
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__ | |
13 | #pragma implementation "wfstream.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #if wxUSE_STREAMS && wxUSE_FILE | |
24 | ||
25 | #include <stdio.h> | |
26 | #include "wx/stream.h" | |
27 | #include "wx/wfstream.h" | |
28 | ||
29 | // ---------------------------------------------------------------------------- | |
30 | // wxFileInputStream | |
31 | // ---------------------------------------------------------------------------- | |
32 | ||
33 | wxFileInputStream::wxFileInputStream(const wxString& fileName) | |
34 | : wxInputStream() | |
35 | { | |
36 | m_file = new wxFile(fileName, wxFile::read); | |
37 | m_file_destroy = TRUE; | |
38 | } | |
39 | ||
40 | wxFileInputStream::wxFileInputStream() | |
41 | : wxInputStream() | |
42 | { | |
43 | m_file_destroy = FALSE; | |
44 | m_file = NULL; | |
45 | } | |
46 | ||
47 | wxFileInputStream::wxFileInputStream(wxFile& file) | |
48 | { | |
49 | m_file = &file; | |
50 | m_file_destroy = FALSE; | |
51 | } | |
52 | ||
53 | wxFileInputStream::wxFileInputStream(int fd) | |
54 | { | |
55 | m_file = new wxFile(fd); | |
56 | m_file_destroy = TRUE; | |
57 | } | |
58 | ||
59 | wxFileInputStream::~wxFileInputStream() | |
60 | { | |
61 | if (m_file_destroy) | |
62 | delete m_file; | |
63 | } | |
64 | ||
65 | size_t wxFileInputStream::GetSize() const | |
66 | { | |
67 | return m_file->Length(); | |
68 | } | |
69 | ||
70 | size_t wxFileInputStream::OnSysRead(void *buffer, size_t size) | |
71 | { | |
72 | off_t ret = m_file->Read(buffer, size); | |
73 | ||
74 | // NB: we can't use a switch here because HP-UX CC doesn't allow | |
75 | // switching over long long (which off_t is in 64bit mode) | |
76 | ||
77 | if ( !ret ) | |
78 | { | |
79 | // nothing read, so nothing more to read | |
80 | m_lasterror = wxSTREAM_EOF; | |
81 | } | |
82 | else if ( ret == wxInvalidOffset ) | |
83 | { | |
84 | m_lasterror = wxSTREAM_READ_ERROR; | |
85 | ret = 0; | |
86 | } | |
87 | else | |
88 | { | |
89 | // normal case | |
90 | m_lasterror = wxSTREAM_NO_ERROR; | |
91 | } | |
92 | ||
93 | return ret; | |
94 | } | |
95 | ||
96 | off_t wxFileInputStream::OnSysSeek(off_t pos, wxSeekMode mode) | |
97 | { | |
98 | return m_file->Seek(pos, mode); | |
99 | } | |
100 | ||
101 | off_t wxFileInputStream::OnSysTell() const | |
102 | { | |
103 | return m_file->Tell(); | |
104 | } | |
105 | ||
106 | // ---------------------------------------------------------------------------- | |
107 | // wxFileOutputStream | |
108 | // ---------------------------------------------------------------------------- | |
109 | ||
110 | wxFileOutputStream::wxFileOutputStream(const wxString& fileName) | |
111 | { | |
112 | m_file = new wxFile(fileName, wxFile::write); | |
113 | m_file_destroy = TRUE; | |
114 | ||
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 | } | |
124 | } | |
125 | ||
126 | wxFileOutputStream::wxFileOutputStream(wxFile& file) | |
127 | { | |
128 | m_file = &file; | |
129 | m_file_destroy = FALSE; | |
130 | } | |
131 | ||
132 | wxFileOutputStream::wxFileOutputStream() | |
133 | : wxOutputStream() | |
134 | { | |
135 | m_file_destroy = FALSE; | |
136 | m_file = NULL; | |
137 | } | |
138 | ||
139 | wxFileOutputStream::wxFileOutputStream(int fd) | |
140 | { | |
141 | m_file = new wxFile(fd); | |
142 | m_file_destroy = TRUE; | |
143 | } | |
144 | ||
145 | wxFileOutputStream::~wxFileOutputStream() | |
146 | { | |
147 | if (m_file_destroy) | |
148 | { | |
149 | Sync(); | |
150 | delete m_file; | |
151 | } | |
152 | } | |
153 | ||
154 | size_t wxFileOutputStream::OnSysWrite(const void *buffer, size_t size) | |
155 | { | |
156 | size_t ret = m_file->Write(buffer, size); | |
157 | ||
158 | m_lasterror = m_file->Error() ? wxSTREAM_WRITE_ERROR : wxSTREAM_NO_ERROR; | |
159 | ||
160 | return ret; | |
161 | } | |
162 | ||
163 | off_t wxFileOutputStream::OnSysTell() const | |
164 | { | |
165 | return m_file->Tell(); | |
166 | } | |
167 | ||
168 | off_t wxFileOutputStream::OnSysSeek(off_t pos, wxSeekMode mode) | |
169 | { | |
170 | return m_file->Seek(pos, mode); | |
171 | } | |
172 | ||
173 | void wxFileOutputStream::Sync() | |
174 | { | |
175 | wxOutputStream::Sync(); | |
176 | m_file->Flush(); | |
177 | } | |
178 | ||
179 | size_t wxFileOutputStream::GetSize() const | |
180 | { | |
181 | return m_file->Length(); | |
182 | } | |
183 | ||
184 | // ---------------------------------------------------------------------------- | |
185 | // wxFileStream | |
186 | // ---------------------------------------------------------------------------- | |
187 | ||
188 | wxFileStream::wxFileStream(const wxString& fileName) | |
189 | : wxFileInputStream(fileName) | |
190 | { | |
191 | wxFileOutputStream::m_file = wxFileInputStream::m_file; | |
192 | } | |
193 | ||
194 | // ---------------------------------------------------------------------------- | |
195 | // wxFFileInputStream | |
196 | // ---------------------------------------------------------------------------- | |
197 | ||
198 | wxFFileInputStream::wxFFileInputStream(const wxString& fileName) | |
199 | : wxInputStream() | |
200 | { | |
201 | m_file = new wxFFile(fileName, _T("rb")); | |
202 | m_file_destroy = TRUE; | |
203 | } | |
204 | ||
205 | wxFFileInputStream::wxFFileInputStream() | |
206 | : wxInputStream() | |
207 | { | |
208 | m_file_destroy = FALSE; | |
209 | m_file = NULL; | |
210 | } | |
211 | ||
212 | wxFFileInputStream::wxFFileInputStream(wxFFile& file) | |
213 | { | |
214 | m_file = &file; | |
215 | m_file_destroy = FALSE; | |
216 | } | |
217 | ||
218 | wxFFileInputStream::wxFFileInputStream(FILE *file) | |
219 | { | |
220 | m_file = new wxFFile(file); | |
221 | m_file_destroy = TRUE; | |
222 | } | |
223 | ||
224 | wxFFileInputStream::~wxFFileInputStream() | |
225 | { | |
226 | if (m_file_destroy) | |
227 | delete m_file; | |
228 | } | |
229 | ||
230 | size_t wxFFileInputStream::GetSize() const | |
231 | { | |
232 | return m_file->Length(); | |
233 | } | |
234 | ||
235 | size_t wxFFileInputStream::OnSysRead(void *buffer, size_t size) | |
236 | { | |
237 | off_t ret; | |
238 | ||
239 | ret = m_file->Read(buffer, size); | |
240 | ||
241 | if (m_file->Eof()) | |
242 | m_lasterror = wxSTREAM_EOF; | |
243 | if (ret == wxInvalidOffset) | |
244 | { | |
245 | m_lasterror = wxSTREAM_READ_ERROR; | |
246 | ret = 0; | |
247 | } | |
248 | ||
249 | return ret; | |
250 | } | |
251 | ||
252 | off_t wxFFileInputStream::OnSysSeek(off_t pos, wxSeekMode mode) | |
253 | { | |
254 | return ( m_file->Seek(pos, mode) ? pos : wxInvalidOffset ); | |
255 | } | |
256 | ||
257 | off_t wxFFileInputStream::OnSysTell() const | |
258 | { | |
259 | return m_file->Tell(); | |
260 | } | |
261 | ||
262 | // ---------------------------------------------------------------------------- | |
263 | // wxFFileOutputStream | |
264 | // ---------------------------------------------------------------------------- | |
265 | ||
266 | wxFFileOutputStream::wxFFileOutputStream(const wxString& fileName) | |
267 | { | |
268 | m_file = new wxFFile(fileName, _T("w+b")); | |
269 | m_file_destroy = TRUE; | |
270 | ||
271 | if (!m_file->IsOpened()) | |
272 | { | |
273 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
274 | } | |
275 | else | |
276 | { | |
277 | if (m_file->Error()) | |
278 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
279 | } | |
280 | } | |
281 | ||
282 | wxFFileOutputStream::wxFFileOutputStream(wxFFile& file) | |
283 | { | |
284 | m_file = &file; | |
285 | m_file_destroy = FALSE; | |
286 | } | |
287 | ||
288 | wxFFileOutputStream::wxFFileOutputStream() | |
289 | : wxOutputStream() | |
290 | { | |
291 | m_file_destroy = FALSE; | |
292 | m_file = NULL; | |
293 | } | |
294 | ||
295 | wxFFileOutputStream::wxFFileOutputStream(FILE *file) | |
296 | { | |
297 | m_file = new wxFFile(file); | |
298 | m_file_destroy = TRUE; | |
299 | } | |
300 | ||
301 | wxFFileOutputStream::~wxFFileOutputStream() | |
302 | { | |
303 | if (m_file_destroy) | |
304 | { | |
305 | Sync(); | |
306 | delete m_file; | |
307 | } | |
308 | } | |
309 | ||
310 | size_t wxFFileOutputStream::OnSysWrite(const void *buffer, size_t size) | |
311 | { | |
312 | size_t ret = m_file->Write(buffer, size); | |
313 | if (m_file->Error()) | |
314 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
315 | else | |
316 | m_lasterror = wxSTREAM_NO_ERROR; | |
317 | return ret; | |
318 | } | |
319 | ||
320 | off_t wxFFileOutputStream::OnSysTell() const | |
321 | { | |
322 | return m_file->Tell(); | |
323 | } | |
324 | ||
325 | off_t wxFFileOutputStream::OnSysSeek(off_t pos, wxSeekMode mode) | |
326 | { | |
327 | return ( m_file->Seek(pos, mode) ? pos : wxInvalidOffset ); | |
328 | } | |
329 | ||
330 | void wxFFileOutputStream::Sync() | |
331 | { | |
332 | wxOutputStream::Sync(); | |
333 | m_file->Flush(); | |
334 | } | |
335 | ||
336 | size_t wxFFileOutputStream::GetSize() const | |
337 | { | |
338 | return m_file->Length(); | |
339 | } | |
340 | ||
341 | // ---------------------------------------------------------------------------- | |
342 | // wxFFileStream | |
343 | // ---------------------------------------------------------------------------- | |
344 | ||
345 | wxFFileStream::wxFFileStream(const wxString& fileName) | |
346 | : wxFFileInputStream(fileName) | |
347 | { | |
348 | wxFFileOutputStream::m_file = wxFFileInputStream::m_file; | |
349 | } | |
350 | ||
351 | #endif | |
352 | // wxUSE_STREAMS && wxUSE_FILE | |
353 |