From 571441e29198db3c90ec93f44663c77dfcbab002 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 19 Mar 2014 16:58:15 +0100 Subject: [PATCH] libsmartcols: support alternative streams Signed-off-by: Karel Zak --- libsmartcols/src/libsmartcols.h.in | 3 +++ libsmartcols/src/smartcolsP.h | 1 + libsmartcols/src/table.c | 17 ++++++++++++++ libsmartcols/src/table_print.c | 36 +++++++++++++++--------------- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index 81283fb0e9..9bb7c64801 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -131,6 +131,9 @@ extern struct libscols_line *scols_table_get_line(struct libscols_table *tb, siz extern struct libscols_table *scols_copy_table(struct libscols_table *tb); extern int scols_table_set_symbols(struct libscols_table *tb, struct libscols_symbols *sy); +extern int scols_table_set_stream(struct libscols_table *tb, FILE *stream); +extern FILE *scols_table_get_stream(struct libscols_table *tb); + /* table_print.c */ extern int scols_print_table(struct libscols_table *tb); diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index 1bba01730b..d8cf2ee56a 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -107,6 +107,7 @@ struct libscols_table { int is_term; /* is a tty? */ int flags; int first_run; + FILE *out; /* output stream */ struct list_head tb_columns; struct list_head tb_lines; diff --git a/libsmartcols/src/table.c b/libsmartcols/src/table.c index e703f8f26c..5c45aeeebf 100644 --- a/libsmartcols/src/table.c +++ b/libsmartcols/src/table.c @@ -47,6 +47,7 @@ struct libscols_table *scols_new_table(int flags, struct libscols_symbols *syms) tb->flags = flags; tb->refcount = 1; tb->first_run = TRUE; + tb->out = stdout; INIT_LIST_HEAD(&tb->tb_lines); INIT_LIST_HEAD(&tb->tb_columns); @@ -253,6 +254,22 @@ int scols_table_get_flags(struct libscols_table *tb) return tb ? tb->flags: -EINVAL; } +int scols_table_set_stream(struct libscols_table *tb, FILE *stream) +{ + assert(tb); + if (!tb) + return -EINVAL; + + tb->out = stream; + return 0; +} + +FILE *scols_table_get_stream(struct libscols_table *tb) +{ + assert(tb); + return tb ? tb->out: NULL; +} + /* * @tb: table * @: number of column (0..N) diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c index 694b9c9cae..ee40c4090a 100644 --- a/libsmartcols/src/table_print.c +++ b/libsmartcols/src/table_print.c @@ -41,18 +41,18 @@ static void print_data(struct libscols_table *tb, /* raw mode */ if (tb->flags & SCOLS_FL_RAW) { - fputs_nonblank(data, stdout); + fputs_nonblank(data, tb->out); if (!is_last_column(tb, cl)) - fputc(' ', stdout); + fputc(' ', tb->out); return; } /* NAME=value mode */ if (tb->flags & SCOLS_FL_EXPORT) { - fprintf(stdout, "%s=", scols_cell_get_data(&cl->header)); - fputs_quoted(data, stdout); + fprintf(tb->out, "%s=", scols_cell_get_data(&cl->header)); + fputs_quoted(data, tb->out); if (!is_last_column(tb, cl)) - fputc(' ', stdout); + fputc(' ', tb->out); return; } @@ -93,33 +93,33 @@ static void print_data(struct libscols_table *tb, if (!(tb->flags & SCOLS_FL_RAW) && (cl->flags & SCOLS_FL_RIGHT)) { size_t xw = cl->width; if (color) - fputs(color, stdout); - fprintf(stdout, "%*s", (int) xw, data); + fputs(color, tb->out); + fprintf(tb->out, "%*s", (int) xw, data); if (color) - fputs(UL_COLOR_RESET, stdout); + fputs(UL_COLOR_RESET, tb->out); if (len < xw) len = xw; } else { if (color) - fputs(color, stdout); - fputs(data, stdout); + fputs(color, tb->out); + fputs(data, tb->out); if (color) - fputs(UL_COLOR_RESET, stdout); + fputs(UL_COLOR_RESET, tb->out); } } for (i = len; i < width; i++) - fputc(' ', stdout); /* padding */ + fputc(' ', tb->out); /* padding */ if (!is_last_column(tb, cl)) { if (len > width && !(cl->flags & SCOLS_FL_TRUNC)) { - fputc('\n', stdout); + fputc('\n', tb->out); for (i = 0; i <= (size_t) cl->seqnum; i++) { struct libscols_column *x = scols_table_get_column(tb, i); - printf("%*s ", -((int)x->width), " "); + fprintf(tb->out, "%*s ", -((int)x->width), " "); } } else - fputc(' ', stdout); /* columns separator */ + fputc(' ', tb->out); /* columns separator */ } free(buf); @@ -221,7 +221,7 @@ static void print_line(struct libscols_table *tb, print_data(tb, cl, ln, scols_line_get_cell(ln, cl->seqnum), line_get_data(tb, ln, cl, buf, bufsz)); - fputc('\n', stdout); + fputc('\n', tb->out); } static void print_header(struct libscols_table *tb, char *buf, size_t bufsz) @@ -245,7 +245,7 @@ static void print_header(struct libscols_table *tb, char *buf, size_t bufsz) buf[bufsz - 1] = '\0'; print_data(tb, cl, NULL, &cl->header, buf); } - fputc('\n', stdout); + fputc('\n', tb->out); } static void print_table(struct libscols_table *tb, char *buf, size_t bufsz) @@ -526,7 +526,7 @@ static size_t strlen_line(struct libscols_line *ln) /* * @tb: table * - * Prints the table to stdout + * Prints the table to the output stream. */ int scols_print_table(struct libscols_table *tb) { -- 2.47.2