#include #include #include "LocalFile.h" #include "Exception.h" LocalFile LocalFile::instance(protocol); void LocalFile::open(const std::string url) { std::string path = url; if (path.starts_with(protocol)) path = path.substr(protocol.size()); _file = new std::ifstream(path, std::ios::in); if (_file->fail() || !_file->is_open()) throw Exception("Cannot open file://%s\n", path.c_str()); } void LocalFile::close() { if (_file == nullptr) return; if (_file->is_open()) _file->close(); } const std::string LocalFile::read() { if (_file == nullptr) throw Exception("File read after destructor\n"); if (!_file->is_open()) throw Exception("File read after close\n"); return std::string( std::istreambuf_iterator(*_file), std::istreambuf_iterator() ); } LocalFile::~LocalFile() { close(); if (_file != nullptr) { delete _file; _file = nullptr; } } LocalFile * LocalFile::clone() const { return new LocalFile(this); } LocalFile::LocalFile(const LocalFile *) : File() { }