summary refs log tree commit diff
path: root/parse.c
blob: 22e7edf4320ca92a8b29dadb4ef10ed3011bcfd8 (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
#include "parse.h"
#include "err.h"

int parse_doc(enum doc_type type, struct doc *d, const buf_t *b) {
	switch (type) {
	case DOC_PLAIN:
		doc_init(d);
		for (size_t i = 0; i < b->sz; i++) {
			char c = b->buf[i];
			if (c == '\n') {
				doc_new_line(d);
			} else {
				doc_add_textn(d, &c, 1);
			}
		}
		goto ok;
	default:
		perr("unsupported doctype");
		goto err;
	}
ok:
	return 0;
err:
	return -1;
}