]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: support alternative streams
authorKarel Zak <kzak@redhat.com>
Wed, 19 Mar 2014 15:58:15 +0000 (16:58 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 3 Apr 2014 10:29:16 +0000 (12:29 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/src/libsmartcols.h.in
libsmartcols/src/smartcolsP.h
libsmartcols/src/table.c
libsmartcols/src/table_print.c

index 81283fb0e958b77fbb82ffb868892eaec4b0f8fb..9bb7c64801013c763cbeac79f660edeea25bfa00 100644 (file)
@@ -131,6 +131,9 @@ extern struct libscols_line *scols_table_get_line(struct libscols_table *tb, siz
 extern struct libscols_table *scols_copy_table(struct libscols_table *tb);
 extern int scols_table_set_symbols(struct libscols_table *tb, struct libscols_symbols *sy);
 
+extern int scols_table_set_stream(struct libscols_table *tb, FILE *stream);
+extern FILE *scols_table_get_stream(struct libscols_table *tb);
+
 /* table_print.c */
 extern int scols_print_table(struct libscols_table *tb);
 
index 1bba01730ba101b1f2333b33c811a236eb64af80..d8cf2ee56a02698c839bba605845925ff7273d19 100644 (file)
@@ -107,6 +107,7 @@ struct libscols_table {
        int     is_term;        /* is a tty? */
        int     flags;
        int     first_run;
+       FILE    *out;           /* output stream */
 
        struct list_head        tb_columns;
        struct list_head        tb_lines;
index e703f8f26c5efc71420e785d8d917b2caf5b9e40..5c45aeeebf7b6f95774d8f5883c02bdfb00622a6 100644 (file)
@@ -47,6 +47,7 @@ struct libscols_table *scols_new_table(int flags, struct libscols_symbols *syms)
        tb->flags = flags;
        tb->refcount = 1;
        tb->first_run = TRUE;
+       tb->out = stdout;
 
        INIT_LIST_HEAD(&tb->tb_lines);
        INIT_LIST_HEAD(&tb->tb_columns);
@@ -253,6 +254,22 @@ int scols_table_get_flags(struct libscols_table *tb)
        return tb ? tb->flags: -EINVAL;
 }
 
+int scols_table_set_stream(struct libscols_table *tb, FILE *stream)
+{
+       assert(tb);
+       if (!tb)
+               return -EINVAL;
+
+       tb->out = stream;
+       return 0;
+}
+
+FILE *scols_table_get_stream(struct libscols_table *tb)
+{
+       assert(tb);
+       return tb ? tb->out: NULL;
+}
+
 /*
  * @tb: table
  * @: number of column (0..N)
index 694b9c9caeadabe12fb98d527a2a974854c7cb92..ee40c4090ac96cac8094c24a2e77d4d4736f1023 100644 (file)
@@ -41,18 +41,18 @@ static void print_data(struct libscols_table *tb,
 
        /* raw mode */
        if (tb->flags & SCOLS_FL_RAW) {
-               fputs_nonblank(data, stdout);
+               fputs_nonblank(data, tb->out);
                if (!is_last_column(tb, cl))
-                       fputc(' ', stdout);
+                       fputc(' ', tb->out);
                return;
        }
 
        /* NAME=value mode */
        if (tb->flags & SCOLS_FL_EXPORT) {
-               fprintf(stdout, "%s=", scols_cell_get_data(&cl->header));
-               fputs_quoted(data, stdout);
+               fprintf(tb->out, "%s=", scols_cell_get_data(&cl->header));
+               fputs_quoted(data, tb->out);
                if (!is_last_column(tb, cl))
-                       fputc(' ', stdout);
+                       fputc(' ', tb->out);
                return;
        }
 
@@ -93,33 +93,33 @@ static void print_data(struct libscols_table *tb,
                if (!(tb->flags & SCOLS_FL_RAW) && (cl->flags & SCOLS_FL_RIGHT)) {
                        size_t xw = cl->width;
                        if (color)
-                               fputs(color, stdout);
-                       fprintf(stdout, "%*s", (int) xw, data);
+                               fputs(color, tb->out);
+                       fprintf(tb->out, "%*s", (int) xw, data);
                        if (color)
-                               fputs(UL_COLOR_RESET, stdout);
+                               fputs(UL_COLOR_RESET, tb->out);
                        if (len < xw)
                                len = xw;
                }
                else {
                        if (color)
-                               fputs(color, stdout);
-                       fputs(data, stdout);
+                               fputs(color, tb->out);
+                       fputs(data, tb->out);
                        if (color)
-                               fputs(UL_COLOR_RESET, stdout);
+                               fputs(UL_COLOR_RESET, tb->out);
                }
        }
        for (i = len; i < width; i++)
-               fputc(' ', stdout);             /* padding */
+               fputc(' ', tb->out);            /* padding */
 
        if (!is_last_column(tb, cl)) {
                if (len > width && !(cl->flags & SCOLS_FL_TRUNC)) {
-                       fputc('\n', stdout);
+                       fputc('\n', tb->out);
                        for (i = 0; i <= (size_t) cl->seqnum; i++) {
                                struct libscols_column *x = scols_table_get_column(tb, i);
-                               printf("%*s ", -((int)x->width), " ");
+                               fprintf(tb->out, "%*s ", -((int)x->width), " ");
                        }
                } else
-                       fputc(' ', stdout);     /* columns separator */
+                       fputc(' ', tb->out);    /* columns separator */
        }
 
        free(buf);
@@ -221,7 +221,7 @@ static void print_line(struct libscols_table *tb,
                print_data(tb, cl, ln,
                                scols_line_get_cell(ln, cl->seqnum),
                                line_get_data(tb, ln, cl, buf, bufsz));
-       fputc('\n', stdout);
+       fputc('\n', tb->out);
 }
 
 static void print_header(struct libscols_table *tb, char *buf, size_t bufsz)
@@ -245,7 +245,7 @@ static void print_header(struct libscols_table *tb, char *buf, size_t bufsz)
                buf[bufsz - 1] = '\0';
                print_data(tb, cl, NULL, &cl->header, buf);
        }
-       fputc('\n', stdout);
+       fputc('\n', tb->out);
 }
 
 static void print_table(struct libscols_table *tb, char *buf, size_t bufsz)
@@ -526,7 +526,7 @@ static size_t strlen_line(struct libscols_line *ln)
 /*
  * @tb: table
  *
- * Prints the table to stdout
+ * Prints the table to the output stream.
  */
 int scols_print_table(struct libscols_table *tb)
 {