put foreach inner loop into separate function

This commit is contained in:
Alain Zscheile 2023-01-02 10:05:47 +01:00
parent 3ae7f219e6
commit a00e9a7dcd

44
idx.c
View file

@ -48,6 +48,30 @@ int y16t_idx_update(
return 0;
}
static int y16t_idx_foreach_inner(
FILE *fh,
long *offset,
uint8_t x,
uint8_t y,
int (*callback)(uint32_t, void*),
void *context
) {
const uint32_t key = y16t_table_at(x, y);
const long delta = ((long)key) - *offset;
if(delta && fseek(fh, delta, SEEK_CUR) == -1)
return -EIO;
*offset = key;
uint32_t value = 0;
if(fread(&value, 4, 1, fh) != 1)
return -EIO;
const int tmp = callback(ntohl(value), context);
if(tmp && *offset) {
fseek(fh, -*offset, SEEK_CUR);
*offset = 0;
}
return tmp;
}
int y16t_idx_foreach_x(
FILE *fh,
uint8_t y,
@ -57,15 +81,7 @@ int y16t_idx_foreach_x(
long offset = 0;
for (uint16_t x = 0; x < 0xff; ++x) {
uint32_t value = 0;
const uint32_t key = y16t_table_at(x, y);
const long delta = ((long)key) - offset;
if(delta && fseek(fh, delta, SEEK_CUR) == -1)
return -EIO;
offset = key;
if(fread(&value, 4, 1, fh) != 1)
return -EIO;
int tmp = callback(ntohl(value), context);
const int tmp = y16t_idx_foreach_inner(fh, &offset, x, y, callback, context);
if(tmp) return tmp;
}
@ -84,15 +100,7 @@ int y16t_idx_foreach_y(
long offset = 0;
for (uint16_t y = 0; y < 0xff; ++y) {
uint32_t value = 0;
const uint32_t key = y16t_table_at(x, y);
const long delta = ((long)key) - offset;
if(delta && fseek(fh, delta, SEEK_CUR) == -1)
return -EIO;
offset = key;
if(fread(&value, 4, 1, fh) != 1)
return -EIO;
int tmp = callback(ntohl(value), context);
const int tmp = y16t_idx_foreach_inner(fh, &offset, x, y, callback, context);
if(tmp) return tmp;
}