From 4e91ebca3e237fb0e7b3aae05505f4ce3b52146a Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 19 Sep 2016 14:07:38 +0200 Subject: libsmartcols: be more strict about empty tables and don't print extra \n for empty table. Signed-off-by: Karel Zak --- libsmartcols/src/table.c | 2 +- libsmartcols/src/table_print.c | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) (limited to 'libsmartcols') diff --git a/libsmartcols/src/table.c b/libsmartcols/src/table.c index c41c6a12c..9aae75c95 100644 --- a/libsmartcols/src/table.c +++ b/libsmartcols/src/table.c @@ -448,7 +448,7 @@ struct libscols_column *scols_table_get_column(struct libscols_table *tb, */ int scols_table_add_line(struct libscols_table *tb, struct libscols_line *ln) { - if (!tb || !ln) + if (!tb || !ln || tb->ncols == 0) return -EINVAL; if (tb->ncols > ln->ncells) { diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c index 41b56d1d6..7612682cd 100644 --- a/libsmartcols/src/table_print.c +++ b/libsmartcols/src/table_print.c @@ -1520,7 +1520,7 @@ int scols_table_print_range_to_string( struct libscols_table *tb, #endif } -static int __scols_print_table(struct libscols_table *tb) +static int __scols_print_table(struct libscols_table *tb, int *is_empty) { int rc = 0; struct libscols_buffer *buf; @@ -1529,9 +1529,17 @@ static int __scols_print_table(struct libscols_table *tb) return -EINVAL; DBG(TAB, ul_debugobj(tb, "printing")); + if (is_empty) + *is_empty = 0; + if (list_empty(&tb->tb_columns)) { + DBG(TAB, ul_debugobj(tb, "error -- no columns")); + return -EINVAL; + } if (list_empty(&tb->tb_lines)) { - DBG(TAB, ul_debugobj(tb, "ignore -- empty table")); + DBG(TAB, ul_debugobj(tb, "ignore -- no lines")); + if (is_empty) + *is_empty = 1; return 0; } @@ -1570,9 +1578,10 @@ done: */ int scols_print_table(struct libscols_table *tb) { - int rc = __scols_print_table(tb); + int empty = 0; + int rc = __scols_print_table(tb, &empty); - if (rc == 0) + if (rc == 0 && !empty) fputc('\n', tb->out); return rc; } @@ -1605,7 +1614,7 @@ int scols_print_table_to_string(struct libscols_table *tb, char **data) old_stream = scols_table_get_stream(tb); scols_table_set_stream(tb, stream); - rc = __scols_print_table(tb); + rc = __scols_print_table(tb, NULL); fclose(stream); scols_table_set_stream(tb, old_stream); -- cgit v1.2.3-55-g7522