The callback and the opaque data are now grouped together in a struct instead of being passed individually into the tokenizer. This also exposes the string source reader struct and therefore removes the need of heap allocating it. Neat!
38 lines
881 B
C
38 lines
881 B
C
#include <assert.h>
|
|
#include <string.h>
|
|
|
|
#include "apfl.h"
|
|
|
|
static bool
|
|
reader_callback(void *opaque, char *buf, size_t *len, bool need)
|
|
{
|
|
(void)need;
|
|
|
|
struct apfl_string_source_reader_data *ctx = opaque;
|
|
|
|
size_t maxlen = *len;
|
|
size_t remain_len = ctx->sv.len - ctx->off;
|
|
*len = maxlen < remain_len ? maxlen : remain_len;
|
|
memcpy(buf, ctx->sv.bytes + ctx->off, *len);
|
|
ctx->off += *len;
|
|
assert(ctx->off <= ctx->sv.len);
|
|
|
|
return true;
|
|
}
|
|
|
|
struct apfl_string_source_reader_data
|
|
apfl_string_source_reader_create(struct apfl_string_view sv)
|
|
{
|
|
return (struct apfl_string_source_reader_data) {
|
|
.sv = sv,
|
|
.off = 0,
|
|
};
|
|
}
|
|
|
|
struct apfl_source_reader apfl_string_source_reader(struct apfl_string_source_reader_data *data)
|
|
{
|
|
return (struct apfl_source_reader) {
|
|
.callback = reader_callback,
|
|
.opaque = data,
|
|
};
|
|
}
|