]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: add scols_sort_table_by_tree()
authorKarel Zak <kzak@redhat.com>
Thu, 13 Apr 2017 09:19:59 +0000 (11:19 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 13 Apr 2017 09:19:59 +0000 (11:19 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/docs/libsmartcols-sections.txt
libsmartcols/src/libsmartcols.h.in
libsmartcols/src/libsmartcols.sym
libsmartcols/src/table.c

index 4be9bf22be17c4a954147f6594bca8b939db82c3..a55716db9d2427ef6198944881eda5345a2fae34 100644 (file)
@@ -105,6 +105,7 @@ scols_copy_table
 scols_new_table
 scols_ref_table
 scols_sort_table
+scols_sort_table_by_tree
 scols_table_add_column
 scols_table_add_line
 scols_table_colors_wanted
index 4b2a67284e93690ae62560ebd357c2d8174a89fa..1ff465e44c91a202067647dcfc912726741c5ab9 100644 (file)
@@ -268,7 +268,7 @@ extern FILE *scols_table_get_stream(const struct libscols_table *tb);
 extern int scols_table_reduce_termwidth(struct libscols_table *tb, size_t reduce);
 
 extern int scols_sort_table(struct libscols_table *tb, struct libscols_column *cl);
-
+extern int scols_sort_table_by_tree(struct libscols_table *tb);
 /*
  *
  */
index 917cca3d6aafd44152ad20a48284dcd78644a110..95fcc31670eb68f25a6132b72cdc9635bf968184 100644 (file)
@@ -163,4 +163,5 @@ SMARTCOLS_2.30 {
 global:
        scols_cell_get_alignment;
        scols_table_move_column;
+       scols_sort_table_by_tree;
 } SMARTCOLS_2.29;
index a67028d4372533a4cb8bad7608e88f8f16026c60..c9f043cc2ef5c79906982b83a1058f4af51328f1 100644 (file)
@@ -1285,7 +1285,8 @@ static int sort_line_children(struct libscols_line *ln, struct libscols_column *
  * @tb: table
  * @cl: order by this column
  *
- * Orders the table by the column. See also scols_column_set_cmpfunc().
+ * Orders the table by the column. See also scols_column_set_cmpfunc(). If the
+ * tree output is enabled then children in the tree are recursively sorted too.
  *
  * Returns: 0, a negative value in case of an error.
  */
@@ -1309,6 +1310,58 @@ int scols_sort_table(struct libscols_table *tb, struct libscols_column *cl)
        return 0;
 }
 
+static struct libscols_line *move_line_and_children(struct libscols_line *ln, struct libscols_line *pre)
+{
+       if (pre) {
+               list_del_init(&ln->ln_lines);                   /* remove from old position */
+               list_add(&ln->ln_lines, &pre->ln_lines);        /* add to the new place (behind @pre) */
+       }
+       pre = ln;
+
+       if (!list_empty(&ln->ln_branch)) {
+               struct list_head *p;
+
+               list_for_each(p, &ln->ln_branch) {
+                       struct libscols_line *chld =
+                                       list_entry(p, struct libscols_line, ln_children);
+                       pre = move_line_and_children(chld, pre);
+               }
+       }
+
+       return pre;
+}
+
+/**
+ * scols_sort_table_by_tree:
+ * @tb: table
+ *
+ * Reorders lines in the tree according to parent->child relation. Note that
+ * order of lines in the table is independent on the tree hierarchy.
+ *
+ * Returns: 0, a negative value in case of an error.
+ */
+int scols_sort_table_by_tree(struct libscols_table *tb)
+{
+       struct libscols_line *ln;
+       struct libscols_iter itr;
+
+       if (!tb)
+               return -EINVAL;
+
+       DBG(TAB, ul_debugobj(tb, "sorting table by tree"));
+
+       scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
+       while (scols_table_next_line(tb, &itr, &ln) == 0) {
+               if (ln->parent)
+                       continue;
+
+               move_line_and_children(ln, NULL);
+       }
+
+       return 0;
+}
+
+
 /**
  * scols_table_set_termforce:
  * @tb: table