aboutsummaryrefslogtreecommitdiff
path: root/parse.c
blob: 2850a51ca072d66ced0bba619b5ba86a45820549 (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
#include "parse.h"

#include <errno.h>
#include <string.h>

#include "macro.h"

int parse_file(FILE* f, vcalendar* cal) {
	int segments = 1;
	SNEW(strbuf, str, segments * SEGSIZE);

	part_context p_ctx = p_key;
	scope_context s_ctx = s_none;

	int keylen = 100;
	int vallen = 100;

	int line = 0;

	NEW(vevent, ev);

	SNEW(content_line, cline, keylen, vallen);

	char c;
	while ( (c = fgetc(f)) != EOF) {
		/*
		 * A carrige return means that the current line is at an
		 * end. The following character should always be \n.
		 * However, if the first character on the next line is a
		 * whitespace then the two lines should be concatenated.
		 */
		if (c == '\r') {

			char s[2];
			s[0] = fgetc(f);
			s[1] = fgetc(f);

			if (s[0] != '\n') { ERR("expected newline after CR", line); }
			else if (s[1] == ' ' || s[1] == '\t') {
				/* Folded line, increase size of key and continue.  */

				/* TODO segments is always incremented here, meaning
				 * that segment grows larger for every multi line
				 * encountered.
				 */
				if (strbuf_realloc(&str, ++segments * SEGSIZE) != 0) {
					ERR("Failed to realloc strbuf", line);
					exit (1);
				}
				continue;
			} else {
				/* Actuall end of line, handle values.  */
				if (ungetc(s[1], f) != s[1]) {
					ERR("Failed to put character back on FILE", line); 
					exit (2);
				}

				/* At TRUE end of line */
				if (str.ptr + 1 > vallen) {
					vallen = str.ptr + 1;
					strbuf_realloc(&cline.val, vallen);
				}

				strbuf_copy(&cline.val, &str);
				strbuf_cap(&cline.val);

				++line;

				/* We just got a value */
				// LINE(line, key.mem, val.mem);
				/*
				if (strbuf_c(&cline.key, "LOCATION")) {
					if (strbuf_c(&cline.val, "")) return 1;
					LINE(line, cline.key.mem, cline.val.mem);
				}
				*/

				handle_kv(cal, ev, &cline, line, &s_ctx);
				strbuf_soft_reset(&str);
				p_ctx = p_key;

				continue;
			}
		} else if (p_ctx == p_key && c == ':') {
			/*
			   if (str.ptr + 1 > keylen) {
			   keylen = str.ptr + 1;
			// TODO this might break everything
			strbuf_realloc(&key, keylen);
			}
			*/
			strbuf_copy(&cline.key, &str);
			strbuf_cap(&cline.key);
			strbuf_soft_reset(&str);
			p_ctx = p_value;
			continue;
		}

		strbuf_append(&str, c);
	}


	if (! feof(f)) {
		ERR("Error parsing", errno);
	} else {
		/*
		 * Close last pair if the file is lacking trailing whitespace.
		 * A file with trailing whitespace would however fail.
		 * TODO check the spec and adjust accordingly
		 */
		if (str.ptr + 1 > vallen) {
			vallen = str.ptr + 1;
			strbuf_realloc(&cline.val, vallen);
		}
		strbuf_copy(&cline.val, &str);
		*strbuf_cur(&cline.val) = 0;
	}
	FREE(strbuf)(&str);
	FREE(content_line)(&cline);

	return 0;
}

int handle_kv(
		vcalendar*     cal,
		vevent*        ev,
		content_line*  cline,
		int            line,
		scope_context* s_ctx
		) {
	switch (*s_ctx) {
		case s_none:
			if (! (strbuf_c(&cline->key, "BEGIN") && strbuf_c(&cline->val, "VCALENDAR"))) {
				ERR("Invalid start of calendar", line);
				return 1;
			}
			*s_ctx = s_calendar;
			break;
		case s_calendar:
			if (strbuf_c(&cline->key, "BEGIN")) {
				if (strbuf_c(&cline->val, "VEVENT")) {
					*s_ctx = s_event;
					break;
				} else {
					ERR("Unsupported start", line);
					return 2;
				}
			} else if (strbuf_c(&cline->key, "END")) {
				if (strbuf_c(&cline->val, "VCALENDAR")) {
					*s_ctx = s_none;
					break;
				}
			}
			break;
		case s_event:
			if (strbuf_c(&cline->key, "END")) {
				if (strbuf_c(&cline->val, "VEVENT")) {
					push_event(cal, ev);
					*s_ctx = s_calendar;
				} else {
					ERR("Trying to end something, expected VEVENT", line);
					return 3;
				}
			} else {
				NEW(content_line, c);
				content_line_copy(c, cline);
				add_content_line (ev, c);
			}
			break;
	}
	return 0;
}