diff options
author | Karel Zak <kzak@redhat.com> | 2014-03-18 12:39:54 +0100 |
---|---|---|
committer | Karel Zak <kzak@redhat.com> | 2014-04-03 12:29:16 +0200 |
commit | 74bd28ad6eadd0d51fb8da5003332eb3255f88eb (patch) | |
tree | 32bfdf8ed7c3a46038be19bfec932133f8cb4821 /libsmartcols | |
parent | 1577b2592e2e31e52ed5d8cbed8fe90868891f8f (diff) | |
download | util-linux-74bd28ad6eadd0d51fb8da5003332eb3255f88eb.tar.gz |
libsmartcols: add lines
Co-Author: Ondrej Oprala <ooprala@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libsmartcols')
-rw-r--r-- | libsmartcols/src/cell.c | 13 | ||||
-rw-r--r-- | libsmartcols/src/column.c | 11 | ||||
-rw-r--r-- | libsmartcols/src/libsmartcols.h.in | 4 | ||||
-rw-r--r-- | libsmartcols/src/line.c | 170 | ||||
-rw-r--r-- | libsmartcols/src/smartcolsP.h | 18 |
5 files changed, 207 insertions, 9 deletions
diff --git a/libsmartcols/src/cell.c b/libsmartcols/src/cell.c index 54dffaed0..31934ab64 100644 --- a/libsmartcols/src/cell.c +++ b/libsmartcols/src/cell.c @@ -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; +} diff --git a/libsmartcols/src/column.c b/libsmartcols/src/column.c index 5e6e01aec..6e6ec1759 100644 --- a/libsmartcols/src/column.c +++ b/libsmartcols/src/column.c @@ -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; diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index 32c26ce41..f0291756f 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -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 index 000000000..589d5a2f6 --- /dev/null +++ b/libsmartcols/src/line.c @@ -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; +} + + diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index 4ee2216bf..f697cc8b9 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -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 */ |