From ed494c8d50d912ee7aa3cfdfc9876540d15994da Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 18 Dec 2013 10:22:32 +0100 Subject: [PATCH] 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 --- include/tt.h | 1 + lib/tt.c | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) 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 -- 2.47.2