};
struct tt {
+ FILE *out; /* output stream */
size_t ncols; /* number of columns */
size_t termwidth; /* terminal width */
int is_term; /* is a tty? */
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 void tt_set_stream(struct tt *tb, FILE *out);
extern struct tt_column *tt_define_column(struct tt *tb, const char *name,
double whint, int flags);
return NULL;
tb->flags = flags;
+ tb->out = stdout;
INIT_LIST_HEAD(&tb->tb_lines);
INIT_LIST_HEAD(&tb->tb_columns);
return tb;
}
+void tt_set_stream(struct tt *tb, FILE *out)
+{
+ if (!tb)
+ return;
+ tb->out = out;
+}
+
void tt_remove_lines(struct tt *tb)
{
if (!tb)
/* raw mode */
if (tb->flags & TT_FL_RAW) {
- tt_fputs_nonblank(data, stdout);
+ tt_fputs_nonblank(data, tb->out);
if (!is_last_column(tb, cl))
- fputc(' ', stdout);
+ fputc(' ', tb->out);
return;
}
/* NAME=value mode */
if (tb->flags & TT_FL_EXPORT) {
- fprintf(stdout, "%s=", cl->name);
- tt_fputs_quoted(data, stdout);
+ fprintf(tb->out, "%s=", cl->name);
+ tt_fputs_quoted(data, tb->out);
if (!is_last_column(tb, cl))
- fputc(' ', stdout);
+ fputc(' ', tb->out);
return;
}
if (data) {
if (!(tb->flags & TT_FL_RAW) && (cl->flags & TT_FL_RIGHT)) {
size_t xw = cl->width;
- fprintf(stdout, "%*s", (int) xw, data);
+ fprintf(tb->out, "%*s", (int) xw, data);
if (len < xw)
len = xw;
}
else
- fputs(data, stdout);
+ fputs(data, 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 & TT_FL_TRUNC)) {
- fputc('\n', stdout);
+ fputc('\n', tb->out);
for (i = 0; i <= (size_t) cl->seqnum; i++) {
struct tt_column *x = tt_get_column(tb, i);
printf("%*s ", -((int)x->width), " ");
}
} else
- fputc(' ', stdout); /* columns separator */
+ fputc(' ', tb->out); /* columns separator */
}
free(buf);
print_data(ln->table, cl, line_get_data(ln, cl, buf, bufsz));
}
- fputc('\n', stdout);
+ fputc('\n', ln->table->out);
}
static void print_header(struct tt *tb, char *buf, size_t bufsz)
buf[bufsz - 1] = '\0';
print_data(tb, cl, buf);
}
- fputc('\n', stdout);
+ fputc('\n', tb->out);
}
static void print_table(struct tt *tb, char *buf, size_t bufsz)
/*
* @tb: table
*
- * Prints the table to stdout
+ * Prints the table to tb->out
*/
int tt_print_table(struct tt *tb)
{