From a0f0220726f84dc62f49b593481a00f005e3443e Mon Sep 17 00:00:00 2001 From: Katharina Fey Date: Sun, 28 Aug 2016 16:31:16 +0200 Subject: First version of the "eztree" api. It provides more straight-forward ways to use libdyntree (in a less safe way however!) eztree_* functions do not provide error chec --- CMakeLists.txt | 7 ++-- include/dtree/eztree.h | 89 +++++++++++++++++++------------------------------- lib/eztree.c | 77 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 57 deletions(-) create mode 100644 lib/eztree.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 2fd3b8e..5735947 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,10 @@ set(CMAKE_BUILD_TYPE Debug) # Create our project for further reference project(libdyntree) -set(DYN_TREE_SRC lib/dtree.c lib/dyn_utils.c) +set(DYN_TREE_SRC + lib/dtree.c + lib/eztree.c + lib/dyn_utils.c) # Define our library in cmake add_library(libdyntree SHARED ${DYN_TREE_SRC}) @@ -26,7 +29,7 @@ set_target_properties(libdyntree PROPERTIES PREFIX "") ################### TESTING CODE BELOW ################### -set(TEST_SRC test/main.c include/dtree/eztree.h) +set(TEST_SRC test/main.c) add_executable(dyntree_test ${TEST_SRC}) # Library dependencies for the http extention diff --git a/include/dtree/eztree.h b/include/dtree/eztree.h index 8b13874..a538831 100644 --- a/include/dtree/eztree.h +++ b/include/dtree/eztree.h @@ -1,87 +1,66 @@ -// -// Created by spacekookie on 28/08/16. -// - #ifndef LIBDYNTREE_EZTREE_H #define LIBDYNTREE_EZTREE_H #include #include +/* Also make sure we're _always_ interpreted as a C file */ +#ifdef __cplusplus +extern "C" { +#endif + +#define EZTREE_LITERAL 0xA +#define EZTREE_NUMERIC 0xB +#define EZTREE_NESTED 0xC + + /** - * Shortcut function that allocates a new string node. Beware however: - * - * THIS FUNCTION DOES NOT RETURN WARNINGS OR ERROR CODES! + * An quick create function for a literal node * * @param string * @return */ -static dtree *dtree_alloc_literal(const char *string) -{ - dtree *node; - dtree_malloc(&node); - dtree_addliteral(node, string); - return node; -} +dtree *eztree_new_literal(const char *string); + /** - * Shortcut function that allocates a new numerical node. - * Beware however: - * - * THIS FUNCTION DOES NOT RETURN WARNINGS OR ERROR CODES! - * + * A quick create function for a number (numeric) node * @param num * @return */ -static dtree *dtree_alloc_numeral(const long num) -{ - dtree *node; - dtree_malloc(&node); - dtree_addnumeral(node, num); - return node; -} +dtree *eztree_new_numeric(const long num); + /** - * Shortcut function which creates two nodes as pair under a root - * node which is returned. Beware however: + * A quick create function for a string key and an arbitrary type value. + * The value needs to be marked properly or errors might occur. * - * THIS FUNCTION DOES NOT RETURN WARNINGS OR ERROR CODES! + * Nested nodes can be passed as values but need to be declared in before. This means + * that the tree needs to be built bottom-up. * - @param key Will be the key node - * @param val Will be the value node - * @return New root node with key-val children + * @param key + * @param val + * @param type + * @return */ -static dtree *dtree_allocpair_new(dtree **key, dtree **val) -{ - dtree *root; - dtree_malloc(&root); - dtree_addpair(root, key, val); - return root; -} +dtree *eztree_new_pair(const char *key, void *val, short type); /** - * Shortcut function which allocates a list of nodes in a list under - * a root node listly. + * A quick create function for a list node with a certain number of children + * ready to go. Children will be placed into a bufer that needs to be provided + * and the new parent will be returned from the function. * - * WARNING: Return value is allocated on heap. MUST FREE MANUALLY! - * WARNING: THIS FUNCTION DOES NOT RETURN WARNINGS OR ERROR CODES! + * Provided size needs to be size of the child-buffer or else errors might occur! * - * @param root - * @param count + * @param list + * @param size * @return */ -static dtree **dtree_alloc_listlist(dtree **root, unsigned int count) -{ - dtree **nodes = malloc(sizeof(dtree**) * count); - - dtree_malloc(root); - - int i; - for(i = 0; i < count; i++) - dtree_addlist(*root, &nodes[i]); +dtree *eztree_new_list(dtree **list, size_t size); - return nodes; +#ifdef __cplusplus } +#endif #endif //LIBDYNTREE_EZTREE_H diff --git a/lib/eztree.c b/lib/eztree.c new file mode 100644 index 0000000..ba8506a --- /dev/null +++ b/lib/eztree.c @@ -0,0 +1,77 @@ +// Include eztree header file +#include + +dtree *eztree_new_literal(const char *string) +{ + dtree *node; + dtree_malloc(&node); + dtree_addliteral(node, string); + return node; +} + + +dtree *eztree_new_numeric(const long num) +{ + dtree *node; + dtree_malloc(&node); + dtree_addnumeral(node, num); + return node; +} + + +dtree *eztree_new_pair(const char *kdata, void *vdata, short type) +{ + dtree *root, *key, *val; + + /* Allocate nodes */ + dtree_malloc(&root); + dtree_addpair(root, &key, &val); + + /* Fill the data */ + dtree_addliteral(key, kdata); + switch(type) { + case EZTREE_LITERAL: + dtree_addliteral(val, (char*) vdata); + break; + + case EZTREE_NUMERIC: + // FIXME: This might be dangerous on 32bit + dtree_addnumeral(val, (long) vdata); + break; + + case EZTREE_NESTED: + { + dtree *tmp; + dtree_addlist(val, &tmp); + + /* Manually override data */ + memcpy(val->payload.list[0], vdata, sizeof(vdata)); + break; + } + + default: break; + } + + return root; +} + +// +//dtree *eztree_new_list(dtree **list, size_t size) +//{ +// /* Prepare our buffer */ +// memset(list, 0, sizeof(dtree*) * size); +// +// /* Prepare root node */ +// dtree *root; +// dtree_malloc(&root); +// +// /* Add apropriate number of children to root node */ +// int i; +// for(i = 0; i < size; i++) { +// dtree *tmp; +// dtree_addlist(root, &tmp); +// list[i] = tmp; +// } +// +// return root; +//} \ No newline at end of file -- cgit v1.2.3