mirror of https://gitlab.com/cppit/jucipp
3 changed files with 36 additions and 27 deletions
@ -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; |
||||||
} |
} |
||||||
Loading…
Reference in new issue