]>
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 license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
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 char *mode
)
52 (void)Open(filename
, mode
);
55 bool wxFFile::Open(const wxChar
*filename
, const char *mode
)
57 wxASSERT_MSG( !m_fp
, _T("should close or detach the old file first") );
59 m_fp
= fopen(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
, _T("invalid parameter") );
97 wxCHECK_MSG( IsOpened(), FALSE
, _T("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
, _T("invalid parameter") );
127 wxCHECK_MSG( IsOpened(), FALSE
, _T("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
, _T("invalid parameter") );
141 wxCHECK_MSG( IsOpened(), FALSE
, _T("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()
158 wxLogSysError(_("failed to flush the file '%s'"), m_name
.c_str());
167 // ----------------------------------------------------------------------------
169 // ----------------------------------------------------------------------------
171 bool wxFFile::Seek(long ofs
, wxSeekMode mode
)
173 wxCHECK_MSG( IsOpened(), FALSE
, _T("can't seek on closed file") );
179 wxFAIL_MSG(_T("unknown seek mode"));
180 // still fall through
195 if ( fseek(m_fp
, ofs
, origin
) != 0 )
197 wxLogSysError(_("Seek error on file '%s'"), m_name
.c_str());
205 size_t wxFFile::Tell() const
207 long rc
= ftell(m_fp
);
210 wxLogSysError(_("Can't find current position in file '%s'"),
217 size_t wxFFile::Length() const
219 wxFFile
& self
= *(wxFFile
*)this; // const_cast
221 size_t posOld
= Tell();
222 if ( posOld
!= (size_t)-1 )
224 if ( self
.SeekEnd() )
228 (void)self
.Seek(posOld
);