From: Karel Zak Date: Tue, 18 Mar 2014 10:22:39 +0000 (+0100) Subject: libsmartcols: add columns X-Git-Tag: v2.25-rc1~385 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1577b2592e2e31e52ed5d8cbed8fe90868891f8f;p=thirdparty%2Futil-linux.git libsmartcols: add columns Signed-off-by: Karel Zak --- diff --git a/libsmartcols/src/Makemodule.am b/libsmartcols/src/Makemodule.am index 7ee7009308..4c4fa4aa46 100644 --- a/libsmartcols/src/Makemodule.am +++ b/libsmartcols/src/Makemodule.am @@ -12,6 +12,7 @@ libsmartcols_la_SOURCES= \ libsmartcols/src/iter.c \ libsmartcols/src/symbols.c \ libsmartcols/src/cell.c \ + libsmartcols/src/column.c \ $(smartcolsinc_HEADERS) nodist_libsmartcols_la_SOURCES = libsmartcols/src/smartcolsP.h diff --git a/libsmartcols/src/cell.c b/libsmartcols/src/cell.c index 0a4999ff29..54dffaed0f 100644 --- a/libsmartcols/src/cell.c +++ b/libsmartcols/src/cell.c @@ -19,6 +19,18 @@ * The cell has no ref-counting, free() and new() functions. All is * handled by libscols_line. */ +int scols_reset_cell(struct libscols_cell *ce) +{ + assert(ce); + + if (!ce) + return -EINVAL; + + free(ce->data); + free(ce->color); + memset(ce, 0, sizeof(*ce)); + return 0; +} int scols_cell_set_data(struct libscols_cell *ce, const char *str) { diff --git a/libsmartcols/src/column.c b/libsmartcols/src/column.c new file mode 100644 index 0000000000..5e6e01aec7 --- /dev/null +++ b/libsmartcols/src/column.c @@ -0,0 +1,156 @@ +/* + * column.c - functions for table handling at the column level + * + * Copyright (C) 2014 Ondrej Oprala + * Copyright (C) 2014 Karel Zak + * + * This file may be redistributed under the terms of the + * GNU Lesser General Public License. + */ + +#include +#include +#include +#include + +#include "smartcolsP.h" + +struct libscols_column *scols_new_column(void) +{ + struct libscols_column *cl; + + cl = calloc(1, sizeof(*cl)); + if (!cl) + return NULL; + + cl->refcount = 1; + INIT_LIST_HEAD(&cl->cl_columns); + return cl; +} + +void scols_ref_column(struct libscols_column *cl) +{ + if (cl) + cl->refcount++; +} + +void scols_unref_column(struct libscols_column *cl) +{ + if (cl && --cl->refcount <= 0) { + list_del(&cl->cl_columns); + scols_reset_cell(&cl->header); + free(cl->color); + free(cl); + } +} + +struct libscols_column *scols_copy_column(const struct libscols_column *cl) +{ + struct libscols_column *ret; + + assert (cl); + if (!cl) + return NULL; + 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))) + goto err; + if (scols_cell_set_color(&ret->header, scols_cell_get_color(&cl->header))) + goto err; + + ret->width = cl->width; + ret->width_min = cl->width_min; + ret->width_max = cl->width_max; + ret->width_avg = cl->width_avg; + ret->width_hint = cl->width_hint; + ret->flags = cl->flags; + ret->is_extreme = cl->is_extreme; + + return ret; +err: + scols_unref_column(ret); + return NULL; +} + +int scols_column_set_whint(struct libscols_column *cl, double whint) +{ + assert(cl); + + if (!cl) + return -EINVAL; + + cl->width_hint = whint; + return 0; +} + +double scols_column_get_whint(struct libscols_column *cl) +{ + assert(cl); + return cl ? cl->width_hint : -EINVAL; +} + +int scols_column_set_flags(struct libscols_column *cl, int flags) +{ + assert(cl); + + if (!cl) + return -EINVAL; + + cl->flags = flags; + return 0; +} + +int scols_column_get_flags(struct libscols_column *cl) +{ + assert(cl); + return cl ? cl->flags : -EINVAL; +} + +const struct libscols_cell *scols_column_get_header(struct libscols_column *cl) +{ + assert(cl); + return cl ? &cl->header : NULL; +} + +/* + * The default color for data cells and column header. + * + * If you want to set header specific color then use scols_column_get_header() + * and scols_cell_set_color(). + * + * If you want to set data cell specific color the use scols_line_get_cell() + + * scols_cell_set_color(). + */ +int scols_column_set_color(struct libscols_column *cl, const char *color) +{ + char *p = NULL; + + assert(cl); + if (!cl) + return -EINVAL; + if (color) { + p = strdup(color); + if (!p) + return -ENOMEM; + } + + free(cl->color); + cl->color = p; + return 0; +} + +const char *scols_column_get_color(struct libscols_column *cl) +{ + assert(cl); + return cl ? cl->color : NULL; +} + + + diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index 7b3d4d837d..32c26ce41a 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -45,11 +45,25 @@ extern int scols_symbols_set_vertical(struct libscols_symbols *sb, const char *s 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_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); extern const char *scols_cell_get_color(const struct libscols_cell *ce); +/* column.c */ +extern struct libscols_column *scols_new_column(void); +extern void scols_ref_column(struct libscols_column *cl); +extern void scols_unref_column(struct libscols_column *cl); +extern struct libscols_column *scols_copy_column(const struct libscols_column *cl); +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 int scols_column_set_color(struct libscols_column *cl, const char *color); +extern const char *scols_column_get_color(struct libscols_column *cl); + #ifdef __cplusplus } #endif diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index 6cb712a46c..4ee2216bf4 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -50,4 +50,27 @@ struct libscols_cell { char *color; }; + +/* + * Table column + */ +struct libscols_column { + int refcount; /* reference counter */ + size_t seqnum; /* column index */ + + size_t width; /* real column width */ + size_t width_min; /* minimal width (usually header width) */ + size_t width_max; /* maximal width */ + size_t width_avg; /* average width, used to detect extreme fields */ + double width_hint; /* hint (N < 1 is in percent of termwidth) */ + + int flags; + int is_extreme; + char *color; /* default column color */ + + struct libscols_cell header; + struct list_head cl_columns; +}; + + #endif /* _LIBSMARTCOLS_PRIVATE_H */ diff --git a/misc-utils/wipefs.c b/misc-utils/wipefs.c index 932acdfa74..5dfaf0c55e 100644 --- a/misc-utils/wipefs.c +++ b/misc-utils/wipefs.c @@ -421,7 +421,7 @@ do_wipe(struct wipe_desc *wp, const char *devname, int flags) fsync(blkid_probe_get_fd(pr)); - if (reread) + if (reread && (mode & O_EXCL)) rereadpt(blkid_probe_get_fd(pr), devname); close(blkid_probe_get_fd(pr));