From bf0cd8d08e48883a22efeafb820e86dd8c875060 Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 2 Sep 2015 12:35:22 +0200 Subject: [PATCH] Now gives warning when opened file is not a valid UTF-8 file. --- src/source.cc | 4 ++-- src/sourcefile.cc | 12 +++++++++--- src/sourcefile.h | 4 ++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/source.cc b/src/source.cc index fa48bf6..adb8d1f 100644 --- a/src/source.cc +++ b/src/source.cc @@ -51,8 +51,8 @@ AspellConfig* Source::View::spellcheck_config=NULL; 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(); - if(!juci::filesystem::read(file_path, get_buffer())) - Singleton::terminal()->print("Error: "+file_path.string()+" is not a valid UTF-8 file.\n"); + if(juci::filesystem::read(file_path, get_buffer())==-1) + Singleton::terminal()->print("Warning: "+file_path.string()+" is not a valid UTF-8 file. Saving might corrupt the file.\n", true); 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 e1434e5..459390e 100644 --- a/src/sourcefile.cc +++ b/src/sourcefile.cc @@ -15,7 +15,7 @@ std::string juci::filesystem::read(const std::string &path) { return ss.str(); } -bool juci::filesystem::read(const std::string &path, Glib::RefPtr text_buffer) { +int juci::filesystem::read(const std::string &path, Glib::RefPtr text_buffer) { std::ifstream input(path, std::ofstream::binary); if(input) { @@ -24,11 +24,14 @@ bool juci::filesystem::read(const std::string &path, Glib::RefPtrinsert_at_cursor(ustr); @@ -54,9 +57,12 @@ bool juci::filesystem::read(const std::string &path, Glib::RefPtrinsert_at_cursor(ustr); //What if insert happens in the middle of an UTF-8 char??? }*/ input.close(); - return true; + if(valid) + return 1; + else + return -1; } - return false; + return 0; } //Only use on small files diff --git a/src/sourcefile.h b/src/sourcefile.h index d1ee608..9ddc937 100644 --- a/src/sourcefile.h +++ b/src/sourcefile.h @@ -10,8 +10,8 @@ namespace juci { public: static std::string read(const std::string &path); static std::string read(const boost::filesystem::path &path) { return read(path.string()); } - static bool read(const std::string &path, Glib::RefPtr text_buffer); - static bool read(const boost::filesystem::path &path, Glib::RefPtr text_buffer) { return read(path.string(), text_buffer); } + static int read(const std::string &path, Glib::RefPtr text_buffer); + static int read(const boost::filesystem::path &path, Glib::RefPtr text_buffer) { return read(path.string(), text_buffer); } static std::vector read_lines(const std::string &path); static std::vector read_lines(const boost::filesystem::path &path) { return read_lines(path.string()); };