-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathport_type.c
284 lines (232 loc) · 6.05 KB
/
port_type.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "port_type.h"
#include "port.h"
#include "cargo.h"
#include "item.h"
#include "stringtrie.h"
#include "log.h"
#include "parseconfig.h"
#include "universe.h"
char port_zone_names[PORT_ZONE_NUM][8] = {
"oceanic",
"surface",
"orbit",
"rogue",
};
static void port_type_init(struct port_type *type)
{
memset(type, 0, sizeof(*type));
INIT_LIST_HEAD(&type->list);
INIT_LIST_HEAD(&type->items);
st_init(&type->item_names);
}
void port_type_free(struct port_type *type)
{
free(type->name);
free(type->desc);
struct cargo *c, *_c;
list_for_each_entry_safe(c, _c, &type->items, list) {
list_del(&c->list);
cargo_free(c),
free(c);
}
st_destroy(&type->item_names, ST_DONT_FREE_DATA);
}
static int set_description(struct port_type *type, struct config *conf)
{
if (conf->str)
type->desc = strdup(conf->str);
else
type->desc = strdup("");
return 0;
}
static int add_zone(struct port_type *type, struct config *conf)
{
struct config *child;
int i;
struct st_root zone_root;
int zones[PORT_ZONE_NUM];
st_init(&zone_root);
for (i = 0; i < PORT_ZONE_NUM; i++)
zones[i] = i;
for (i = 0; i < PORT_ZONE_NUM; i++)
if (st_add_string(&zone_root, port_zone_names[i], &zones[i]))
return -1;
list_for_each_entry(child, &conf->children, list) {
int *j = st_lookup_string(&zone_root, child->key);
if (!j)
return -1;
type->zones[*j] = 1;
}
st_destroy(&zone_root, ST_DONT_FREE_DATA);
return 0;
}
static int set_item_capacity(struct cargo *cargo, struct st_root *item_names, struct config *conf)
{
cargo->max = conf->l;
return 0;
}
static int set_item_produces(struct cargo *cargo, struct st_root *item_names, struct config *conf)
{
cargo->daily_change += conf->l;
return 0;
}
static int set_item_consumes(struct cargo *cargo, struct st_root *item_names, struct config *conf)
{
cargo->daily_change -= conf->l;
return 0;
}
static int set_item_requires(struct cargo *cargo, struct st_root *item_names, struct config *conf)
{
struct cargo *req = st_lookup_string(item_names, conf->str);
if (!req) {
log_printfn(LOG_CONFIG, "item '%s' does not exist in this port", conf->str);
return -1;
}
ptrlist_push(&cargo->requires, req);
return 0;
}
static int build_item_cmdtree(struct st_root *root)
{
if (st_add_string(root, "capacity", set_item_capacity))
return -1;
if (st_add_string(root, "produces", set_item_produces))
return -1;
if (st_add_string(root, "consumes", set_item_consumes))
return -1;
if (st_add_string(root, "requires", set_item_requires))
return -1;
return 0;
}
static int add_item(struct port_type *type, struct config *conf)
{
struct st_root cmd_root;
st_init(&cmd_root);
if (build_item_cmdtree(&cmd_root))
goto err;
if (!conf->str)
goto err;
struct cargo *cargo = st_lookup_string(&type->item_names, conf->str);
if (!cargo)
goto err;
int (*func)(struct cargo*, struct st_root *item_names, struct config*);
struct config *child;
list_for_each_entry(child, &conf->children, list) {
func = st_lookup_string(&cmd_root, child->key);
if (!func) {
log_printfn(LOG_CONFIG, "unknown port type item key: \"%s\"\n", child->key);
continue;
}
if (func(cargo, &type->item_names, child))
continue;
}
st_destroy(&cmd_root, ST_DONT_FREE_DATA);
return 0;
err:
st_destroy(&cmd_root, ST_DONT_FREE_DATA);
return -1;
}
static int pre_add_item(struct port_type *type, struct config *conf)
{
struct cargo *cargo = NULL;
if (!conf->str) {
log_printfn(LOG_CONFIG, "syntax error after \"item\"");
goto err;
}
struct item *item = st_lookup_string(&univ.item_names, conf->str);
if (!item) {
log_printfn(LOG_CONFIG, "unknown item: \"%s\"", conf->str);
goto err;
}
cargo = malloc(sizeof(*cargo));
if (!cargo)
goto err;
cargo_init(cargo);
cargo->item = item;
if (st_add_string(&type->item_names, item->name, cargo))
goto err;
list_add(&cargo->list, &type->items);
return 0;
err:
if (cargo)
cargo_free(cargo);
free(cargo);
return -1;
}
static int build_command_tree(struct st_root *root)
{
if (st_add_string(root, "description", set_description))
return -1;
if (st_add_string(root, "zones", add_zone))
return -1;
if (st_add_string(root, "item", pre_add_item))
return -1;
return 0;
}
int load_ports_from_file(const char * const file, struct universe * const universe)
{
struct list_head conf_root = LIST_HEAD_INIT(conf_root);
struct st_root cmd_root;
struct st_root item_root;
struct config *conf, *child;
struct port_type *type;
int (*func)(struct port_type*, struct config*);
assert(file);
st_init(&cmd_root);
st_init(&item_root);
if (build_command_tree(&cmd_root))
goto err;
if (st_add_string(&item_root, "item", add_item))
goto err;
if (parse_config_file(file, &conf_root))
goto err;
list_for_each_entry(conf, &conf_root, list) {
type = malloc(sizeof(*type));
if (!type)
goto err;
port_type_init(type);
type->name = strdup(conf->key);
if (!type->name) {
free(type);
goto err;
}
/*
* We need to parse in two steps because parsing the items requires
* all items to exist (as they have mutual dependencies). The first
* step allocates the items and builds the stringtree, while the
* second step does the actual item parsing.
*/
list_for_each_entry(child, &conf->children, list) {
func = st_lookup_string(&cmd_root, child->key);
if (!func) {
log_printfn(LOG_CONFIG, "unknown port type key: \"%s\"\n", child->key);
continue;
}
if (func(type, child))
goto err;
}
list_for_each_entry(child, &conf->children, list) {
func = st_lookup_string(&item_root, child->key);
if (func && func(type, child))
goto err;
}
if (st_add_string(&universe->port_type_names, type->name, type)) {
port_type_free(type);
free(type);
goto err;
}
list_add(&type->list, &universe->port_types);
}
destroy_config(&conf_root);
st_destroy(&item_root, ST_DONT_FREE_DATA);
st_destroy(&cmd_root, ST_DONT_FREE_DATA);
return 0;
err:
destroy_config(&universe->port_types);
st_destroy(&item_root, ST_DONT_FREE_DATA);
st_destroy(&cmd_root, ST_DONT_FREE_DATA);
return -1;
}