Merge pull request 'Extend ReadFile implementation' (#135) from misc into master
Reviewed-on: https://git.madvoxel.net/OpenVulkano/OpenVulkano/pulls/135 Reviewed-by: Georg Hagen <georg.hagen@madvoxel.com>
This commit is contained in:
@@ -12,6 +12,7 @@
|
|||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#endif
|
#endif
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <filesystem>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <locale>
|
#include <locale>
|
||||||
#include <codecvt>
|
#include <codecvt>
|
||||||
@@ -61,13 +62,21 @@ namespace OpenVulkano
|
|||||||
#endif
|
#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);
|
std::ifstream file(filePath, std::ios::ate | std::ios::binary);
|
||||||
if (!file.is_open())
|
if (!file.is_open())
|
||||||
{
|
{
|
||||||
if (emptyOnMissing) return {};
|
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());
|
const size_t fileSize = static_cast<size_t>(file.tellg());
|
||||||
Array<char> data(fileSize + nullTerminateString);
|
Array<char> data(fileSize + nullTerminateString);
|
||||||
@@ -77,4 +86,9 @@ namespace OpenVulkano
|
|||||||
file.close();
|
file.close();
|
||||||
return data;
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -185,7 +185,8 @@ namespace OpenVulkano
|
|||||||
return subs;
|
return subs;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Array<char> ReadFile(const std::string& filePath, bool emptyOnMissing = false,
|
template<typename T>
|
||||||
|
static Array<char> ReadFile(const T& filePath, bool emptyOnMissing = false,
|
||||||
bool nullTerminateString = false);
|
bool nullTerminateString = false);
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
|
|||||||
Reference in New Issue
Block a user