diff options
Diffstat (limited to 'LocalFile.cpp')
-rw-r--r-- | LocalFile.cpp | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/LocalFile.cpp b/LocalFile.cpp index 1ff595a..ee5bf74 100644 --- a/LocalFile.cpp +++ b/LocalFile.cpp @@ -11,37 +11,47 @@ void LocalFile::open(const std::string 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()) + 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 LocalFile::close() { - if (_file == nullptr) return; + if (this->file == nullptr) return; - if (_file->is_open()) - _file->close(); + if (this->file->is_open()) + this->file->close(); } const std::string LocalFile::read() { - if (_file == nullptr) + if (this->content != nullptr) + return *this->content; + + if (this->file == nullptr) throw Exception("File read after destructor\n"); - if (!_file->is_open()) + if (!this->file->is_open()) throw Exception("File read after close\n"); - return std::string( - std::istreambuf_iterator<char>(*_file), + this->content = new std::string( + std::istreambuf_iterator<char>(*this->file), std::istreambuf_iterator<char>() ); + + return *this->content; } LocalFile::~LocalFile() { close(); - if (_file != nullptr) { - delete _file; - _file = nullptr; + if (this->file != nullptr) { + delete this->file; + this->file = nullptr; + } + + if (this->content != nullptr) { + delete this->content; + this->content = nullptr; } } |