]>
git.saurik.com Git - wxWidgets.git/blob - src/common/ffile.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFFile encapsulates "FILE *" IO stream
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "ffile.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
40 // ============================================================================
42 // ============================================================================
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 wxFFile::wxFFile(const wxChar
*filename
, const wxChar
*mode
)
52 (void)Open(filename
, mode
);
55 bool wxFFile::Open(const wxChar
*filename
, const wxChar
*mode
)
57 wxASSERT_MSG( !m_fp
, wxT("should close or detach the old file first") );
59 m_fp
= wxFopen(filename
, mode
);
63 wxLogSysError(_("can't open file '%s'"), filename
);
77 if ( fclose(m_fp
) != 0 )
79 wxLogSysError(_("can't close file '%s'"), m_name
.c_str());
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
94 bool wxFFile::ReadAll(wxString
*str
)
96 wxCHECK_MSG( str
, FALSE
, wxT("invalid parameter") );
97 wxCHECK_MSG( IsOpened(), FALSE
, wxT("can't read from closed file") );
102 str
->Alloc(Length());
105 static const size_t nSize
= WXSIZEOF(buf
) - 1; // -1 for trailing '\0'
108 size_t nRead
= fread(buf
, sizeof(wxChar
), nSize
, m_fp
);
109 if ( (nRead
< nSize
) && Error() )
111 wxLogSysError(_("Read error on file '%s'"), m_name
.c_str());
124 size_t wxFFile::Read(void *pBuf
, size_t nCount
)
126 wxCHECK_MSG( pBuf
, FALSE
, wxT("invalid parameter") );
127 wxCHECK_MSG( IsOpened(), FALSE
, wxT("can't read from closed file") );
129 size_t nRead
= fread(pBuf
, 1, nCount
, m_fp
);
130 if ( (nRead
< nCount
) && Error() )
132 wxLogSysError(_("Read error on file '%s'"), m_name
.c_str());
138 size_t wxFFile::Write(const void *pBuf
, size_t nCount
)
140 wxCHECK_MSG( pBuf
, FALSE
, wxT("invalid parameter") );
141 wxCHECK_MSG( IsOpened(), FALSE
, wxT("can't write to closed file") );
143 size_t nWritten
= fwrite(pBuf
, 1, nCount
, m_fp
);
144 if ( nWritten
< nCount
)
146 wxLogSysError(_("Write error on file '%s'"), m_name
.c_str());
152 bool wxFFile::Flush()
156 // fflush returns non-zero on error
160 wxLogSysError(_("failed to flush the file '%s'"), m_name
.c_str());
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
173 bool wxFFile::Seek(long ofs
, wxSeekMode mode
)
175 wxCHECK_MSG( IsOpened(), FALSE
, wxT("can't seek on closed file") );
181 wxFAIL_MSG(wxT("unknown seek mode"));
182 // still fall through
197 if ( fseek(m_fp
, ofs
, origin
) != 0 )
199 wxLogSysError(_("Seek error on file '%s'"), m_name
.c_str());
207 size_t wxFFile::Tell() const
209 long rc
= ftell(m_fp
);
212 wxLogSysError(_("Can't find current position in file '%s'"),
219 size_t wxFFile::Length() const
221 wxFFile
& self
= *(wxFFile
*)this; // const_cast
223 size_t posOld
= Tell();
224 if ( posOld
!= (size_t)-1 )
226 if ( self
.SeekEnd() )
230 (void)self
.Seek(posOld
);
239 #endif // wxUSE_FFILE