]>
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 licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 wxFileOffset
wxFileInputStream::GetLength() const
67 return m_file
->Length();
70 size_t wxFileInputStream::OnSysRead(void *buffer
, size_t size
)
72 ssize_t ret
= m_file
->Read(buffer
, size
);
74 // NB: we can't use a switch here because HP-UX CC doesn't allow
75 // switching over long long (which size_t is in 64bit mode)
79 // nothing read, so nothing more to read
80 m_lasterror
= wxSTREAM_EOF
;
82 else if ( ret
== wxInvalidOffset
)
84 m_lasterror
= wxSTREAM_READ_ERROR
;
90 m_lasterror
= wxSTREAM_NO_ERROR
;
96 wxFileOffset
wxFileInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
98 return m_file
->Seek(pos
, mode
);
101 wxFileOffset
wxFileInputStream::OnSysTell() const
103 return m_file
->Tell();
106 // ----------------------------------------------------------------------------
107 // wxFileOutputStream
108 // ----------------------------------------------------------------------------
110 wxFileOutputStream::wxFileOutputStream(const wxString
& fileName
)
112 m_file
= new wxFile(fileName
, wxFile::write
);
113 m_file_destroy
= true;
115 if (!m_file
->IsOpened())
117 m_lasterror
= wxSTREAM_WRITE_ERROR
;
122 m_lasterror
= wxSTREAM_WRITE_ERROR
;
126 wxFileOutputStream::wxFileOutputStream(wxFile
& file
)
129 m_file_destroy
= false;
132 wxFileOutputStream::wxFileOutputStream()
135 m_file_destroy
= false;
139 wxFileOutputStream::wxFileOutputStream(int fd
)
141 m_file
= new wxFile(fd
);
142 m_file_destroy
= true;
145 wxFileOutputStream::~wxFileOutputStream()
154 size_t wxFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
156 size_t ret
= m_file
->Write(buffer
, size
);
158 m_lasterror
= m_file
->Error() ? wxSTREAM_WRITE_ERROR
: wxSTREAM_NO_ERROR
;
163 wxFileOffset
wxFileOutputStream::OnSysTell() const
165 return m_file
->Tell();
168 wxFileOffset
wxFileOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
170 return m_file
->Seek(pos
, mode
);
173 void wxFileOutputStream::Sync()
175 wxOutputStream::Sync();
179 wxFileOffset
wxFileOutputStream::GetLength() const
181 return m_file
->Length();
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
188 wxFileStream::wxFileStream(const wxString
& fileName
)
189 : wxFileInputStream(fileName
)
191 wxFileOutputStream::m_file
= wxFileInputStream::m_file
;
194 // ----------------------------------------------------------------------------
195 // wxFFileInputStream
196 // ----------------------------------------------------------------------------
198 wxFFileInputStream::wxFFileInputStream(const wxString
& fileName
,
202 m_file
= new wxFFile(fileName
, mode
);
203 m_file_destroy
= true;
206 wxFFileInputStream::wxFFileInputStream()
210 m_file_destroy
= false;
213 wxFFileInputStream::wxFFileInputStream(wxFFile
& file
)
216 m_file_destroy
= false;
219 wxFFileInputStream::wxFFileInputStream(FILE *file
)
221 m_file
= new wxFFile(file
);
222 m_file_destroy
= true;
225 wxFFileInputStream::~wxFFileInputStream()
231 wxFileOffset
wxFFileInputStream::GetLength() const
233 return m_file
->Length();
236 size_t wxFFileInputStream::OnSysRead(void *buffer
, size_t size
)
238 ssize_t ret
= m_file
->Read(buffer
, size
);
240 // It is not safe to call Eof() if the file is not opened.
241 if (!m_file
->IsOpened() || m_file
->Eof())
242 m_lasterror
= wxSTREAM_EOF
;
243 if (ret
== wxInvalidOffset
)
245 m_lasterror
= wxSTREAM_READ_ERROR
;
252 wxFileOffset
wxFFileInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
254 return m_file
->Seek(pos
, mode
) ? m_file
->Tell() : wxInvalidOffset
;
257 wxFileOffset
wxFFileInputStream::OnSysTell() const
259 return m_file
->Tell();
262 // ----------------------------------------------------------------------------
263 // wxFFileOutputStream
264 // ----------------------------------------------------------------------------
266 wxFFileOutputStream::wxFFileOutputStream(const wxString
& fileName
,
269 m_file
= new wxFFile(fileName
, mode
);
270 m_file_destroy
= true;
272 if (!m_file
->IsOpened())
274 m_lasterror
= wxSTREAM_WRITE_ERROR
;
279 m_lasterror
= wxSTREAM_WRITE_ERROR
;
283 wxFFileOutputStream::wxFFileOutputStream(wxFFile
& file
)
286 m_file_destroy
= false;
289 wxFFileOutputStream::wxFFileOutputStream()
293 m_file_destroy
= false;
296 wxFFileOutputStream::wxFFileOutputStream(FILE *file
)
298 m_file
= new wxFFile(file
);
299 m_file_destroy
= true;
302 wxFFileOutputStream::~wxFFileOutputStream()
311 size_t wxFFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
313 size_t ret
= m_file
->Write(buffer
, size
);
314 // It is not safe to call Error() if the file is not opened.
315 if (!m_file
->IsOpened() || m_file
->Error())
316 m_lasterror
= wxSTREAM_WRITE_ERROR
;
318 m_lasterror
= wxSTREAM_NO_ERROR
;
322 wxFileOffset
wxFFileOutputStream::OnSysTell() const
324 return m_file
->Tell();
327 wxFileOffset
wxFFileOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
329 return m_file
->Seek(pos
, mode
) ? m_file
->Tell() : wxInvalidOffset
;
332 void wxFFileOutputStream::Sync()
334 wxOutputStream::Sync();
338 wxFileOffset
wxFFileOutputStream::GetLength() const
340 return m_file
->Length();
343 // ----------------------------------------------------------------------------
345 // ----------------------------------------------------------------------------
347 wxFFileStream::wxFFileStream(const wxString
& fileName
)
348 : wxFFileInputStream(fileName
)
350 wxFFileOutputStream::m_file
= wxFFileInputStream::m_file
;
353 #endif // wxUSE_STREAMS && wxUSE_FILE