summaryrefslogtreecommitdiffstats
path: root/libsmartcols/src/table.c
diff options
context:
space:
mode:
authorKarel Zak2017-03-29 14:06:37 +0200
committerKarel Zak2017-04-07 14:34:11 +0200
commit7bdefc7fc0c3c321363f8074f3ab1e2faaca6b2d (patch)
treef74e138427c06e029e414cfd4cd9bb20d977681c /libsmartcols/src/table.c
parentlibfdisk: (gpt) fix ents zeroize (diff)
downloadkernel-qcow2-util-linux-7bdefc7fc0c3c321363f8074f3ab1e2faaca6b2d.tar.gz
kernel-qcow2-util-linux-7bdefc7fc0c3c321363f8074f3ab1e2faaca6b2d.tar.xz
kernel-qcow2-util-linux-7bdefc7fc0c3c321363f8074f3ab1e2faaca6b2d.zip
libsmartcols: add scols_table_move_column()
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libsmartcols/src/table.c')
-rw-r--r--libsmartcols/src/table.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/libsmartcols/src/table.c b/libsmartcols/src/table.c
index f1ddacc9f..84c0f6a25 100644
--- a/libsmartcols/src/table.c
+++ b/libsmartcols/src/table.c
@@ -252,6 +252,38 @@ int scols_table_remove_columns(struct libscols_table *tb)
}
/**
+ * scols_table_move_column:
+ * @tb: table
+ * @pre: column before the column
+ * @cl: colum to move
+ *
+ * Move the @cl behind @pre. The the @pre is NULL then the @col is the fist
+ * column in the table.
+ *
+ * Returns: 0, a negative number in case of an error.
+ */
+int scols_table_move_column(struct libscols_table *tb,
+ struct libscols_column *pre,
+ struct libscols_column *cl)
+{
+ struct list_head *head;
+ struct libscols_iter itr;
+ size_t n = 0;
+
+ list_del_init(&cl->cl_columns); /* remove from old position */
+
+ head = pre ? &pre->cl_columns : &tb->tb_columns;
+ list_add(&cl->cl_columns, head); /* add to the new place */
+
+ /* fix seq. numbers */
+ scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
+ while (scols_table_next_column(tb, &itr, &cl) == 0)
+ cl->seqnum = n++;
+
+ return 0;
+}
+
+/**
* scols_table_new_column:
* @tb: table
* @name: column header