From: Karel Zak Date: Wed, 18 Dec 2013 09:22:32 +0000 (+0100) Subject: include/tt: add function to convert table to string X-Git-Tag: v2.25-rc1~515 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ed494c8d50d912ee7aa3cfdfc9876540d15994da;p=thirdparty%2Futil-linux.git include/tt: add function to convert table to string Note that open_memstream() is POSIX-1.2008, so it's possible than not all libc have already implemented this function. Signed-off-by: Karel Zak --- diff --git a/include/tt.h b/include/tt.h index 781b953aeb..198ccdcb7d 100644 --- a/include/tt.h +++ b/include/tt.h @@ -85,6 +85,7 @@ extern void tt_set_termreduce(struct tt *tb, size_t re); extern void tt_free_table(struct tt *tb); extern void tt_remove_lines(struct tt *tb); extern int tt_print_table(struct tt *tb); +extern int tt_print_table_to_string(struct tt *tb, char **data); extern void tt_set_stream(struct tt *tb, FILE *out); extern struct tt_column *tt_define_column(struct tt *tb, const char *name, diff --git a/lib/tt.c b/lib/tt.c index 3cca4817f6..60534dfa33 100644 --- a/lib/tt.c +++ b/lib/tt.c @@ -986,6 +986,32 @@ int tt_print_table(struct tt *tb) return 0; } + +/* + * @tb: table + * + * Prints the table to string + */ +int tt_print_table_to_string(struct tt *tb, char **data) +{ + FILE *stream; + size_t sz; + + if (!tb) + return -EINVAL; + + /* create a streem for output */ + stream = open_memstream(data, &sz); + if (!stream) + return -ENOMEM; + + tt_set_stream(tb, stream); + tt_print_table(tb); + fclose(stream); + + return 0; +} + #ifdef TEST_PROGRAM #include