]>
git.saurik.com Git - wxWidgets.git/blob - src/common/ffile.cpp
a3eaac2586b11b3efa6d00e2ee0cc98629795db4
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
, wxT("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 #if defined(__WXMAC__) && !defined(__UNIX__)
68 m_fp
= fopen(wxUnix2MacFilename( tmp_fname
), mode
);
70 m_fp
= fopen(tmp_fname
, mode
);
75 #if defined(__WXMAC__) && !defined(__UNIX__)
76 m_fp
= fopen(wxUnix2MacFilename( filename
), mode
);
78 m_fp
= fopen(filename
, mode
);
85 wxLogSysError(_("can't open file '%s'"), filename
);
99 if ( fclose(m_fp
) != 0 )
101 wxLogSysError(_("can't close file '%s'"), m_name
.c_str());
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 bool wxFFile::ReadAll(wxString
*str
)
118 wxCHECK_MSG( str
, FALSE
, wxT("invalid parameter") );
119 wxCHECK_MSG( IsOpened(), FALSE
, wxT("can't read from closed file") );
124 str
->Alloc(Length());
127 static const size_t nSize
= WXSIZEOF(buf
) - 1; // -1 for trailing '\0'
130 size_t nRead
= fread(buf
, sizeof(wxChar
), nSize
, m_fp
);
131 if ( (nRead
< nSize
) && Error() )
133 wxLogSysError(_("Read error on file '%s'"), m_name
.c_str());
146 size_t wxFFile::Read(void *pBuf
, size_t nCount
)
148 wxCHECK_MSG( pBuf
, FALSE
, wxT("invalid parameter") );
149 wxCHECK_MSG( IsOpened(), FALSE
, wxT("can't read from closed file") );
151 size_t nRead
= fread(pBuf
, 1, nCount
, m_fp
);
152 if ( (nRead
< nCount
) && Error() )
154 wxLogSysError(_("Read error on file '%s'"), m_name
.c_str());
160 size_t wxFFile::Write(const void *pBuf
, size_t nCount
)
162 wxCHECK_MSG( pBuf
, FALSE
, wxT("invalid parameter") );
163 wxCHECK_MSG( IsOpened(), FALSE
, wxT("can't write to closed file") );
165 size_t nWritten
= fwrite(pBuf
, 1, nCount
, m_fp
);
166 if ( nWritten
< nCount
)
168 wxLogSysError(_("Write error on file '%s'"), m_name
.c_str());
174 bool wxFFile::Flush()
178 // fflush returns non-zero on error
182 wxLogSysError(_("failed to flush the file '%s'"), m_name
.c_str());
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
195 bool wxFFile::Seek(long ofs
, wxSeekMode mode
)
197 wxCHECK_MSG( IsOpened(), FALSE
, wxT("can't seek on closed file") );
203 wxFAIL_MSG(wxT("unknown seek mode"));
204 // still fall through
219 if ( fseek(m_fp
, ofs
, origin
) != 0 )
221 wxLogSysError(_("Seek error on file '%s'"), m_name
.c_str());
229 size_t wxFFile::Tell() const
231 long rc
= ftell(m_fp
);
234 wxLogSysError(_("Can't find current position in file '%s'"),
241 size_t wxFFile::Length() const
243 wxFFile
& self
= *(wxFFile
*)this; // const_cast
245 size_t posOld
= Tell();
246 if ( posOld
!= (size_t)-1 )
248 if ( self
.SeekEnd() )
252 (void)self
.Seek(posOld
);