Cleaned up Util functions

This commit is contained in:
Pasha Bibko
2025-07-20 21:04:24 +01:00
parent cc0300b66c
commit 6a0d2fc0a2
3 changed files with 107 additions and 103 deletions

View File

@@ -5,6 +5,7 @@
namespace LXC::Util
{
// Error returned when Util::ReadFile runs into errors //
struct FileReadError
{
// Different reasons why the error can occur //
@@ -38,20 +39,21 @@ namespace LXC::Util
}
};
// Util function to read a file as quick as possible with error handling //
inline ReturnVal<std::string, FileReadError> ReadFile(const std::filesystem::path& filepath)
{
// Checks the file exists //
if (!std::filesystem::exists(filepath))
return FunctionFail(FileReadError(std::filesystem::absolute(filepath), FileReadError::FileNotFound));
return FunctionFail<FileReadError>(std::filesystem::absolute(filepath), FileReadError::FileNotFound);
// Checks it is a regular file //
if (!std::filesystem::is_regular_file(filepath))
return FunctionFail(FileReadError(std::filesystem::absolute(filepath), FileReadError::NotAFile));
return FunctionFail<FileReadError>(std::filesystem::absolute(filepath), FileReadError::NotAFile);
// Checks it can open the file //
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
if (!file)
return FunctionFail(FileReadError(std::filesystem::absolute(filepath), FileReadError::PermissionDenied));
return FunctionFail<FileReadError>(std::filesystem::absolute(filepath), FileReadError::PermissionDenied);
// Copies the file to the output string //
const std::streamsize len = file.tellg();