]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: (filter) improve scols_filter_assign_column()
authorKarel Zak <kzak@redhat.com>
Wed, 13 Sep 2023 09:28:36 +0000 (11:28 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 20 Nov 2023 21:25:46 +0000 (22:25 +0100)
* 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 <kzak@redhat.com>
libsmartcols/samples/filter.c
libsmartcols/src/filter.c
libsmartcols/src/libsmartcols.h.in

index 6567854cc11f67644107c82cfafb858303dbd39e..844fc7d2f9ae40b60a99e22ef6789f549f8c137d 100644 (file)
@@ -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;
index 5a4957b8bb6dde40e61f76a1617f327996b83807..17ec8ec1b06caedd66406ebec4aadb579f677e0c 100644 (file)
@@ -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,
index 3ceca417fa0ac55498544cbc28e35baaf071c327..90a3978a584794c6be930e1207e0e41ca37ed7bd 100644 (file)
@@ -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