From d73d8b6484206730ab501c6f7fe62691fd2bf10b Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 5 Sep 2023 10:54:53 +0200 Subject: [PATCH] libsmartcols: (filter) evaluate params Signed-off-by: Karel Zak --- libsmartcols/samples/filter.c | 14 ++++++++++++-- libsmartcols/src/filter-param.c | 31 ++++++++++++++++++++++++++++++ libsmartcols/src/filter.c | 25 ++++++++++++++++++++++++ libsmartcols/src/libsmartcols.h.in | 3 +++ libsmartcols/src/libsmartcols.sym | 1 + libsmartcols/src/smartcolsP.h | 4 ++++ 6 files changed, 76 insertions(+), 2 deletions(-) diff --git a/libsmartcols/samples/filter.c b/libsmartcols/samples/filter.c index aaf7fb69ea..fe8121c8e1 100644 --- a/libsmartcols/samples/filter.c +++ b/libsmartcols/samples/filter.c @@ -155,8 +155,18 @@ int main(int argc, char *argv[]) goto done; } - for (i = 0; i < 10; i++) - add_line(tb, i + 1); + for (i = 0; i < 10; i++) { + struct libscols_line *ln = add_line(tb, i + 1); + int rc, status = 0; + + if (!fltr) + continue; + rc = scols_line_apply_filter(ln, fltr, &status); + if (rc) + goto done; + if (status == 0) /* false */ + scols_table_remove_line(tb, ln); + } scols_print_table(tb); done: diff --git a/libsmartcols/src/filter-param.c b/libsmartcols/src/filter-param.c index cec98691de..f67efa509a 100644 --- a/libsmartcols/src/filter-param.c +++ b/libsmartcols/src/filter-param.c @@ -81,6 +81,37 @@ void filter_dump_param(struct ul_jsonwrt *json, struct filter_param *n) ul_jsonwrt_object_close(json); } +int filter_eval_param(struct libscols_filter *fltr __attribute__((__unused__)), + struct filter_param *n, + struct libscols_line *ln __attribute__((__unused__)), + int *status) +{ + int rc = 0; + + switch (n->type) { + case F_PARAM_NAME: /* TODO */ + break; + case F_PARAM_STRING: + *status = n->val.str != NULL && *n->val.str != '\0'; + break; + case F_PARAM_NUMBER: + *status = n->val.num != 0; + break; + case F_PARAM_FLOAT: + *status = n->val.fnum != 0.0; + break; + case F_PARAM_BOOLEAN: + *status = n->val.boolean != false; + break; + default: + rc = -EINVAL; + break; + } + + DBG(FLTR, ul_debugobj(fltr, "eval param [rc=%d, status=%d]", rc, *status)); + return rc; +} + int filter_next_param(struct libscols_filter *fltr, struct libscols_iter *itr, struct filter_param **prm) { diff --git a/libsmartcols/src/filter.c b/libsmartcols/src/filter.c index e960ba66d2..4a1e56b28a 100644 --- a/libsmartcols/src/filter.c +++ b/libsmartcols/src/filter.c @@ -188,3 +188,28 @@ int scols_filter_assign_column(struct libscols_filter *fltr, return ct == 0 ? 1 : 0; } + +int filter_eval_node(struct libscols_filter *fltr, struct filter_node *n, + struct libscols_line *ln, int *status) +{ + switch (n->type) { + case F_NODE_PARAM: + return filter_eval_param(fltr, (struct filter_param *) n, ln, status); + /* TODO + case F_NODE_EXPR: + return filter_eval_expr(fltr, (struct filter_expr *) n, ln, status); + */ + default: + break; + } + return -EINVAL; +} + +int scols_line_apply_filter(struct libscols_line *ln, + struct libscols_filter *fltr, int *status) +{ + if (!ln || !fltr || !fltr->root) + return -EINVAL; + + return filter_eval_node(fltr, fltr->root, ln, status); +} diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index bb23e1f08b..ae1f581450 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -374,6 +374,9 @@ extern void scols_unref_filter(struct libscols_filter *fltr); extern int scols_dump_filter(struct libscols_filter *fltr, FILE *out); extern const char *scols_filter_get_errmsg(struct libscols_filter *fltr); +extern int scols_line_apply_filter(struct libscols_line *ln, + struct libscols_filter *fltr, int *status); + extern int scols_filter_next_name(struct libscols_filter *fltr, struct libscols_iter *itr, const char **name); extern int scols_filter_assign_column(struct libscols_filter *fltr, diff --git a/libsmartcols/src/libsmartcols.sym b/libsmartcols/src/libsmartcols.sym index b50c80d79b..5e0c560696 100644 --- a/libsmartcols/src/libsmartcols.sym +++ b/libsmartcols/src/libsmartcols.sym @@ -228,4 +228,5 @@ SMARTCOLS_2.40 { scols_filter_get_errmsg; scols_filter_next_name; scols_filter_assign_column; + scols_line_apply_filter; } SMARTCOLS_2.39; diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index 0119cedfd5..0407b089a0 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -559,9 +559,13 @@ void filter_ref_node(struct filter_node *n); void filter_unref_node(struct filter_node *n); void filter_dump_node(struct ul_jsonwrt *json, struct filter_node *n); +int filter_eval_node(struct libscols_filter *fltr, struct filter_node *n, + struct libscols_line *ln, int *status); /* param */ void filter_dump_param(struct ul_jsonwrt *json, struct filter_param *n); +int filter_eval_param(struct libscols_filter *fltr, struct filter_param *n, + struct libscols_line *ln, int *status); void filter_free_param(struct filter_param *n); int filter_next_param(struct libscols_filter *fltr, -- 2.47.3