libtreestore

view include/treestore.h @ 5:f3ade599cfbb

ts_free*/ts_destroy* functions shouldn't bork when passed a null pointer
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 13 Nov 2016 20:40:07 +0200
parents 48d75df3ef04
children
line source
1 #ifndef TREESTORE_H_
2 #define TREESTORE_H_
4 #include <stdarg.h>
6 #ifdef __cplusplus
7 #define TS_DEFVAL(x) =(x)
8 extern "C" {
9 #else
10 #define TS_DEFVAL(x)
11 #endif
13 enum ts_value_type { TS_STRING, TS_NUMBER, TS_VECTOR, TS_ARRAY };
15 /** treestore node attribute value */
16 struct ts_value {
17 enum ts_value_type type;
19 char *str; /**< string values will have this set */
20 int inum; /**< numeric values will have this set */
21 float fnum; /**< numeric values will have this set */
23 /** vector values (arrays containing ONLY numbers) will have this set */
24 float *vec; /**< elements of the vector */
25 int vec_size; /**< size of the vector (in elements), same as array_size */
27 /** array values (including vectors) will have this set */
28 struct ts_value *array; /**< elements of the array */
29 int array_size; /**< size of the array (in elements) */
30 };
32 int ts_init_value(struct ts_value *tsv);
33 void ts_destroy_value(struct ts_value *tsv);
35 struct ts_value *ts_alloc_value(void); /**< also calls ts_init_value */
36 void ts_free_value(struct ts_value *tsv); /**< also calls ts_destroy_value */
38 /** perform a deep-copy of a ts_value */
39 int ts_copy_value(struct ts_value *dest, struct ts_value *src);
41 /** ts_set_value will try to parse the string and initialize the value type fields */
42 int ts_set_value(struct ts_value *tsv, const char *str);
44 /** set a ts_value from a list of integers */
45 int ts_set_valueiv(struct ts_value *tsv, int count, ...);
46 int ts_set_valueiv_va(struct ts_value *tsv, int count, va_list ap);
47 int ts_set_valuei(struct ts_value *tsv, int inum); /**< equiv: ts_set_valueiv(val, 1, inum) */
49 /** set a ts_value from a list of floats */
50 int ts_set_valuefv(struct ts_value *tsv, int count, ...);
51 int ts_set_valuefv_va(struct ts_value *tsv, int count, va_list ap);
52 int ts_set_valuef(struct ts_value *tsv, float fnum); /**< equiv: ts_set_valuefv(val, 1, fnum) */
54 /** set a ts_value from a list of ts_value pointers. they are deep-copied as per ts_copy_value */
55 int ts_set_valuev(struct ts_value *tsv, int count, ...);
56 int ts_set_valuev_va(struct ts_value *tsv, int count, va_list ap);
59 /** treestore node attribute */
60 struct ts_attr {
61 char *name;
62 struct ts_value val;
64 struct ts_attr *next;
65 };
67 int ts_init_attr(struct ts_attr *attr);
68 void ts_destroy_attr(struct ts_attr *attr);
70 struct ts_attr *ts_alloc_attr(void); /**< also calls ts_init_attr */
71 void ts_free_attr(struct ts_attr *attr); /**< also calls ts_destroy_attr */
73 /** perform a deep-copy of a ts_attr */
74 int ts_copy_attr(struct ts_attr *dest, struct ts_attr *src);
76 int ts_set_attr_name(struct ts_attr *attr, const char *name);
80 /** treestore node */
81 struct ts_node {
82 char *name;
84 int attr_count;
85 struct ts_attr *attr_list, *attr_tail;
87 int child_count;
88 struct ts_node *child_list, *child_tail;
89 struct ts_node *parent;
91 struct ts_node *next; /* next sibling */
92 };
94 int ts_init_node(struct ts_node *node);
95 void ts_destroy_node(struct ts_node *node);
97 struct ts_node *ts_alloc_node(void); /**< also calls ts_init_node */
98 void ts_free_node(struct ts_node *n); /**< also calls ts_destroy_node */
100 /** recursively destroy all the nodes of the tree */
101 void ts_free_tree(struct ts_node *tree);
103 void ts_add_attr(struct ts_node *node, struct ts_attr *attr);
104 struct ts_attr *ts_get_attr(struct ts_node *node, const char *name);
106 const char *ts_get_attr_str(struct ts_node *node, const char *aname,
107 const char *def_val TS_DEFVAL(0));
108 float ts_get_attr_num(struct ts_node *node, const char *aname,
109 float def_val TS_DEFVAL(0.0f));
110 int ts_get_attr_int(struct ts_node *node, const char *aname,
111 int def_val TS_DEFVAL(0.0f));
112 float *ts_get_attr_vec(struct ts_node *node, const char *aname,
113 float *def_val TS_DEFVAL(0));
114 struct ts_value *ts_get_attr_array(struct ts_node *node, const char *aname,
115 struct ts_value *def_val TS_DEFVAL(0));
118 void ts_add_child(struct ts_node *node, struct ts_node *child);
119 int ts_remove_child(struct ts_node *node, struct ts_node *child);
120 struct ts_node *ts_get_child(struct ts_node *node, const char *name);
122 struct ts_node *ts_load(const char *fname);
123 int ts_save(struct ts_node *tree, const char *fname);
126 #ifdef __cplusplus
127 }
128 #endif
130 #endif /* TREESTORE_H_ */