Cleaned up Util functions
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user