From: Karel Zak Date: Wed, 18 Dec 2013 09:22:32 +0000 (+0100) Subject: libsmartcols: add function to convert table to string X-Git-Tag: v2.25-rc1~375 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2a965b804ac754ef3c36f753af0ae988b4e79bb3;p=thirdparty%2Futil-linux.git libsmartcols: 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/configure.ac b/configure.ac index 5e43dc1459..188bf96559 100644 --- a/configure.ac +++ b/configure.ac @@ -307,6 +307,7 @@ AC_CHECK_FUNCS([ \ lseek64 \ mempcpy \ nanosleep \ + open_memstream \ personality \ posix_fadvise \ prctl \ diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index 93b205e2f0..8a61668bf7 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -138,6 +138,7 @@ extern int scols_table_reduce_termwidth(struct libscols_table *tb, size_t reduce /* table_print.c */ extern int scols_print_table(struct libscols_table *tb); +extern int scols_print_table_to_string(struct libscols_table *tb, char **data); #ifdef __cplusplus } diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c index 7da79598a4..8f0297a4c9 100644 --- a/libsmartcols/src/table_print.c +++ b/libsmartcols/src/table_print.c @@ -581,3 +581,28 @@ int scols_print_table(struct libscols_table *tb) free(line); return 0; } + +int scols_print_table_to_string(struct libscols_table *tb, char **data) +{ +#ifdef HAVE_OPEN_MEMSTREAM + FILE *stream; + size_t sz; + + if (!tb) + return -EINVAL; + + /* create a streem for output */ + stream = open_memstream(data, &sz); + if (!stream) + return -ENOMEM; + + scols_table_set_stream(tb, stream); + scols_print_table(tb); + fclose(stream); + + return 0; +#else + return -ENOSYS; +#endif +} +