From cfa185721e53444568f545fc058fba23087cf144 Mon Sep 17 00:00:00 2001 From: Igor Gnatenko Date: Sun, 21 Feb 2016 21:13:50 +0100 Subject: [PATCH] libsmartcols: implement scols_table_print_range_to_string Reference: https://github.com/karelzak/util-linux/issues/283 Signed-off-by: Igor Gnatenko --- libsmartcols/docs/libsmartcols-docs.xml | 2 +- libsmartcols/docs/libsmartcols-sections.txt | 1 + libsmartcols/src/libsmartcols.h.in | 4 ++ libsmartcols/src/libsmartcols.sym | 1 + libsmartcols/src/table_print.c | 44 +++++++++++++++++++++ 5 files changed, 51 insertions(+), 1 deletion(-) diff --git a/libsmartcols/docs/libsmartcols-docs.xml b/libsmartcols/docs/libsmartcols-docs.xml index b8e02242a..d118bbb5a 100644 --- a/libsmartcols/docs/libsmartcols-docs.xml +++ b/libsmartcols/docs/libsmartcols-docs.xml @@ -9,7 +9,7 @@ libsmartcols Reference Manual for libsmartcols version &version; - 2014-2015 + 2014-2016 Karel Zak <kzak@redhat.com> diff --git a/libsmartcols/docs/libsmartcols-sections.txt b/libsmartcols/docs/libsmartcols-sections.txt index c9bbec45b..75ff0eb2c 100644 --- a/libsmartcols/docs/libsmartcols-sections.txt +++ b/libsmartcols/docs/libsmartcols-sections.txt @@ -145,6 +145,7 @@ scols_unref_table scols_print_table scols_print_table_to_string scols_table_print_range +scols_table_print_range_to_string
diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index 664822e77..b2a750f1b 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -250,6 +250,10 @@ extern int scols_print_table_to_string(struct libscols_table *tb, char **data); extern int scols_table_print_range( struct libscols_table *tb, struct libscols_line *start, struct libscols_line *end); +extern int scols_table_print_range_to_string( struct libscols_table *tb, + struct libscols_line *start, + struct libscols_line *end, + char **data); #ifdef __cplusplus } diff --git a/libsmartcols/src/libsmartcols.sym b/libsmartcols/src/libsmartcols.sym index 8f64c09c9..862262c8c 100644 --- a/libsmartcols/src/libsmartcols.sym +++ b/libsmartcols/src/libsmartcols.sym @@ -133,5 +133,6 @@ global: scols_cell_get_flags; scols_cell_set_flags; scols_table_print_range; + scols_table_print_range_to_string; scols_table_enable_nolinesep; } SMARTCOLS_2.27; diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c index 75e828edd..f65cce383 100644 --- a/libsmartcols/src/table_print.c +++ b/libsmartcols/src/table_print.c @@ -1351,6 +1351,50 @@ done: return rc; } +/** + * scols_table_print_range_to_string: + * @tb: table + * @start: first printed line or NULL to print from the beggin of the table + * @end: last printed line or NULL to print all from start. + * @data: pointer to the beginning of a memory area to print to + * + * The same as scols_table_print_range(), but prints to @data instead of + * stream. + * + * Returns: 0, a negative value in case of an error. + */ +int scols_table_print_range_to_string( struct libscols_table *tb, + struct libscols_line *start, + struct libscols_line *end, + char **data) +{ +#ifdef HAVE_OPEN_MEMSTREAM + FILE *stream, *old_stream; + size_t sz; + int rc; + + if (!tb) + return -EINVAL; + + DBG(TAB, ul_debugobj(tb, "printing range to string")); + + /* create a stream for output */ + stream = open_memstream(data, &sz); + if (!stream) + return -ENOMEM; + + old_stream = scols_table_get_stream(tb); + scols_table_set_stream(tb, stream); + rc = scols_table_print_range(tb, start, end); + fclose(stream); + scols_table_set_stream(tb, old_stream); + + return rc; +#else + return -ENOSYS; +#endif +} + /** * scols_print_table: * @tb: table