]>
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") );
63 fname_len
= wxStrlen(filename
)+1;
64 tmp_fname
= new char[fname_len
];
65 wxWX2MB(tmp_fname
, filename
, fname_len
);
67 m_fp
= fopen(tmp_fname
, mode
);
71 m_fp
= fopen(filename
, mode
);
77 wxLogSysError(_("can't open file '%s'"), filename
);
91 if ( fclose(m_fp
) != 0 )
93 wxLogSysError(_("can't close file '%s'"), m_name
.c_str());
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 bool wxFFile::ReadAll(wxString
*str
)
110 wxCHECK_MSG( str
, FALSE
, _T("invalid parameter") );
111 wxCHECK_MSG( IsOpened(), FALSE
, _T("can't read from closed file") );
116 str
->Alloc(Length());
119 static const size_t nSize
= WXSIZEOF(buf
) - 1; // -1 for trailing '\0'
122 size_t nRead
= fread(buf
, sizeof(wxChar
), nSize
, m_fp
);
123 if ( (nRead
< nSize
) && Error() )
125 wxLogSysError(_("Read error on file '%s'"), m_name
.c_str());
138 size_t wxFFile::Read(void *pBuf
, size_t nCount
)
140 wxCHECK_MSG( pBuf
, FALSE
, _T("invalid parameter") );
141 wxCHECK_MSG( IsOpened(), FALSE
, _T("can't read from closed file") );
143 size_t nRead
= fread(pBuf
, 1, nCount
, m_fp
);
144 if ( (nRead
< nCount
) && Error() )
146 wxLogSysError(_("Read error on file '%s'"), m_name
.c_str());
152 size_t wxFFile::Write(const void *pBuf
, size_t nCount
)
154 wxCHECK_MSG( pBuf
, FALSE
, _T("invalid parameter") );
155 wxCHECK_MSG( IsOpened(), FALSE
, _T("can't write to closed file") );
157 size_t nWritten
= fwrite(pBuf
, 1, nCount
, m_fp
);
158 if ( nWritten
< nCount
)
160 wxLogSysError(_("Write error on file '%s'"), m_name
.c_str());
166 bool wxFFile::Flush()
172 wxLogSysError(_("failed to flush the file '%s'"), m_name
.c_str());
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
185 bool wxFFile::Seek(long ofs
, wxSeekMode mode
)
187 wxCHECK_MSG( IsOpened(), FALSE
, _T("can't seek on closed file") );
193 wxFAIL_MSG(_T("unknown seek mode"));
194 // still fall through
209 if ( fseek(m_fp
, ofs
, origin
) != 0 )
211 wxLogSysError(_("Seek error on file '%s'"), m_name
.c_str());
219 size_t wxFFile::Tell() const
221 long rc
= ftell(m_fp
);
224 wxLogSysError(_("Can't find current position in file '%s'"),
231 size_t wxFFile::Length() const
233 wxFFile
& self
= *(wxFFile
*)this; // const_cast
235 size_t posOld
= Tell();
236 if ( posOld
!= (size_t)-1 )
238 if ( self
.SeekEnd() )
242 (void)self
.Seek(posOld
);