diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-18 15:48:14 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-18 15:48:14 +0200 |
commit | d8289105193707daede1a5b59137f18e20f20aeb (patch) | |
tree | 939908b9c4c6f7aaef8aa61ee2e04be3e85610b6 /LocalFileReader.cpp | |
parent | 76e61d68bbf568ec0d7fc4632e52d4de5496b003 (diff) |
(2/2) rename
Diffstat (limited to 'LocalFileReader.cpp')
-rw-r--r-- | LocalFileReader.cpp | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/LocalFileReader.cpp b/LocalFileReader.cpp new file mode 100644 index 0000000..1f9d574 --- /dev/null +++ b/LocalFileReader.cpp @@ -0,0 +1,63 @@ +#include <cstdio> +#include <iterator> + +#include "LocalFileReader.h" +#include "Exception.h" + +LocalFileReader LocalFileReader::instance(protocol); + +void LocalFileReader::open(const std::string url) { + std::string path = url; + if (path.starts_with(protocol)) + path = path.substr(protocol.size()); + + this->file = new std::ifstream(path, std::ios::in); + if (this->file->fail() || !this->file->is_open()) + throw Exception("cannot open file://%s\n", path.c_str()); +} + +void LocalFileReader::close() { + if (this->file == nullptr) return; + + if (this->file->is_open()) + this->file->close(); +} + +const std::string LocalFileReader::read() { + if (this->content != nullptr) + return *this->content; + + if (this->file == nullptr) + throw Exception("FileReader read after destructor\n"); + + if (!this->file->is_open()) + throw Exception("FileReader read after close\n"); + + this->content = new std::string( + std::istreambuf_iterator<char>(*this->file), + std::istreambuf_iterator<char>() + ); + + return *this->content; +} + +LocalFileReader::~LocalFileReader() { + this->close(); + + if (this->file != nullptr) { + delete this->file; + this->file = nullptr; + } + + if (this->content != nullptr) { + delete this->content; + this->content = nullptr; + } +} + +LocalFileReader * LocalFileReader::clone() const { + return new LocalFileReader(this); +} + +LocalFileReader::LocalFileReader(const LocalFileReader *) : FileReader() { } + |