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();
}
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();
}
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)
@ -420,11 +422,6 @@ bool Source::View::save() {
Directories::get().on_save_file(file_path);
return true;
}
else {
Terminal::get().print("Error: could not save file " + file_path.string() + '\n', true);
return false;
}
}
void Source::View::configure() {
SpellCheckView::configure();

66
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,62 +107,27 @@ 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<char>(input), std::istreambuf_iterator<char>());
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<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());
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);
return true;

Loading…
Cancel
Save