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

#include <kalmia.h>
#include <ezxml.h>
#include "geometry.h"


static int copy_str(char **dest, const char *src)
{
	if (src == NULL) {
		*dest = NULL;
		return 0;
	}

	size_t len = strlen(src)+1;
	*dest = malloc(len * sizeof(char));
	if (*dest == NULL)
		return -1;
	strncpy(*dest, src, len);
	return 0;
}


ka_float_array_t * kai_parse_float_array(ezxml_t tag)
{
	if (strcmp(ezxml_name(tag), "float_array") != 0) {
		/* wrong tag type */
		return NULL;
	}

	/* allocate struct */
	ka_float_array_t *a = malloc(sizeof(ka_float_array_t));
	if (a == NULL) return NULL;

	/* inspect attributes */
	const char *count_str = ezxml_attr(tag, "count");
	a->count = strtol(count_str, NULL, 10);

	const char *id_str = ezxml_attr(tag, "id");
	if (copy_str(&(a->id), id_str) < 0) {
		free(a);
		return NULL;
	}

	/* parse data */
	a->array = malloc(a->count * sizeof(ka_real_t));
	if (a->array == NULL) {
		free(a->id);
		free(a);
		return NULL;
	}

	char *data = ezxml_txt(tag);
	char *end;
	int i;
	for (i=0; i<a->count; i++) {
		a->array[i] = KA_STR_TO_REAL(data, &end);
		data = end;
	}

	return a;
}


void kai_free_float_array(ka_float_array_t *a)
{
	free(a->array);
	free(a->id);
	free(a);
}