9 #define TS_DEFVAL(x) =(x)
15 /** set of user-supplied I/O functions, for ts_load_io/ts_save_io */
19 long (*read)(void *buf, size_t bytes, void *uptr);
20 long (*write)(const void *buf, size_t bytes, void *uptr);
23 enum ts_value_type { TS_STRING, TS_NUMBER, TS_VECTOR, TS_ARRAY };
25 /** treestore node attribute value */
27 enum ts_value_type type;
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 */
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 */
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) */
42 int ts_init_value(struct ts_value *tsv);
43 void ts_destroy_value(struct ts_value *tsv);
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 */
48 /** perform a deep-copy of a ts_value */
49 int ts_copy_value(struct ts_value *dest, struct ts_value *src);
51 /** set a ts_value as a string */
52 int ts_set_value_str(struct ts_value *tsv, const char *str);
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) */
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) */
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);
72 /** treestore node attribute */
80 int ts_init_attr(struct ts_attr *attr);
81 void ts_destroy_attr(struct ts_attr *attr);
83 struct ts_attr *ts_alloc_attr(void); /**< also calls ts_init_attr */
84 void ts_free_attr(struct ts_attr *attr); /**< also calls ts_destroy_attr */
86 /** perform a deep-copy of a ts_attr */
87 int ts_copy_attr(struct ts_attr *dest, struct ts_attr *src);
89 int ts_set_attr_name(struct ts_attr *attr, const char *name);
98 struct ts_attr *attr_list, *attr_tail;
101 struct ts_node *child_list, *child_tail;
102 struct ts_node *parent;
104 struct ts_node *next; /* next sibling */
107 int ts_init_node(struct ts_node *node);
108 void ts_destroy_node(struct ts_node *node);
110 struct ts_node *ts_alloc_node(void); /**< also calls ts_init_node */
111 void ts_free_node(struct ts_node *n); /**< also calls ts_destroy_node */
113 /** recursively destroy all the nodes of the tree */
114 void ts_free_tree(struct ts_node *tree);
116 void ts_add_attr(struct ts_node *node, struct ts_attr *attr);
117 struct ts_attr *ts_get_attr(struct ts_node *node, const char *name);
119 const char *ts_get_attr_str(struct ts_node *node, const char *aname,
120 const char *def_val TS_DEFVAL(0));
121 float ts_get_attr_num(struct ts_node *node, const char *aname,
122 float def_val TS_DEFVAL(0.0f));
123 int ts_get_attr_int(struct ts_node *node, const char *aname,
124 int def_val TS_DEFVAL(0.0f));
125 float *ts_get_attr_vec(struct ts_node *node, const char *aname,
126 float *def_val TS_DEFVAL(0));
127 struct ts_value *ts_get_attr_array(struct ts_node *node, const char *aname,
128 struct ts_value *def_val TS_DEFVAL(0));
131 void ts_add_child(struct ts_node *node, struct ts_node *child);
132 int ts_remove_child(struct ts_node *node, struct ts_node *child);
133 struct ts_node *ts_get_child(struct ts_node *node, const char *name);
135 /* load/save by opening the specified file */
136 struct ts_node *ts_load(const char *fname);
137 int ts_save(struct ts_node *tree, const char *fname);
139 /* load/save using the supplied FILE pointer */
140 struct ts_node *ts_load_file(FILE *fp);
141 int ts_save_file(struct ts_node *tree, FILE *fp);
143 /* load/save using custom I/O functions */
144 struct ts_node *ts_load_io(struct ts_io *io);
145 int ts_save_io(struct ts_node *tree, struct ts_io *io);
148 struct ts_attr *ts_lookup(struct ts_node *root, const char *path);
149 const char *ts_lookup_str(struct ts_node *root, const char *path,
150 const char *def_val TS_DEFVAL(0));
151 float ts_lookup_num(struct ts_node *root, const char *path,
152 float def_val TS_DEFVAL(0.0f));
153 int ts_lookup_int(struct ts_node *root, const char *path,
154 int def_val TS_DEFVAL(0));
155 float *ts_lookup_vec(struct ts_node *root, const char *path,
156 float *def_val TS_DEFVAL(0));
157 struct ts_value *ts_lookup_array(struct ts_node *root, const char *path,
158 struct ts_value *def_val TS_DEFVAL(0));
165 #endif /* TREESTORE_H_ */