libtreestore
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/include/treestore.h Thu Nov 10 16:19:44 2016 +0200 1.3 @@ -0,0 +1,114 @@ 1.4 +#ifndef TREESTORE_H_ 1.5 +#define TREESTORE_H_ 1.6 + 1.7 +#include <stdarg.h> 1.8 + 1.9 +#ifdef __cplusplus 1.10 +extern "C" { 1.11 +#endif 1.12 + 1.13 +enum ts_value_type { TS_UNKNOWN, TS_NUMBER, TS_VECTOR, TS_ARRAY }; 1.14 + 1.15 +/** treestore node attribute value */ 1.16 +struct ts_value { 1.17 + enum ts_value_type type; 1.18 + 1.19 + char *str; /**< all values have a string representation */ 1.20 + int inum; /**< numeric values TS_NUMBER will have this set */ 1.21 + float fnum; /**< numeric values TS_NUMBER will have this set */ 1.22 + 1.23 + /** vector values (arrays containing ONLY numbers) will have this set */ 1.24 + float *vec; /**< elements of the vector */ 1.25 + int vec_size; /**< size of the vector (in elements), same as array_size */ 1.26 + 1.27 + /** array values (including vectors) will have this set */ 1.28 + struct ts_value *array; /**< elements of the array */ 1.29 + int array_size; /**< size of the array (in elements) */ 1.30 +}; 1.31 + 1.32 +int ts_init_value(struct ts_value *tsv); 1.33 +void ts_destroy_value(struct ts_value *tsv); 1.34 + 1.35 +struct ts_value *ts_alloc_value(void); /**< also calls ts_init_value */ 1.36 +void ts_free_value(struct ts_value *tsv); /**< also calls ts_destroy_value */ 1.37 + 1.38 +/** perform a deep-copy of a ts_value */ 1.39 +int ts_copy_value(struct ts_value *dest, struct ts_value *src); 1.40 + 1.41 +/** ts_set_value will try to parse the string and initialize the value type fields */ 1.42 +int ts_set_value(struct ts_value *tsv, const char *str); 1.43 + 1.44 +/** set a ts_value from a list of integers */ 1.45 +int ts_set_valueiv(struct ts_value *tsv, int count, ...); 1.46 +int ts_set_valueiv_va(struct ts_value *tsv, int count, va_list ap); 1.47 +int ts_set_valuei(struct ts_value *tsv, int inum); /**< equiv: ts_set_valueiv(val, 1, inum) */ 1.48 + 1.49 +/** set a ts_value from a list of floats */ 1.50 +int ts_set_valuefv(struct ts_value *tsv, int count, ...); 1.51 +int ts_set_valuefv_va(struct ts_value *tsv, int count, va_list ap); 1.52 +int ts_set_valuef(struct ts_value *tsv, int fnum); /**< equiv: ts_set_valuefv(val, 1, fnum) */ 1.53 + 1.54 +/** set a ts_value from a list of ts_value pointers. they are deep-copied as per ts_copy_value */ 1.55 +int ts_set_valuev(struct ts_value *tsv, int count, ...); 1.56 +int ts_set_valuev_va(struct ts_value *tsv, int count, va_list ap); 1.57 + 1.58 + 1.59 +/** treestore node attribute */ 1.60 +struct ts_attr { 1.61 + char *name; 1.62 + struct ts_value val; 1.63 + 1.64 + struct ts_attr *next; 1.65 +}; 1.66 + 1.67 +int ts_init_attr(struct ts_attr *attr); 1.68 +void ts_destroy_attr(struct ts_attr *attr); 1.69 + 1.70 +struct ts_attr *ts_alloc_attr(void); /**< also calls ts_init_attr */ 1.71 +void ts_free_attr(struct ts_attr *attr); /**< also calls ts_destroy_attr */ 1.72 + 1.73 +/** perform a deep-copy of a ts_attr */ 1.74 +int ts_copy_attr(struct ts_attr *dest, struct ts_attr *src); 1.75 + 1.76 +int ts_set_attr_name(struct ts_attr *attr, const char *name); 1.77 + 1.78 + 1.79 + 1.80 +/** treestore node */ 1.81 +struct ts_node { 1.82 + char *name; 1.83 + 1.84 + int attr_count; 1.85 + struct ts_attr *attr_list, *attr_tail; 1.86 + 1.87 + int child_count; 1.88 + struct ts_node *child_list, *child_tail; 1.89 + struct ts_node *parent; 1.90 + 1.91 + struct ts_node *next; /* next sibling */ 1.92 +}; 1.93 + 1.94 +int ts_init_node(struct ts_node *node); 1.95 +void ts_destroy_node(struct ts_node *node); 1.96 + 1.97 +struct ts_node *ts_alloc_node(void); /**< also calls ts_init_node */ 1.98 +void ts_free_node(struct ts_node *n); /**< also calls ts_destroy_node */ 1.99 + 1.100 +/** recursively destroy all the nodes of the tree */ 1.101 +void ts_free_tree(struct ts_node *tree); 1.102 + 1.103 +void ts_add_attr(struct ts_node *node, struct ts_attr *attr); 1.104 +struct ts_attr *ts_get_attr(struct ts_node *node, const char *name); 1.105 + 1.106 +void ts_add_child(struct ts_node *node, struct ts_node *child); 1.107 +int ts_remove_child(struct ts_node *node, struct ts_node *child); 1.108 +struct ts_node *ts_get_child(struct ts_node *node, const char *name); 1.109 + 1.110 +struct ts_node *ts_load(const char *fname); 1.111 +int ts_save(struct ts_node *tree, const char *fname); 1.112 + 1.113 +#ifdef __cplusplus 1.114 +} 1.115 +#endif 1.116 + 1.117 +#endif /* TREESTORE_H_ */