extend ReadFile implementation

This commit is contained in:
ohyzha
2024-10-07 11:01:27 +03:00
parent 8c2cf49054
commit f8b85690b8
2 changed files with 18 additions and 3 deletions

View File

@@ -12,6 +12,7 @@
#include <pthread.h>
#endif
#include <fstream>
#include <filesystem>
#include <string>
#include <locale>
#include <codecvt>
@@ -61,13 +62,21 @@ namespace OpenVulkano
#endif
}
Array<char> Utils::ReadFile(const std::string& filePath, bool emptyOnMissing, bool nullTerminateString)
template<typename T>
Array<char> Utils::ReadFile(const T& filePath, bool emptyOnMissing, bool nullTerminateString)
{
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 '" + filePath + "'!");
if constexpr (std::is_same_v<std::remove_reference_t<std::remove_cv_t<T>>, std::filesystem::path>)
{
throw std::runtime_error("Failed to open file '" + filePath.string() + "'!");
}
else
{
throw std::runtime_error("Failed to open file '" + filePath + "'!");
}
}
const size_t fileSize = static_cast<size_t>(file.tellg());
Array<char> data(fileSize + nullTerminateString);
@@ -77,4 +86,9 @@ namespace OpenVulkano
file.close();
return data;
}
template Array<char> Utils::ReadFile<std::string>(const std::string& filePath, bool emptyOnMissing, bool nullTerminateString);
template Array<char> Utils::ReadFile<std::filesystem::path>(const std::filesystem::path& filePath,
bool emptyOnMissing, bool nullTerminateString);
}