aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatharina Fey <kookie@spacekookie.de>2016-08-23 00:28:06 +0200
committerKatharina Fey <kookie@spacekookie.de>2016-08-23 00:28:06 +0200
commitd57c4094e24554389f46bdf34b29c2d12380ad24 (patch)
treecf933364999a6a38e3806531178bb224cbc1a50e
parent1fdedc5f41a36b774a7a691edb5c8722f2529f82 (diff)
Adding some helper functions
-rw-r--r--include/dtree/dyn_tree.h79
1 files changed, 79 insertions, 0 deletions
diff --git a/include/dtree/dyn_tree.h b/include/dtree/dyn_tree.h
index 4155af4..42aa378 100644
--- a/include/dtree/dyn_tree.h
+++ b/include/dtree/dyn_tree.h
@@ -297,6 +297,85 @@ 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, REAL_STRLEN(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_reclist(dtree **root, unsigned int count)
+{
+ dtree **nodes = malloc(sizeof(dtree**) * count);
+
+ dtree_malloc(root);
+
+ int i;
+ for(i = 0; i < count; i++)
+ dtree_addrecursive(*root, &nodes[i]);
+
+ return nodes;
+}
+
#ifdef __cplusplus
}
#endif