libsmartcols: be more strict about empty tables

and don't print extra \n for empty table.

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2016-09-19 14:07:38 +02:00
parent 166897ceb3
commit 4e91ebca3e
2 changed files with 15 additions and 6 deletions

View File

@ -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) {

View File

@ -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);