added treestore and started a bit on the tilesets for the maze parts
[dosdemo] / src / treestor.h
1 #ifndef TREESTORE_H_
2 #define TREESTORE_H_
3
4 #include <stdarg.h>
5
6 #ifdef __cplusplus
7 #define TS_DEFVAL(x) =(x)
8 extern "C" {
9 #else
10 #define TS_DEFVAL(x)
11 #endif
12
13 enum ts_value_type { TS_STRING, TS_NUMBER, TS_VECTOR, TS_ARRAY };
14
15 /** treestore node attribute value */
16 struct ts_value {
17         enum ts_value_type type;
18
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 */
22
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 */
26
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 };
31
32 int ts_init_value(struct ts_value *tsv);
33 void ts_destroy_value(struct ts_value *tsv);
34
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 */
37
38 /** perform a deep-copy of a ts_value */
39 int ts_copy_value(struct ts_value *dest, struct ts_value *src);
40
41 /** set a ts_value as a string */
42 int ts_set_value_str(struct ts_value *tsv, const char *str);
43
44 /** set a ts_value from a list of integers */
45 int ts_set_valuei_arr(struct ts_value *tsv, int count, const int *arr);
46 int ts_set_valueiv(struct ts_value *tsv, int count, ...);
47 int ts_set_valueiv_va(struct ts_value *tsv, int count, va_list ap);
48 int ts_set_valuei(struct ts_value *tsv, int inum);      /**< equiv: ts_set_valueiv(val, 1, inum) */
49
50 /** set a ts_value from a list of floats */
51 int ts_set_valuef_arr(struct ts_value *tsv, int count, const float *arr);
52 int ts_set_valuefv(struct ts_value *tsv, int count, ...);
53 int ts_set_valuefv_va(struct ts_value *tsv, int count, va_list ap);
54 int ts_set_valuef(struct ts_value *tsv, float fnum);    /**< equiv: ts_set_valuefv(val, 1, fnum) */
55
56 /** set a ts_value from a list of ts_value pointers. they are deep-copied as per ts_copy_value */
57 int ts_set_value_arr(struct ts_value *tsv, int count, const struct ts_value *arr);
58 int ts_set_valuev(struct ts_value *tsv, int count, ...);
59 int ts_set_valuev_va(struct ts_value *tsv, int count, va_list ap);
60
61
62 /** treestore node attribute */
63 struct ts_attr {
64         char *name;
65         struct ts_value val;
66
67         struct ts_attr *next;
68 };
69
70 int ts_init_attr(struct ts_attr *attr);
71 void ts_destroy_attr(struct ts_attr *attr);
72
73 struct ts_attr *ts_alloc_attr(void);            /**< also calls ts_init_attr */
74 void ts_free_attr(struct ts_attr *attr);        /**< also calls ts_destroy_attr */
75
76 /** perform a deep-copy of a ts_attr */
77 int ts_copy_attr(struct ts_attr *dest, struct ts_attr *src);
78
79 int ts_set_attr_name(struct ts_attr *attr, const char *name);
80
81
82
83 /** treestore node */
84 struct ts_node {
85         char *name;
86
87         int attr_count;
88         struct ts_attr *attr_list, *attr_tail;
89
90         int child_count;
91         struct ts_node *child_list, *child_tail;
92         struct ts_node *parent;
93
94         struct ts_node *next;   /* next sibling */
95 };
96
97 int ts_init_node(struct ts_node *node);
98 void ts_destroy_node(struct ts_node *node);
99
100 struct ts_node *ts_alloc_node(void);    /**< also calls ts_init_node */
101 void ts_free_node(struct ts_node *n);   /**< also calls ts_destroy_node */
102
103 /** recursively destroy all the nodes of the tree */
104 void ts_free_tree(struct ts_node *tree);
105
106 void ts_add_attr(struct ts_node *node, struct ts_attr *attr);
107 struct ts_attr *ts_get_attr(struct ts_node *node, const char *name);
108
109 const char *ts_get_attr_str(struct ts_node *node, const char *aname,
110                 const char *def_val TS_DEFVAL(0));
111 float ts_get_attr_num(struct ts_node *node, const char *aname,
112                 float def_val TS_DEFVAL(0.0f));
113 int ts_get_attr_int(struct ts_node *node, const char *aname,
114                 int def_val TS_DEFVAL(0.0f));
115 float *ts_get_attr_vec(struct ts_node *node, const char *aname,
116                 float *def_val TS_DEFVAL(0));
117 struct ts_value *ts_get_attr_array(struct ts_node *node, const char *aname,
118                 struct ts_value *def_val TS_DEFVAL(0));
119
120
121 void ts_add_child(struct ts_node *node, struct ts_node *child);
122 int ts_remove_child(struct ts_node *node, struct ts_node *child);
123 struct ts_node *ts_get_child(struct ts_node *node, const char *name);
124
125 struct ts_node *ts_load(const char *fname);
126 int ts_save(struct ts_node *tree, const char *fname);
127
128 struct ts_attr *ts_lookup(struct ts_node *root, const char *path);
129 const char *ts_lookup_str(struct ts_node *root, const char *path,
130                 const char *def_val TS_DEFVAL(0));
131 float ts_lookup_num(struct ts_node *root, const char *path,
132                 float def_val TS_DEFVAL(0.0f));
133 int ts_lookup_int(struct ts_node *root, const char *path,
134                 int def_val TS_DEFVAL(0));
135 float *ts_lookup_vec(struct ts_node *root, const char *path,
136                 float *def_val TS_DEFVAL(0));
137 struct ts_value *ts_lookup_array(struct ts_node *root, const char *path,
138                 struct ts_value *def_val TS_DEFVAL(0));
139
140
141 #ifdef __cplusplus
142 }
143 #endif
144
145 #endif  /* TREESTORE_H_ */