summaryrefslogtreecommitdiff
path: root/src/io.hh
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2025-10-07 09:12:22 +0200
committerJoel Klinghed <the_jk@spawned.biz>2025-10-07 09:13:15 +0200
commitc87f9627efc8b612eb9b000acfcc6731cad15765 (patch)
tree34eb4b9e70a51c2f3db3a97c2aef31ba0b139ec9 /src/io.hh
Initial commit
Diffstat (limited to 'src/io.hh')
-rw-r--r--src/io.hh51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/io.hh b/src/io.hh
new file mode 100644
index 0000000..7c21028
--- /dev/null
+++ b/src/io.hh
@@ -0,0 +1,51 @@
+#ifndef IO_HH
+#define IO_HH
+
+#include <cstddef>
+#include <expected>
+#include <memory>
+#include <string>
+
+namespace io {
+
+enum class ReadError {
+ Error,
+ Eof,
+ InvalidData, // invalid data read (not used by raw file)
+ MaxTooSmall, // max argument needs to be bigger (not used by raw file)
+};
+
+enum class OpenError {
+ NoSuchFile,
+ NoAccess,
+ Error,
+};
+
+class Reader {
+ public:
+ virtual ~Reader() = default;
+
+ [[nodiscard]] virtual std::expected<size_t, ReadError> read(void* dst,
+ size_t max) = 0;
+ [[nodiscard]] virtual std::expected<size_t, ReadError> skip(size_t max) = 0;
+
+ [[nodiscard]] std::expected<size_t, ReadError> repeat_read(void* dst,
+ size_t max);
+ [[nodiscard]] std::expected<size_t, ReadError> repeat_skip(size_t max);
+
+ protected:
+ Reader() = default;
+
+ Reader(Reader const&) = delete;
+ Reader& operator=(Reader const&) = delete;
+};
+
+[[nodiscard]] std::expected<std::unique_ptr<Reader>, OpenError> open(
+ const std::string& file_path);
+[[nodiscard]] std::expected<std::unique_ptr<Reader>, OpenError> openat(
+ int dirfd, const std::string& file_path);
+[[nodiscard]] std::unique_ptr<Reader> memory(std::string data);
+
+} // namespace io
+
+#endif // IO_HH