X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/89220a94281cb6693464dafa4f562eaa9ae0d540..2747a51b243ef5d44cec5b4e3757d56af56352a2:/src/common/ffile.cpp diff --git a/src/common/ffile.cpp b/src/common/ffile.cpp index d34756376a..932498efcd 100644 --- a/src/common/ffile.cpp +++ b/src/common/ffile.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: ffile.cpp +// Name: src/common/ffile.cpp // Purpose: wxFFile encapsulates "FILE *" IO stream // Author: Vadim Zeitlin // Modified by: @@ -29,6 +29,7 @@ #ifndef WX_PRECOMP #include "wx/intl.h" #include "wx/log.h" + #include "wx/crt.h" #endif #ifdef __WINDOWS__ @@ -41,30 +42,18 @@ // implementation // ============================================================================ -// ---------------------------------------------------------------------------- -// seek and tell with large file support if available -// ---------------------------------------------------------------------------- - -#ifdef HAVE_FSEEKO -# define wxFseek fseeko -# define wxFtell ftello -#else -# define wxFseek fseek -# define wxFtell ftell -#endif - // ---------------------------------------------------------------------------- // opening the file // ---------------------------------------------------------------------------- -wxFFile::wxFFile(const wxChar *filename, const wxChar *mode) +wxFFile::wxFFile(const wxString& filename, const wxString& mode) { Detach(); (void)Open(filename, mode); } -bool wxFFile::Open(const wxChar *filename, const wxChar *mode) +bool wxFFile::Open(const wxString& filename, const wxString& mode) { wxASSERT_MSG( !m_fp, wxT("should close or detach the old file first") ); @@ -103,7 +92,7 @@ bool wxFFile::Close() // read/write // ---------------------------------------------------------------------------- -bool wxFFile::ReadAll(wxString *str, wxMBConv& conv) +bool wxFFile::ReadAll(wxString *str, const wxMBConv& conv) { wxCHECK_MSG( str, false, wxT("invalid parameter") ); wxCHECK_MSG( IsOpened(), false, wxT("can't read from closed file") ); @@ -113,14 +102,14 @@ bool wxFFile::ReadAll(wxString *str, wxMBConv& conv) clearerr(m_fp); - wxCharBuffer buf(length + 1); + wxCharBuffer buf(length); // note that real length may be less than file length for text files with DOS EOLs // ('\r's get dropped by CRT when reading which means that we have // realLen = fileLen - numOfLinesInTheFile) - length = fread(buf.data(), sizeof(char), length, m_fp); + length = fread(buf.data(), 1, length, m_fp); - if ( Error() ) + if ( Error() ) { wxLogSysError(_("Read error on file '%s'"), m_name.c_str()); @@ -128,7 +117,9 @@ bool wxFFile::ReadAll(wxString *str, wxMBConv& conv) } buf.data()[length] = 0; - *str = wxString(buf, conv); + + wxString strTmp(buf, conv); + str->swap(strTmp); return true; } @@ -161,13 +152,21 @@ size_t wxFFile::Write(const void *pBuf, size_t nCount) return nWritten; } +bool wxFFile::Write(const wxString& s, const wxMBConv& conv) +{ + const wxWX2MBbuf buf = s.mb_str(conv); + if ( !buf ) + return false; + + const size_t size = strlen(buf); // FIXME: use buf.length() when available + return Write(buf, size) == size; +} + bool wxFFile::Flush() { if ( IsOpened() ) { - // fflush returns non-zero on error - // - if ( fflush(m_fp) ) + if ( fflush(m_fp) != 0 ) { wxLogSysError(_("failed to flush the file '%s'"), m_name.c_str()); @@ -206,7 +205,7 @@ bool wxFFile::Seek(wxFileOffset ofs, wxSeekMode mode) break; } -#ifndef HAVE_FSEEKO +#ifndef wxHAS_LARGE_FFILES if ((long)ofs != ofs) { wxLogError(_("Seek error on file '%s' (large files not supported by stdio)"), m_name.c_str()); @@ -230,7 +229,7 @@ bool wxFFile::Seek(wxFileOffset ofs, wxSeekMode mode) wxFileOffset wxFFile::Tell() const { wxCHECK_MSG( IsOpened(), wxInvalidOffset, - _T("wxFFile::Tell(): file is closed!") ); + wxT("wxFFile::Tell(): file is closed!") ); wxFileOffset rc = wxFtell(m_fp); if ( rc == wxInvalidOffset ) @@ -245,9 +244,9 @@ wxFileOffset wxFFile::Tell() const wxFileOffset wxFFile::Length() const { wxCHECK_MSG( IsOpened(), wxInvalidOffset, - _T("wxFFile::Length(): file is closed!") ); + wxT("wxFFile::Length(): file is closed!") ); - wxFFile& self = *(wxFFile *)this; // const_cast + wxFFile& self = *const_cast(this); wxFileOffset posOld = Tell(); if ( posOld != wxInvalidOffset )