]>
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"
26 #include "wx/stream.h"
27 #include "wx/wfstream.h"
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 wxFileInputStream::wxFileInputStream(const wxString
& fileName
)
38 m_file
= new wxFile(fileName
, wxFile::read
);
39 m_file_destroy
= true;
40 if ( !m_file
->IsOpened() )
41 m_lasterror
= wxSTREAM_READ_ERROR
;
44 wxFileInputStream::wxFileInputStream()
47 m_file_destroy
= false;
51 wxFileInputStream::wxFileInputStream(wxFile
& file
)
54 m_file_destroy
= false;
57 wxFileInputStream::wxFileInputStream(int fd
)
59 m_file
= new wxFile(fd
);
60 m_file_destroy
= true;
63 wxFileInputStream::~wxFileInputStream()
69 wxFileOffset
wxFileInputStream::GetLength() const
71 return m_file
->Length();
74 size_t wxFileInputStream::OnSysRead(void *buffer
, size_t size
)
76 ssize_t ret
= m_file
->Read(buffer
, size
);
78 // NB: we can't use a switch here because HP-UX CC doesn't allow
79 // switching over long long (which size_t is in 64bit mode)
83 // nothing read, so nothing more to read
84 m_lasterror
= wxSTREAM_EOF
;
86 else if ( ret
== wxInvalidOffset
)
88 m_lasterror
= wxSTREAM_READ_ERROR
;
94 m_lasterror
= wxSTREAM_NO_ERROR
;
100 wxFileOffset
wxFileInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
102 return m_file
->Seek(pos
, mode
);
105 wxFileOffset
wxFileInputStream::OnSysTell() const
107 return m_file
->Tell();
110 // ----------------------------------------------------------------------------
111 // wxFileOutputStream
112 // ----------------------------------------------------------------------------
114 wxFileOutputStream::wxFileOutputStream(const wxString
& fileName
)
116 m_file
= new wxFile(fileName
, wxFile::write
);
117 m_file_destroy
= true;
119 if (!m_file
->IsOpened())
120 m_lasterror
= wxSTREAM_WRITE_ERROR
;
123 wxFileOutputStream::wxFileOutputStream(wxFile
& file
)
126 m_file_destroy
= false;
129 wxFileOutputStream::wxFileOutputStream()
132 m_file_destroy
= false;
136 wxFileOutputStream::wxFileOutputStream(int fd
)
138 m_file
= new wxFile(fd
);
139 m_file_destroy
= true;
142 wxFileOutputStream::~wxFileOutputStream()
151 size_t wxFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
153 size_t ret
= m_file
->Write(buffer
, size
);
155 m_lasterror
= m_file
->Error() ? wxSTREAM_WRITE_ERROR
: wxSTREAM_NO_ERROR
;
160 wxFileOffset
wxFileOutputStream::OnSysTell() const
162 return m_file
->Tell();
165 wxFileOffset
wxFileOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
167 return m_file
->Seek(pos
, mode
);
170 void wxFileOutputStream::Sync()
172 wxOutputStream::Sync();
176 wxFileOffset
wxFileOutputStream::GetLength() const
178 return m_file
->Length();
181 // ----------------------------------------------------------------------------
182 // wxTempFileOutputStream
183 // ----------------------------------------------------------------------------
185 wxTempFileOutputStream::wxTempFileOutputStream(const wxString
& fileName
)
187 m_file
= new wxTempFile(fileName
);
189 if (!m_file
->IsOpened())
190 m_lasterror
= wxSTREAM_WRITE_ERROR
;
193 wxTempFileOutputStream::~wxTempFileOutputStream()
195 if (m_file
->IsOpened())
200 size_t wxTempFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
202 if (IsOk() && m_file
->Write(buffer
, size
))
204 m_lasterror
= wxSTREAM_WRITE_ERROR
;
208 // ----------------------------------------------------------------------------
210 // ----------------------------------------------------------------------------
212 wxFileStream::wxFileStream(const wxString
& fileName
)
213 : wxFileInputStream(fileName
)
215 wxFileOutputStream::m_file
= wxFileInputStream::m_file
;
222 // ----------------------------------------------------------------------------
223 // wxFFileInputStream
224 // ----------------------------------------------------------------------------
226 wxFFileInputStream::wxFFileInputStream(const wxString
& fileName
,
230 m_file
= new wxFFile(fileName
, mode
);
231 m_file_destroy
= true;
234 wxFFileInputStream::wxFFileInputStream()
238 m_file_destroy
= false;
241 wxFFileInputStream::wxFFileInputStream(wxFFile
& file
)
244 m_file_destroy
= false;
247 wxFFileInputStream::wxFFileInputStream(FILE *file
)
249 m_file
= new wxFFile(file
);
250 m_file_destroy
= true;
253 wxFFileInputStream::~wxFFileInputStream()
259 wxFileOffset
wxFFileInputStream::GetLength() const
261 return m_file
->Length();
264 size_t wxFFileInputStream::OnSysRead(void *buffer
, size_t size
)
266 ssize_t ret
= m_file
->Read(buffer
, size
);
268 // It is not safe to call Eof() if the file is not opened.
269 if (!m_file
->IsOpened() || m_file
->Eof())
270 m_lasterror
= wxSTREAM_EOF
;
271 if (ret
== wxInvalidOffset
)
273 m_lasterror
= wxSTREAM_READ_ERROR
;
280 wxFileOffset
wxFFileInputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
282 return m_file
->Seek(pos
, mode
) ? m_file
->Tell() : wxInvalidOffset
;
285 wxFileOffset
wxFFileInputStream::OnSysTell() const
287 return m_file
->Tell();
290 // ----------------------------------------------------------------------------
291 // wxFFileOutputStream
292 // ----------------------------------------------------------------------------
294 wxFFileOutputStream::wxFFileOutputStream(const wxString
& fileName
,
297 m_file
= new wxFFile(fileName
, mode
);
298 m_file_destroy
= true;
300 if (!m_file
->IsOpened())
302 m_lasterror
= wxSTREAM_WRITE_ERROR
;
307 m_lasterror
= wxSTREAM_WRITE_ERROR
;
311 wxFFileOutputStream::wxFFileOutputStream(wxFFile
& file
)
314 m_file_destroy
= false;
317 wxFFileOutputStream::wxFFileOutputStream()
321 m_file_destroy
= false;
324 wxFFileOutputStream::wxFFileOutputStream(FILE *file
)
326 m_file
= new wxFFile(file
);
327 m_file_destroy
= true;
330 wxFFileOutputStream::~wxFFileOutputStream()
339 size_t wxFFileOutputStream::OnSysWrite(const void *buffer
, size_t size
)
341 size_t ret
= m_file
->Write(buffer
, size
);
342 // It is not safe to call Error() if the file is not opened.
343 if (!m_file
->IsOpened() || m_file
->Error())
344 m_lasterror
= wxSTREAM_WRITE_ERROR
;
346 m_lasterror
= wxSTREAM_NO_ERROR
;
350 wxFileOffset
wxFFileOutputStream::OnSysTell() const
352 return m_file
->Tell();
355 wxFileOffset
wxFFileOutputStream::OnSysSeek(wxFileOffset pos
, wxSeekMode mode
)
357 return m_file
->Seek(pos
, mode
) ? m_file
->Tell() : wxInvalidOffset
;
360 void wxFFileOutputStream::Sync()
362 wxOutputStream::Sync();
366 wxFileOffset
wxFFileOutputStream::GetLength() const
368 return m_file
->Length();
371 // ----------------------------------------------------------------------------
373 // ----------------------------------------------------------------------------
375 wxFFileStream::wxFFileStream(const wxString
& fileName
)
376 : wxFFileInputStream(fileName
)
378 wxFFileOutputStream::m_file
= wxFFileInputStream::m_file
;
383 #endif // wxUSE_STREAMS