From bc02054d56118110a36aea72d21f9d5e73d07d1f Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 22 Oct 2024 14:00:41 +0200 Subject: refactor file reading factory --- LocalFileReader.cpp | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) (limited to 'LocalFileReader.cpp') diff --git a/LocalFileReader.cpp b/LocalFileReader.cpp index 1f9d574..a505616 100644 --- a/LocalFileReader.cpp +++ b/LocalFileReader.cpp @@ -1,17 +1,18 @@ #include +#include #include #include "LocalFileReader.h" #include "Exception.h" -LocalFileReader LocalFileReader::instance(protocol); +using namespace std; -void LocalFileReader::open(const std::string url) { - std::string path = url; - if (path.starts_with(protocol)) - path = path.substr(protocol.size()); +void LocalFileReader::open() { + string path = this->url; + if (path.starts_with("file://")) + path = path.substr(strlen("file://")); - this->file = new std::ifstream(path, std::ios::in); + this->file = new ifstream(path, ios::in); if (this->file->fail() || !this->file->is_open()) throw Exception("cannot open file://%s\n", path.c_str()); } @@ -23,7 +24,7 @@ void LocalFileReader::close() { this->file->close(); } -const std::string LocalFileReader::read() { +const string LocalFileReader::read() { if (this->content != nullptr) return *this->content; @@ -33,9 +34,9 @@ const std::string LocalFileReader::read() { if (!this->file->is_open()) throw Exception("FileReader read after close\n"); - this->content = new std::string( - std::istreambuf_iterator(*this->file), - std::istreambuf_iterator() + this->content = new string( + istreambuf_iterator(*this->file), + istreambuf_iterator() ); return *this->content; @@ -55,9 +56,3 @@ LocalFileReader::~LocalFileReader() { } } -LocalFileReader * LocalFileReader::clone() const { - return new LocalFileReader(this); -} - -LocalFileReader::LocalFileReader(const LocalFileReader *) : FileReader() { } - -- cgit v1.2.3