libtreestore

view include/treestore.h @ 3:48d75df3ef04

picking this up again, converted to cmake, and started a text format reader/writer
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 10 Nov 2016 16:19:44 +0200
parents src/treestore.h@a31eae25c0e6
children bb873449cf59
line source
1 #ifndef TREESTORE_H_
2 #define TREESTORE_H_
4 #include <stdarg.h>
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
10 enum ts_value_type { TS_UNKNOWN, TS_NUMBER, TS_VECTOR, TS_ARRAY };
12 /** treestore node attribute value */
13 struct ts_value {
14 enum ts_value_type type;
16 char *str; /**< all values have a string representation */
17 int inum; /**< numeric values TS_NUMBER will have this set */
18 float fnum; /**< numeric values TS_NUMBER will have this set */
20 /** vector values (arrays containing ONLY numbers) will have this set */
21 float *vec; /**< elements of the vector */
22 int vec_size; /**< size of the vector (in elements), same as array_size */
24 /** array values (including vectors) will have this set */
25 struct ts_value *array; /**< elements of the array */
26 int array_size; /**< size of the array (in elements) */
27 };
29 int ts_init_value(struct ts_value *tsv);
30 void ts_destroy_value(struct ts_value *tsv);
32 struct ts_value *ts_alloc_value(void); /**< also calls ts_init_value */
33 void ts_free_value(struct ts_value *tsv); /**< also calls ts_destroy_value */
35 /** perform a deep-copy of a ts_value */
36 int ts_copy_value(struct ts_value *dest, struct ts_value *src);
38 /** ts_set_value will try to parse the string and initialize the value type fields */
39 int ts_set_value(struct ts_value *tsv, const char *str);
41 /** set a ts_value from a list of integers */
42 int ts_set_valueiv(struct ts_value *tsv, int count, ...);
43 int ts_set_valueiv_va(struct ts_value *tsv, int count, va_list ap);
44 int ts_set_valuei(struct ts_value *tsv, int inum); /**< equiv: ts_set_valueiv(val, 1, inum) */
46 /** set a ts_value from a list of floats */
47 int ts_set_valuefv(struct ts_value *tsv, int count, ...);
48 int ts_set_valuefv_va(struct ts_value *tsv, int count, va_list ap);
49 int ts_set_valuef(struct ts_value *tsv, int fnum); /**< equiv: ts_set_valuefv(val, 1, fnum) */
51 /** set a ts_value from a list of ts_value pointers. they are deep-copied as per ts_copy_value */
52 int ts_set_valuev(struct ts_value *tsv, int count, ...);
53 int ts_set_valuev_va(struct ts_value *tsv, int count, va_list ap);
56 /** treestore node attribute */
57 struct ts_attr {
58 char *name;
59 struct ts_value val;
61 struct ts_attr *next;
62 };
64 int ts_init_attr(struct ts_attr *attr);
65 void ts_destroy_attr(struct ts_attr *attr);
67 struct ts_attr *ts_alloc_attr(void); /**< also calls ts_init_attr */
68 void ts_free_attr(struct ts_attr *attr); /**< also calls ts_destroy_attr */
70 /** perform a deep-copy of a ts_attr */
71 int ts_copy_attr(struct ts_attr *dest, struct ts_attr *src);
73 int ts_set_attr_name(struct ts_attr *attr, const char *name);
77 /** treestore node */
78 struct ts_node {
79 char *name;
81 int attr_count;
82 struct ts_attr *attr_list, *attr_tail;
84 int child_count;
85 struct ts_node *child_list, *child_tail;
86 struct ts_node *parent;
88 struct ts_node *next; /* next sibling */
89 };
91 int ts_init_node(struct ts_node *node);
92 void ts_destroy_node(struct ts_node *node);
94 struct ts_node *ts_alloc_node(void); /**< also calls ts_init_node */
95 void ts_free_node(struct ts_node *n); /**< also calls ts_destroy_node */
97 /** recursively destroy all the nodes of the tree */
98 void ts_free_tree(struct ts_node *tree);
100 void ts_add_attr(struct ts_node *node, struct ts_attr *attr);
101 struct ts_attr *ts_get_attr(struct ts_node *node, const char *name);
103 void ts_add_child(struct ts_node *node, struct ts_node *child);
104 int ts_remove_child(struct ts_node *node, struct ts_node *child);
105 struct ts_node *ts_get_child(struct ts_node *node, const char *name);
107 struct ts_node *ts_load(const char *fname);
108 int ts_save(struct ts_node *tree, const char *fname);
110 #ifdef __cplusplus
111 }
112 #endif
114 #endif /* TREESTORE_H_ */