Browse Source

Now gives warning when opened file is not a valid UTF-8 file.

merge-requests/365/head
eidheim 10 years ago
parent
commit
bf0cd8d08e
  1. 4
      src/source.cc
  2. 12
      src/sourcefile.cc
  3. 4
      src/sourcefile.h

4
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();

12
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<Gtk::TextBuffer> text_buffer) {
int juci::filesystem::read(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer) {
std::ifstream input(path, std::ofstream::binary);
if(input) {
@ -24,11 +24,14 @@ bool juci::filesystem::read(const std::string &path, Glib::RefPtr<Gtk::TextBuffe
ss << input.rdbuf();
Glib::ustring ustr=std::move(ss.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, "?");
if(valid)
valid=false;
}
text_buffer->insert_at_cursor(ustr);
@ -54,9 +57,12 @@ bool juci::filesystem::read(const std::string &path, Glib::RefPtr<Gtk::TextBuffe
text_buffer->insert_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

4
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<Gtk::TextBuffer> text_buffer);
static bool read(const boost::filesystem::path &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer) { return read(path.string(), text_buffer); }
static int read(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer);
static int read(const boost::filesystem::path &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer) { return read(path.string(), text_buffer); }
static std::vector<std::string> read_lines(const std::string &path);
static std::vector<std::string> read_lines(const boost::filesystem::path &path) { return read_lines(path.string()); };

Loading…
Cancel
Save