From dc28b7fb2870d83b140acad57e9e42862a9038fb Mon Sep 17 00:00:00 2001 From: YGauroa Date: Fri, 20 Dec 2024 15:38:30 +0800 Subject: [PATCH] fix --- include/tgfx/core/WriteStream.h | 20 ++++++++++---------- src/core/utils/WriteStream.cpp | 23 +++++++++++++---------- test/src/DataViewTest.cpp | 10 +++++++++- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/include/tgfx/core/WriteStream.h b/include/tgfx/core/WriteStream.h index e897503b..5c5cf36a 100644 --- a/include/tgfx/core/WriteStream.h +++ b/include/tgfx/core/WriteStream.h @@ -41,7 +41,7 @@ class WriteStream { * Writes bytes to the stream. Returns true if the write was successful. The actual write * operation is implemented in the subclasses. */ - virtual bool write(const char* data, size_t size) = 0; + virtual bool write(const void* data, size_t size) = 0; /** * Writes text to the stream. Returns true if the write was successful. @@ -87,7 +87,7 @@ class FileWriteStream : public WriteStream { /** * Writes bytes to the file stream. Returns true if the write was successful. */ - bool write(const char* data, size_t size) override; + bool write(const void* data, size_t size) override; /** * Returns the current size of the written bytes. @@ -135,7 +135,7 @@ class MemoryWriteStream : public WriteStream { /** * Writes bytes to the memory stream. Returns true if the write was successful. */ - bool write(const char* data, size_t size) override; + bool write(const void* data, size_t size) override; /** * Returns the current size of the written bytes. @@ -148,16 +148,16 @@ class MemoryWriteStream : public WriteStream { void flush() override{}; /** - * Reads data from the stream at the specified offset and size. Returns true if the read was - * successful. + * Copies a range of the buffer bytes into a Data object. If offset or length is out of range, + * they are clamped to the beginning and end of the buffer. Returns nullptr if the clamped length + * is 0. */ - bool read(char* data, size_t size, size_t offset); + std::shared_ptr copyRange(size_t offset, size_t length); /** - * Dumps all the written data as a Data object. The Data object is a copy of the current buffer - * and its lifecycle is independent of the MemoryWriteStream. + * Copies all the buffer bytes into a Data object. */ - std::shared_ptr dumpAsData(); + std::shared_ptr copy(); private: /** @@ -165,7 +165,7 @@ class MemoryWriteStream : public WriteStream { */ MemoryWriteStream() = default; - std::vector buffer; + std::vector buffer; }; } // namespace tgfx \ No newline at end of file diff --git a/src/core/utils/WriteStream.cpp b/src/core/utils/WriteStream.cpp index 79f7c310..e9a01f20 100644 --- a/src/core/utils/WriteStream.cpp +++ b/src/core/utils/WriteStream.cpp @@ -46,7 +46,7 @@ size_t FileWriteStream::size() const { return static_cast(ftell(file)); } -bool FileWriteStream::write(const char* data, size_t size) { +bool FileWriteStream::write(const void* data, size_t size) { if (file == nullptr) { return false; } @@ -78,8 +78,9 @@ std::unique_ptr MemoryWriteStream::Make() { return std::unique_ptr(new MemoryWriteStream()); } -bool MemoryWriteStream::write(const char* data, size_t size) { - buffer.insert(buffer.end(), data, data + size); +bool MemoryWriteStream::write(const void* data, size_t size) { + const auto* bytes = static_cast(data); + buffer.insert(buffer.end(), bytes, bytes + size); return true; } @@ -87,16 +88,18 @@ size_t MemoryWriteStream::size() const { return buffer.size(); } -bool MemoryWriteStream::read(char* data, size_t size, size_t offset) { - if (offset + size > buffer.size()) { - return false; +std::shared_ptr MemoryWriteStream::copyRange(size_t offset, size_t length) { + if (offset >= buffer.size()) { + return nullptr; } - std::copy(buffer.begin() + static_cast(offset), - buffer.begin() + static_cast(offset + size), data); - return true; + length = std::min(length, buffer.size() - offset); + if (length == 0) { + return nullptr; + } + return Data::MakeWithCopy(buffer.data() + offset, length); } -std::shared_ptr MemoryWriteStream::dumpAsData() { +std::shared_ptr MemoryWriteStream::copy() { return Data::MakeWithCopy(buffer.data(), buffer.size()); } diff --git a/test/src/DataViewTest.cpp b/test/src/DataViewTest.cpp index dfd0d5c0..4244b664 100644 --- a/test/src/DataViewTest.cpp +++ b/test/src/DataViewTest.cpp @@ -98,10 +98,18 @@ TGFX_TEST(DataViewTest, MemoryWriteStream) { const char* text = "TGFX"; stream->write(text, std::strlen(text)); - auto data = stream->dumpAsData(); + auto data = stream->copy(); ASSERT_TRUE(data != nullptr); EXPECT_EQ(data->size(), 10U); EXPECT_EQ(std::string((char*)data->bytes(), data->size()), "Hello\nTGFX"); + + data = stream->copyRange(6, 4); + ASSERT_TRUE(data != nullptr); + EXPECT_EQ(data->size(), 4U); + EXPECT_EQ(std::string((char*)data->bytes(), data->size()), "TGFX"); + + data = stream->copyRange(10, 10); + ASSERT_TRUE(data == nullptr); } TGFX_TEST(DataViewTest, FileWriteStream) {