From 50fe09ee566a7ef38db3a32b347ca64ba12d2650 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Mon, 26 Sep 2022 02:40:32 +0200 Subject: [PATCH] Util: Fix reading binary files --- src/ruc/file.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/ruc/file.cpp b/src/ruc/file.cpp index f9ce208..4d802fb 100644 --- a/src/ruc/file.cpp +++ b/src/ruc/file.cpp @@ -7,7 +7,7 @@ #include // int32_t #include #include // std::ifstream, std::ofstream -#include // std::make_shared, std::shared_ptr +#include // std::make_shared, std::make_unique, std::shared_ptr #include #include @@ -19,7 +19,21 @@ namespace ruc { File::File(std::string_view path) : m_path(path) { - m_data = std::string(File::raw(path).get()); + // Create input stream object and open file + std::ifstream file(path.data()); + VERIFY(file.is_open(), "failed to open file: '{}'", path); + + // Get length of the file + int32_t size = File::length(path, file); + + // Allocate memory filled with zeros + auto buffer = std::make_unique(size); + + // Fill buffer with file contents + file.read(buffer.get(), size); + file.close(); + + m_data = std::string(buffer.get(), size); } File::~File() @@ -47,6 +61,7 @@ int32_t File::length(std::string_view path, std::ifstream& file) return length; } +// FIXME: Deduplicate code with constructor, this broke binary files only std::shared_ptr File::raw(std::string_view path) { // Create input stream object and open file