aboutsummaryrefslogtreecommitdiff
path: root/src/vcal.c
blob: 3c9885bad0c1f731a08334da078385be391d455d (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
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
#include "vcal.h"

#include <string.h>

#define TYPE strbuf
#include "linked_list.inc.h"
#undef TYPE

#define TYPE param_set
#include "trie.inc.h"
#undef TYPE

#define TYPE content_set
#include "linked_list.inc.h"
#undef TYPE

#define T strbuf
	#define V TRIE(param_set)
		#include "pair.inc.h"
	#undef V
#undef T

#define TYPE content_line
// #include "hash.inc"
#include "trie.inc.h"
#undef TYPE

#define TYPE vcomponent
// #include "vector.inc.h"
#include "linked_list.inc.h"
#undef TYPE

INIT_F(vcomponent) {
	INIT(TRIE(content_line), &self->clines);
	INIT(LLIST(vcomponent), &self->components);

	// vcomponent_push_val (self, "X-HNH-FILENAME", "VIRTUAL");
	vcomponent_push_val (self, "X-HNH-SOURCETYPE", "virtual");
	char* type = "VIRTUAL";
	self->type = (char*) calloc(sizeof(*type), strlen(type) + 1);
	strcpy(self->type, type);

	self->parent = NULL;
	self->scm = NULL;
	self->scmtype = NULL;

	return 0;

}

INIT_F(vcomponent, const char* type) {
	return INIT(vcomponent, self, type, NULL);
}

INIT_F(vcomponent, const char* type, const char* filename) {

	INIT(TRIE(content_line), &self->clines);
	INIT(LLIST(vcomponent), &self->components);

	if (filename != NULL) {
		/*
		 * NOTE
		 * RFC-7986 adds additional parameters linked to this one.
		 * - `SOURCE' :: where a (possibly) updated version of the data can be
		 *   found 
		 * - `URL' :: Where the same data can be fonud, but
		 *   differently (but not where the original data can be fonud
		 *   agani).
		 */
		vcomponent_push_val (self, "X-HNH-FILENAME", filename);
	}

	self->type = (char*) calloc(sizeof(*type), strlen(type) + 1);
	strcpy(self->type, type);

	self->parent = NULL;
	self->scm = NULL;
	self->scmtype = NULL;

	return 0;
}

content_line* get_attributes (vcomponent* ev, const char* key) {
	size_t len = strlen(key) + 1;
	char* cpy = (char*) (calloc(sizeof(*cpy), len));
	strncpy (cpy, key, len);

	content_line* ret = GET(TRIE(content_line))(&ev->clines, cpy);

	free (cpy);
	return ret;
}

FREE_F(vcomponent) {
	free(self->type);

	if (FREE(TRIE(content_line))(&self->clines) != 0) {
		ERR("Error freeing vcomponent");
	}

	FREE(LLIST(vcomponent))(&self->components);

	return 0;
}

int PUSH(vcomponent)(vcomponent* parent, vcomponent* child) {
	child->parent = parent;
	return PUSH(LLIST(vcomponent))(&parent->components, child);
}

int DEEP_COPY(vcomponent)(vcomponent* a, vcomponent* b) {
	(void) a;
	(void) b;
	ERR("Deep copy not implemented for vcomponent");
	return -1;
}

int vcomponent_copy(vcomponent* dest, vcomponent* src) {

	DEEP_COPY(TRIE(content_line))(&dest->clines, &src->clines);

	/* Children are the same objects */
	FOR(LLIST, vcomponent, c, &src->components) {
		PUSH(LLIST(vcomponent))(&dest->components, c);
	}

	PUSH(vcomponent)(src->parent, dest);

	return 0;
}

FMT_F(vcomponent) {
	int seek = 0;

	for (int i = 0; i < 40; i++) fmtf("_");

	seek += sprintf(buf + seek, _YELLOW);
	seek += sprintf(buf + seek, "\nVComponet (Type := %s)\n", self->type);
	seek += sprintf(buf + seek, _RESET);
	seek += FMT(TRIE(content_line))(&self->clines, buf + seek);
	seek += sprintf(buf + seek, "\nComponents:\n");
	FOR(LLIST, vcomponent, comp, &self->components) {
		seek += FMT(vcomponent)(comp, buf + seek);
	}

	return seek;
}

int vcomponent_push_val (vcomponent* comp, const char* key, const char* val) {
	NEW(content_line, cl);
	NEW(content_set, cs);
	strbuf_load(&cs->key, val);
	PUSH(content_line)(cl, cs);

	char* key_cpy = calloc(sizeof(*key_cpy), strlen(key) + 1);
	strcpy (key_cpy, key);
	PUSH(TRIE(content_line))(&comp->clines, key_cpy, cl);
	free (key_cpy);

	return 0;
}

char* vcomponent_get_val (vcomponent* comp, const char* key) {
	char* key_cpy = calloc(sizeof(*key_cpy), strlen(key) + 1);
	strcpy (key_cpy, key);
	content_line* cl = GET(TRIE(content_line))(&comp->clines, key_cpy);
	free (key_cpy);

	if (cl != NULL && cl->cval != NULL) {
		return cl->cval->key.mem;
	}

	return NULL;
}