From 0d54459af43a9cdbd47feb1b9793979ca1346bcb Mon Sep 17 00:00:00 2001 From: eidheim Date: Fri, 31 Jul 2015 11:13:02 +0200 Subject: [PATCH] Can now open and save large files efficiently. Also checking if one can read the file that is being opened. --- src/notebook.cc | 17 +++++++++++++---- src/source.cc | 4 +++- src/sourcefile.cc | 43 ++++++++++++++++++++++++++++++++++++++++--- src/sourcefile.h | 5 +++++ 4 files changed, 61 insertions(+), 8 deletions(-) diff --git a/src/notebook.cc b/src/notebook.cc index 6270f42..2884d32 100644 --- a/src/notebook.cc +++ b/src/notebook.cc @@ -2,6 +2,7 @@ #include "logging.h" #include "sourcefile.h" #include "singletons.h" +#include namespace sigc { SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE @@ -38,6 +39,13 @@ void Notebook::open(std::string path) { } } + std::ifstream can_read(path); + if(!can_read) { + Singleton::terminal()->print("Error: could not open "+path+"\n"); + return; + } + can_read.close(); + auto tmp_project_path=project_path; if(tmp_project_path=="") { tmp_project_path=boost::filesystem::path(path).parent_path().string(); @@ -84,10 +92,11 @@ bool Notebook::save(int page) { return false; auto view=get_view(page); if (view->file_path != "" && view->get_buffer()->get_modified()) { - juci::filesystem::save(view->file_path, view->get_buffer()->get_text()); - view->get_buffer()->set_modified(false); - Singleton::terminal()->print("File saved to: " +view->file_path+"\n"); - return true; + if(juci::filesystem::save(view->file_path, view->get_buffer())) { + view->get_buffer()->set_modified(false); + Singleton::terminal()->print("File saved to: " +view->file_path+"\n"); + return true; + } } return false; } diff --git a/src/source.cc b/src/source.cc index 0df15fb..ec037ba 100644 --- a/src/source.cc +++ b/src/source.cc @@ -41,9 +41,11 @@ file_path(file_path), project_path(project_path) { set_smart_home_end(Gsv::SMART_HOME_END_BEFORE); set_show_line_numbers(Singleton::Config::source()->show_line_numbers); set_highlight_current_line(Singleton::Config::source()->highlight_current_line); + get_source_buffer()->get_undo_manager()->begin_not_undoable_action(); - get_source_buffer()->set_text(juci::filesystem::open(file_path)); + juci::filesystem::open(file_path, get_buffer()); get_source_buffer()->get_undo_manager()->end_not_undoable_action(); + get_buffer()->place_cursor(get_buffer()->get_iter_at_offset(0)); search_settings = gtk_source_search_settings_new(); gtk_source_search_settings_set_wrap_around(search_settings, true); diff --git a/src/sourcefile.cc b/src/sourcefile.cc index 7993656..6c4b784 100644 --- a/src/sourcefile.cc +++ b/src/sourcefile.cc @@ -2,8 +2,8 @@ #include #include -//TODO: make bool juci::filesystem::open(const std::string &path, Glib::RefPtr buffer) -//TODO: make bool juci::filesystem::save(const std::string &path, Glib::RefPtr buffer) +const size_t buffer_size=131072; + //Only use on small files std::string juci::filesystem::open(const std::string &path) { std::stringstream ss; @@ -15,6 +15,19 @@ std::string juci::filesystem::open(const std::string &path) { return ss.str(); } +bool juci::filesystem::open(const std::string &path, Glib::RefPtr text_buffer) { + std::ifstream input(path); + if(input) { + std::vector buffer(buffer_size); + size_t read_length; + while((read_length=input.read(&buffer[0], buffer_size).gcount())>0) + text_buffer->insert_at_cursor(&buffer[0], &buffer[read_length]); + input.close(); + return true; + } + return false; +} + //Only use on small files std::vector juci::filesystem::lines(const std::string &path) { std::vector res; @@ -35,4 +48,28 @@ bool juci::filesystem::save(const std::string &path, const std::string &new_cont return false; output.close(); return true; -} \ No newline at end of file +} + +bool juci::filesystem::save(const std::string &path, Glib::RefPtr buffer) { + std::ofstream output(path); + if(output) { + auto start_iter=buffer->begin(); + auto end_iter=start_iter; + bool end_reached=false; + while(!end_reached) { + for(size_t c=0;cget_text(start_iter, end_iter); + start_iter=end_iter; + } + output.close(); + return true; + } + return false; +} diff --git a/src/sourcefile.h b/src/sourcefile.h index a109ac1..4b9efb1 100644 --- a/src/sourcefile.h +++ b/src/sourcefile.h @@ -3,18 +3,23 @@ #include #include #include +#include "gtkmm.h" namespace juci { class filesystem { public: static std::string open(const std::string &path); static std::string open(const boost::filesystem::path &path) { return open(path.string()); } + static bool open(const std::string &path, Glib::RefPtr text_buffer); + static std::vector lines(const std::string &path); static std::vector lines(const boost::filesystem::path &path) { return lines(path.string()); }; + static bool save(const std::string &path, const std::string &new_content); static bool save(const boost::filesystem::path &path, const std::string &new_content) { return save(path.string(), new_content); } static bool save(const std::string &path) { return save(path, ""); }; static bool save(const boost::filesystem::path &path) { return save(path, ""); }; + static bool save(const std::string &path, Glib::RefPtr text_buffer); }; } // namepace juci #endif // JUCI_SOURCEFILE_H_