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