]>
git.saurik.com Git - wxWidgets.git/blob - src/common/wfstream.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: "File stream" classes
4 // Author: Julian Smart
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "wfstream.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #if wxUSE_STREAMS && wxUSE_FILE
26 #include "wx/stream.h"
27 #include "wx/wfstream.h"
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 wxFileInputStream::wxFileInputStream(const wxString
& fileName
)
36 m_file
= new wxFile(fileName
, wxFile::read
);
37 m_file_destroy
= TRUE
;
40 wxFileInputStream::wxFileInputStream()
43 m_file_destroy
= FALSE
;
47 wxFileInputStream::wxFileInputStream(wxFile
& file
)
50 m_file_destroy
= FALSE
;
53 wxFileInputStream::wxFileInputStream(int fd
)
55 m_file
= new wxFile(fd
);
56 m_file_destroy
= TRUE
;
59 wxFileInputStream::~wxFileInputStream()
65 size_t wxFileInputStream::GetSize() const
67 return m_file
->Length();
70 size_t wxFileInputStream::OnSysRead(void *buffer
, size_t size
)
72 off_t ret
= m_file
->Read(buffer
, size
);
77 m_lasterror
= wxSTREAM_EOF
;
81 m_lasterror
= wxSTREAM_READ_ERROR
;
86 m_lasterror
= wxSTREAM_NO_ERROR
;
92 off_t
wxFileInputStream::OnSysSeek(off_t pos
, wxSeekMode mode
)
94 return m_file
->Seek(pos
, mode
);
97 off_t
wxFileInputStream::OnSysTell() const
99 return m_file
->Tell();
102 // ----------------------------------------------------------------------------
103 // wxFileOutputStream
104 // ----------------------------------------------------------------------------
106 wxFileOutputStream::wxFileOutputStream(const wxString
& fileName
)
108 m_file
= new wxFile(fileName
, wxFile::write
);
109 m_file_destroy
= TRUE
;
111 if (!m_file
->IsOpened())
113 m_lasterror
= wxSTREAM_WRITE_ERROR
;
118 m_lasterror
= wxSTREAM_WRITE_ERROR
;
122 wxFileOutputStream::wxFileOutputStream(wxFile
& file
)
125 m_file_destroy
= FALSE
;
128 wxFileOutputStream::wxFileOutputStream()
131 m_file_destroy
= FALSE
;
135 wxFileOutputStream::wxFileOutputStream(int fd
)
137 m_file
= new wxFile(fd
);
138 m_file_destroy
= TRUE
;
141 wxFileOutputStream::~wxFileOutputStream()
150 size_t wxFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
152 size_t ret
= m_file
->Write(buffer
, size
);
154 m_lasterror
= m_file
->Error() ? wxSTREAM_WRITE_ERROR
: wxSTREAM_NO_ERROR
;
159 off_t
wxFileOutputStream::OnSysTell() const
161 return m_file
->Tell();
164 off_t
wxFileOutputStream::OnSysSeek(off_t pos
, wxSeekMode mode
)
166 return m_file
->Seek(pos
, mode
);
169 void wxFileOutputStream::Sync()
171 wxOutputStream::Sync();
175 size_t wxFileOutputStream::GetSize() const
177 return m_file
->Length();
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
184 wxFileStream::wxFileStream(const wxString
& fileName
)
185 : wxFileInputStream(fileName
)
187 wxFileOutputStream::m_file
= wxFileInputStream::m_file
;
190 // ----------------------------------------------------------------------------
191 // wxFFileInputStream
192 // ----------------------------------------------------------------------------
194 wxFFileInputStream::wxFFileInputStream(const wxString
& fileName
)
197 m_file
= new wxFFile(fileName
, "rb");
198 m_file_destroy
= TRUE
;
201 wxFFileInputStream::wxFFileInputStream()
204 m_file_destroy
= FALSE
;
208 wxFFileInputStream::wxFFileInputStream(wxFFile
& file
)
211 m_file_destroy
= FALSE
;
214 wxFFileInputStream::wxFFileInputStream(FILE *file
)
216 m_file
= new wxFFile(file
);
217 m_file_destroy
= TRUE
;
220 wxFFileInputStream::~wxFFileInputStream()
226 size_t wxFFileInputStream::GetSize() const
228 return m_file
->Length();
231 size_t wxFFileInputStream::OnSysRead(void *buffer
, size_t size
)
235 ret
= m_file
->Read(buffer
, size
);
238 m_lasterror
= wxSTREAM_EOF
;
239 if (ret
== wxInvalidOffset
)
241 m_lasterror
= wxSTREAM_READ_ERROR
;
248 off_t
wxFFileInputStream::OnSysSeek(off_t pos
, wxSeekMode mode
)
250 return ( m_file
->Seek(pos
, mode
) ? pos
: wxInvalidOffset
);
253 off_t
wxFFileInputStream::OnSysTell() const
255 return m_file
->Tell();
258 // ----------------------------------------------------------------------------
259 // wxFFileOutputStream
260 // ----------------------------------------------------------------------------
262 wxFFileOutputStream::wxFFileOutputStream(const wxString
& fileName
)
264 m_file
= new wxFFile(fileName
, "w+b");
265 m_file_destroy
= TRUE
;
267 if (!m_file
->IsOpened())
269 m_lasterror
= wxSTREAM_WRITE_ERROR
;
274 m_lasterror
= wxSTREAM_WRITE_ERROR
;
278 wxFFileOutputStream::wxFFileOutputStream(wxFFile
& file
)
281 m_file_destroy
= FALSE
;
284 wxFFileOutputStream::wxFFileOutputStream()
287 m_file_destroy
= FALSE
;
291 wxFFileOutputStream::wxFFileOutputStream(FILE *file
)
293 m_file
= new wxFFile(file
);
294 m_file_destroy
= TRUE
;
297 wxFFileOutputStream::~wxFFileOutputStream()
306 size_t wxFFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
308 size_t ret
= m_file
->Write(buffer
, size
);
310 m_lasterror
= wxSTREAM_WRITE_ERROR
;
312 m_lasterror
= wxSTREAM_NO_ERROR
;
316 off_t
wxFFileOutputStream::OnSysTell() const
318 return m_file
->Tell();
321 off_t
wxFFileOutputStream::OnSysSeek(off_t pos
, wxSeekMode mode
)
323 return ( m_file
->Seek(pos
, mode
) ? pos
: wxInvalidOffset
);
326 void wxFFileOutputStream::Sync()
328 wxOutputStream::Sync();
332 size_t wxFFileOutputStream::GetSize() const
334 return m_file
->Length();
337 // ----------------------------------------------------------------------------
339 // ----------------------------------------------------------------------------
341 wxFFileStream::wxFFileStream(const wxString
& fileName
)
342 : wxFFileInputStream(fileName
)
344 wxFFileOutputStream::m_file
= wxFFileInputStream::m_file
;
348 // wxUSE_STREAMS && wxUSE_FILE