summaryrefslogtreecommitdiffstats
path: root/libsmartcols
diff options
context:
space:
mode:
authorKarel Zak2017-03-03 10:37:37 +0100
committerKarel Zak2017-03-03 10:37:37 +0100
commit1765814521bf2aba7b8c03d3ab2e3b784e1ce2c4 (patch)
tree7e699b3b7f3122b1c5d880fe6b25830c5d8f2aeb /libsmartcols
parentlibsmartcols: support columns separators greater than one output cell (diff)
downloadkernel-qcow2-util-linux-1765814521bf2aba7b8c03d3ab2e3b784e1ce2c4.tar.gz
kernel-qcow2-util-linux-1765814521bf2aba7b8c03d3ab2e3b784e1ce2c4.tar.xz
kernel-qcow2-util-linux-1765814521bf2aba7b8c03d3ab2e3b784e1ce2c4.zip
libsmartcols: allow to add column to already used table
Now it's impossible to add new column if the table already contains lines with data. This patch forces library to realloc cell array in the lines to accept a new column. Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libsmartcols')
-rw-r--r--libsmartcols/src/table.c25
1 files changed, 18 insertions, 7 deletions
diff --git a/libsmartcols/src/table.c b/libsmartcols/src/table.c
index 4f3b13db4..f1ddacc9f 100644
--- a/libsmartcols/src/table.c
+++ b/libsmartcols/src/table.c
@@ -171,7 +171,11 @@ struct libscols_cell *scols_table_get_title(struct libscols_table *tb)
*/
int scols_table_add_column(struct libscols_table *tb, struct libscols_column *cl)
{
- if (!tb || !cl || !list_empty(&tb->tb_lines) || cl->table)
+ struct libscols_iter itr;
+ struct libscols_line *ln;
+ int rc = 0;
+
+ if (!tb || !cl || cl->table)
return -EINVAL;
if (cl->flags & SCOLS_FL_TREE)
@@ -183,13 +187,20 @@ int scols_table_add_column(struct libscols_table *tb, struct libscols_column *cl
cl->table = tb;
scols_ref_column(cl);
- /* TODO:
- *
- * Currently it's possible to add/remove columns only if the table is
- * empty (see list_empty(tb->tb_lines) above). It would be nice to
- * enlarge/reduce lines cells[] always when we add/remove a new column.
+ if (list_empty(&tb->tb_lines))
+ return 0;
+
+ scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
+
+ /* Realloc line cell arrays
*/
- return 0;
+ while (scols_table_next_line(tb, &itr, &ln) == 0) {
+ rc = scols_line_alloc_cells(ln, tb->ncols);
+ if (rc)
+ break;
+ }
+
+ return rc;
}
/**