From a2aec7f2ff6bd4f1d3d70e111d93555997f7a100 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Thu, 4 Feb 2021 01:39:08 +0100 Subject: [PATCH] Move file length check to separate function --- inferno/src/inferno/io/file.cpp | 25 ++++++++++++++++--------- inferno/src/inferno/io/file.h | 1 + 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/inferno/src/inferno/io/file.cpp b/inferno/src/inferno/io/file.cpp index 4399e27..74b8ebb 100644 --- a/inferno/src/inferno/io/file.cpp +++ b/inferno/src/inferno/io/file.cpp @@ -9,19 +9,16 @@ namespace Inferno { std::string File::read(const std::string &path) { // Create input stream object and open file - std::ifstream file(path); - ASSERT(file.is_open(), "File could not open: '{}'", path); + std::ifstream ifstream(path); + ASSERT(ifstream.is_open(), "File could not open '{}'", path); // Check if file exists - if (!file.is_open()) { + if (!ifstream.is_open()) { return nullptr; } // Get length of the file - file.seekg(0, std::ios::end); - int length = file.tellg(); - file.seekg(0, std::ios::beg); - ASSERT(length != -1, "File could not determine length: '{}'", path); + int32_t length = File::length(path, ifstream); // Check for valid file length if (length == -1) { @@ -32,11 +29,21 @@ namespace Inferno { auto buffer = std::make_unique(length); // Fill buffer with file contents - file.read(buffer.get(), length); - file.close(); + ifstream.read(buffer.get(), length); + ifstream.close(); // Create string from the buffer and return return std::string(buffer.get(), length); } + int32_t File::length(const std::string& path, std::ifstream& ifstream) + { + ifstream.seekg(0, std::ios::end); + int32_t length = ifstream.tellg(); + ifstream.seekg(0, std::ios::beg); + ASSERT(length != -1, "File could not determine length '{}'", path); + + return length; + } + } diff --git a/inferno/src/inferno/io/file.h b/inferno/src/inferno/io/file.h index 63aaad5..aa65ad6 100644 --- a/inferno/src/inferno/io/file.h +++ b/inferno/src/inferno/io/file.h @@ -14,6 +14,7 @@ namespace Inferno { class File { public: static std::string read(const std::string& path); + static int32_t length(const std::string& path, std::ifstream& ifstream); template static void ioRead(T& t, const std::string& path)