From 7200166d998c0d446af76860604f34da3d17a354 Mon Sep 17 00:00:00 2001 From: ohyzha Date: Fri, 27 Dec 2024 16:49:43 +0200 Subject: [PATCH] support file reading from string literal --- openVulkanoCpp/Base/Utils.cpp | 2 +- openVulkanoCpp/Base/Utils.hpp | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/openVulkanoCpp/Base/Utils.cpp b/openVulkanoCpp/Base/Utils.cpp index 98c80c7..1e2f28f 100644 --- a/openVulkanoCpp/Base/Utils.cpp +++ b/openVulkanoCpp/Base/Utils.cpp @@ -70,7 +70,7 @@ namespace OpenVulkano if (!file.is_open()) { if (emptyOnMissing) return {}; - if constexpr (std::is_same_v>, std::filesystem::path>) + if constexpr (std::is_same_v, std::filesystem::path>) { throw std::runtime_error("Failed to open file '" + filePath.string() + "'!"); } diff --git a/openVulkanoCpp/Base/Utils.hpp b/openVulkanoCpp/Base/Utils.hpp index 8782429..c8ac6e0 100644 --- a/openVulkanoCpp/Base/Utils.hpp +++ b/openVulkanoCpp/Base/Utils.hpp @@ -15,6 +15,7 @@ #include #include #include +#include #include "Data/Containers/Array.hpp" namespace OpenVulkano @@ -190,6 +191,31 @@ namespace OpenVulkano static Array ReadFile(const T& filePath, bool emptyOnMissing = false, bool nullTerminateString = false); + template + static Array ReadFile(const char (&filePath)[N], bool emptyOnMissing = false, + bool nullTerminateString = false) + { + std::ifstream file(filePath, std::ios::ate | std::ios::binary); + if (!file.is_open()) + { + if (emptyOnMissing) + { + return {}; + } + throw std::runtime_error("Failed to open file '" + std::string(filePath) + "'!"); + } + const size_t fileSize = static_cast(file.tellg()); + Array data(fileSize + nullTerminateString); + file.seekg(0); + file.read(data.Data(), fileSize); + if (nullTerminateString) + { + data[fileSize] = '\0'; + } + file.close(); + return data; + } + template static int GetUniqueTypeId() {