Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
YGaurora committed Dec 20, 2024
1 parent bedaa0c commit dc28b7f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
20 changes: 10 additions & 10 deletions include/tgfx/core/WriteStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -148,24 +148,24 @@ 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<Data> 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<Data> dumpAsData();
std::shared_ptr<Data> copy();

private:
/**
* Creates a new MemoryWriteStream object.
*/
MemoryWriteStream() = default;

std::vector<char> buffer;
std::vector<uint8_t> buffer;
};

} // namespace tgfx
23 changes: 13 additions & 10 deletions src/core/utils/WriteStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ size_t FileWriteStream::size() const {
return static_cast<size_t>(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;
}
Expand Down Expand Up @@ -78,25 +78,28 @@ std::unique_ptr<MemoryWriteStream> MemoryWriteStream::Make() {
return std::unique_ptr<MemoryWriteStream>(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<const uint8_t*>(data);
buffer.insert(buffer.end(), bytes, bytes + size);
return true;
}

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<Data> MemoryWriteStream::copyRange(size_t offset, size_t length) {
if (offset >= buffer.size()) {
return nullptr;
}
std::copy(buffer.begin() + static_cast<std::ptrdiff_t>(offset),
buffer.begin() + static_cast<std::ptrdiff_t>(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<Data> MemoryWriteStream::dumpAsData() {
std::shared_ptr<Data> MemoryWriteStream::copy() {
return Data::MakeWithCopy(buffer.data(), buffer.size());
}

Expand Down
10 changes: 9 additions & 1 deletion test/src/DataViewTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit dc28b7f

Please sign in to comment.