From eff91ea46a89a4da64e0fe49825604f708a11a89 Mon Sep 17 00:00:00 2001 From: eidheim Date: Fri, 31 Jul 2015 10:24:29 +0200 Subject: [PATCH] Cleanup of sourcefile.*. Also fixed a new crash when trying to create a new file without write rights (for instance trying to create new file '/untitled'). --- src/sourcefile.cc | 34 ++++++++++++++++++++-------------- src/sourcefile.h | 16 ++++++++-------- src/window.cc | 13 ++++++++----- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/sourcefile.cc b/src/sourcefile.cc index 9f80b03..7993656 100644 --- a/src/sourcefile.cc +++ b/src/sourcefile.cc @@ -1,32 +1,38 @@ #include "sourcefile.h" #include +#include -std::string juci::filesystem::open(std::string path) { - std::string res; - for (auto &line : lines(path)) { - res += line+'\n'; +//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) +//Only use on small files +std::string juci::filesystem::open(const std::string &path) { + std::stringstream ss; + std::ifstream input(path); + if(input) { + ss << input.rdbuf(); + input.close(); } - return res; + return ss.str(); } -std::vector juci::filesystem::lines(std::string path) { +//Only use on small files +std::vector juci::filesystem::lines(const std::string &path) { std::vector res; std::ifstream input(path); - if (input.is_open()) { + if (input) { do { res.emplace_back(); } while(getline(input, res.back())); } input.close(); return res; } -int juci::filesystem::save(std::string path, std::string new_content) { +//Only use on small files +bool juci::filesystem::save(const std::string &path, const std::string &new_content) { std::ofstream output(path); - if(output.is_open()) { + if(output) output << new_content; - } else { - output.close(); - return 1; - } + else + return false; output.close(); - return 0; + return true; } \ No newline at end of file diff --git a/src/sourcefile.h b/src/sourcefile.h index c86bc89..a109ac1 100644 --- a/src/sourcefile.h +++ b/src/sourcefile.h @@ -7,14 +7,14 @@ namespace juci { class filesystem { public: - static std::string open(std::string); - static std::string open(boost::filesystem::path path) { return open(path.string()); } - static std::vector lines(std::string); - static std::vector lines(boost::filesystem::path path) { return lines(path.string()); }; - static int save(std::string, std::string); - static int save(boost::filesystem::path path, std::string new_content) { return save(path.string(), new_content); } - static int save(std::string path) { return save(path, ""); }; - static int save(boost::filesystem::path path) { return save(path, ""); }; + static std::string open(const std::string &path); + static std::string open(const boost::filesystem::path &path) { return open(path.string()); } + 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, ""); }; }; } // namepace juci #endif // JUCI_SOURCEFILE_H_ diff --git a/src/window.cc b/src/window.cc index 180ac64..c56aa3c 100644 --- a/src/window.cc +++ b/src/window.cc @@ -279,11 +279,14 @@ void Window::new_file_entry() { Singleton::terminal()->print("Error: "+p.string()+" already exists.\n"); } else { - juci::filesystem::save(p); - notebook.open(boost::filesystem::canonical(p).string()); - Singleton::terminal()->print("New file "+p.string()+" created.\n"); - if(notebook.project_path!="") - directories.open_folder(notebook.project_path); //TODO: Do refresh instead + if(juci::filesystem::save(p)) { + notebook.open(boost::filesystem::canonical(p).string()); + Singleton::terminal()->print("New file "+p.string()+" created.\n"); + if(notebook.project_path!="") + directories.open_folder(notebook.project_path); //TODO: Do refresh instead + } + else + Singleton::terminal()->print("Error: could not create new file "+p.string()+".\n"); } } entry_box.hide();