diff options
Diffstat (limited to 'include/cru')
-rw-r--r-- | include/cru/common/io/FileStream.h | 28 | ||||
-rw-r--r-- | include/cru/common/io/OpenFileFlag.h | 5 | ||||
-rw-r--r-- | include/cru/common/platform/web/WebFileStream.h | 26 |
3 files changed, 56 insertions, 3 deletions
diff --git a/include/cru/common/io/FileStream.h b/include/cru/common/io/FileStream.h index 6f84526c..333b6d64 100644 --- a/include/cru/common/io/FileStream.h +++ b/include/cru/common/io/FileStream.h @@ -1,3 +1,31 @@ +/** + * Here are some notes about FileStream: + * + * 1. FileStream is currently implemented as a typedef of the corresponding + * specific XxxFileStream class implemented on each platform and controled with + * preprocessor commands. There might be some other ways like proxy pattern but + * I do this way for simplicity. So your duty to implement a new platform is to + * define a new class and ensure it implements all the required interface. And + * in this way you are free to expose any other additional interface like for + * specific platform. + * + * 2. Since each platform defines their own way to open a file, especially the + * flags to open a file, we have to define a common interface. I decide to + * mimic the C++ std stream open flags + * (https://en.cppreference.com/w/cpp/io/basic_filebuf/open) so on platforms + * where there is no direct support on certain flags we try our best to + * simulate it and make a note for users. The difference between cru design and + * std design is: + * 1. cru deletes `binary` flag because Stream does not have a so-called + * _text_ mode and it is always byte based. + * 2. cru adds `OpenFileFlags::Create` and `OpenFileFlags::Exclusive` flags + * mimicking Linux behavior which does not exist in std. + * + * (TODO: Currently the problem is that when I implemented for Windows and UNIX + * I didn't take this into consideration so I have to fix this inconsistency + * later.) + */ + #pragma once #include "../PreConfig.h" diff --git a/include/cru/common/io/OpenFileFlag.h b/include/cru/common/io/OpenFileFlag.h index bfbdd38a..bca1d737 100644 --- a/include/cru/common/io/OpenFileFlag.h +++ b/include/cru/common/io/OpenFileFlag.h @@ -12,7 +12,8 @@ struct OpenFileFlags { static constexpr OpenFileFlag Read{0x1}; static constexpr OpenFileFlag Write{0x2}; static constexpr OpenFileFlag Append{0x4}; - static constexpr OpenFileFlag Create{0x8}; - static constexpr OpenFileFlag Truncate{0x10}; + static constexpr OpenFileFlag Truncate{0x8}; + static constexpr OpenFileFlag Create{0x10}; + static constexpr OpenFileFlag Exclusive{0x20}; }; } // namespace cru::io diff --git a/include/cru/common/platform/web/WebFileStream.h b/include/cru/common/platform/web/WebFileStream.h index 66ee0903..171df495 100644 --- a/include/cru/common/platform/web/WebFileStream.h +++ b/include/cru/common/platform/web/WebFileStream.h @@ -1,10 +1,12 @@ #pragma once +#include <_stdio.h> #include "../../PreConfig.h" #ifdef CRU_PLATFORM_EMSCRIPTEN #include "../../io/Stream.h" +#include "cru/common/io/OpenFileFlag.h" namespace cru::platform::web { /** @@ -14,7 +16,29 @@ namespace cru::platform::web { */ class WebFileStream : public io::Stream { public: - // TODO: go on this! + WebFileStream(String path, io::OpenFileFlag flags); + + ~WebFileStream() override; + + public: + bool CanSeek() override; + Index Seek(Index offset, SeekOrigin origin = SeekOrigin::Current) override; + + bool CanRead() override; + Index Read(std::byte* buffer, Index offset, Index size) override; + using Stream::Read; + + bool CanWrite() override; + Index Write(const std::byte* buffer, Index offset, Index size) override; + using Stream::Write; + + void Close() override; + + private: + String path_; + io::OpenFileFlag flags_; + + FILE* file_; }; } // namespace cru::platform::web |