Skip to content

Commit

Permalink
reader: expect iterator to return bool from _next
Browse files Browse the repository at this point in the history
Currently the iterator's next function returns an int which
returns >0 on success, 0 on end of iterator and <0 on error.
This commit changes the generic iterator implementation to return
a bool instead of an int. The bool is true if the iterator has
more data and false if the iterator is at the end. The error
code is now passed as a pointer parameter to the next function.
  • Loading branch information
Gottox committed Aug 21, 2023
1 parent ed2b8b2 commit 2e34e7b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion include/sqsh_reader_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ extern "C" {
*/

struct SqshReaderIteratorImpl {
int (*next)(void *iterator, size_t desired_size);
bool (*next)(void *iterator, size_t desired_size, int *err);
const uint8_t *(*data)(const void *iterator);
size_t (*size)(const void *iterator);
};
Expand Down
8 changes: 5 additions & 3 deletions lib/file/file_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@

#include "../utils/utils.h"

static int
file_iterator_next(void *iterator, size_t desired_size) {
return sqsh_file_iterator_next(iterator, desired_size);
static bool
file_iterator_next(void *iterator, size_t desired_size, int *err) {
int rv = sqsh_file_iterator_next(iterator, desired_size);
*err = rv;
return rv > 0;
}
static const uint8_t *
file_iterator_data(const void *iterator) {
Expand Down
8 changes: 5 additions & 3 deletions lib/mapper/map_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
#include "../../include/sqsh_error.h"
#include "../utils/utils.h"

static int
map_iterator_next(void *iterator, size_t desired_size) {
static bool
map_iterator_next(void *iterator, size_t desired_size, int *err) {
(void)desired_size;
return sqsh__map_iterator_next(iterator);
int rv = sqsh__map_iterator_next(iterator);
*err = rv;
return rv > 0;
}
static const uint8_t *
map_iterator_data(const void *iterator) {
Expand Down
8 changes: 5 additions & 3 deletions lib/metablock/metablock_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@
#include "../../include/sqsh_error.h"
#include "../utils/utils.h"

static int
metablock_iterator_next(void *iterator, size_t desired_size) {
static bool
metablock_iterator_next(void *iterator, size_t desired_size, int *err) {
(void)desired_size;
return sqsh__metablock_iterator_next(iterator);
int rv = sqsh__metablock_iterator_next(iterator);
*err = rv;
return rv > 0;
}
static const uint8_t *
metablock_iterator_data(const void *iterator) {
Expand Down
18 changes: 10 additions & 8 deletions lib/reader/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@

#include <assert.h>

static int
reader_iterator_next(struct SqshReader *reader, size_t desired_size) {
int rv = reader->iterator_impl->next(reader->iterator, desired_size);
if (rv == 0) {
rv = -SQSH_ERROR_OUT_OF_BOUNDS;
static bool
reader_iterator_next(struct SqshReader *reader, size_t desired_size, int *err) {
bool has_next = reader->iterator_impl->next(reader->iterator, desired_size, err);
if (has_next == false && *err == 0) {
*err = -SQSH_ERROR_OUT_OF_BOUNDS;
return false;
} else {
return has_next;
}
return rv;
}

/**
Expand Down Expand Up @@ -72,7 +74,7 @@ reader_iterator_skip(

while (current_size <= *offset) {
*offset -= current_size;
rv = reader_iterator_next(reader, desired_size);
reader_iterator_next(reader, desired_size, &rv);
if (rv < 0) {
goto out;
}
Expand Down Expand Up @@ -133,7 +135,7 @@ reader_fill_buffer(struct SqshReader *reader, size_t size) {
}

iterator_offset = size - remaining_size;
rv = reader_iterator_next(reader, remaining_size);
reader_iterator_next(reader, remaining_size, &rv);
if (rv < 0) {
goto out;
}
Expand Down
10 changes: 6 additions & 4 deletions test/reader/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,19 @@ test_iter_size(const void *data) {
}
}

static int
test_iter_next(void *data, size_t desired_size) {
static bool
test_iter_next(void *data, size_t desired_size, int *err) {
struct TestIterator *iter = (struct TestIterator *)data;
(void)desired_size;
(void)err;
if (iter->remaining == 0) {
iter->current_data = "";
return 0;
return false;
}
iter->current_data = iter->data;
iter->remaining--;
return strlen(iter->current_data);

return true;
}

static const struct SqshReaderIteratorImpl test_iter = {
Expand Down

0 comments on commit 2e34e7b

Please sign in to comment.