diff options
author | crupest <crupest@outlook.com> | 2023-10-05 22:11:38 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2023-10-05 22:11:38 +0800 |
commit | 301b74c63deab5f09cc09d4edc07e0c86faa7cfa (patch) | |
tree | 8c98835eb5b775e95ac88db5bcd8173a1664ebc4 | |
parent | 1550828518ff4719db88f35e088207816866a073 (diff) | |
download | cru-301b74c63deab5f09cc09d4edc07e0c86faa7cfa.tar.gz cru-301b74c63deab5f09cc09d4edc07e0c86faa7cfa.tar.bz2 cru-301b74c63deab5f09cc09d4edc07e0c86faa7cfa.zip |
Remove WebFileStream.
-rw-r--r-- | include/cru/common/platform/web/WebFileStream.h | 44 | ||||
-rw-r--r-- | src/common/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/common/platform/web/WebFileStream.cpp | 111 |
3 files changed, 0 insertions, 156 deletions
diff --git a/include/cru/common/platform/web/WebFileStream.h b/include/cru/common/platform/web/WebFileStream.h deleted file mode 100644 index 76f3fa84..00000000 --- a/include/cru/common/platform/web/WebFileStream.h +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once - -#include "../../PreConfig.h" - -#ifdef CRU_PLATFORM_EMSCRIPTEN - -#include "../../io/Stream.h" -#include "cru/common/io/OpenFileFlag.h" - -namespace cru::platform::web { -/** - * \remark Web just hates filesystems. But luckily emscripten just creates a - * good simulation of it. All we need to do is to use the C api and let - * emscripten take care of it for us. - */ -class WebFileStream : public io::Stream { - public: - 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 - -#endif diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 97b49baf..b8d2ae16 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -44,7 +44,6 @@ if (EMSCRIPTEN) target_sources(CruBase PRIVATE platform/web/WebException.cpp - platform/web/WebFileStream.cpp ) endif() diff --git a/src/common/platform/web/WebFileStream.cpp b/src/common/platform/web/WebFileStream.cpp deleted file mode 100644 index 036be9a6..00000000 --- a/src/common/platform/web/WebFileStream.cpp +++ /dev/null @@ -1,111 +0,0 @@ -#include "cru/common/platform/web/WebFileStream.h" -#include "cru/common/io/OpenFileFlag.h" -#include "cru/common/platform/web/WebException.h" - -#include <cstdio> -#include <stdexcept> - -namespace cru::platform::web { -WebFileStream::WebFileStream(String path, io::OpenFileFlag flags) - : path_(std::move(path)), flags_(flags) { - // 1. Check all invalid flag combinations. - - using io::OpenFileFlags; - - if (!(flags & OpenFileFlags::Read) && !(flags & OpenFileFlags::Write)) { - throw std::invalid_argument( - "At least one of Read and Write flag must be specified when opening a " - "file."); - } - - if (!(flags & OpenFileFlags::Write) && - (flags & OpenFileFlags::Truncate || flags & OpenFileFlags::Append)) { - throw std::invalid_argument( - "Truncate and Append flag can only be used when opening a file with " - "Write flag."); - } - - if (flags & OpenFileFlags::Truncate && flags & OpenFileFlags::Append) { - throw std::invalid_argument( - "Truncate and Append flag can not be used together."); - } - - if (!(flags & OpenFileFlags::Create) && flags & OpenFileFlags::Exclusive) { - // Although linux doc says that this is an undefined behavior, but let's - // make it an error so you won't get some unexpected flags without noticing. - throw std::invalid_argument("Exclusive flag must be set with Create flag."); - } - - // 2. Handle with OpenFileFlags::Create and OpenFileFlags::Exclusive. - const auto utf8_path = path.ToUtf8(); - - if (!(flags & OpenFileFlags::Create)) { - auto file = std::fopen(utf8_path.c_str(), "r"); - if (file == NULL) { - // Note: Is there any other possible reason why fopen fails? - throw WebException(u"File does not exist."); - } else { - if (!(flags & OpenFileFlags::Write)) { - // We are not going to write the file, so we can just use this handle. - file_ = file; - return; - } else { - // We are going to write the file, so we have to close this handle and - // create a new one. - std::fclose(file); - } - } - } else { - if (flags & OpenFileFlags::Exclusive) { - auto file = std::fopen(utf8_path.c_str(), "r"); - if (file != NULL) { - std::fclose(file); - throw WebException(u"File already exists."); - } - } - } - - // 3. Now everything is ok, we can open the file with correct flags. - // There is only one edge case for C fopen: if you open a file read-only and - // specify Create flag, fopen won't create the file for you. - - if (flags & OpenFileFlags::Write) { - if (flags & OpenFileFlags::Read) { - if (flags & OpenFileFlags::Append) { - file_ = std::fopen(utf8_path.c_str(), "a+"); - } else { - file_ = std::fopen(utf8_path.c_str(), "w+"); - } - } else { - if (flags & OpenFileFlags::Append) { - file_ = std::fopen(utf8_path.c_str(), "a"); - } else { - file_ = std::fopen(utf8_path.c_str(), "w"); - } - } - } else { - // Open file read-only. - auto file = std::fopen(utf8_path.c_str(), "r"); - if (file == NULL) { - file = std::fopen(utf8_path.c_str(), "w"); - if (file == NULL) { - throw WebException(u"Failed to create file."); - } - std::fclose(file); - // Open it again. - file = std::fopen(utf8_path.c_str(), "r"); - } - file_ = file; - } - - if (file_ == NULL) { - throw WebException(u"Failed to open file."); - } -} - -WebFileStream::~WebFileStream() { - if (file_ != NULL) { - std::fclose(file_); - } -} -} // namespace cru::platform::web |