Browse Source

Simplified read/write source files, and improved error messages

pipelines/235045657
eidheim 6 years ago
parent
commit
4df357a9b7
  1. 25
      src/source.cc
  2. 66
      src/source_base.cc

25
src/source.cc

@ -397,19 +397,21 @@ bool Source::View::save() {
hide_tooltips(); hide_tooltips();
} }
std::ofstream output(file_path.string(), std::ofstream::binary); try {
if(output) { auto io_channel = Glib::IOChannel::create_from_file(file_path.string(), "w");
auto start_iter = get_buffer()->begin(); auto start_iter = get_buffer()->begin();
auto end_iter = start_iter; auto end_iter = start_iter;
bool end_reached = false; while(start_iter) {
while(!end_reached) { end_iter.forward_chars(131072);
if(!end_iter.forward_chars(131072)) io_channel->write(get_buffer()->get_text(start_iter, end_iter));
end_reached = true;
auto text = get_buffer()->get_text(start_iter, end_iter).raw();
output.write(text.c_str(), text.size());
start_iter = end_iter; start_iter = end_iter;
} }
output.close(); }
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; boost::system::error_code ec;
last_write_time = boost::filesystem::last_write_time(file_path, ec); last_write_time = boost::filesystem::last_write_time(file_path, ec);
if(ec) if(ec)
@ -420,11 +422,6 @@ bool Source::View::save() {
Directories::get().on_save_file(file_path); Directories::get().on_save_file(file_path);
return true; return true;
} }
else {
Terminal::get().print("Error: could not save file " + file_path.string() + '\n', true);
return false;
}
}
void Source::View::configure() { void Source::View::configure() {
SpellCheckView::configure(); SpellCheckView::configure();

66
src/source_base.cc

@ -1,5 +1,6 @@
#include "source_base.h" #include "source_base.h"
#include "config.h" #include "config.h"
#include "filesystem.h"
#include "git.h" #include "git.h"
#include "info.h" #include "info.h"
#include "selection_dialog.h" #include "selection_dialog.h"
@ -106,62 +107,27 @@ bool Source::BaseView::load(bool not_undoable_action) {
disable_spellcheck = false; disable_spellcheck = false;
}}; }};
if(language) { if(boost::filesystem::exists(file_path, ec)) {
std::ifstream input(file_path.string(), std::ofstream::binary); try {
if(input) { auto io_channel = Glib::IOChannel::create_from_file(file_path.string(), "r");
std::string str; Glib::ustring text;
input.seekg(0, std::ios::end); if(get_buffer()->size() == 0) {
auto size = input.tellg(); Glib::IOStatus status;
input.seekg(0, std::ios::beg); do {
str.reserve(size); status = io_channel->read(text, 131072);
str.assign(std::istreambuf_iterator<char>(input), std::istreambuf_iterator<char>()); get_buffer()->insert_at_cursor(text);
Glib::ustring ustr(std::move(str)); } while(status == Glib::IOStatus::IO_STATUS_NORMAL);
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<char>(input), std::istreambuf_iterator<char>());
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());
} }
else { else {
Terminal::get().print("Error: " + file_path.string() + " is not a valid UTF-8 file.\n", true); io_channel->read_to_end(text);
return false; 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; return false;
} }
}
get_buffer()->set_modified(false); get_buffer()->set_modified(false);
return true; return true;

Loading…
Cancel
Save