]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: add function to convert table to string
authorKarel Zak <kzak@redhat.com>
Wed, 18 Dec 2013 09:22:32 +0000 (10:22 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 3 Apr 2014 10:29:17 +0000 (12:29 +0200)
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 <kzak@redhat.com>
configure.ac
libsmartcols/src/libsmartcols.h.in
libsmartcols/src/table_print.c

index 5e43dc145964c80a61920c1c47b211b4265ba195..188bf96559f64db6884979f420651ba916603e80 100644 (file)
@@ -307,6 +307,7 @@ AC_CHECK_FUNCS([ \
        lseek64 \
        mempcpy \
        nanosleep \
+       open_memstream \
        personality \
        posix_fadvise \
        prctl \
index 93b205e2f0e694a42655093cf4f01fc0ff1386ee..8a61668bf705bcb82f0e37f3c978807dfa1b9317 100644 (file)
@@ -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
 }
index 7da79598a4d1dea77a0a92611a788cae1c9ae920..8f0297a4c9f53b515df7d0a05edfd2ddf23bc099 100644 (file)
@@ -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
+}
+