Browse Source

Can now open and save large files efficiently. Also checking if one can read the file that is being opened.

merge-requests/365/head
eidheim 11 years ago
parent
commit
0d54459af4
  1. 11
      src/notebook.cc
  2. 4
      src/source.cc
  3. 41
      src/sourcefile.cc
  4. 5
      src/sourcefile.h

11
src/notebook.cc

@ -2,6 +2,7 @@
#include "logging.h" #include "logging.h"
#include "sourcefile.h" #include "sourcefile.h"
#include "singletons.h" #include "singletons.h"
#include <fstream>
namespace sigc { namespace sigc {
SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE 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; auto tmp_project_path=project_path;
if(tmp_project_path=="") { if(tmp_project_path=="") {
tmp_project_path=boost::filesystem::path(path).parent_path().string(); tmp_project_path=boost::filesystem::path(path).parent_path().string();
@ -84,11 +92,12 @@ bool Notebook::save(int page) {
return false; return false;
auto view=get_view(page); auto view=get_view(page);
if (view->file_path != "" && view->get_buffer()->get_modified()) { if (view->file_path != "" && view->get_buffer()->get_modified()) {
juci::filesystem::save(view->file_path, view->get_buffer()->get_text()); if(juci::filesystem::save(view->file_path, view->get_buffer())) {
view->get_buffer()->set_modified(false); view->get_buffer()->set_modified(false);
Singleton::terminal()->print("File saved to: " +view->file_path+"\n"); Singleton::terminal()->print("File saved to: " +view->file_path+"\n");
return true; return true;
} }
}
return false; return false;
} }

4
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_smart_home_end(Gsv::SMART_HOME_END_BEFORE);
set_show_line_numbers(Singleton::Config::source()->show_line_numbers); set_show_line_numbers(Singleton::Config::source()->show_line_numbers);
set_highlight_current_line(Singleton::Config::source()->highlight_current_line); set_highlight_current_line(Singleton::Config::source()->highlight_current_line);
get_source_buffer()->get_undo_manager()->begin_not_undoable_action(); 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_source_buffer()->get_undo_manager()->end_not_undoable_action();
get_buffer()->place_cursor(get_buffer()->get_iter_at_offset(0)); get_buffer()->place_cursor(get_buffer()->get_iter_at_offset(0));
search_settings = gtk_source_search_settings_new(); search_settings = gtk_source_search_settings_new();
gtk_source_search_settings_set_wrap_around(search_settings, true); gtk_source_search_settings_set_wrap_around(search_settings, true);

41
src/sourcefile.cc

@ -2,8 +2,8 @@
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
//TODO: make bool juci::filesystem::open(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> buffer) const size_t buffer_size=131072;
//TODO: make bool juci::filesystem::save(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> buffer)
//Only use on small files //Only use on small files
std::string juci::filesystem::open(const std::string &path) { std::string juci::filesystem::open(const std::string &path) {
std::stringstream ss; std::stringstream ss;
@ -15,6 +15,19 @@ std::string juci::filesystem::open(const std::string &path) {
return ss.str(); return ss.str();
} }
bool juci::filesystem::open(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer) {
std::ifstream input(path);
if(input) {
std::vector<char> 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 //Only use on small files
std::vector<std::string> juci::filesystem::lines(const std::string &path) { std::vector<std::string> juci::filesystem::lines(const std::string &path) {
std::vector<std::string> res; std::vector<std::string> res;
@ -36,3 +49,27 @@ bool juci::filesystem::save(const std::string &path, const std::string &new_cont
output.close(); output.close();
return true; return true;
} }
bool juci::filesystem::save(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> 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;c<buffer_size;c++) {
if(end_iter)
end_iter++;
else {
end_reached=true;
break;
}
}
output << buffer->get_text(start_iter, end_iter);
start_iter=end_iter;
}
output.close();
return true;
}
return false;
}

5
src/sourcefile.h

@ -3,18 +3,23 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include "gtkmm.h"
namespace juci { namespace juci {
class filesystem { class filesystem {
public: public:
static std::string open(const std::string &path); static std::string open(const std::string &path);
static std::string open(const boost::filesystem::path &path) { return open(path.string()); } static std::string open(const boost::filesystem::path &path) { return open(path.string()); }
static bool open(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer);
static std::vector<std::string> lines(const std::string &path); static std::vector<std::string> lines(const std::string &path);
static std::vector<std::string> lines(const boost::filesystem::path &path) { return lines(path.string()); }; static std::vector<std::string> 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 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 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 std::string &path) { return save(path, ""); };
static bool save(const boost::filesystem::path &path) { return save(path, ""); }; static bool save(const boost::filesystem::path &path) { return save(path, ""); };
static bool save(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer);
}; };
} // namepace juci } // namepace juci
#endif // JUCI_SOURCEFILE_H_ #endif // JUCI_SOURCEFILE_H_

Loading…
Cancel
Save