diff options
Diffstat (limited to 'Private/KernelKit/FileManager.hpp')
| -rw-r--r-- | Private/KernelKit/FileManager.hpp | 303 |
1 files changed, 168 insertions, 135 deletions
diff --git a/Private/KernelKit/FileManager.hpp b/Private/KernelKit/FileManager.hpp index 04055815..b3754f88 100644 --- a/Private/KernelKit/FileManager.hpp +++ b/Private/KernelKit/FileManager.hpp @@ -9,9 +9,9 @@ #pragma once -#include <NewKit/Stream.hpp> -#include <NewKit/Ref.hpp> #include <FSKit/NewFS.hxx> +#include <NewKit/Ref.hpp> +#include <NewKit/Stream.hpp> #include <NewKit/ErrorID.hpp> #include <NewKit/Ref.hpp> @@ -29,186 +29,219 @@ namespace hCore { - enum - { - kFileWriteAll = 100, - kFileReadAll = 101, - kFileReadChunk = 102, - kFileWriteChunk = 103, - }; +enum +{ + kFileWriteAll = 100, + kFileReadAll = 101, + kFileReadChunk = 102, + kFileWriteChunk = 103, +}; - typedef VoidPtr NodePtr; +typedef VoidPtr NodePtr; - class IFilesystemManager - { - public: - IFilesystemManager() = default; - virtual ~IFilesystemManager() = default; +class IFilesystemManager +{ + public: + IFilesystemManager() = default; + virtual ~IFilesystemManager() = default; - public: - HCORE_COPY_DEFAULT(IFilesystemManager); + public: + HCORE_COPY_DEFAULT(IFilesystemManager); - public: - static bool Mount(IFilesystemManager* pMount); - static IFilesystemManager* Unmount(); - static IFilesystemManager* GetMounted(); + public: + static bool Mount(IFilesystemManager *pMount); + static IFilesystemManager *Unmount(); + static IFilesystemManager *GetMounted(); - public: - virtual NodePtr Create(const char* path) = 0; - virtual NodePtr CreateAlias(const char* path) = 0; - virtual NodePtr CreateDirectory(const char* path) = 0; + public: + virtual NodePtr Create(const char *path) = 0; + virtual NodePtr CreateAlias(const char *path) = 0; + virtual NodePtr CreateDirectory(const char *path) = 0; - public: - virtual bool Remove(const char* path) = 0; + public: + virtual bool Remove(const char *path) = 0; - public: - virtual NodePtr Open(const char* path, const char* r) = 0; + public: + virtual NodePtr Open(const char *path, const char *r) = 0; - public: - virtual void Write(NodePtr node, VoidPtr data, Int32 flags) = 0; - virtual VoidPtr Read(NodePtr node, Int32 flags, SizeT sz) = 0; + public: + virtual void Write(NodePtr node, VoidPtr data, Int32 flags) = 0; + virtual VoidPtr Read(NodePtr node, Int32 flags, SizeT sz) = 0; - public: - virtual bool Seek(NodePtr node, SizeT off) = 0; + public: + virtual bool Seek(NodePtr node, SizeT off) = 0; - public: - virtual SizeT Tell(NodePtr node) = 0; - virtual bool Rewind(NodePtr node) = 0; + public: + virtual SizeT Tell(NodePtr node) = 0; + virtual bool Rewind(NodePtr node) = 0; +}; - }; +#define kNPos (SizeT)0xFFFFFF; - /** - * @brief Child of IFilesystemManager, takes care of managing NewFS disks. - */ - class NewFilesystemManager final : public IFilesystemManager - { - public: - explicit NewFilesystemManager(); - ~NewFilesystemManager() override; +/** + * @brief Child of IFilesystemManager, takes care of managing NewFS disks. + */ +class NewFilesystemManager final : public IFilesystemManager +{ + public: + explicit NewFilesystemManager(); + ~NewFilesystemManager() override; - public: - HCORE_COPY_DEFAULT(NewFilesystemManager); + public: + HCORE_COPY_DEFAULT(NewFilesystemManager); - public: - NodePtr Create(const char* path) override; - NodePtr CreateAlias(const char* path) override; - NodePtr CreateDirectory(const char* path) override; + public: + NodePtr Create(const char *path) override; + NodePtr CreateAlias(const char *path) override; + NodePtr CreateDirectory(const char *path) override; - public: - bool Remove(const char* node) override; + public: + bool Remove(const char *node) override; - public: - NodePtr Open(const char* path, const char* r) override { return nullptr; } + public: + NodePtr Open(const char *path, const char *r) override + { + if (!path || *path == 0) + return nullptr; + + if (!r || *r == 0) + return nullptr; - public: - void Write(NodePtr node, VoidPtr data, Int32 flags) override { } - VoidPtr Read(NodePtr node, Int32 flags, SizeT sz) override { return nullptr; } + return this->Open(path, r); + } - public: - bool Seek(NodePtr node, SizeT off) override { return true; } + public: + Void Write(NodePtr node, VoidPtr data, Int32 flags) override + { + this->Write(node, data, flags); + } - public: - SizeT Tell(NodePtr node) override { return 0; } - bool Rewind(NodePtr node) override { this->Seek(node, 0); return true; } + VoidPtr Read(NodePtr node, Int32 flags, SizeT sz) override + { + return this->Read(node, flags, sz); + } - public: - NewFSImpl* fIO{ nullptr }; + public: + bool Seek(NodePtr node, SizeT off) override + { + if (!node || off == 0) + return false; - }; + return this->Seek(node, off); + } - /** - * Usable FileStream - * @tparam Encoding file encoding (char, wchar_t...) - * @tparam FSClass Filesystem contract who takes care of it. - */ - template <typename Encoding = char, typename FSClass = NewFilesystemManager> - class FileStream final + public: + SizeT Tell(NodePtr node) override { - public: - explicit FileStream(const Encoding* path); - ~FileStream(); + if (!node) + return kNPos; - public: - FileStream &operator=(const FileStream &); \ - FileStream(const FileStream &); + return this->Tell(node); + } - public: - ErrorOr<Int64> WriteAll(const VoidPtr data) noexcept - { - if (data == nullptr) - return ErrorOr<Int64>(H_INVALID_DATA); + bool Rewind(NodePtr node) override + { + if (!node) + return false; - auto man = FSClass::GetMounted(); + return this->Seek(node, 0); + } - if (man) - { - man->Write(fFile, data, kFileWriteAll); - return ErrorOr<Int64>(0); - } + public: + NewFSImpl *fIO{nullptr}; +}; - return ErrorOr<Int64>(H_INVALID_DATA); - } +/** + * Usable FileStream + * @tparam Encoding file encoding (char, wchar_t...) + * @tparam FSClass Filesystem contract who takes care of it. + */ +template <typename Encoding = char, typename FSClass = NewFilesystemManager> class FileStream final +{ + public: + explicit FileStream(const Encoding *path); + ~FileStream(); - VoidPtr ReadAll() noexcept - { - auto man = FSClass::GetMounted(); + public: + FileStream &operator=(const FileStream &); + FileStream(const FileStream &); - if (man) - { - VoidPtr ret = man->Read(fFile, kFileReadAll, 0); - return ret; - } + public: + ErrorOr<Int64> WriteAll(const VoidPtr data) noexcept + { + if (data == nullptr) + return ErrorOr<Int64>(H_INVALID_DATA); - return nullptr; - } + auto man = FSClass::GetMounted(); - voidPtr Read(SizeT offset, SizeT sz) + if (man) { - auto man = FSClass::GetMounted(); + man->Write(fFile, data, kFileWriteAll); + return ErrorOr<Int64>(0); + } - if (man) - { - man->Seek(fFile, offset); - auto ret = man->Read(fFile, kFileReadChunk, sz); + return ErrorOr<Int64>(H_INVALID_DATA); + } - return ret; - } + VoidPtr ReadAll() noexcept + { + auto man = FSClass::GetMounted(); - return nullptr; + if (man) + { + VoidPtr ret = man->Read(fFile, kFileReadAll, 0); + return ret; } - void Write(SizeT offset, voidPtr data, SizeT sz) + return nullptr; + } + + voidPtr Read(SizeT offset, SizeT sz) + { + auto man = FSClass::GetMounted(); + + if (man) { - auto man = FSClass::GetMounted(); + man->Seek(fFile, offset); + auto ret = man->Read(fFile, kFileReadChunk, sz); - if (man) - { - man->Seek(fFile, offset); - man->Write(fFile, data, sz, kFileReadChunk); - } + return ret; } - public: - const char* MIME() noexcept { return fMime; } + return nullptr; + } - private: - NodePtr fFile; - Char* fMime{ "application-type/*" }; + void Write(SizeT offset, voidPtr data, SizeT sz) + { + auto man = FSClass::GetMounted(); - }; + if (man) + { + man->Seek(fFile, offset); + man->Write(fFile, data, sz, kFileReadChunk); + } + } - using FileStreamUTF8 = FileStream<char>; - using FileStreamUTF16 = FileStream<wchar_t>; + public: + const char *MIME() noexcept + { + return fMime; + } - template <typename Encoding, typename Class> - FileStream<Encoding, Class>::FileStream(const Encoding* path) - : fFile(Class::GetMounted()->Open(path, "r+")) - {} + private: + NodePtr fFile; + Char *fMime{"application-type/*"}; +}; - template <typename Encoding, typename Class> - FileStream<Encoding, Class>::~FileStream() = default; -} +using FileStreamUTF8 = FileStream<char>; +using FileStreamUTF16 = FileStream<wchar_t>; -#define node_cast(PTR) reinterpret_cast<hCore::NodePtr>(PTR) +template <typename Encoding, typename Class> +FileStream<Encoding, Class>::FileStream(const Encoding *path) : fFile(Class::GetMounted()->Open(path, "r+")) +{ +} +template <typename Encoding, typename Class> FileStream<Encoding, Class>::~FileStream() = default; +} // namespace hCore +#define node_cast(PTR) reinterpret_cast<hCore::NodePtr>(PTR) |
