Browse Source

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').

merge-requests/365/head
eidheim 11 years ago
parent
commit
eff91ea46a
  1. 34
      src/sourcefile.cc
  2. 16
      src/sourcefile.h
  3. 5
      src/window.cc

34
src/sourcefile.cc

@ -1,32 +1,38 @@
#include "sourcefile.h" #include "sourcefile.h"
#include <fstream> #include <fstream>
#include <sstream>
std::string juci::filesystem::open(std::string path) { //TODO: make bool juci::filesystem::open(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> buffer)
std::string res; //TODO: make bool juci::filesystem::save(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> buffer)
for (auto &line : lines(path)) { //Only use on small files
res += line+'\n'; 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<std::string> juci::filesystem::lines(std::string path) { //Only use on small files
std::vector<std::string> juci::filesystem::lines(const std::string &path) {
std::vector<std::string> res; std::vector<std::string> res;
std::ifstream input(path); std::ifstream input(path);
if (input.is_open()) { if (input) {
do { res.emplace_back(); } while(getline(input, res.back())); do { res.emplace_back(); } while(getline(input, res.back()));
} }
input.close(); input.close();
return res; 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); std::ofstream output(path);
if(output.is_open()) { if(output)
output << new_content; output << new_content;
} else { else
output.close(); return false;
return 1;
}
output.close(); output.close();
return 0; return true;
} }

16
src/sourcefile.h

@ -7,14 +7,14 @@
namespace juci { namespace juci {
class filesystem { class filesystem {
public: public:
static std::string open(std::string); static std::string open(const std::string &path);
static std::string open(boost::filesystem::path path) { return open(path.string()); } static std::string open(const boost::filesystem::path &path) { return open(path.string()); }
static std::vector<std::string> lines(std::string); static std::vector<std::string> lines(const std::string &path);
static std::vector<std::string> lines(boost::filesystem::path path) { return lines(path.string()); }; static std::vector<std::string> lines(const boost::filesystem::path &path) { return lines(path.string()); };
static int save(std::string, std::string); static bool save(const std::string &path, const std::string &new_content);
static int save(boost::filesystem::path path, 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 int save(std::string path) { return save(path, ""); }; static bool save(const std::string &path) { return save(path, ""); };
static int save(boost::filesystem::path path) { return save(path, ""); }; static bool save(const boost::filesystem::path &path) { return save(path, ""); };
}; };
} // namepace juci } // namepace juci
#endif // JUCI_SOURCEFILE_H_ #endif // JUCI_SOURCEFILE_H_

5
src/window.cc

@ -279,12 +279,15 @@ void Window::new_file_entry() {
Singleton::terminal()->print("Error: "+p.string()+" already exists.\n"); Singleton::terminal()->print("Error: "+p.string()+" already exists.\n");
} }
else { else {
juci::filesystem::save(p); if(juci::filesystem::save(p)) {
notebook.open(boost::filesystem::canonical(p).string()); notebook.open(boost::filesystem::canonical(p).string());
Singleton::terminal()->print("New file "+p.string()+" created.\n"); Singleton::terminal()->print("New file "+p.string()+" created.\n");
if(notebook.project_path!="") if(notebook.project_path!="")
directories.open_folder(notebook.project_path); //TODO: Do refresh instead 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(); entry_box.hide();
}); });

Loading…
Cancel
Save