From 5940a2978d0dea2a5fdff5987117b077c9875b3f Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Mon, 30 Jun 2003 17:37:34 +0000 Subject: [PATCH] Check for and allow zero length files git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_4_BRANCH@21517 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- contrib/src/stc/stc.cpp | 38 ++++++++++++++++++++++---------------- contrib/src/stc/stc.cpp.in | 34 ++++++++++++++++++++-------------- 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/contrib/src/stc/stc.cpp b/contrib/src/stc/stc.cpp index 17e9958b6a..c437fee5a0 100644 --- a/contrib/src/stc/stc.cpp +++ b/contrib/src/stc/stc.cpp @@ -475,7 +475,7 @@ void wxStyledTextCtrl::MarkerDefineBitmap(int markerNumber, const wxBitmap& bmp) buff[len] = 0; SendMsg(2049, markerNumber, (long)buff); delete [] buff; - + } // Set a margin to be either numeric or symbolic. @@ -858,7 +858,7 @@ void wxStyledTextCtrl::RegisterImage(int type, const wxBitmap& bmp) { buff[len] = 0; SendMsg(2405, type, (long)buff); delete [] buff; - + } // Clear all the registered images. @@ -2093,23 +2093,29 @@ bool wxStyledTextCtrl::SaveFile(const wxString& filename) bool wxStyledTextCtrl::LoadFile(const wxString& filename) { + bool success = false; wxFile file(filename, wxFile::read); - if (!file.IsOpened()) - return FALSE; - - wxString contents; - off_t len = file.Length(); - - wxChar *buf = contents.GetWriteBuf(len); - bool success = (file.Read(buf, len) == len); - contents.UngetWriteBuf(); - - if (success) + if (file.IsOpened()) { - SetText(contents); - EmptyUndoBuffer(); - SetSavePoint(); + wxString contents; + off_t len = file.Length(); + + if (len > 0) + { + wxChar *buf = contents.GetWriteBuf(len); + success = (file.Read(buf, len) == len); + contents.UngetWriteBuf(); + } + else + success = true; // empty file is ok + + if (success) + { + SetText(contents); + EmptyUndoBuffer(); + SetSavePoint(); + } } return success; diff --git a/contrib/src/stc/stc.cpp.in b/contrib/src/stc/stc.cpp.in index 8952840b6c..60360d977e 100644 --- a/contrib/src/stc/stc.cpp.in +++ b/contrib/src/stc/stc.cpp.in @@ -325,23 +325,29 @@ bool wxStyledTextCtrl::SaveFile(const wxString& filename) bool wxStyledTextCtrl::LoadFile(const wxString& filename) { + bool success = false; wxFile file(filename, wxFile::read); - if (!file.IsOpened()) - return FALSE; - - wxString contents; - off_t len = file.Length(); - - wxChar *buf = contents.GetWriteBuf(len); - bool success = (file.Read(buf, len) == len); - contents.UngetWriteBuf(); - - if (success) + if (file.IsOpened()) { - SetText(contents); - EmptyUndoBuffer(); - SetSavePoint(); + wxString contents; + off_t len = file.Length(); + + if (len > 0) + { + wxChar *buf = contents.GetWriteBuf(len); + success = (file.Read(buf, len) == len); + contents.UngetWriteBuf(); + } + else + success = true; // empty file is ok + + if (success) + { + SetText(contents); + EmptyUndoBuffer(); + SetSavePoint(); + } } return success;