aboutsummaryrefslogtreecommitdiff
path: root/vcal.c
blob: a61d3d9f1d37587d789abf3472183df04f7ce0c4 (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
175
176
177
178
179
180
181
182
183
184
#include "vcal.h"

#include <string.h>

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

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

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

INIT_F(vcomponent) {
	(void) this;
	ERR("Do not use");
	return 0;
}

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

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

	INIT(TRIE(content_line), &this->clines);
	INIT(VECT(vcomponent), &this->components);

	this->filename = NULL;
	if (filename != NULL) {
		this->filename = calloc(sizeof(*filename), strlen(filename) + 1);
		strcpy(this->filename, filename);
	}

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

	this->parent = NULL;

	return 0;
}

content_line* RESOLVE(content_line)
	(content_line* dest, content_line* new)
{
	if (dest == NULL) return new;

	if (strbuf_cmp(&dest->key, &new->key) != 0) {
		ERR("Can't resolve between these two types");
		return NULL;
	}

	/*
	 * This destroys new.
	 */
	APPEND(LLIST(strbuf)) (&dest->vals, &new->vals);

	APPEND(LLIST(key_val)) (&dest->params, &new->params);

	FREE(strbuf)(&new->key);
	free(new);

	return dest;
}

content_line* get_property (vcomponent* ev, char* key) {
	return GET(TRIE(content_line))(&ev->clines, key);
}

INIT_F(content_line) {
	INIT(strbuf, &this->key);
	INIT( LLIST(strbuf), &this->vals );

	INIT( LLIST(key_val), &this->params );

	return 0;
}

INIT_F(content_line, int keylen, int vallen) {
	INIT(strbuf, &this->key, keylen);
	INIT( LLIST(strbuf), &this->vals );
	NEW(strbuf, s, vallen);
	PUSH(LLIST(strbuf))(&this->vals, s);

	INIT( LLIST(key_val), &this->params );

	return 0;
}

FREE_F(content_line) {
	FREE(strbuf)(&this->key);
	FREE(LLIST(strbuf))(&this->vals);

	FREE(LLIST(key_val))(&this->params);

	return 0;
}

int content_line_copy (content_line* dest, content_line* src) {
	DEEP_COPY(strbuf)(&dest->key, &src->key);
	DEEP_COPY(LLIST(strbuf))(&dest->vals, &src->vals);
	DEEP_COPY(LLIST(key_val))(&dest->params, &src->params);

	return 0;
}

FREE_F(vcomponent) {
	if (this->filename != NULL) free(this->filename);
	free(this->type);

	if (FREE(TRIE(content_line))(&this->clines) != 0) {
		fprintf(stderr, "Error freeing vcomponent belonging to file \n %s \n",
				this->filename);
	}

	FREE(VECT(vcomponent))(&this->components);

	return 0;
}

int PUSH(vcomponent)(vcomponent* parent, vcomponent* child) {
	return PUSH(VECT(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;
}

/*
 * TODO this doesn't seem to work.
 */
FMT_F(vcomponent) {
	int seek = 0;
	FOR(int, i, 40) fmtf("-");

	seek += sprintf(buf, _YELLOW);
	seek += sprintf(buf, "VComponet (Type := %s)\n", this->type); 
	seek += FMT(TRIE(content_line))(&this->clines, buf + seek);
	seek += sprintf(buf, _RESET);

	return seek;
}

FMT_F(content_line) {
	char str_a[100], str_b[100], str_c[100];;
	FMT(strbuf)(&this->key, str_a);
	FMT(LLIST(key_val))(&this->params, str_b);
	FMT(LLIST(strbuf))(&this->vals, str_c);
	return sprintf(buf, "[[cl|%s] params := %s vals := %s]", str_a, str_b, str_c);
}

INIT_F(key_val) {
	INIT(strbuf, &this->key, 100);
	INIT(strbuf, &this->val, 100);
	return 0;
}

FREE_F(key_val) {
	FREE(strbuf)(&this->key);
	FREE(strbuf)(&this->val);
	return 0;
}

FMT_F(key_val) {
	char keybuf[100];
	char valbuf[100];
	FMT(strbuf)(&this->key, keybuf);
	FMT(strbuf)(&this->val, valbuf);

	return sprintf(buf, "[[%s] := [%s]]", keybuf, valbuf);
}

int DEEP_COPY(key_val) (key_val* dest, key_val* src) {
	strbuf_copy(&dest->key, &src->key);
	strbuf_copy(&dest->val, &src->val);
	return 0;
}