From 6d1072696ce3e4ae9dc8b4a37805fb00bfc1d4e1 Mon Sep 17 00:00:00 2001 From: Ondrej Oprala Date: Mon, 17 Mar 2014 13:00:52 +0100 Subject: [PATCH] libsmartcols: add cells [kzak@redhat.com: - remove copy, free -- all have to be handled by lines] Signed-off-by: Karel Zak --- libsmartcols/src/Makemodule.am | 1 + libsmartcols/src/cell.c | 70 ++++++++++++++++++++++++++++++ libsmartcols/src/libsmartcols.h.in | 7 +++ libsmartcols/src/smartcolsP.h | 8 ++++ 4 files changed, 86 insertions(+) create mode 100644 libsmartcols/src/cell.c diff --git a/libsmartcols/src/Makemodule.am b/libsmartcols/src/Makemodule.am index 5f8d180126..7ee7009308 100644 --- a/libsmartcols/src/Makemodule.am +++ b/libsmartcols/src/Makemodule.am @@ -11,6 +11,7 @@ libsmartcols_la_SOURCES= \ libsmartcols/src/smartcolsP.h \ libsmartcols/src/iter.c \ libsmartcols/src/symbols.c \ + libsmartcols/src/cell.c \ $(smartcolsinc_HEADERS) nodist_libsmartcols_la_SOURCES = libsmartcols/src/smartcolsP.h diff --git a/libsmartcols/src/cell.c b/libsmartcols/src/cell.c new file mode 100644 index 0000000000..0a4999ff29 --- /dev/null +++ b/libsmartcols/src/cell.c @@ -0,0 +1,70 @@ +/* + * cell.c - functions for table handling at the cell 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" + +/* + * The cell has no ref-counting, free() and new() functions. All is + * handled by libscols_line. + */ + +int scols_cell_set_data(struct libscols_cell *ce, const char *str) +{ + char *p = NULL; + + assert(ce); + + if (!ce) + return -EINVAL; + if (str) { + p = strdup(str); + if (!p) + return -ENOMEM; + } + free(ce->data); + ce->data = p; + return 0; +} + +const char *scols_cell_get_data(const struct libscols_cell *ce) +{ + assert(ce); + return ce ? ce->data : NULL; +} + +int scols_cell_set_color(struct libscols_cell *ce, const char *color) +{ + char *p = NULL; + + assert(ce); + + if (!ce) + return -EINVAL; + if (color) { + p = strdup(color); + if (!p) + return -ENOMEM; + } + free(ce->color); + ce->color = p; + return 0; +} + +const char *scols_cell_get_color(const struct libscols_cell *ce) +{ + assert(ce); + return ce ? ce->color : NULL; +} + diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index eefac6c63a..7b3d4d837d 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -21,6 +21,7 @@ extern "C" { struct libscols_iter; struct libscols_symbols; +struct libscols_cell; /* iter.c */ @@ -43,6 +44,12 @@ extern int scols_symbols_set_branch(struct libscols_symbols *sb, const char *str extern int scols_symbols_set_vertical(struct libscols_symbols *sb, const char *str); extern int scols_symbols_set_right(struct libscols_symbols *sb, const char *str); +/* cell.c */ +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); + #ifdef __cplusplus } #endif diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index ca662a2047..6cb712a46c 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -42,4 +42,12 @@ struct libscols_symbols { char *right; }; +/* + * Table cells + */ +struct libscols_cell { + char *data; + char *color; +}; + #endif /* _LIBSMARTCOLS_PRIVATE_H */ -- 2.47.3