#include #include #include #include "LocalFileReader.h" #include "Exception.h" using namespace std; void LocalFileReader::open() { string path = this->url; if (path.starts_with("file://")) path = path.substr(strlen("file://")); 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()); } void LocalFileReader::close() { if (this->file == nullptr) return; if (this->file->is_open()) this->file->close(); } const 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 string( istreambuf_iterator(*this->file), istreambuf_iterator() ); 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; } }