From 47a5abd3ce92592c87f86d0cf33f57fff687c246 Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Thu, 28 Jul 2022 19:24:22 -0300 Subject: keyword: Make `keyword_cb`, and `set_keyword()` return an `int` `set_keyword()` now checks the return value of the callback to determine whether to return early, fallback to the default, or return an error if the callback returns true, false, and -1 respectivly. --- keyword.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'keyword.c') diff --git a/keyword.c b/keyword.c index 8b92191..c033242 100644 --- a/keyword.c +++ b/keyword.c @@ -29,10 +29,12 @@ keyword_val get_keyword_value(const keyword *key, char *value, int *error) { return val; } -void set_keyword(const keyword *key, keyword_val val, void *ret, void *ctx) { - if (key->callback != NULL) { - key->callback(ctx, ret, key, val); +int set_keyword(const keyword *key, keyword_val val, void *ret, void *ctx) { + const int callback_ret = (key->callback != NULL) ? key->callback(ctx, ret, key, val) : 0; + if (callback_ret < 0 || callback_ret > 0) { + return (callback_ret > 0) ? 0 : 5; } else { + char *tmp_ret = (char *)get_keyword_offset_ptr(key, ret); char *tmp_ret = (char *)ret; size_t offset = -1; for (int i = 0; (int64_t)key->offsets[i] >= 0; ++i) { @@ -48,8 +50,9 @@ void set_keyword(const keyword *key, keyword_val val, void *ret, void *ctx) { case TYPE_TIME : *(time_t *)(tmp_ret+offset) = val.t; break; case TYPE_STRING: *(char **)(tmp_ret+offset) = val.str; break; case TYPE_FLOAT : *(float *)(tmp_ret+offset) = val.f; break; - default : break; + default : return 6; break; } + return 0; } } @@ -73,8 +76,7 @@ int parse_keywords(const keyword **keys, char *key, char *value, void *ret, void val = parse_keyword(keys[i], key, value, &error); if (!error) { - set_keyword(keys[i], val, ret, ctx); - return 0; + return set_keyword(keys[i], val, ret, ctx); } } return error; -- cgit v1.2.3-13-gbd6f