aboutsummaryrefslogtreecommitdiff
path: root/parse.cpp
blob: d6da0bb4e3e5c065867596dad53cdd27ff7e58f9 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "parse.h"

#include <cstring>

#include <errno.h>
#include <assert.h>

#include "vcal.h"
#include "strbuf.h"

#include "err.h"

/*
 * name *(";" param) ":" value CRLF
 */
int parse_file(char* filename, FILE* f, vcomponent* root) {
	part_context p_ctx = p_key;

	parse_ctx ctx(filename);
	ctx.comp_stack.push(root);

	content_line cline;

	char c;
	while ( (c = fgetc(f)) != EOF) {

		/* We have a linebreak */
		if (c == '\r' || c == '\n') {

			if (fold(f, &ctx, c) > 0) {
				/* Actuall end of line, handle value */

				cline.push_value(ctx.str);
				ctx.str.soft_reset();

				handle_kv(&cline, &ctx);

				p_ctx = p_key;
			} /* Else continue on current line */

		/* We have an escaped character */
		} else if (c == '\\') {
			char esc = fgetc(f);
			char target;

			/*
			 * An actuall linebreak after the backslash. Unfold the
			 * line and find the next "real" character, and escape
			 * that.
			 */
			if (esc == '\r' || esc == '\n') {
				int ret;
				if ( (ret = fold(f, &ctx, esc)) != 0) {
					if (ret == 1) ERR_P(&ctx, "ESC before not folded line");
					else          ERR_P(&ctx, "other error: val = %i", ret);
					exit (2);
				} else {
					esc = fgetc(f);
				}
			}

			/* Escaped newline */
			if (esc == 'n' || esc == 'N') {
				target = '\n';

			/* "Standard" escaped character */
			} else if (esc == ';' || esc == ',' || esc == '\\') {
				target = esc;

			/* Invalid escaped character */
			} else {
				ERR_P(&ctx, "Non escapable character '%c' (%i)", esc, esc);
			}

			/* save escapade character as a normal character */
			ctx.str += target;

			++ctx.column;
			++ctx.pcolumn;


		/* Border between param {key, value} */
		} else if (p_ctx == p_param_name && c == '=') {

			// ctx.str holds param_key;
			// cline.values.back()->params.push_back(new __param_set(ctx.str));
			cline.values.back()->push_param_key(ctx.str);

			ctx.str.soft_reset();

			p_ctx = p_param_value;

		/*
		 * One of four cases:
		 * 1) end of key  , start of value
		 * 2)   ,,   key  ,    ,,    param
		 * 3)   ,,   param,    ,,    param
		 * 4)   ,,   param,    ,,    value
		 */
		} else if ((p_ctx == p_key || p_ctx == p_param_value) && (c == ':' || c == ';')) {

			if (p_ctx == p_param_value) {
				/* push kv pair */

				cline.values.back()->push_param_value(ctx.str);
				ctx.str.soft_reset();
			}

			if (p_ctx == p_key) {
				/* set key for content line */
				cline.key = ctx.str;
				cline.key.cap();
				ctx.str.soft_reset();


			}

			if      (c == ':') p_ctx = p_value;
			else if (c == ';') p_ctx = p_param_name;

		} else {
			ctx.str += c;

			++ctx.column;
			++ctx.pcolumn;
		}
	}

	if (! feof(f)) {
		ERR("Error parsing");
	}
	/* Check to see if empty line */
	else if (ctx.str.ptr != 0) {
		/*
		 * The standard (3.4, l. 2675) says that each icalobject must
		 * end with CRLF. My files however does not, so we also parse
		 * the end here.
		 */

		++ctx.line;
		ctx.column = 0;

		cline.values.back()->value = ctx.str;
		cline.values.back()->value.cap();
		ctx.str.soft_reset();

		handle_kv(&cline, &ctx);

	}

	// assert(POP(LLIST(vcomponent))(&ctx.comp_stack) == root);
	// assert(EMPTY(LLIST(strbuf))(&ctx.key_stack));
	// assert(EMPTY(LLIST(vcomponent))(&ctx.comp_stack));

	return 0;
}

int handle_kv (
	content_line* cline,
	parse_ctx* ctx
	) {

	if (cline->key == "BEGIN") {
		/* should be one of:
		 * VCALENDAR, VEVENT, VALARM, VTODO, VTIMEZONE,
		 * and possibly some others I forget.
		 */

		ctx->key_stack.push(new strbuf(ctx->str));

		// cline->values.reset();
		// TODO ompty cline->second here;
		// RESET(LLIST(content_set))(&cline->val);

		auto e = new vcomponent(ctx->str.to_string(), ctx->filename);
		e->parent = ctx->comp_stack.top();
		ctx->comp_stack.push(e);

	} else if (cline->key == "END") {
		// strbuf* s = POP(LLIST(strbuf))(&ctx->key_stack);
		strbuf* s = ctx->key_stack.top(); ctx->key_stack.pop();
		if (*s == ctx->str) {
#if 0
			ERR_P(ctx, "Expected END:%s, got END:%s.\n%s line",
					s->mem,
					CLINE_CUR_VAL(cline)->mem,
					PEEK(LLIST(vcomponent))(&ctx->comp_stack)->filename);
#endif
			ctx->key_stack.push(s);
			return -1;

		} else {
			delete s;

			/* Received propper end, push cur into parent */
			vcomponent* cur = ctx->comp_stack.top(); ctx->comp_stack.pop();

			// TODO should self instead be done at creation time?
			ctx->comp_stack.push(cur);
		}
	} else {
		content_line* c = new content_line;
		// TODO make sure deep-copy
		*c = *cline;

		// PUSH(TRIE(content_line))(
		// 		&PEEK(LLIST(vcomponent))(&ctx->comp_stack)->clines,
		// 		c->key.mem, c);

		// TODO?
		// ctx->comp_stack.top()->clines.push_back(c->first, c);
		// TODO
		// RESET(LLIST(content_set))(&cline->val);
	}

	return 0;
}

int fold(FILE* f, parse_ctx* ctx, char c) {
	int retval;

	char buf[2] = {
		(c == '\n' ? '\n' : (char) fgetc(f)),
		(char) fgetc(f)
	};

	ctx->pcolumn = 1;

	if (buf[0] != '\n') {
		ERR_P(ctx, "expected new_line after CR");
		retval = -1;

	} else if (buf[1] == ' ' || buf[1] == '\t') {
		retval = 0;
		ctx->pcolumn++;

	} else if (ungetc(buf[1], f) != buf[1]) {
		ERR_P(ctx, "Failed to put character back on FILE");
		retval = -2;

	} else {
		retval = 1;
		++ctx->line;
		ctx->column = 0;
	}

	++ctx->pline;

	return retval;
}