aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatharina Fey <kookie@spacekookie.de>2016-08-28 14:05:15 +0200
committerKatharina Fey <kookie@spacekookie.de>2019-06-04 20:21:03 +0200
commitbcf48a0e4bd5bbf97c22cf9f7c9494b54d1f22ff (patch)
treece8122fb9c4d062da66f6dd5f68a3b4a8601691b
parentcb73654ecc93d8efed08ab337812bd0d63170241 (diff)
Moving utility functions into new file with a new API namespace
-rw-r--r--CMakeLists.txt2
-rw-r--r--include/dtree/dtree.h98
-rw-r--r--include/dtree/eztree.h87
-rw-r--r--lib/dtree.c102
4 files changed, 148 insertions, 141 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e738040..2fd3b8e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -26,7 +26,7 @@ set_target_properties(libdyntree PROPERTIES PREFIX "")
################### TESTING CODE BELOW ###################
-set(TEST_SRC test/main.c)
+set(TEST_SRC test/main.c include/dtree/eztree.h)
add_executable(dyntree_test ${TEST_SRC})
# Library dependencies for the http extention
diff --git a/include/dtree/dtree.h b/include/dtree/dtree.h
index e4ced5b..e9acf1a 100644
--- a/include/dtree/dtree.h
+++ b/include/dtree/dtree.h
@@ -4,15 +4,15 @@
*
* With the API you can easily create structures like the following:
*
- * root [recursive]
- * child1 [recursive]
+ * root [list]
+ * child1 [list]
* key [literal] - "Username"
* value [literal] - "spacekookie"
- * child2 [recursive]
+ * child2 [list]
* key [literal] - "Age"
* value [numerical] - 23
* child3
- * subchild [recursive]
+ * subchild [list]
* ...
*
* Freeing the root node will free all children
@@ -50,7 +50,7 @@ typedef struct dtree {
union {
char *literal;
long numeral;
- struct dtree *(*recursive);
+ struct dtree *(*list);
void *pointer;
} payload;
} dtree;
@@ -169,11 +169,11 @@ dt_err dtree_split_trees(dtree *data, dtree *sp);
/**
- * This function is very simmilar to dt_err "dtree_addrecursive"
+ * This function is very simmilar to dt_err "dtree_addlist"
* with the difference that it doesn't allocate new memory but instead
* works with existing nodes.
*
- * You need to provide a ROOT node which is of type recursive. It will
+ * You need to provide a ROOT node which is of type list. It will
* procede to add the second (merge) node into the child-list of the
* root data node - essentially making them related.
*
@@ -200,7 +200,6 @@ dt_err dtree_merge_trees(dtree *data, dtree *merge);
dt_err dtree_search_payload(dtree *data, dtree *(*found), void *payload, dt_uni_t type);
-
/**
* Much like #{dtree_search_payload} but limiting it's search to keys in a list structure of certain depth.
* This means that in a key-value store structure only top-level items can be searched or the entire
@@ -214,6 +213,7 @@ dt_err dtree_search_payload(dtree *data, dtree *(*found), void *payload, dt_uni_
*/
dt_err dtree_search_keypayload(dtree *data, dtree *(*found), void *payload, dt_uni_t type, int depth);
+
/**
* Performs a deep copy of a data node hirarchy. Does not copy externally
* pointed structures. Does garuantee safety of hirarchy.
@@ -320,7 +320,7 @@ const char *dtree_err_getmsg(dt_err *e);
dt_err dtree_encode_set(dtree *data, short setting);
/**
- * A simple recursive node walker that encodes a dyn_tree node hirarchy
+ * A simple list node walker that encodes a dyn_tree node hirarchy
* into a json string. Requires the encoding setting to be set on the
* root node in order to successfully encode.
*
@@ -344,86 +344,6 @@ dt_err dtree_encode_json(dtree *data, char *json_data);
*/
dt_err dtree_decode_json(dtree *(*data), const char *json_data);
-
-
-/*** Some helper functions for more non-C oriented developers ***/
-
-/**
- * Shortcut function that allocates a new string node. Beware however:
- *
- * THIS FUNCTION DOES NOT RETURN WARNINGS OR ERROR CODES!
- *
- * @param string
- * @return
- */
-static dtree *dtree_alloc_literal(const char *string)
-{
- dtree *node;
- dtree_malloc(&node);
- dtree_addliteral(node, string);
- return node;
-}
-
-/**
- * Shortcut function that allocates a new numerical node.
- * Beware however:
- *
- * THIS FUNCTION DOES NOT RETURN WARNINGS OR ERROR CODES!
- *
- * @param num
- * @return
- */
-static dtree *dtree_alloc_numeral(const long num)
-{
- dtree *node;
- dtree_malloc(&node);
- dtree_addnumeral(node, num);
- return node;
-}
-
-/**
- * Shortcut function which creates two nodes as pair under a root
- * node which is returned. Beware however:
- *
- * THIS FUNCTION DOES NOT RETURN WARNINGS OR ERROR CODES!
- *
- @param key Will be the key node
- * @param val Will be the value node
- * @return New root node with key-val children
- */
-static dtree *dtree_allocpair_new(dtree **key, dtree **val)
-{
- dtree *root;
- dtree_malloc(&root);
- dtree_addpair(root, key, val);
- return root;
-}
-
-
-/**
- * Shortcut function which allocates a list of nodes in a list under
- * a root node recursively.
- *
- * WARNING: Return value is allocated on heap. MUST FREE MANUALLY!
- * WARNING: THIS FUNCTION DOES NOT RETURN WARNINGS OR ERROR CODES!
- *
- * @param root
- * @param count
- * @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]);
-
- return nodes;
-}
-
#ifdef __cplusplus
}
#endif
diff --git a/include/dtree/eztree.h b/include/dtree/eztree.h
new file mode 100644
index 0000000..8b13874
--- /dev/null
+++ b/include/dtree/eztree.h
@@ -0,0 +1,87 @@
+//
+// Created by spacekookie on 28/08/16.
+//
+
+#ifndef LIBDYNTREE_EZTREE_H
+#define LIBDYNTREE_EZTREE_H
+
+#include <dtree/dtree.h>
+#include <stdlib.h>
+
+/**
+ * Shortcut function that allocates a new string node. Beware however:
+ *
+ * THIS FUNCTION DOES NOT RETURN WARNINGS OR ERROR CODES!
+ *
+ * @param string
+ * @return
+ */
+static dtree *dtree_alloc_literal(const char *string)
+{
+ dtree *node;
+ dtree_malloc(&node);
+ dtree_addliteral(node, string);
+ return node;
+}
+
+/**
+ * Shortcut function that allocates a new numerical node.
+ * Beware however:
+ *
+ * THIS FUNCTION DOES NOT RETURN WARNINGS OR ERROR CODES!
+ *
+ * @param num
+ * @return
+ */
+static dtree *dtree_alloc_numeral(const long num)
+{
+ dtree *node;
+ dtree_malloc(&node);
+ dtree_addnumeral(node, num);
+ return node;
+}
+
+/**
+ * Shortcut function which creates two nodes as pair under a root
+ * node which is returned. Beware however:
+ *
+ * THIS FUNCTION DOES NOT RETURN WARNINGS OR ERROR CODES!
+ *
+ @param key Will be the key node
+ * @param val Will be the value node
+ * @return New root node with key-val children
+ */
+static dtree *dtree_allocpair_new(dtree **key, dtree **val)
+{
+ dtree *root;
+ dtree_malloc(&root);
+ dtree_addpair(root, key, val);
+ return root;
+}
+
+
+/**
+ * Shortcut function which allocates a list of nodes in a list under
+ * a root node listly.
+ *
+ * WARNING: Return value is allocated on heap. MUST FREE MANUALLY!
+ * WARNING: THIS FUNCTION DOES NOT RETURN WARNINGS OR ERROR CODES!
+ *
+ * @param root
+ * @param count
+ * @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]);
+
+ return nodes;
+}
+
+#endif //LIBDYNTREE_EZTREE_H
diff --git a/lib/dtree.c b/lib/dtree.c
index 30d8c9a..cd9b1f2 100644
--- a/lib/dtree.c
+++ b/lib/dtree.c
@@ -16,9 +16,9 @@
/*** Forward declared functions ***/
-int recursive_search(dtree**, dtree *, dtree *);
+int list_search(dtree**, dtree *, dtree *);
-void recursive_print(dtree *data, const char *offset);
+void list_print(dtree *data, const char *offset);
/******/
@@ -47,7 +47,7 @@ dt_err dtree_resettype(dtree *data)
int i;
dt_err err;
for(i = 0; i < data->size; i++) {
- err = dtree_free(data->payload.recursive[i]);
+ err = dtree_free(data->payload.list[i]);
if(err) return err;
}
}
@@ -135,17 +135,17 @@ dt_err dtree_addlist(dtree *data, dtree *(*new_data))
// TODO Use Realloc
dtree **tmp = (dtree**) malloc(sizeof(dtree*) * data->size);
- memcpy(tmp, data->payload.recursive, sizeof(dtree*) * data->used);
+ memcpy(tmp, data->payload.list, sizeof(dtree*) * data->used);
/* Free the list WITHOUT the children! */
- free(data->payload.recursive);
- data->payload.recursive = tmp;
+ free(data->payload.list);
+ data->payload.list = tmp;
}
/* This means the data object is new */
} else {
dtree **tmp = (dtree**) malloc(sizeof(dtree*) * data->size);
- data->payload.recursive = tmp;
+ data->payload.list = tmp;
data->type = LIST;
data->used = 0;
data->size = RDB_REC_DEF_SIZE;
@@ -155,7 +155,7 @@ dt_err dtree_addlist(dtree *data, dtree *(*new_data))
if(err) return err;
/* Reference the slot, assign it, then move our ctr */
- data->payload.recursive[data->used] = *new_data;
+ data->payload.list[data->used] = *new_data;
data->used++;
return SUCCESS;
@@ -181,12 +181,12 @@ dt_err dtree_addpair(dtree *data, dtree *(*key), dtree *(*value))
dtree **tmp = (dtree**) malloc(sizeof(dtree*) * data->size);
if(!tmp) goto cleanup;
- data->payload.recursive = tmp;
+ data->payload.list = tmp;
{ /* Assign data to new array */
- data->payload.recursive[data->used] = *key;
+ data->payload.list[data->used] = *key;
data->used++;
- data->payload.recursive[data->used] = *value;
+ data->payload.list[data->used] = *value;
data->used++;
}
@@ -210,19 +210,19 @@ dt_err dtree_split_trees(dtree *data, dtree *sp)
/* Check that sp is really a child of data */
dtree *dp;
- int ret = recursive_search(&dp, data, sp);
+ int ret = list_search(&dp, data, sp);
if(ret != 0) return DATA_NOT_RELATED;
if(dp == NULL) return NODE_NOT_FOUND;
- /* Find the exact recursive reference and remove it */
+ /* Find the exact list reference and remove it */
int i;
for(i = 0; i < dp->used; i++) {
- if(dp->payload.recursive[i] == NULL) continue;
+ if(dp->payload.list[i] == NULL) continue;
/* Manually remove the entry */
- if(dp->payload.recursive[i] == sp) {
+ if(dp->payload.list[i] == sp) {
dp->used--;
- dp->payload.recursive[i] = NULL;
+ dp->payload.list[i] = NULL;
}
}
@@ -245,24 +245,24 @@ dt_err dtree_merge_trees(dtree *data, dtree *merge)
data->size += RDB_REC_MULTIPLY;
dtree **tmp = (dtree**) malloc(sizeof(dtree*) * data->size);
- memcpy(tmp, data->payload.recursive, sizeof(dtree*) * data->used);
+ memcpy(tmp, data->payload.list, sizeof(dtree*) * data->used);
/* Free the list WITHOUT the children! */
- free(data->payload.recursive);
- data->payload.recursive = tmp;
+ free(data->payload.list);
+ data->payload.list = tmp;
}
/* This means the data object is new */
} else {
dtree **tmp = (dtree**) malloc(sizeof(dtree*) * data->size);
- data->payload.recursive = tmp;
+ data->payload.list = tmp;
data->type = LIST;
data->used = 0;
data->size = RDB_REC_DEF_SIZE;
}
/* Reference the slot, assign it, then move our ctr */
- data->payload.recursive[data->used] = merge;
+ data->payload.list[data->used] = merge;
data->used++;
return SUCCESS;
@@ -296,7 +296,7 @@ dt_err dtree_copy_deep(dtree *data, dtree *(*copy))
int num = (int) data->used;
for(i = 0; i < num; i++) {
- dtree *node = data->payload.recursive[i];
+ dtree *node = data->payload.list[i];
dtree *new;
dtree_addlist((*copy), &new);
@@ -311,8 +311,8 @@ dt_err dtree_copy_deep(dtree *data, dtree *(*copy))
dtree *key, *val;
dtree_addpair((*copy), &key, &val);
- dtree *orig_key = data->payload.recursive[0];
- dtree *orig_val = data->payload.recursive[1];
+ dtree *orig_key = data->payload.list[0];
+ dtree *orig_val = data->payload.list[1];
dtree_copy_deep(orig_key, &key);
dtree_copy_deep(orig_val, &val);
@@ -356,14 +356,14 @@ dt_err dtree_copy(dtree *data, dtree *(*copy))
case LIST:
(*copy)->type = LIST;
- (*copy)->payload.recursive = (dtree**) malloc(sizeof(dtree*) * data->size);
- memcpy((*copy)->payload.recursive, data->payload.recursive, sizeof(dtree*) * data->used);
+ (*copy)->payload.list = (dtree**) malloc(sizeof(dtree*) * data->size);
+ memcpy((*copy)->payload.list, data->payload.list, sizeof(dtree*) * data->used);
break;
case PAIR:
(*copy)->type = PAIR;
- (*copy)->payload.recursive = (dtree**) malloc(sizeof(dtree*) * data->size);
- memcpy((*copy)->payload.recursive, data->payload.recursive, sizeof(dtree*) * data->used);
+ (*copy)->payload.list = (dtree**) malloc(sizeof(dtree*) * data->size);
+ memcpy((*copy)->payload.list, data->payload.list, sizeof(dtree*) * data->used);
break;
case POINTER:
@@ -391,7 +391,7 @@ dt_err dtree_search_payload(dtree *data, dtree *(*found), void *payload, dt_uni_
int i;
for(i = 0; i < data->used; i++) {
- dt_err err = dtree_search_payload(data->payload.recursive[i], found, payload, type);
+ dt_err err = dtree_search_payload(data->payload.list[i], found, payload, type);
if(err == SUCCESS) return SUCCESS;
}
@@ -425,7 +425,7 @@ dt_err dtree_search_payload(dtree *data, dtree *(*found), void *payload, dt_uni_
}
-void recursive_print(dtree *data, const char *offset)
+void list_print(dtree *data, const char *offset)
{
dt_uni_t type = data->type;
@@ -444,23 +444,23 @@ void recursive_print(dtree *data, const char *offset)
case PAIR:
{
- dt_uni_t k_type = data->payload.recursive[0]->type;
- dt_uni_t v_type = data->payload.recursive[1]->type;
+ dt_uni_t k_type = data->payload.list[0]->type;
+ dt_uni_t v_type = data->payload.list[1]->type;
- if(k_type == LITERAL) printf("%s['%s']", offset, data->payload.recursive[0]->payload.literal);
- if(k_type == NUMERIC) printf("%s[%lu]", offset, data->payload.recursive[0]->payload.numeral);
+ if(k_type == LITERAL) printf("%s['%s']", offset, data->payload.list[0]->payload.literal);
+ if(k_type == NUMERIC) printf("%s[%lu]", offset, data->payload.list[0]->payload.numeral);
char new_offset[REAL_STRLEN(offset) + 2];
strcpy(new_offset, offset);
strcat(new_offset, " ");
- if(k_type == LIST || k_type == PAIR) recursive_print(data->payload.recursive[0], new_offset);
+ if(k_type == LIST || k_type == PAIR) list_print(data->payload.list[0], new_offset);
/* Print the value now */
- if(v_type == LITERAL) printf(" => ['%s']\n", data->payload.recursive[1]->payload.literal);
- if(v_type== NUMERIC) printf(" => [%lu]\n", data->payload.recursive[1]->payload.numeral);
+ if(v_type == LITERAL) printf(" => ['%s']\n", data->payload.list[1]->payload.literal);
+ if(v_type== NUMERIC) printf(" => [%lu]\n", data->payload.list[1]->payload.numeral);
- if(v_type == LIST || k_type == PAIR) recursive_print(data->payload.recursive[1], new_offset);
+ if(v_type == LIST || k_type == PAIR) list_print(data->payload.list[1], new_offset);
break;
}
@@ -470,7 +470,7 @@ void recursive_print(dtree *data, const char *offset)
int i;
printf("%s[LIST]\n", offset);
for(i = 0; i < data->used; i++) {
- dt_uni_t t = data->payload.recursive[i]->type;
+ dt_uni_t t = data->payload.list[i]->type;
/* Calculate the new offset */
char new_offset[REAL_STRLEN(offset) + 2];
@@ -480,16 +480,16 @@ void recursive_print(dtree *data, const char *offset)
switch(t) {
case LITERAL:
case NUMERIC:
- recursive_print(data->payload.recursive[i], new_offset);
+ list_print(data->payload.list[i], new_offset);
continue;
case LIST:
- recursive_print(data->payload.recursive[i], new_offset);
+ list_print(data->payload.list[i], new_offset);
continue;
case PAIR:
printf("%s[PAIR] <==> ", new_offset);
- recursive_print(data->payload.recursive[i], new_offset);
+ list_print(data->payload.list[i], new_offset);
continue;
default:
@@ -508,14 +508,14 @@ void recursive_print(dtree *data, const char *offset)
void dtree_print(dtree *data)
{
- recursive_print(data, "");
+ list_print(data, "");
}
dt_err dtree_get(dtree *data, void *(*val))
{
if(data->type == LITERAL) *val = data->payload.literal;
if(data->type == NUMERIC) *val = &data->payload.numeral;
- if(data->type == LIST || data->type == PAIR) *val = (dtree*) data->payload.recursive;
+ if(data->type == LIST || data->type == PAIR) *val = (dtree*) data->payload.list;
return SUCCESS;
}
@@ -533,11 +533,11 @@ dt_err dtree_free(dtree *data)
for(i = 0; i < data->used; i++) {
if(data->copy == SHALLOW) continue;
- err = dtree_free(data->payload.recursive[i]);
+ err = dtree_free(data->payload.list[i]);
if(err) return err;
}
- free(data->payload.recursive);
+ free(data->payload.list);
} else if(data->type == POINTER) {
if(data->copy != SHALLOW && data->payload.pointer)
@@ -559,11 +559,11 @@ dt_err dtree_free_shallow(dtree *data)
int i;
dt_err err;
for(i = 0; i < data->size; i++) {
- err = dtree_free(data->payload.recursive[i]);
+ err = dtree_free(data->payload.list[i]);
if(err) return err;
}
- free(data->payload.recursive);
+ free(data->payload.list);
}
free(data);
@@ -588,14 +588,14 @@ const char *dtree_dtype(dtree *data)
/**
- * Steps down the recursive hirarchy of a dyntree node to
+ * Steps down the list hirarchy of a dyntree node to
* find a sub-child target. Returns 0 if it can be found.
*
* @param data
* @param target
* @return
*/
-int recursive_search(dtree **direct_parent, dtree *data, dtree *target)
+int list_search(dtree **direct_parent, dtree *data, dtree *target)
{
/* Check if data is actually valid */
if(data == NULL) return 1;
@@ -607,7 +607,7 @@ int recursive_search(dtree **direct_parent, dtree *data, dtree *target)
if(data->type == LIST || data->type == PAIR) {
int i;
for(i = 0; i < data->used; i++) {
- res = recursive_search(direct_parent, data->payload.recursive[i], target);
+ res = list_search(direct_parent, data->payload.list[i], target);
if(res == 0) {
/* Save the node that contains our child for later */