From 07d30629d78474ed810cad73ead71db96079aff0 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Sat, 28 Sep 2024 00:23:22 -0500 Subject: Add directory interface. --- include/fud_c_file.hpp | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to 'include/fud_c_file.hpp') diff --git a/include/fud_c_file.hpp b/include/fud_c_file.hpp index f563a35..674130c 100644 --- a/include/fud_c_file.hpp +++ b/include/fud_c_file.hpp @@ -18,8 +18,8 @@ #ifndef FUD_C_FILE_HPP #define FUD_C_FILE_HPP -#include "fud_string.hpp" #include "fud_result.hpp" +#include "fud_string.hpp" #include #include @@ -88,7 +88,8 @@ enum class FileStatus Error, }; -static inline const char* FileStatusToString(FileStatus status) { +static inline const char* FileStatusToString(FileStatus status) +{ switch (status) { case FileStatus::Success: return "Success"; @@ -114,6 +115,11 @@ struct [[nodiscard]] ReadResult { FileStatus status{FileStatus::Success}; }; +struct [[nodiscard]] WriteResult { + size_t bytesWritten{0}; + FileStatus status{FileStatus::Success}; +}; + class CBinaryFile { public: CBinaryFile(const String& filename, CFileMode mode); @@ -146,6 +152,32 @@ class CBinaryFile { return read(&destination, sizeof(destination), length, offset); } + [[nodiscard]] WriteResult write(const void* source, size_t sourceSize, size_t length); + + [[nodiscard]] WriteResult write(const void* source, size_t sourceSize, size_t length, size_t offset); + + template + [[nodiscard]] WriteResult write(const T& source) + { + return write(source, sizeof(source), sizeof(source)); + } + + template + [[nodiscard]] WriteResult write(const T& source, size_t sourceSize, size_t length) + { + auto offsetResult = size(); + if (offsetResult.isError()) { + return WriteResult{0, offsetResult.getError()}; + } + return write(source, sourceSize, length, offsetResult.getOkay()); + } + + template + [[nodiscard]] WriteResult write(const T& source, size_t sourceSize, size_t length, size_t offset) + { + return write(static_cast(&source), sourceSize, length, offset); + } + private: FileStatus reset() const; @@ -158,5 +190,4 @@ class CBinaryFile { } // namespace fud - #endif -- cgit v1.2.3