From d2cb7328ffc0ae0a22c72c0305e1cc819aa54c16 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 13 Sep 2023 11:28:36 +0200 Subject: [PATCH] libsmartcols: (filter) improve scols_filter_assign_column() * make it more usable with scols_filter_next_holder() and avoid name * make it usable with only with name * fix return codes Signed-off-by: Karel Zak --- libsmartcols/samples/filter.c | 2 +- libsmartcols/src/filter.c | 49 ++++++++++++++++++++++++------ libsmartcols/src/libsmartcols.h.in | 1 + 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/libsmartcols/samples/filter.c b/libsmartcols/samples/filter.c index 6567854cc1..844fc7d2f9 100644 --- a/libsmartcols/samples/filter.c +++ b/libsmartcols/samples/filter.c @@ -149,7 +149,7 @@ int main(int argc, char *argv[]) rc++; continue; } - scols_filter_assign_column(fltr, name, col); + scols_filter_assign_column(fltr, itr, name, col); } if (rc) goto done; diff --git a/libsmartcols/src/filter.c b/libsmartcols/src/filter.c index 5a4957b8bb..17ec8ec1b0 100644 --- a/libsmartcols/src/filter.c +++ b/libsmartcols/src/filter.c @@ -175,23 +175,52 @@ int scols_filter_next_holder(struct libscols_filter *fltr, return rc; } +/** + * scols_filter_assign_column: + * @fltr: pointer to filter + * @itr: iterator + * @name: holder name + * @col: column + * + * Assign @col to filter parametr. The parametr is addressed by @itr or by @name. + * + * Returns: 0, a negative value in case of an error. + */ int scols_filter_assign_column(struct libscols_filter *fltr, + struct libscols_iter *itr, const char *name, struct libscols_column *col) { - struct libscols_iter itr; - struct filter_param *prm = NULL; - int ct = 0; + struct filter_param *n = NULL; + + if (itr && itr->p) { + struct list_head *p = IS_ITER_FORWARD(itr) ? + itr->p->prev : itr->p->next; + n = list_entry(p, struct filter_param, pr_params); + } else if (name) { + struct libscols_iter xitr; + struct filter_param *x = NULL; + + scols_reset_iter(&xitr, SCOLS_ITER_FORWARD); + while (filter_next_param(fltr, &xitr, &x) == 0) { + if (x->col + || x->holder != F_HOLDER_COLUMN + || strcmp(name, x->holder_name) != 0) + continue; + n = x; + break; + } + } - scols_reset_iter(&itr, SCOLS_ITER_FORWARD); - while (filter_next_param(fltr, &itr, &prm) == 0) { - if (prm->holder != F_HOLDER_COLUMN || strcmp(name, prm->holder_name) != 0) - continue; - prm->col = col; + if (n) { + if (n->col) + scols_unref_column(n->col); + + DBG(FPARAM, ul_debugobj(n, "assing %s to column", name)); + n->col = col; scols_ref_column(col); - ct++; } - return ct == 0 ? 1 : 0; + return n ? 0 : -EINVAL; } int filter_eval_node(struct libscols_filter *fltr, struct libscols_line *ln, diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index 3ceca417fa..90a3978a58 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -381,6 +381,7 @@ extern int scols_line_apply_filter(struct libscols_line *ln, extern int scols_filter_next_holder(struct libscols_filter *fltr, struct libscols_iter *itr, const char **name, int type); extern int scols_filter_assign_column(struct libscols_filter *fltr, + struct libscols_iter *itr, const char *name, struct libscols_column *col); #ifdef __cplusplus -- 2.47.3