aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatharina Fey <kookie@spacekookie.de>2016-08-19 16:21:21 +0200
committerKatharina Fey <kookie@spacekookie.de>2016-08-19 16:21:21 +0200
commitf71bbbe6d36f5a473c60566adc5c54e3985d6225 (patch)
treeee063602b43bd09c60abe72a51ff3b382dbedfd1
parent3c9a1ad3d80e8c3e89a18d779702b91803ca0982 (diff)
parent2317004e19eadae8742645dc71ca18957c8f43c1 (diff)
Merge branch 'master' of https://github.com/reepass/libdyntree
-rw-r--r--README162
-rw-r--r--include/dtree/dyn_tree.h125
-rw-r--r--lib/dyn_tree.c55
3 files changed, 255 insertions, 87 deletions
diff --git a/README b/README
index 4c30bef..b21dbd1 100644
--- a/README
+++ b/README
@@ -19,82 +19,85 @@ This will create a `.a` file. If you require a shared object, you can change the
Using libdyntree is straighforward with a comprehensive API. Everything resolves around `dtree` objects and providing fields to API functions. Every function is documented as outlined in the header files.
-
-
-Looking at this piece of code. What do you think it does?
+Generally, memory is managed for you by libdyntree. So allocating a new node is as easy as passing a reference pointer to a malloc function:
```C
- void main(void) {
- rdb_data *root;
- err = rdb_data_malloc(&root);
+dtree *data;
+dtree_malloc(&data);
+```
- rdb_data_addnumeral(root, 1337);
- }
+The above code will initialise an empty dtree node that can then be written into. While it's possible to edit the values in the struct yourself it is not recomended.
-```
+Instead you should use the utility functions provided to you by libdyntree that will make sure that you don't accidentally leak any memory or try to write something invalid into your structures.
+
+**Possible Nodes**
+ - Unset
+ - Literal
+ - Numerical
+ - Recursive
+ - Pair
+ - Pointer
-It creates a new rdb_data object which gets allocated and initialised by the `rdb_data_malloc` function. Then you pass a pointer to the allocated object and afterwards assigns the number "1337" to it.
+```C
-Note how now the type of the rdb_data node is now `NUMERICAL`. We can no longer store any other type of data in it unless we call `rdb_data_resettype(root)`.
+ dtree_addliteral(data, "My String", REAL_STRLEN("My String"));
-The reedb data storage API is meant to give you an easy recursive and flexible storage system for whatever type of data you might require in C. It was originally written to allow for write and read operations on the C API of libreedb. But I feel that it has uses outside of this project as well. So please use it if you think it is useful.
+ dtree_addnumeral(data, 42);
-### Node Types
+ dtree_addpair(data, &key, &value);
-In total there are five different node types in rdb_data.
+ dtree_addrecursive(data, &sub_d);
- - UNSET
- - LITERAL
- - NUMERICAL
- - RECURSIVE
- - PAIR
+ dtree_addpointer(data, my_ptr);
+```
-The name `recursive` might be a bit misleading. What it means is that the node contains a list of data - potentially a list of other nodes - and can thus be recursive.
-A pair is simply a mapping of a key to a value. It takes up one node. So to implement a traditional key-value store you take a RECURSIVE node and you store PAIR nodes inside it.
+For more detail on how to use each individual function, please consult the libdyntree header file. Following will be a more complex example of how to represent.
-The API is very straightforward.
```C
- rdb_data *root;
- err = rdb_data_malloc(&root);
+ dtree *root;
+ err = dtree_malloc(&root);
- rdb_data *pair1, *pair2;
+ dtree *pair1, *pair2;
/* Add the two nodes to the recursive list */
- rdb_data_mallocrecursive(root, &pair1);
- rdb_data_mallocrecursive(root, &pair2);
+ dtree_addrecursive(root, &pair1);
+ dtree_addrecursive(root, &pair2);
/* This code makes 4 new nodes and assigns them as pair nodes */
- rdb_data *pair1_key, *pair1_val, *pair2_key, *pair2_val
- rdb_data_mallocpair(pair1, &pair1_key, &pair1_val);
- rdb_data_mallocpair(pair2, &pair2_key, &pair2_val);
+ dtree *pair1_key, *pair1_val, *pair2_key, *pair2_val
+ dtree_addpair(pair1, &pair1_key, &pair1_val);
+ dtree_addpair(pair2, &pair2_key, &pair2_val);
```
-At this point the structure of our rdb_data set would look somewhat like this:
+At this point the structure of our dtree set would look somewhat like this:
```
- [root]
+
+[root]
[pair1] => [ [pair1_key] => [pair1_val] ]
[pair2] => [ [pair2_key] => [pair2_val] ]
+
```
-### A more complex example
+### An even more complicated Example
From here on out you can then normally put values into the key and value items. You could even have a PAIR or RECURSIVE element as a key or value! The options are limitless.
-Also...don't be afraid of reusing your pointers: you don't need to keep them. rdb_data allocates the fields and keeps a reference to each field. This means that a single call can free an entire nested structure. And you don't need to keep coming up with variable names.
+Also...don't be afraid of reusing your pointers: you don't need to keep them. dtree allocates the fields and keeps a reference to each field. This means that a single call can free an entire nested structure. And you don't need to keep coming up with variable names.
You just need one pointer somewhere as a buffer to work on.
```C
- rdb_data *root;
- err = rdb_data_malloc(&root);
- rdb_data *pair, *key, *val;
+ dtree *root;
+ err = dtree_malloc(&root);
+
+ dtree *pair, *key, *val;
- rdb_data_mallocrecursive(root, &pair);
- rdb_data_mallocpair(pair, &key, &val);
+ dtree_addrecursive(root, &pair);
+ dtree_addpair(pair, &key, &val);
// ... Assign data to key and val
@@ -102,69 +105,86 @@ You just need one pointer somewhere as a buffer to work on.
pair = key = val = NULL;
/* Start again */
- rdb_data_mallocrecursive(root, &pair);
- rdb_data_mallocpair(pair, &key, &val);
+ dtree_addrecursive(root, &pair);
+ dtree_addpair(pair, &key, &val);
// ... Assign data to key and val
```
-I hope you like this library and can do awesome stuff with rdb_data. If you find any bugs, please report them on the libreedb github repository. This project might become it's own repository at some point but until then, we shall see :)
+I hope you like this library and can do awesome stuff with dtree. If you find any bugs, please report them on the libreedb github repository. This project might become it's own repository at some point but until then, we shall see :)
Below there is a slightly more complicated example, including several nested types and printing the structure.
```
-#include <reedb/reedb.h>
-#include <reedb/data.h>
-
+#include <dtree/dyn_tree.h>
#include <stdio.h>
int main(void)
{
rdb_err_t err;
- rdb_data *root;
- err = rdb_data_malloc(&root);
+ dtree *root;
+ err = dtree_malloc(&root);
printf("Malloc returned: %s\n", rdb_error_getmsg(&err));
- rdb_data *lit, *num, *pair1, *pair2, *rec2;
- rdb_data_mallocrecursive(root, &lit);
- rdb_data_mallocrecursive(root, &num);
- rdb_data_mallocrecursive(root, &rec2);
- rdb_data_mallocrecursive(root, &pair1);
- rdb_data_mallocrecursive(root, &pair2);
+ dtree *lit, *num, *pair1, *pair2, *rec2;
+ dtree_addrecursive(root, &lit);
+ dtree_addrecursive(root, &num);
+ dtree_addrecursive(root, &rec2);
+ dtree_addrecursive(root, &pair1);
+ dtree_addrecursive(root, &pair2);
- rdb_data_addliteral(lit, "This is a string", REAL_STRLEN("This is a string"));
- rdb_data_addnumeral(num, 1337);
+ dtree_addliteral(lit, "This is a string", REAL_STRLEN("This is a string"));
+ dtree_addnumeral(num, 1337);
- rdb_data *rec_d1, *rec_d2;
- rdb_data_mallocrecursive(rec2, &rec_d1);
- rdb_data_addliteral(rec_d1, "Reedb is awesome!", REAL_STRLEN("Reedb is awesome!"));
+ dtree *rec_d1, *rec_d2;
+ dtree_addrecursive(rec2, &rec_d1);
+ dtree_addliteral(rec_d1, "Reedb is awesome!", REAL_STRLEN("Reedb is awesome!"));
- rdb_data_mallocrecursive(rec2, &rec_d2);
- rdb_data_addnumeral(rec_d2, 666);
+ dtree_addrecursive(rec2, &rec_d2);
+ dtree_addnumeral(rec_d2, 666);
- rdb_data *pair1_key, *pair1_val;
- rdb_data_mallocpair(pair1, &pair1_key, &pair1_val);
+ dtree *pair1_key, *pair1_val;
+ dtree_addpair(pair1, &pair1_key, &pair1_val);
- rdb_data_addliteral(pair1_key, "Username", REAL_STRLEN("Username"));
- rdb_data_addliteral(pair1_val, "spacekookie", REAL_STRLEN("spacekookie"));
+ dtree_addliteral(pair1_key, "Username", REAL_STRLEN("Username"));
+ dtree_addliteral(pair1_val, "spacekookie", REAL_STRLEN("spacekookie"));
- rdb_data *pair2_key, *pair2_val;
- rdb_data_mallocpair(pair2, &pair2_key, &pair2_val);
+ dtree *pair2_key, *pair2_val;
+ dtree_addpair(pair2, &pair2_key, &pair2_val);
- rdb_data_addliteral(pair2_key, "Website", REAL_STRLEN("Website"));
- rdb_data_addliteral(pair2_val, "www.spacekookie.de", REAL_STRLEN("www.spacekookie.de"));
+ dtree_addliteral(pair2_key, "Website", REAL_STRLEN("Website"));
+ dtree_addliteral(pair2_val, "www.spacekookie.de", REAL_STRLEN("www.spacekookie.de"));
/* Print our structure */
- rdb_data_print(root);
+ dtree_print(root);
/* Free everything */
- rdb_data_free(root);
+ dtree_free(root);
return 0;
}
```
+
+The above program would have the following output:
+
+```
+[RECURSIVE]
+ ['This is a string']
+ [1337]
+ [RECURSIVE]
+ ['Reedb is awesome!']
+ [666]
+ [PAIR] <==> ['Username'] => ['spacekookie']
+ [PAIR] <==> ['Website'] => ['www.spacekookie.de']
+
+```
+
## License
-The library is licensed under LGPL-3 (the same as it's origin project [libreedb](https://github.com/reepass/libreedb)). Have fun <3s
+This program is free software; you can redistribute it and/or modify it under the terms of the Lesser GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
+
+This library is part of the **Reepass Project**, namely [libreedb](https://github.com/reepass/libreedb). It was exported from the trunk to be useful for other people.
+
+I hope you enjoy :heart_decoration: \ No newline at end of file
diff --git a/include/dtree/dyn_tree.h b/include/dtree/dyn_tree.h
index e1db1d6..cfd7239 100644
--- a/include/dtree/dyn_tree.h
+++ b/include/dtree/dyn_tree.h
@@ -30,8 +30,9 @@
extern "C" {
#endif
+/* Type that determines what data is stored inside a tree-node */
typedef enum {
- UNSET, LITERAL, NUMERAL, RECURSIVE, PAIR
+ UNSET, LITERAL, NUMERAL, RECURSIVE, PAIR, POINTER
} dt_uni_t;
typedef struct dtree {
@@ -40,35 +41,139 @@ typedef struct dtree {
union {
char *literal;
int numeral;
- struct dtree *(*recursive);
+ struct dtree *(*recursive);
+ void *pointer;
} payload;
} dtree;
-/** Malloc a new dtree object */
+
+/**
+ * Malloc a new dtree object
+ *
+ * @param data Reference pointer to dtree element
+ * @return
+ */
dt_err dtree_malloc(dtree *(*data));
+
+/**
+ * Reset the type of a node and free child data
+ *
+ * @param data
+ * @return
+ */
dt_err dtree_resettype(dtree *data);
-/** Set the data element to a literal and save it's length */
+
+/**
+ * Set the data element to a literal and save it's length
+ *
+ * @param data Reference to a dtree object
+ * @param literal String to store
+ * @param length TRUE string length to use.
+ * @return
+ */
dt_err dtree_addliteral(dtree *data, const char *literal, size_t length);
-/** Set the data element to a numeral */
+
+/**
+ * Set the data element to a numeral
+ *
+ * @param data Reference to a dtree object
+ * @param numeral Number to store
+ * @return
+ */
dt_err dtree_addnumeral(dtree *data, int numeral);
-/** Add two new elements as a PAIR node under an existing node */
+
+/**
+ * Add two new elements as a PAIR node under an existing node
+ *
+ * @param data dtree node to become the sub-root
+ * @param key Reference pointer to the key node
+ * @param value Reference pointer to the value node
+ * @return
+ */
dt_err dtree_addpair(dtree *data, dtree *(*key), dtree *(*value));
-/** Add a new data element to the resursive data store */
+
+/**
+ * Add a new data element to the resursive data store
+ *
+ * @param data Root reference
+ * @param new_data Reference pointer to a new dtree node
+ * @return
+ */
dt_err dtree_addrecursive(dtree *data, dtree *(*new_data));
+
+/**
+ * This function enables you to store your own structures in a node. It however
+ * also requires you to do some of your own memory management.
+ *
+ * WARNING: Can leak memory if pointer is previously set!
+ *
+ * To make sure that this function CAN NOT leak memory you should run
+ * "dtree_resettype" on the root element to remove the pointer.
+ *
+ * Also make sure that no other part of your application will use the
+ * pointer at a later date!
+ *
+ * @param data Root reference
+ * @param ptr A pointer to store in this node
+ * @return
+ */
+dt_err dtree_addpointer(dtree *data, void *ptr);
+
+
+/**
+ * A retrieve function to get data back from a node that doesn't require
+ * you to manually access parts of the struct.
+ *
+ * Needs to be provided with a reference to a pointer that can then be
+ * written to. You can make the reference type specific if you know
+ * what kind of data you're expecting or leave it as a void* to let
+ * libdyntree do the casting for you.
+ *
+ * @param data Node reference to access
+ * @param val Reference pointer to write into
+ * @return
+ */
dt_err dtree_get(dtree *data, void *(*val));
-const char *dtree_dtype(dt_uni_t type);
-/** Prints*/
+/**
+ * Return the type of a node as plain text
+ *
+ * @param data
+ * @return
+ */
+const char *dtree_dtype(dtree *data);
+
+
+/**
+ * Prints the data dtree object and all of its children
+ *
+ * @param data
+ */
void dtree_print(dtree *data);
-/** Will free all memory allocated by this element and it's children */
+/**
+ * Will free the data reference and all of it's children. It will however NOT
+ * touch pointers to objects that weren't allocated by libdyntree!
+ *
+ * @param data
+ * @return
+ */
+dt_err dtree_free_shallow(dtree *data);
+
+/**
+ * Like #{dtree_free_shallow} but will also remove structs that
+ * weren't allocated by libdyntree
+ *
+ * @param data
+ * @return
+ */
dt_err dtree_free(dtree *data);
#ifdef __cplusplus
diff --git a/lib/dyn_tree.c b/lib/dyn_tree.c
index 4665332..095d379 100644
--- a/lib/dyn_tree.c
+++ b/lib/dyn_tree.c
@@ -74,6 +74,21 @@ dt_err dtree_addliteral(dtree *data, const char *literal, size_t length)
return SUCCESS;
}
+
+dt_err dtree_addpointer(dtree *data, void *ptr)
+{
+ if(data->type != UNSET)
+ if(data->type != POINTER) return INVALID_PAYLOAD;
+
+ data->payload.pointer = ptr;
+ data->type = POINTER;
+ data->size = sizeof(ptr);
+ data->used = sizeof(*ptr);
+
+ return SUCCESS;
+}
+
+
dt_err dtree_addnumeral(dtree *data, int numeral)
{
/* Make sure we are a literal or unset data object */
@@ -130,6 +145,7 @@ dt_err dtree_addrecursive(dtree *data, dtree *(*new_data))
return SUCCESS;
}
+
dt_err dtree_addpair(dtree *data, dtree *(*key), dtree *(*value))
{
/* Make sure we are a literal or unset data object */
@@ -265,6 +281,7 @@ dt_err dtree_free(dtree *data)
if(data->type == LITERAL) {
if(data->payload.literal) free(data->payload.literal);
+
} else if(data->type == RECURSIVE || data->type == PAIR) {
int i;
dt_err err;
@@ -274,19 +291,45 @@ dt_err dtree_free(dtree *data)
}
free(data->payload.recursive);
+
+ } else if(data->type == POINTER) {
+ if(data->payload.pointer) free(data->payload.pointer);
}
free(data);
return SUCCESS;
}
-const char *dtree_dtype(dt_uni_t type)
+dt_err dtree_free_shallow(dtree *data)
{
- switch(type) {
- case LITERAL: return "Literal";
- case NUMERAL: return "Numeral";
- case RECURSIVE: return "Recursive";
- default: return "Unknown";
+ if(data == NULL) return SUCCESS;
+
+ if(data->type == LITERAL) {
+ if(data->payload.literal) free(data->payload.literal);
+ } else if(data->type == RECURSIVE || data->type == PAIR) {
+ int i;
+ dt_err err;
+ for(i = 0; i < data->size; i++) {
+ err = dtree_free(data->payload.recursive[i]);
+ if(err) return err;
+ }
+
+ free(data->payload.recursive);
+ }
+
+ free(data);
+ return SUCCESS;
+}
+
+const char *dtree_dtype(dtree *data)
+{
+ switch(data->type) {
+ case LITERAL: return "Literal";
+ case NUMERAL: return "Numeral";
+ case RECURSIVE: return "Recursive";
+ case PAIR: return "Pair";
+ case POINTER: return "Pointer";
+ default: return "Unknown";
}
}