-
Notifications
You must be signed in to change notification settings - Fork 2
/
struct.c
81 lines (63 loc) · 1.54 KB
/
struct.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
#include <sys/types.h>
#include <stddef.h>
#include <stdlib.h>
#include "alloc.h"
#include "struct.h"
#include "value.h"
struct cb_struct_spec *cb_struct_spec_new(size_t name, size_t nfields)
{
struct cb_struct_spec *spec;
spec = cb_malloc(sizeof(struct cb_struct_spec)
+ sizeof(size_t) * nfields, NULL);
spec->name = name;
spec->nfields = nfields;
return spec;
}
void cb_struct_spec_set_field_name(struct cb_struct_spec *spec, size_t i,
size_t name)
{
spec->fields[i] = name;
}
struct cb_struct *cb_struct_spec_instantiate(struct cb_struct_spec *spec)
{
struct cb_struct *val;
size_t i;
val = cb_malloc(sizeof(struct cb_struct) +
spec->nfields * sizeof(struct cb_value), NULL);
val->spec = spec;
for (i = 0; i < spec->nfields; i += 1)
val->fields[i].type = CB_VALUE_NULL;
return val;
}
static ssize_t field_index(const struct cb_struct_spec *spec, size_t name)
{
size_t i;
for (i = 0; i < spec->nfields; i += 1) {
if (spec->fields[i] == name)
return (ssize_t) i;
}
return -1;
}
struct cb_value *cb_struct_get_field(struct cb_struct *s, size_t name,
ssize_t *idx_out)
{
ssize_t idx;
idx = field_index(s->spec, name);
if (idx_out)
*idx_out = idx;
if (idx < 0 || (size_t) idx > s->spec->nfields)
return NULL;
return &s->fields[idx];
}
int cb_struct_set_field(struct cb_struct *s, size_t name, struct cb_value val,
ssize_t *idx_out)
{
ssize_t idx;
idx = field_index(s->spec, name);
if (idx_out)
*idx_out = idx;
if (idx < 0 || (size_t) idx > s->spec->nfields)
return 1;
s->fields[idx] = val;
return 0;
}