aboutsummaryrefslogtreecommitdiff
path: root/include/dtree/dyn_tree.h
blob: e1db1d64bb18fa1e39d111e8dc9d4ce6971364cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * Please use the API to create and destroy objects as only this way
 * memory-safety and memory leaks can be guaranteed.
 *
 * With the API you can easily create structures like the following:
 *
 * root [recursive]
 *    child1 [recursive]
 *       key [literal] - "Username"
 *       value [literal] - "spacekookie"
 *    child2 [recursive]
 *       key [literal] - "Age"
 *       value [numerical] - 23
 *    child3
 *       subchild [recursive]
 *          ...
 *
 * Freeing the root node will free all children
 */

#ifndef _DYNTREE_H_
#define _DYNTREE_H_

#include "dyn_err.h"
#include <memory.h>


/* Also make sure we're _always_ interpreted as a C file */
#ifdef __cplusplus
extern "C" {
#endif

typedef enum {
    UNSET, LITERAL, NUMERAL, RECURSIVE, PAIR
} dt_uni_t;

typedef struct dtree {
    dt_uni_t        type;
    size_t          size, used;
    union {
        char                *literal;
        int                 numeral;
        struct dtree     *(*recursive);
    } payload;
} dtree;

/** Malloc a new dtree object */
dt_err dtree_malloc(dtree *(*data));

dt_err dtree_resettype(dtree *data);

/** Set the data element to a literal and save it's length */
dt_err dtree_addliteral(dtree *data, const char *literal, size_t length);

/** Set the data element to a numeral */
dt_err dtree_addnumeral(dtree *data, int numeral);

/** Add two new elements as a PAIR node under an existing node */
dt_err dtree_addpair(dtree *data, dtree *(*key), dtree *(*value));

/** Add a new data element to the resursive data store */
dt_err dtree_addrecursive(dtree *data, dtree *(*new_data));

dt_err dtree_get(dtree *data, void *(*val));

const char *dtree_dtype(dt_uni_t type);

/** Prints*/
void dtree_print(dtree *data);

/** Will free all memory allocated by this element and it's children */
dt_err dtree_free(dtree *data);

#ifdef __cplusplus
}
#endif
#endif //_DYNTREE_H_