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. 13
      src/window.cc

34
src/sourcefile.cc

@ -1,32 +1,38 @@
#include "sourcefile.h"
#include <fstream>
#include <sstream>
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<Gtk::TextBuffer> buffer)
//TODO: make bool juci::filesystem::save(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> 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<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::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;
}

16
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<std::string> lines(std::string);
static std::vector<std::string> 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<std::string> lines(const std::string &path);
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 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_

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

Loading…
Cancel
Save