options/config
[demo_prior] / libs / treestore / src / treestore.h
1 #ifndef TREESTORE_H_
2 #define TREESTORE_H_
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdarg.h>
7
8 #ifdef __cplusplus
9 #define TS_DEFVAL(x) =(x)
10 extern "C" {
11 #else
12 #define TS_DEFVAL(x)
13 #endif
14
15 /** set of user-supplied I/O functions, for ts_load_io/ts_save_io */
16 struct ts_io {
17         void *data;
18
19         long (*read)(void *buf, size_t bytes, void *uptr);
20         long (*write)(const void *buf, size_t bytes, void *uptr);
21 };
22
23 enum ts_value_type { TS_STRING, TS_NUMBER, TS_VECTOR, TS_ARRAY };
24
25 /** treestore node attribute value */
26 struct ts_value {
27         enum ts_value_type type;
28
29         char *str;              /**< string values will have this set */
30         int inum;               /**< numeric values will have this set */
31         float fnum;             /**< numeric values will have this set */
32
33         /** vector values (arrays containing ONLY numbers) will have this set */
34         float *vec;             /**< elements of the vector */
35         int vec_size;   /**< size of the vector (in elements), same as array_size */
36
37         /** array values (including vectors) will have this set */
38         struct ts_value *array; /**< elements of the array */
39         int array_size;                 /**< size of the array (in elements) */
40 };
41
42 int ts_init_value(struct ts_value *tsv);
43 void ts_destroy_value(struct ts_value *tsv);
44
45 struct ts_value *ts_alloc_value(void);          /**< also calls ts_init_value */
46 void ts_free_value(struct ts_value *tsv);       /**< also calls ts_destroy_value */
47
48 /** perform a deep-copy of a ts_value */
49 int ts_copy_value(struct ts_value *dest, struct ts_value *src);
50
51 /** set a ts_value as a string */
52 int ts_set_value_str(struct ts_value *tsv, const char *str);
53
54 /** set a ts_value from a list of integers */
55 int ts_set_valuei_arr(struct ts_value *tsv, int count, const int *arr);
56 int ts_set_valueiv(struct ts_value *tsv, int count, ...);
57 int ts_set_valueiv_va(struct ts_value *tsv, int count, va_list ap);
58 int ts_set_valuei(struct ts_value *tsv, int inum);      /**< equiv: ts_set_valueiv(val, 1, inum) */
59
60 /** set a ts_value from a list of floats */
61 int ts_set_valuef_arr(struct ts_value *tsv, int count, const float *arr);
62 int ts_set_valuefv(struct ts_value *tsv, int count, ...);
63 int ts_set_valuefv_va(struct ts_value *tsv, int count, va_list ap);
64 int ts_set_valuef(struct ts_value *tsv, float fnum);    /**< equiv: ts_set_valuefv(val, 1, fnum) */
65
66 /** set a ts_value from a list of ts_value pointers. they are deep-copied as per ts_copy_value */
67 int ts_set_value_arr(struct ts_value *tsv, int count, const struct ts_value *arr);
68 int ts_set_valuev(struct ts_value *tsv, int count, ...);
69 int ts_set_valuev_va(struct ts_value *tsv, int count, va_list ap);
70
71
72 /** treestore node attribute */
73 struct ts_attr {
74         char *name;
75         struct ts_value val;
76
77         struct ts_attr *next;
78         struct ts_node *node;
79 };
80
81 int ts_init_attr(struct ts_attr *attr);
82 void ts_destroy_attr(struct ts_attr *attr);
83
84 struct ts_attr *ts_alloc_attr(void);            /**< also calls ts_init_attr */
85 void ts_free_attr(struct ts_attr *attr);        /**< also calls ts_destroy_attr */
86
87 /** perform a deep-copy of a ts_attr */
88 int ts_copy_attr(struct ts_attr *dest, struct ts_attr *src);
89
90 int ts_set_attr_name(struct ts_attr *attr, const char *name);
91
92
93
94 /** treestore node */
95 struct ts_node {
96         char *name;
97
98         int attr_count;
99         struct ts_attr *attr_list, *attr_tail;
100
101         int child_count;
102         struct ts_node *child_list, *child_tail;
103         struct ts_node *parent;
104
105         struct ts_node *next;   /* next sibling */
106 };
107
108 int ts_init_node(struct ts_node *node);
109 void ts_destroy_node(struct ts_node *node);
110
111 struct ts_node *ts_alloc_node(void);    /**< also calls ts_init_node */
112 void ts_free_node(struct ts_node *n);   /**< also calls ts_destroy_node */
113
114 /** recursively destroy all the nodes of the tree */
115 void ts_free_tree(struct ts_node *tree);
116
117 void ts_add_attr(struct ts_node *node, struct ts_attr *attr);
118 int ts_remove_attr(struct ts_node *node, struct ts_attr *attr);
119 struct ts_attr *ts_get_attr(struct ts_node *node, const char *name);
120
121 const char *ts_get_attr_str(struct ts_node *node, const char *aname,
122                 const char *def_val TS_DEFVAL(0));
123 float ts_get_attr_num(struct ts_node *node, const char *aname,
124                 float def_val TS_DEFVAL(0.0f));
125 int ts_get_attr_int(struct ts_node *node, const char *aname,
126                 int def_val TS_DEFVAL(0.0f));
127 float *ts_get_attr_vec(struct ts_node *node, const char *aname,
128                 float *def_val TS_DEFVAL(0));
129 struct ts_value *ts_get_attr_array(struct ts_node *node, const char *aname,
130                 struct ts_value *def_val TS_DEFVAL(0));
131
132
133 void ts_add_child(struct ts_node *node, struct ts_node *child);
134 int ts_remove_child(struct ts_node *node, struct ts_node *child);
135 struct ts_node *ts_get_child(struct ts_node *node, const char *name);
136
137 /* load/save by opening the specified file */
138 struct ts_node *ts_load(const char *fname);
139 int ts_save(struct ts_node *tree, const char *fname);
140
141 /* load/save using the supplied FILE pointer */
142 struct ts_node *ts_load_file(FILE *fp);
143 int ts_save_file(struct ts_node *tree, FILE *fp);
144
145 /* load/save using custom I/O functions */
146 struct ts_node *ts_load_io(struct ts_io *io);
147 int ts_save_io(struct ts_node *tree, struct ts_io *io);
148
149
150 struct ts_attr *ts_lookup(struct ts_node *root, const char *path);
151 const char *ts_lookup_str(struct ts_node *root, const char *path,
152                 const char *def_val TS_DEFVAL(0));
153 float ts_lookup_num(struct ts_node *root, const char *path,
154                 float def_val TS_DEFVAL(0.0f));
155 int ts_lookup_int(struct ts_node *root, const char *path,
156                 int def_val TS_DEFVAL(0));
157 float *ts_lookup_vec(struct ts_node *root, const char *path,
158                 float *def_val TS_DEFVAL(0));
159 struct ts_value *ts_lookup_array(struct ts_node *root, const char *path,
160                 struct ts_value *def_val TS_DEFVAL(0));
161
162
163 #ifdef __cplusplus
164 }
165 #endif
166
167 #endif  /* TREESTORE_H_ */