]>
git.saurik.com Git - wxWidgets.git/blob - src/common/ffile.cpp
69d1c04af8d5db3718a2135dc0072ba1f91caba4
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"
38 // ============================================================================
40 // ============================================================================
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 wxFFile::wxFFile(const wxChar
*filename
, const char *mode
)
50 (void)Open(filename
, mode
);
53 bool wxFFile::Open(const wxChar
*filename
, const char *mode
)
55 wxASSERT_MSG( !m_fp
, _T("should close or detach the old file first") );
57 m_fp
= fopen(filename
, mode
);
61 wxLogSysError(_("can't open file '%s'"), filename
);
77 wxLogSysError(_("can't close file '%s'"), m_name
.c_str());
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 bool wxFFile::ReadAll(wxString
*str
)
94 wxCHECK_MSG( str
, FALSE
, _T("invalid parameter") );
95 wxCHECK_MSG( IsOpened(), FALSE
, _T("can't read from closed file") );
100 str
->Alloc(Length());
103 static const size_t nSize
= WXSIZEOF(buf
) - 1; // -1 for trailing '\0'
106 size_t nRead
= fread(buf
, sizeof(wxChar
), nSize
, m_fp
);
107 if ( (nRead
< nSize
) && Error() )
109 wxLogSysError(_("Read error on file '%s'"), m_name
.c_str());
122 size_t wxFFile::Read(void *pBuf
, size_t nCount
)
124 wxCHECK_MSG( pBuf
, FALSE
, _T("invalid parameter") );
125 wxCHECK_MSG( IsOpened(), FALSE
, _T("can't read from closed file") );
127 size_t nRead
= fread(pBuf
, 1, nCount
, m_fp
);
128 if ( (nRead
< nCount
) && Error() )
130 wxLogSysError(_("Read error on file '%s'"), m_name
.c_str());
136 size_t wxFFile::Write(const void *pBuf
, size_t nCount
)
138 wxCHECK_MSG( pBuf
, FALSE
, _T("invalid parameter") );
139 wxCHECK_MSG( IsOpened(), FALSE
, _T("can't write to closed file") );
141 size_t nWritten
= fwrite(pBuf
, 1, nCount
, m_fp
);
142 if ( nWritten
< nCount
)
144 wxLogSysError(_("Write error on file '%s'"), m_name
.c_str());
150 bool wxFFile::Flush()
156 wxLogSysError(_("failed to flush the file '%s'"), m_name
.c_str());
165 // ----------------------------------------------------------------------------
167 // ----------------------------------------------------------------------------
169 bool wxFFile::Seek(long ofs
, wxSeekMode mode
)
171 wxCHECK_MSG( IsOpened(), FALSE
, _T("can't seek on closed file") );
177 wxFAIL_MSG(_T("unknown seek mode"));
178 // still fall through
193 if ( fseek(m_fp
, ofs
, origin
) != 0 )
195 wxLogSysError(_("Seek error on file '%s'"), m_name
.c_str());
203 size_t wxFFile::Tell() const
205 long rc
= ftell(m_fp
);
208 wxLogSysError(_("Can't find current position in file '%s'"),
215 size_t wxFFile::Length() const
217 wxFFile
& self
= *(wxFFile
*)this; // const_cast
219 size_t posOld
= Tell();
220 if ( posOld
!= (size_t)-1 )
222 if ( self
.SeekEnd() )
226 (void)self
.Seek(posOld
);