summaryrefslogtreecommitdiff
path: root/src/geometry/geometry.c
blob: c3eb40cd773581d602d777dfcb4c3eb54f463c25 (plain)
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
#include <string.h>

#include <kalmia.h>
#include "util/util.h"
#include "xml/xml.h"
#include "geometry.h"


/* macro to ensure tag type matches what we expect */
#define CHECK_TAG_TYPE(expected) \
if (strcmp(src->type, expected) != 0) { \
	return -1; \
}


int kai_read_float_array(struct ka_float_array_t *dest, struct kai_tag_t *src)
{
	CHECK_TAG_TYPE("float_array");

	long count = kai_tag_attr_to_long(src, "count", -1);
	if (count == -1) {
		/* count not present */
		return -1;
	}

	dest->count = count;
	dest->id = kai_tag_attr_to_dup(src, "id");
	dest->digits = kai_tag_attr_to_long(src, "digits", 6);
	dest->magnitude = kai_tag_attr_to_long(src, "magnitude", 38);

	/* allocate & fill buffer */
	dest->buf = kai_alloc(
		dest->count * sizeof(ka_real_t),
		"float_array buffer"
	);
	if (dest->buf == NULL) {
		free(dest->id);
		return -1;
	}
	kai_text_to_reals(dest->buf, src->content, dest->count);

	return 0;
}


int kai_read_param(struct ka_param_t *dest, struct kai_tag_t *src)
{
	CHECK_TAG_TYPE("param");

	char *type = kai_tag_attr_to_dup(src, "type");
	if (type == NULL) {
		return -1;
	}

	dest->name = kai_tag_attr_to_dup(src, "name");
	dest->sid = kai_tag_attr_to_dup(src, "sid");
	dest->type = type;
	dest->semantic = kai_tag_attr_to_dup(src, "semantic");

	return 0;
}


int kai_read_accessor(struct ka_accessor_t *dest, struct kai_tag_t *src)
{
	CHECK_TAG_TYPE("accessor");

	long count = kai_tag_attr_to_long(src, "count", -1);
	if (count == -1) { return -1; }
	char *source = kai_tag_attr_to_dup(src, "source");
	if (source == NULL) { return -1; }

	dest->count = count;
	dest->offset = kai_tag_attr_to_long(src, "offset", 0);
	dest->source = source;
	dest->stride = kai_tag_attr_to_long(src, "stride", 1);

	dest->param_count = kai_tag_num_children(src);
	dest->param = kai_alloc(
		dest->param_count * sizeof(struct ka_param_t),
		"accessor tag param array"
	);
	if (dest->param == NULL) {
		free(source);
		return -1;
	}
	struct kai_tag_t *t = src->children;
	int i;
	for (i=0; i<dest->param_count; i++) {
		int result = kai_read_param(dest->param + i, t);
		if (result != 0) {
			free(dest->param);
			free(dest->source);
			return -1;
		}
		t = t->next;
	}

	return 0;
}