summaryrefslogtreecommitdiff
path: root/include/fud_c_file.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'include/fud_c_file.hpp')
-rw-r--r--include/fud_c_file.hpp37
1 files changed, 34 insertions, 3 deletions
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 <cstdint>
#include <cstdio>
@@ -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 <typename T>
+ [[nodiscard]] WriteResult write(const T& source)
+ {
+ return write(source, sizeof(source), sizeof(source));
+ }
+
+ template <typename T>
+ [[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 <typename T>
+ [[nodiscard]] WriteResult write(const T& source, size_t sourceSize, size_t length, size_t offset)
+ {
+ return write(static_cast<const void*>(&source), sourceSize, length, offset);
+ }
+
private:
FileStatus reset() const;
@@ -158,5 +190,4 @@ class CBinaryFile {
} // namespace fud
-
#endif