From 27b61f783f0e562ff34a7954c5ff8b19e7ed0471 Mon Sep 17 00:00:00 2001 From: eidheim Date: Sun, 16 Aug 2015 14:08:36 +0200 Subject: [PATCH] Instead of crashing when opening a binary file, an error message is now instead printed in terminal. --- src/source.cc | 3 ++- src/sourcefile.cc | 11 +++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/source.cc b/src/source.cc index 8cb9570..8890604 100644 --- a/src/source.cc +++ b/src/source.cc @@ -39,7 +39,8 @@ Glib::RefPtr Source::guess_language(const boost::filesystem::path Source::View::View(const boost::filesystem::path &file_path): file_path(file_path) { set_smart_home_end(Gsv::SMART_HOME_END_BEFORE); get_source_buffer()->begin_not_undoable_action(); - juci::filesystem::read(file_path, get_buffer()); + if(!juci::filesystem::read(file_path, get_buffer())) + Singleton::terminal()->print("Error: "+file_path.string()+" is not a valid UTF-8 file.\n"); get_source_buffer()->end_not_undoable_action(); get_buffer()->place_cursor(get_buffer()->get_iter_at_offset(0)); search_settings = gtk_source_search_settings_new(); diff --git a/src/sourcefile.cc b/src/sourcefile.cc index b6f0786..ae5780b 100644 --- a/src/sourcefile.cc +++ b/src/sourcefile.cc @@ -20,8 +20,15 @@ bool juci::filesystem::read(const std::string &path, Glib::RefPtr buffer(buffer_size); size_t read_length; - while((read_length=input.read(&buffer[0], buffer_size).gcount())>0) - text_buffer->insert_at_cursor(&buffer[0], &buffer[read_length]); + while((read_length=input.read(&buffer[0], buffer_size).gcount())>0) { + auto ustr=Glib::ustring(std::string(&buffer[0], read_length)); + if(ustr.validate()) + text_buffer->insert_at_cursor(ustr); + else { + input.close(); + return false; + } + } input.close(); return true; }