aboutsummaryrefslogtreecommitdiff
path: root/app/source/Cplt/Utils/IO/FileStream.hpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2022-06-30 21:38:53 -0700
committerrtk0c <[email protected]>2022-06-30 21:38:53 -0700
commit7fe47a9d5b1727a61dc724523b530762f6d6ba19 (patch)
treee95be6e66db504ed06d00b72c579565bab873277 /app/source/Cplt/Utils/IO/FileStream.hpp
parent2cf952088d375ac8b2f45b144462af0953436cff (diff)
Restructure project
Diffstat (limited to 'app/source/Cplt/Utils/IO/FileStream.hpp')
-rw-r--r--app/source/Cplt/Utils/IO/FileStream.hpp97
1 files changed, 97 insertions, 0 deletions
diff --git a/app/source/Cplt/Utils/IO/FileStream.hpp b/app/source/Cplt/Utils/IO/FileStream.hpp
new file mode 100644
index 0000000..453ddbe
--- /dev/null
+++ b/app/source/Cplt/Utils/IO/FileStream.hpp
@@ -0,0 +1,97 @@
+#pragma once
+
+#include <cstddef>
+#include <cstdint>
+#include <filesystem>
+#include <memory>
+
+// TODO switch to custom when unit tests are ready and bugs are fixed
+#define CPLT_FILESTREAM_USE_CSTDIO
+
+struct IoResult
+{
+ enum ErrorKind
+ {
+ ERR_None,
+ ERR_PermissionDenied,
+ ERR_UnexpectedEof,
+ ERR_Unsupported,
+ ERR_OutOfSpace,
+ ERR_Other,
+ };
+
+ ErrorKind Error;
+ uint32_t SystemError;
+ size_t BytesMoved;
+};
+
+class InputFileStream
+{
+private:
+#if defined(CPLT_FILESTREAM_USE_CSTDIO)
+ FILE* mFile;
+#else
+ alignas(void*) char mOsFileHandle[sizeof(void*)];
+
+ // mBuffer is always mReadInSize size
+ std::unique_ptr<std::byte[]> mBuffer;
+ int mReadInSize = 1024;
+
+ int mFirstByteIdx = 0;
+ int mAvailableBytes = 0;
+
+ bool mEof = false;
+#endif
+
+public:
+ InputFileStream(const std::filesystem::path& path);
+ ~InputFileStream();
+
+ InputFileStream(const InputFileStream&) = delete;
+ InputFileStream& operator=(const InputFileStream&) = delete;
+ InputFileStream(InputFileStream&&);
+ InputFileStream& operator=(InputFileStream&&);
+
+ int GetReadInSize() const;
+ void SetReadInSize(int size);
+
+ bool IsEof() const;
+
+ IoResult ReadBytes(size_t bufferLength, std::byte* buffer);
+};
+
+class OutputFileStream
+{
+public:
+ enum WriteMode
+ {
+ AppendFile,
+ TruncateFile,
+ };
+
+private:
+#if defined(CPLT_FILESTREAM_USE_CSTDIO)
+ FILE* mFile;
+#else
+ alignas(void*) char mOsFileHandle[sizeof(void*)];
+ std::unique_ptr<std::byte[]> mBuffer;
+ int mMaxBufferSize = 1024;
+ int mCurrentBufferSize = 0;
+#endif
+
+public:
+ OutputFileStream(const std::filesystem::path& path, WriteMode mode);
+ ~OutputFileStream();
+
+ OutputFileStream(const OutputFileStream&) = delete;
+ OutputFileStream& operator=(const OutputFileStream&) = delete;
+ OutputFileStream(OutputFileStream&&);
+ OutputFileStream& operator=(OutputFileStream&&);
+
+ int GetMaxBufferSize() const;
+ void SetMaxBufferSize(int maxSize);
+
+ IoResult WriteBytes(size_t bufferLength, const std::byte* buffer);
+
+ void FlushBuffer();
+};