From b36992fb2a44a49c21a608a18cd7da9ceef349c7 Mon Sep 17 00:00:00 2001 From: Ondrej Oprala Date: Mon, 17 Mar 2014 12:19:48 +0100 Subject: [PATCH] libsmartcols: add symbols [kzak@redhat.com: - split scols_new_symbols() to regular getters functions] Signed-off-by: Karel Zak --- libsmartcols/src/Makemodule.am | 1 + libsmartcols/src/libsmartcols.h.in | 9 +++ libsmartcols/src/smartcolsP.h | 9 +++ libsmartcols/src/symbols.c | 103 +++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 libsmartcols/src/symbols.c diff --git a/libsmartcols/src/Makemodule.am b/libsmartcols/src/Makemodule.am index 3dbac9ca1e..5f8d180126 100644 --- a/libsmartcols/src/Makemodule.am +++ b/libsmartcols/src/Makemodule.am @@ -10,6 +10,7 @@ libsmartcols_la_SOURCES= \ \ libsmartcols/src/smartcolsP.h \ libsmartcols/src/iter.c \ + libsmartcols/src/symbols.c \ $(smartcolsinc_HEADERS) nodist_libsmartcols_la_SOURCES = libsmartcols/src/smartcolsP.h diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index 46a06a0ce6..eefac6c63a 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -20,6 +20,7 @@ extern "C" { #define LIBSMARTCOLS_VERSION "@LIBSMARTCOLS_VERSION@" struct libscols_iter; +struct libscols_symbols; /* iter.c */ @@ -34,6 +35,14 @@ extern void scols_free_iter(struct libscols_iter *itr); extern void scols_reset_iter(struct libscols_iter *itr, int direction); extern int scols_iter_get_direction(struct libscols_iter *itr); +/* symbols.c */ +extern struct libscols_symbols *scols_new_symbols(void); +struct libscols_symbols *scols_copy_symbols(const struct libscols_symbols *sb); +extern void scols_free_symbols(struct libscols_symbols *sb); +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); + #ifdef __cplusplus } #endif diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h index c424575805..ca662a2047 100644 --- a/libsmartcols/src/smartcolsP.h +++ b/libsmartcols/src/smartcolsP.h @@ -33,4 +33,13 @@ struct libscols_iter { int direction; /* SCOLS_ITER_{FOR,BACK}WARD */ }; +/* + * Tree symbols + */ +struct libscols_symbols { + char *branch; + char *vert; + char *right; +}; + #endif /* _LIBSMARTCOLS_PRIVATE_H */ diff --git a/libsmartcols/src/symbols.c b/libsmartcols/src/symbols.c new file mode 100644 index 0000000000..344ddcf53a --- /dev/null +++ b/libsmartcols/src/symbols.c @@ -0,0 +1,103 @@ +#include +#include +#include +#include + +#include "smartcolsP.h" + +struct libscols_symbols *scols_new_symbols(void) +{ + return calloc(1, sizeof(struct libscols_symbols)); +} + +int scols_symbols_set_branch(struct libscols_symbols *sb, const char *str) +{ + char *p = NULL; + + assert(sb); + + if (!sb) + return -EINVAL; + if (str) { + p = strdup(str); + if (!p) + return -ENOMEM; + } + free(sb->branch); + sb->branch = p; + return 0; +} + +int scols_symbols_set_vertical(struct libscols_symbols *sb, const char *str) +{ + char *p = NULL; + + assert(sb); + + if (!sb) + return -EINVAL; + if (str) { + p = strdup(str); + if (!p) + return -ENOMEM; + } + free(sb->vert); + sb->vert = p; + return 0; +} + +int scols_symbols_set_right(struct libscols_symbols *sb, const char *str) +{ + char *p = NULL; + + assert(sb); + + if (!sb) + return -EINVAL; + if (str) { + p = strdup(str); + if (!p) + return -ENOMEM; + } + free(sb->right); + sb->right = p; + return 0; +} + + +struct libscols_symbols *scols_copy_symbols(const struct libscols_symbols *sb) +{ + struct libscols_symbols *ret; + int rc; + + assert(sb); + if (!sb) + return NULL; + + ret = scols_new_symbols(); + if (!ret) + return NULL; + + rc = scols_symbols_set_branch(ret, sb->branch); + if (!rc) + rc = scols_symbols_set_vertical(ret, sb->vert); + if (!rc) + rc = scols_symbols_set_right(ret, sb->right); + if (!rc) + return ret; + + scols_free_symbols(ret); + return NULL; + +} + +void scols_free_symbols(struct libscols_symbols *sb) +{ + if (!sb) + return; + + free(sb->branch); + free(sb->vert); + free(sb->right); + free(sb); +} -- 2.47.2