From: Karel Zak Date: Wed, 13 Sep 2023 11:06:06 +0000 (+0200) Subject: libsmartcols: (filter) move struct filter_param X-Git-Tag: v2.40-rc1~151^2~63 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=778981eea339a8fa5c09feb57ca252ccc867eb99;p=thirdparty%2Futil-linux.git libsmartcols: (filter) move struct filter_param Signed-off-by: Karel Zak --- diff --git a/libsmartcols/src/filter-expr.c b/libsmartcols/src/filter-expr.c index c4d7ba8b40..b92023d346 100644 --- a/libsmartcols/src/filter-expr.c +++ b/libsmartcols/src/filter-expr.c @@ -133,7 +133,7 @@ static enum filter_data node_get_datatype(struct filter_node *n) case F_NODE_EXPR: return F_DATA_BOOLEAN; case F_NODE_PARAM: - return ((struct filter_param *) n)->type; + return filter_param_get_datatype((struct filter_param *) n); } return F_DATA_NONE; } @@ -151,8 +151,8 @@ static enum filter_data guess_expr_datatype(struct filter_expr *n) /* for expression like "FOO > 5.5" preffer type defined by a real param * rather than by holder (FOO) */ - l_holder = is_filter_holder_param(n->left); - r_holder = is_filter_holder_param(n->right); + l_holder = is_filter_holder_node(n->left); + r_holder = is_filter_holder_node(n->right); if (l_holder && !r_holder) type = r; diff --git a/libsmartcols/src/filter-param.c b/libsmartcols/src/filter-param.c index 86201ae1b9..aeb286f4fb 100644 --- a/libsmartcols/src/filter-param.c +++ b/libsmartcols/src/filter-param.c @@ -5,6 +5,25 @@ #include "smartcolsP.h" +struct filter_param { + struct filter_node node; + enum filter_data type; + enum filter_holder holder; + + union { + char *str; + unsigned long long num; + long double fnum; + bool boolean; + } val; + + struct list_head pr_params; + struct libscols_column *col; + char *holder_name; + + unsigned int has_value :1; +}; + static int cast_param(enum filter_data type, struct filter_param *n); static inline const char *datatype2str(enum filter_data type) @@ -114,6 +133,17 @@ void filter_free_param(struct filter_param *n) free(n); } +enum filter_data filter_param_get_datatype(struct filter_param *n) +{ + return n ? n->type : F_DATA_NONE; +} + +int is_filter_holder_node(struct filter_node *n) +{ + return n && filter_node_get_type(n) == F_NODE_PARAM + && ((struct filter_param *)(n))->holder; +} + void filter_dump_param(struct ul_jsonwrt *json, struct filter_param *n) { ul_jsonwrt_object_open(json, "param"); @@ -158,10 +188,8 @@ void filter_dump_param(struct ul_jsonwrt *json, struct filter_param *n) int filter_param_reset_holder(struct filter_param *n) { - if (!n->holder) + if (!n->holder || !n->col) return 0; - if (!n->col) - return -EINVAL; param_reset_data(n); @@ -617,3 +645,72 @@ int filter_next_param(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 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; + } + } + + 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); + } + + return n ? 0 : -EINVAL; +} + +int scols_filter_next_holder(struct libscols_filter *fltr, + struct libscols_iter *itr, + const char **name, + int type) +{ + struct filter_param *prm = NULL; + int rc = 0; + + *name = NULL; + if (!type) + type = F_HOLDER_COLUMN; /* default */ + + do { + rc = filter_next_param(fltr, itr, &prm); + if (rc == 0 && (int) prm->holder == type) { + *name = prm->holder_name; + } + } while (rc == 0 && !*name); + + return rc; +} diff --git a/libsmartcols/src/filter.c b/libsmartcols/src/filter.c index 17ec8ec1b0..8d9e8a4a11 100644 --- a/libsmartcols/src/filter.c +++ b/libsmartcols/src/filter.c @@ -153,76 +153,6 @@ const char *scols_filter_get_errmsg(struct libscols_filter *fltr) return fltr ? fltr->errmsg : NULL; } -int scols_filter_next_holder(struct libscols_filter *fltr, - struct libscols_iter *itr, - const char **name, - int type) -{ - struct filter_param *prm = NULL; - int rc = 0; - - *name = NULL; - if (!type) - type = F_HOLDER_COLUMN; /* default */ - - do { - rc = filter_next_param(fltr, itr, &prm); - if (rc == 0 && (int) prm->holder == type) { - *name = prm->holder_name; - } - } while (rc == 0 && !*name); - - 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 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; - } - } - - 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); - } - - return n ? 0 : -EINVAL; -} - int filter_eval_node(struct libscols_filter *fltr, struct libscols_line *ln, struct filter_node *n, int *status) { @@ -250,8 +180,7 @@ int scols_line_apply_filter(struct libscols_line *ln, /* reset column data and types stored in the filter */ scols_reset_iter(&itr, SCOLS_ITER_FORWARD); while (filter_next_param(fltr, &itr, &prm) == 0) { - if (prm->col) - filter_param_reset_holder(prm); + filter_param_reset_holder(prm); } rc = filter_eval_node(fltr, ln, fltr->root, status); diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index 0ae0aedee1..4ad86a858b 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -527,25 +527,7 @@ struct filter_node { #define filter_node_get_type(n) (((struct filter_node *)(n))->type) -struct filter_param { - struct filter_node node; - enum filter_data type; - enum filter_holder holder; - - union { - char *str; - unsigned long long num; - long double fnum; - bool boolean; - } val; - - struct list_head pr_params; - struct libscols_column *col; - char *holder_name; - - unsigned int has_value :1; -}; - +struct filter_param; struct filter_expr; struct libscols_filter { @@ -571,6 +553,7 @@ int filter_eval_param(struct libscols_filter *fltr, struct libscols_line *ln, struct filter_param *n, int *status); void filter_free_param(struct filter_param *n); int filter_param_reset_holder(struct filter_param *n); +enum filter_data filter_param_get_datatype(struct filter_param *n); int filter_next_param(struct libscols_filter *fltr, struct libscols_iter *itr, struct filter_param **prm); @@ -587,10 +570,7 @@ int filter_cast_param(struct libscols_filter *fltr, struct filter_param *n, struct filter_param **result); -#define is_filter_holder_param(_n) \ - (filter_node_get_type(_n) == F_NODE_PARAM \ - && ((struct filter_param *)(_n))->holder) - +int is_filter_holder_node(struct filter_node *n); /* expr */ void filter_free_expr(struct filter_expr *n);