Skip to content

Commit

Permalink
Merge pull request #44 from Gottox/feature/refactor-reader
Browse files Browse the repository at this point in the history
reader: refactor reader_fill_buffer()
  • Loading branch information
Gottox authored Aug 18, 2023
2 parents 0295b78 + 4301074 commit 2ab7f29
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/reader/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,36 +114,38 @@ reader_fill_buffer(struct SqshReader *reader, size_t size) {
const struct SqshReaderIteratorImpl *impl = reader->iterator_impl;
struct SqshBuffer *buffer = &reader->buffer;
sqsh_index_t offset = reader->offset;
sqsh_index_t iterator_offset = reader->iterator_offset;

size_t buffer_size = sqsh__buffer_size(buffer);
size_t remaining_size = size - sqsh__buffer_size(buffer);
for (;;) {
const uint8_t *data = impl->data(iterator);
const size_t data_size = impl->size(iterator);
const size_t copy_size =
SQSH_MIN(data_size - offset, size - buffer_size);
const size_t copy_size = SQSH_MIN(data_size - offset, remaining_size);
rv = sqsh__buffer_append(buffer, &data[offset], copy_size);
if (rv < 0) {
goto out;
}

offset = 0;
buffer_size += copy_size;
if (size <= buffer_size) {
assert(size == buffer_size);
remaining_size -= copy_size;
if (remaining_size == 0) {
break;
}

rv = reader_iterator_next(reader, size);
iterator_offset = size - remaining_size;
rv = reader_iterator_next(reader, remaining_size);
if (rv < 0) {
goto out;
}
reader->iterator_offset = buffer_size;
}

assert(size == sqsh__buffer_size(buffer));
assert(iterator_offset != 0);

reader->iterator_offset = iterator_offset;
reader->offset = 0;
reader->data = sqsh__buffer_data(buffer);
reader->size = size;
assert(reader->iterator_offset != 0);
out:
return rv;
}
Expand Down

0 comments on commit 2ab7f29

Please sign in to comment.