]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
include/tt: 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>
Tue, 11 Mar 2014 10:35:12 +0000 (11:35 +0100)
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>
include/tt.h
lib/tt.c

index 781b953aeb9a75f04610d741b239dade0d552700..198ccdcb7daa138234607602f2cd6b939bdcfc3a 100644 (file)
@@ -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,
index 3cca4817f6a1e6a6e4a3c8f14d9a967509e906e1..60534dfa3344c1f3f981b17e297b0d3acde21812 100644 (file)
--- 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 <errno.h>