From: Robin Dunn Date: Mon, 30 Jun 2003 17:37:34 +0000 (+0000) Subject: Check for and allow zero length files X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/041973c55e06e98c724846d3ffac5e5284f07167?hp=7142ac377a669c0d336054a5b59c081e6605562d Check for and allow zero length files git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@21517 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- 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;