scols_table_enable_noheadings
scols_table_enable_raw
scols_table_get_column
+scols_table_get_column_separator
scols_table_get_line
+scols_table_get_line_separator
scols_table_get_ncols
scols_table_get_nlines
scols_table_get_stream
scols_table_remove_columns
scols_table_remove_line
scols_table_remove_lines
+scols_table_set_column_separator
+scols_table_set_line_separator
scols_table_set_stream
scols_table_set_symbols
scols_unref_table
extern int scols_table_enable_export(struct libscols_table *tb, int enable);
extern int scols_table_enable_maxout(struct libscols_table *tb, int enable);
+extern int scols_table_set_column_separator(struct libscols_table *tb, char *sep);
+extern int scols_table_set_line_separator(struct libscols_table *tb, char *sep);
+
extern struct libscols_table *scols_new_table(void);
extern void scols_ref_table(struct libscols_table *tb);
extern void scols_unref_table(struct libscols_table *tb);
extern int scols_table_remove_columns(struct libscols_table *tb);
extern struct libscols_column *scols_table_new_column(struct libscols_table *tb, const char *name, double whint, int flags);
extern int scols_table_next_column(struct libscols_table *tb, struct libscols_iter *itr, struct libscols_column **cl);
+extern char *scols_table_get_column_separator(struct libscols_table *tb);
+extern char *scols_table_get_line_separator(struct libscols_table *tb);
extern int scols_table_get_ncols(struct libscols_table *tb);
extern int scols_table_get_nlines(struct libscols_table *tb);
extern struct libscols_column *scols_table_get_column(struct libscols_table *tb, size_t n);
scols_table_enable_noheadings;
scols_table_enable_raw;
scols_table_get_column;
+ scols_table_get_column_separator;
scols_table_get_line;
+ scols_table_get_line_separator;
scols_table_get_ncols;
scols_table_get_nlines;
scols_table_get_stream;
scols_table_remove_columns;
scols_table_remove_line;
scols_table_remove_lines;
+ scols_table_set_column_separator;
+ scols_table_set_line_separator;
scols_table_set_stream;
scols_table_set_symbols;
scols_unref_column;
size_t termreduce; /* extra blank space */
FILE *out; /* output stream */
+ char *colsep; /* column separator */
+ char *linesep; /* line separator */
+
struct list_head tb_columns;
struct list_head tb_lines;
struct libscols_symbols *symbols;
scols_table_remove_lines(tb);
scols_table_remove_columns(tb);
scols_unref_symbols(tb->symbols);
+ free(tb->linesep);
+ free(tb->colsep);
free(tb);
}
}
scols_unref_line(newln);
}
+ /* separators */
+ if (scols_table_set_column_separator(ret, tb->colsep) ||
+ scols_table_set_line_separator(ret, tb->linesep))
+ goto err;
+
return ret;
err:
scols_unref_table(ret);
assert(tb);
return tb && tb->ntreecols > 0;
}
+
+/**
+ * scols_table_set_column_separator:
+ * @tb: table
+ * @sep: separator
+ *
+ * Sets the column separator of @tb to @sep.
+ * Please note that @sep should always take up a single cell in the output.
+ *
+ * Returns: 0, a negative value in case of an error.
+ */
+int scols_table_set_column_separator(struct libscols_table *tb, char *sep)
+{
+ assert (tb);
+
+ if (!tb)
+ return -EINVAL;
+
+ sep = strdup(sep);
+ if (!sep)
+ return -ENOMEM;
+
+ free(tb->colsep);
+ tb->colsep = sep;
+
+ return 0;
+}
+
+/**
+ * scols_table_set_line_separator:
+ * @tb: table
+ * @sep: separator
+ *
+ * Sets the line separator of @tb to @sep.
+ *
+ * Returns: 0, a negative value in case of an error.
+ */
+int scols_table_set_line_separator(struct libscols_table *tb, char *sep)
+{
+ assert (tb);
+
+ if (!tb)
+ return -EINVAL;
+
+ sep = strdup(sep);
+ if (!sep)
+ return -ENOMEM;
+
+ free(tb->linesep);
+ tb->linesep = sep;
+
+ return 0;
+}
+
+/**
+ * scols_table_get_column_separator:
+ * @tb: table
+ *
+ * Returns: @tb column separator, NULL in case of an error
+ */
+char *scols_table_get_column_separator(struct libscols_table *tb)
+{
+ assert (tb);
+
+ if (!tb)
+ return NULL;
+ return tb->colsep;
+}
+
+/**
+ * scols_table_get_line_separator:
+ * @tb: table
+ *
+ * Returns: @tb line separator, NULL in case of an error
+ */
+char *scols_table_get_line_separator(struct libscols_table *tb)
+{
+ assert (tb);
+
+ if (!tb)
+ return NULL;
+ return tb->linesep;
+
+}
#define is_last_column(_tb, _cl) \
list_entry_is_last(&(_cl)->cl_columns, &(_tb)->tb_columns)
+#define colsep(tb) ((tb)->colsep ? (tb)->colsep : " ")
+#define linesep(tb) ((tb)->linesep ? (tb)->linesep : "\n")
static void print_data(struct libscols_table *tb,
struct libscols_column *cl,
struct libscols_line *ln, /* optional */
if (scols_table_is_raw(tb)) {
fputs_nonblank(data, tb->out);
if (!is_last_column(tb, cl))
- fputc(' ', tb->out);
+ fputs(colsep(tb), tb->out);
return;
}
fprintf(tb->out, "%s=", scols_cell_get_data(&cl->header));
fputs_quoted(data, tb->out);
if (!is_last_column(tb, cl))
- fputc(' ', tb->out);
+ fputs(colsep(tb), tb->out);
return;
}
}
}
for (i = len; i < width; i++)
- fputc(' ', tb->out); /* padding */
+ fputs(colsep(tb), tb->out); /* padding */
if (!is_last_column(tb, cl)) {
if (len > width && !scols_column_is_trunc(cl)) {
- fputc('\n', tb->out);
+ fputs(linesep(tb), tb->out);
for (i = 0; i <= (size_t) cl->seqnum; i++) {
struct libscols_column *x = scols_table_get_column(tb, i);
fprintf(tb->out, "%*s ", -((int)x->width), " ");
}
} else
- fputc(' ', tb->out); /* columns separator */
+ fputs(colsep(tb), tb->out); /* columns separator */
}
free(buf);
print_data(tb, cl, ln,
scols_line_get_cell(ln, cl->seqnum),
line_get_data(tb, ln, cl, buf, bufsz));
- fputc('\n', tb->out);
+ fputs(linesep(tb), tb->out);
}
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', tb->out);
+ fputs(linesep(tb), tb->out);
}
static void print_table(struct libscols_table *tb, char *buf, size_t bufsz)
fputs(_(" -n, --noheadings don't print headings\n"), out);
fputs(_(" -p, --pairs use key=\"value\" output format\n"), out);
fputs(_(" -r, --raw use raw output format\n"), out);
+ fputs(_(" -c, --csv display a csv-like output\n"), out);
fputs(USAGE_SEPARATOR, out);
fputs(USAGE_HELP, out);
{ "noheadings", 0, 0, 'n' },
{ "list", 0, 0, 'l' },
{ "ascii", 0, 0, 'i' },
- { "raw", 0, 0, 'r' },
{ "pairs", 0, 0, 'p' },
{ "clone", 0, 0, 'C' },
+ { "csv", 0, 0, 'c' },
{ NULL, 0, 0, 0 },
};
if (!tb)
err(EXIT_FAILURE, "faild to create output table");
- while((c = getopt_long(argc, argv, "nlirpC", longopts, NULL)) != -1) {
+ while((c = getopt_long(argc, argv, "nlirpCc", longopts, NULL)) != -1) {
switch(c) {
case 'h':
usage(stdout);
scols_table_enable_raw(tb, 1);
notree = 1;
break;
+ case 'c':
+ scols_table_set_line_separator(tb, ",");
+ /* a column separator should always take up one cell */
+ scols_table_set_column_separator(tb, ":");
+ scols_table_enable_raw(tb, 1);
+ break;
case 'C':
clonetb = 1;
default: