diff --git a/src/source.cc b/src/source.cc index 904aa91..17095a6 100644 --- a/src/source.cc +++ b/src/source.cc @@ -397,33 +397,30 @@ bool Source::View::save() { hide_tooltips(); } - std::ofstream output(file_path.string(), std::ofstream::binary); - if(output) { + try { + auto io_channel = Glib::IOChannel::create_from_file(file_path.string(), "w"); auto start_iter = get_buffer()->begin(); auto end_iter = start_iter; - bool end_reached = false; - while(!end_reached) { - if(!end_iter.forward_chars(131072)) - end_reached = true; - auto text = get_buffer()->get_text(start_iter, end_iter).raw(); - output.write(text.c_str(), text.size()); + while(start_iter) { + end_iter.forward_chars(131072); + io_channel->write(get_buffer()->get_text(start_iter, end_iter)); start_iter = end_iter; } - output.close(); - boost::system::error_code ec; - last_write_time = boost::filesystem::last_write_time(file_path, ec); - if(ec) - last_write_time = static_cast(-1); - // Remonitor file in case it did not exist before - monitor_file(); - get_buffer()->set_modified(false); - Directories::get().on_save_file(file_path); - return true; } - else { - Terminal::get().print("Error: could not save file " + file_path.string() + '\n', true); + catch(const Glib::Error &error) { + Terminal::get().print("Error: Could not save file " + filesystem::get_short_path(file_path).string() + ": " + error.what() + '\n', true); return false; } + + boost::system::error_code ec; + last_write_time = boost::filesystem::last_write_time(file_path, ec); + if(ec) + last_write_time = static_cast(-1); + // Remonitor file in case it did not exist before + monitor_file(); + get_buffer()->set_modified(false); + Directories::get().on_save_file(file_path); + return true; } void Source::View::configure() { diff --git a/src/source_base.cc b/src/source_base.cc index df799d0..b3b3a18 100644 --- a/src/source_base.cc +++ b/src/source_base.cc @@ -1,5 +1,6 @@ #include "source_base.h" #include "config.h" +#include "filesystem.h" #include "git.h" #include "info.h" #include "selection_dialog.h" @@ -106,61 +107,26 @@ bool Source::BaseView::load(bool not_undoable_action) { disable_spellcheck = false; }}; - if(language) { - std::ifstream input(file_path.string(), std::ofstream::binary); - if(input) { - std::string str; - input.seekg(0, std::ios::end); - auto size = input.tellg(); - input.seekg(0, std::ios::beg); - str.reserve(size); - str.assign(std::istreambuf_iterator(input), std::istreambuf_iterator()); - Glib::ustring ustr(std::move(str)); - - bool valid = true; - Glib::ustring::iterator iter; - while(!ustr.validate(iter)) { - auto next_char_iter = iter; - next_char_iter++; - ustr.replace(iter, next_char_iter, "?"); - valid = false; - } - - if(!valid) - Terminal::get().print("Warning: " + file_path.string() + " is not a valid UTF-8 file. Saving might corrupt the file.\n"); - - if(get_buffer()->size() == 0) - get_buffer()->insert_at_cursor(ustr); - else - replace_text(ustr.raw()); - } - else - return false; - } - else { - std::ifstream input(file_path.string(), std::ofstream::binary); - if(input) { - std::string str; - input.seekg(0, std::ios::end); - auto size = input.tellg(); - input.seekg(0, std::ios::beg); - str.reserve(size); - str.assign(std::istreambuf_iterator(input), std::istreambuf_iterator()); - Glib::ustring ustr(std::move(str)); - - if(ustr.validate()) { - if(get_buffer()->size() == 0) - get_buffer()->insert_at_cursor(ustr); - else - replace_text(ustr.raw()); + if(boost::filesystem::exists(file_path, ec)) { + try { + auto io_channel = Glib::IOChannel::create_from_file(file_path.string(), "r"); + Glib::ustring text; + if(get_buffer()->size() == 0) { + Glib::IOStatus status; + do { + status = io_channel->read(text, 131072); + get_buffer()->insert_at_cursor(text); + } while(status == Glib::IOStatus::IO_STATUS_NORMAL); } else { - Terminal::get().print("Error: " + file_path.string() + " is not a valid UTF-8 file.\n", true); - return false; + io_channel->read_to_end(text); + replace_text(text.raw()); } } - else + catch(const Glib::Error &error) { + Terminal::get().print("Error: Could not read file " + filesystem::get_short_path(file_path).string() + ": " + error.what() + '\n', true); return false; + } } get_buffer()->set_modified(false);