From 041973c55e06e98c724846d3ffac5e5284f07167 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Mon, 30 Jun 2003 17:37:34 +0000 Subject: [PATCH 1/1] Check for and allow zero length files git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@21517 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- contrib/src/stc/stc.cpp | 34 ++++++++++++++++++++-------------- contrib/src/stc/stc.cpp.in | 34 ++++++++++++++++++++-------------- src/stc/stc.cpp | 34 ++++++++++++++++++++-------------- src/stc/stc.cpp.in | 34 ++++++++++++++++++++-------------- 4 files changed, 80 insertions(+), 56 deletions(-) diff --git a/contrib/src/stc/stc.cpp b/contrib/src/stc/stc.cpp index d4fbcec50c..c437fee5a0 100644 --- a/contrib/src/stc/stc.cpp +++ b/contrib/src/stc/stc.cpp @@ -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; diff --git a/src/stc/stc.cpp b/src/stc/stc.cpp index d4fbcec50c..c437fee5a0 100644 --- a/src/stc/stc.cpp +++ b/src/stc/stc.cpp @@ -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/src/stc/stc.cpp.in b/src/stc/stc.cpp.in index 8952840b6c..60360d977e 100644 --- a/src/stc/stc.cpp.in +++ b/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; -- 2.45.2