summaryrefslogtreecommitdiff
path: root/src/image/image.c
blob: 464b86db2b7e606692f70225041a3d893bad4aa9 (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
#include <string.h>
#include <stdio.h>

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


int kai_read_init_from(struct ka_init_from_t *dest, struct kai_tag_t *src)
{
	KAI_CHECK_TAG_TYPE("init_from");

	struct kai_tag_t *ref = kai_tag_get_first_child_with_type(src, "ref");
	if (ref != NULL) {
		dest->ref = strdup(ref->content);
	}
	else {
		dest->ref = strdup(src->content);
	}
	return 0;
}

void kai_release_init_from(struct ka_init_from_t i)
{
	free(i.ref);
}


int kai_read_image(struct ka_image_t *dest, struct kai_tag_t *src)
{
	KAI_CHECK_TAG_TYPE("image");

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

	struct kai_tag_t *init_from = kai_tag_get_first_child_with_type(src, "init_from");
	if (init_from == NULL) { 
		free(dest->id);
		free(dest->sid);
		free(dest->name);
		return -1; 
	}

	int result = kai_read_init_from(&(dest->init_from), init_from);
	if (result != 0) { 
		free(dest->id);
		free(dest->sid);
		free(dest->name);
		return -1; 
	}

	return 0;
}

void kai_release_image(struct ka_image_t i)
{
	free(i.id);
	free(i.sid);
	free(i.name);
	kai_release_init_from(i.init_from);
}


int kai_read_library_images(struct ka_library_images_t *dest, struct kai_tag_t *src)
{
	KAI_CHECK_TAG_TYPE("library_images");

	dest->id = kai_tag_attr_to_dup(src, "id");
	dest->name = kai_tag_attr_to_dup(src, "name");

	int result;
	KAI_FILL_ARRAY_FROM_TAGS(
		result,
		dest->image, dest->image_count, struct ka_image_t,
		src, "image",
		kai_read_image, kai_release_image
	);

	if (result != 0) {
		kai_release_library_images(*dest);
		return -1;
	}

	return 0;
}

void kai_release_library_images(struct ka_library_images_t l)
{
	free(l.id);
	free(l.name);
	
	int i;
	for (i=0; i<l.image_count; i++) {
		kai_release_image(l.image[i]);
	}
	free(l.image);
}