]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: add separator getters/setters
authorOndrej Oprala <ooprala@redhat.com>
Wed, 9 Apr 2014 14:58:28 +0000 (16:58 +0200)
committerOndrej Oprala <ooprala@redhat.com>
Mon, 14 Apr 2014 10:37:21 +0000 (12:37 +0200)
Signed-off-by: Ondrej Oprala <ooprala@redhat.com>
libsmartcols/docs/libsmartcols-sections.txt
libsmartcols/src/libsmartcols.h.in
libsmartcols/src/libsmartcols.sym
libsmartcols/src/smartcolsP.h
libsmartcols/src/table.c
libsmartcols/src/table_print.c
libsmartcols/src/test.c

index a8dc884abe0010316beee810551051d51c527e4f..bdb35082f7b4a3f2df1a3e181c5479cb2d43c89d 100644 (file)
@@ -92,7 +92,9 @@ scols_table_enable_maxout
 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
@@ -112,6 +114,8 @@ scols_table_remove_column
 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
index b3ceeeab96572e1a0254a7f92b183672fa49d619..1307013a18e6d22159c7bf14fed275123b3cc75a 100644 (file)
@@ -174,6 +174,9 @@ extern int scols_table_enable_noheadings(struct libscols_table *tb, int enable);
 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);
@@ -182,6 +185,8 @@ extern int scols_table_remove_column(struct libscols_table *tb, struct libscols_
 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);
index 3984751cf2367dd09241d50739ffc07025ed7041..4b64f280ce2ab6ed9870f8c8db3f83ce98064835 100644 (file)
@@ -71,7 +71,9 @@ global:
        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;
@@ -91,6 +93,8 @@ global:
        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;
index b7f1f2b6d00554253a43eed9417a96499629a027..ebdc9295f67924083a45246f66a3a6f4ab3040ed 100644 (file)
@@ -125,6 +125,9 @@ struct libscols_table {
        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;
index 914cc2ed9afcf15f00d4c72f4d5fccba1846586d..34b43b508a8137b782029c1aefafd7819846a1c0 100644 (file)
@@ -84,6 +84,8 @@ void scols_unref_table(struct libscols_table *tb)
                scols_table_remove_lines(tb);
                scols_table_remove_columns(tb);
                scols_unref_symbols(tb->symbols);
+               free(tb->linesep);
+               free(tb->colsep);
                free(tb);
        }
 }
@@ -609,6 +611,11 @@ struct libscols_table *scols_copy_table(struct libscols_table *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);
@@ -880,3 +887,87 @@ int scols_table_is_tree(struct libscols_table *tb)
        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;
+
+}
index d25bd252dfaf0d2702f0e1a2ccb89f0d0473d0e6..0a3a3076018295034b5289965079a5003fd92650 100644 (file)
@@ -31,6 +31,8 @@
 #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 */
@@ -51,7 +53,7 @@ static void print_data(struct libscols_table *tb,
        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;
        }
 
@@ -60,7 +62,7 @@ static void print_data(struct libscols_table *tb,
                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;
        }
 
@@ -117,17 +119,17 @@ static void print_data(struct libscols_table *tb,
                }
        }
        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);
@@ -229,7 +231,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', tb->out);
+       fputs(linesep(tb), tb->out);
 }
 
 static void print_header(struct libscols_table *tb, char *buf, size_t bufsz)
@@ -252,7 +254,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', tb->out);
+       fputs(linesep(tb), tb->out);
 }
 
 static void print_table(struct libscols_table *tb, char *buf, size_t bufsz)
index 90f9df443f9178074a473b2ae7aef4976c261afd..eaff49e7e12c23b0830ce38db872a9174511b789 100644 (file)
@@ -157,6 +157,7 @@ static void __attribute__((__noreturn__)) usage(FILE *out)
        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);
@@ -175,9 +176,9 @@ int main(int argc, char *argv[])
                { "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 },
        };
 
@@ -191,7 +192,7 @@ int main(int argc, char *argv[])
        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);
@@ -213,6 +214,12 @@ int main(int argc, char *argv[])
                        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: