]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: add lines
authorKarel Zak <kzak@redhat.com>
Tue, 18 Mar 2014 11:39:54 +0000 (12:39 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 3 Apr 2014 10:29:16 +0000 (12:29 +0200)
Co-Author: Ondrej Oprala <ooprala@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/src/cell.c
libsmartcols/src/column.c
libsmartcols/src/libsmartcols.h.in
libsmartcols/src/line.c [new file with mode: 0644]
libsmartcols/src/smartcolsP.h

index 54dffaed0fd9a2a381dc92baa5533bd73e4c5e20..31934ab644cbd694d6c924722f23451b42cf8fe7 100644 (file)
@@ -80,3 +80,16 @@ const char *scols_cell_get_color(const struct libscols_cell *ce)
        return ce ? ce->color : NULL;
 }
 
+int scols_cell_copy_content(struct libscols_cell *dest,
+                           const struct libscols_cell *src)
+{
+       int rc;
+
+       assert(dest);
+       assert(src);
+
+       rc = scols_cell_set_data(dest, scols_cell_get_data(src));
+       if (!rc)
+               rc = scols_cell_set_color(dest, scols_cell_get_color(src));
+       return rc;
+}
index 5e6e01aec7f7413fd8b6d12dca69313c13cfce8b..6e6ec1759b3d5706e7daf691771492d70790536f 100644 (file)
@@ -54,15 +54,10 @@ struct libscols_column *scols_copy_column(const struct libscols_column *cl)
        ret = scols_new_column();
        if (!ret)
                return NULL;
-       if (cl->color) {
-               ret->color = strdup(cl->color);
-               if (!ret->color)
-                       goto err;
-       }
 
-       if (scols_cell_set_data(&ret->header, scols_cell_get_data(&cl->header)))
+       if (scols_column_set_color(ret, cl->color))
                goto err;
-       if (scols_cell_set_color(&ret->header, scols_cell_get_color(&cl->header)))
+       if (scols_cell_copy_content(&ret->header, &cl->header))
                goto err;
 
        ret->width      = cl->width;
@@ -113,7 +108,7 @@ int scols_column_get_flags(struct libscols_column *cl)
        return cl ? cl->flags : -EINVAL;
 }
 
-const struct libscols_cell *scols_column_get_header(struct libscols_column *cl)
+struct libscols_cell *scols_column_get_header(struct libscols_column *cl)
 {
        assert(cl);
        return cl ? &cl->header : NULL;
index 32c26ce41a9ab7f1da9328cc11f9d2d962f8cd56..f0291756f489efc669eadbcfed13ae1087da5969 100644 (file)
@@ -46,6 +46,8 @@ extern int scols_symbols_set_right(struct libscols_symbols *sb, const char *str)
 
 /* cell.c */
 extern int scols_reset_cell(struct libscols_cell *ce);
+extern int scols_cell_copy_content(struct libscols_cell *dest,
+                                  const struct libscols_cell *src);
 extern int scols_cell_set_data(struct libscols_cell *ce, const char *str);
 extern const char *scols_cell_get_data(const struct libscols_cell *ce);
 extern int scols_cell_set_color(struct libscols_cell *ce, const char *color);
@@ -60,7 +62,7 @@ extern int scols_column_set_whint(struct libscols_column *cl, double whint);
 extern double scols_column_get_whint(struct libscols_column *cl);
 extern int scols_column_set_flags(struct libscols_column *cl, int flags);
 extern int scols_column_get_flags(struct libscols_column *cl);
-extern const struct libscols_cell *scols_column_get_header(struct libscols_column *cl);
+extern struct libscols_cell *scols_column_get_header(struct libscols_column *cl);
 extern int scols_column_set_color(struct libscols_column *cl, const char *color);
 extern const char *scols_column_get_color(struct libscols_column *cl);
 
diff --git a/libsmartcols/src/line.c b/libsmartcols/src/line.c
new file mode 100644 (file)
index 0000000..589d5a2
--- /dev/null
@@ -0,0 +1,170 @@
+/*
+ * line.c - functions for table handling at the line level
+ *
+ * Copyright (C) 2014 Karel Zak <kzak@redhat.com>
+ *
+ * This file may be redistributed under the terms of the
+ * GNU Lesser General Public License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#include "smartcolsP.h"
+
+struct libscols_line *scols_new_line(void)
+{
+       struct libscols_line *ln;
+
+       ln = calloc(1, sizeof(*ln));
+       if (!ln)
+               return NULL;
+
+       ln->refcount = 1;
+       INIT_LIST_HEAD(&ln->ln_lines);
+       INIT_LIST_HEAD(&ln->ln_children);
+       INIT_LIST_HEAD(&ln->ln_branch);
+       return ln;
+}
+
+void scols_ref_line(struct libscols_line *ln)
+{
+       if (ln)
+               ln->refcount++;
+}
+
+void scols_unref_line(struct libscols_line *ln)
+{
+       if (ln && --ln->refcount <= 0) {
+               size_t i;
+
+               list_del(&ln->ln_lines);
+               list_del(&ln->ln_children);
+
+               for (i = 0; i <= ln->ncells; i++)
+                       scols_reset_cell(ln->cells[i]);
+               free(ln->data);
+               free(ln->color);
+               free(ln);
+       }
+}
+
+int scols_line_set_userdata(struct libscols_line *ln, void *data)
+{
+       assert(ln);
+       if (!ln)
+               return -EINVAL;
+       ln->userdata = data;
+       return 0;
+}
+
+void *scols_line_get_userdata(struct libscols_line *ln)
+{
+       assert(ln);
+       return ln ? ln->userdata : NULL;
+}
+
+int scols_line_set_parent(struct libscols_line *ln, struct libscols_line *parent)
+{
+       assert(ln);
+       if (!ln)
+               return -EINVAL;
+       ln->parent = parent;
+       return 0;
+}
+
+struct libscols_line *scols_line_get_parent(struct libscols_line *ln)
+{
+       assert(ln);
+       return ln ? ln->parent : NULL;
+}
+
+/*
+ * The default line color, used when cell color unspecified.
+ */
+int scols_line_set_color(struct libscols_line *ln, const char *color)
+{
+       char *p = NULL;
+
+       assert(ln);
+       if (!ln)
+               return -EINVAL;
+       if (color) {
+               p = strdup(color);
+               if (!p)
+                       return -ENOMEM;
+       }
+
+       free(ln->color);
+       ln->color = p;
+       return 0;
+}
+
+const char *scols_line_get_color(struct libscols_line *ln)
+{
+       assert(ln);
+       return ln ? ln->color : NULL;
+}
+
+size_t scols_line_get_datasize(struct libscols_line *ln)
+{
+       assert(ln);
+       return ln ? ln->data_sz : 0;
+}
+
+size_t scols_line_get_ncells(struct libscols_line *ln)
+{
+       assert(ln);
+       return ln ? ln->ncells : 0;
+}
+
+struct libscols_cell *scols_line_get_cell(struct libscols_line *ln,
+                                         size_t n)
+{
+       assert(ln);
+
+       if (!ln || n >= ln->ncells)
+               return NULL;
+       return ln->cells[n];
+}
+
+struct libscols_line *scols_copy_line(struct libscols_line *ln)
+{
+       struct libscols_line *ret;
+
+       assert (ln);
+       if (!ln)
+               return NULL;
+
+       ret = scols_new_line();
+       if (!ret)
+               return NULL;
+       if (scols_line_set_color(ret, ln->color))
+               goto err;
+       ret->userdata = ln->userdata;
+       ret->parent   = ln->parent;
+       ret->data_sz  = ln->data_sz;
+       ret->ncells   = ln->ncells;
+
+       if (ln->ncells) {
+               size_t i;
+
+               ret->cells = calloc(ln->ncells, sizeof(struct libscols_cell));
+               if (!ret->cells)
+                       goto err;
+
+               for (i = 0; i < ret->ncells; ++i) {
+                       if (scols_cell_copy_content(ret->cells[i], ln->cells[i]))
+                               goto err;
+               }
+       }
+
+       return ret;
+err:
+       scols_unref_line(ret);
+       return NULL;
+}
+
+
index 4ee2216bf472f9f905373443c27c96eecd0d0a2e..f697cc8b9d5c55a85334becd4d3d4cc35836192f 100644 (file)
@@ -72,5 +72,23 @@ struct libscols_column {
        struct list_head        cl_columns;
 };
 
+/*
+ * Table line
+ */
+struct libscols_line {
+       int     refcount;
+       void    *userdata;
+       size_t  data_sz;        /* strlen of all data */
+       char    *color;         /* default line color */
+
+       struct libscols_cell    **cells;        /* array with data */
+       size_t                  ncells;         /* number of cells */
+
+       struct list_head        ln_lines;       /* table lines */
+       struct list_head        ln_branch;      /* begin of branch (head of ln_children) */
+       struct list_head        ln_children;
+
+       struct libscols_line    *parent;
+};
 
 #endif /* _LIBSMARTCOLS_PRIVATE_H */