diff options
Diffstat (limited to 'LocalFile.cpp')
-rw-r--r-- | LocalFile.cpp | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/LocalFile.cpp b/LocalFile.cpp index 2f4140d..1ff595a 100644 --- a/LocalFile.cpp +++ b/LocalFile.cpp @@ -11,21 +11,38 @@ void LocalFile::open(const std::string url) { if (path.starts_with(protocol)) path = path.substr(protocol.size()); - std::ifstream _file(path); - if (!_file.is_open()) + _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.is_open()) _file.close(); + if (_file == nullptr) return; + + if (_file->is_open()) + _file->close(); } const std::string LocalFile::read() { - return std::string(std::istreambuf_iterator<char>(_file), std::istreambuf_iterator<char>()); + 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<char>(*_file), + std::istreambuf_iterator<char>() + ); } LocalFile::~LocalFile() { close(); + + if (_file != nullptr) { + delete _file; + _file = nullptr; + } } LocalFile * LocalFile::clone() const { |