diff options
author | Roland Reichwein <mail@reichwein.it> | 2020-04-12 14:01:40 +0200 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2020-04-12 14:01:40 +0200 |
commit | 3f778eecc705990598f1033e6245522f42e2fcb5 (patch) | |
tree | dfa2af27ef4e6b6a299ecb014a684c272db77992 /plugins/webbox/file.cpp | |
parent | 77a68fbe16246245937c5d692bb8c89dc14d7800 (diff) |
Refactor path concept
Diffstat (limited to 'plugins/webbox/file.cpp')
-rw-r--r-- | plugins/webbox/file.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/plugins/webbox/file.cpp b/plugins/webbox/file.cpp new file mode 100644 index 0000000..47ab8be --- /dev/null +++ b/plugins/webbox/file.cpp @@ -0,0 +1,46 @@ +#include "file.h" + +#include <fstream> + +namespace fs = std::filesystem; + +using namespace std::string_literals; + +std::string File::getFile(const fs::path& filename) +{ + std::ifstream file(filename.string(), std::ios::in | std::ios::binary | std::ios::ate); + + if (file.is_open()) { + std::ifstream::pos_type fileSize = file.tellg(); + file.seekg(0, std::ios::beg); + + std::string bytes(fileSize, ' '); + file.read(reinterpret_cast<char*>(bytes.data()), fileSize); + + return bytes; + + } else { + throw std::runtime_error("Opening "s + filename.string() + " for reading"); + } +} + +void File::setFile(const fs::path& filename, const std::string& s) +{ + File::setFile(filename, s.data(), s.size()); +} + +void File::setFile(const fs::path& filename, const char* data, size_t size) +{ + std::ofstream file(filename.string(), std::ios::out | std::ios::binary); + if (file.is_open()) { + file.write(data, size); + } else { + throw std::runtime_error("Opening "s + filename.string() + " for writing"); + } +} + +void File::setFile(const fs::path& filename, const std::vector<uint8_t>& data) +{ + File::setFile(filename, reinterpret_cast<const char*>(data.data()), data.size()); +} + |