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
|
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "doc.h"
#include "err.h"
void doc_init(struct doc *d) {
buf_init(&d->txt, sizeof(struct doc_line));
buf_init(&d->lnk, 1);
d->txt.sz = sizeof(struct doc_line);
*(struct doc_line *)d->txt.buf = (struct doc_line) {
.prev = 0,
.link = DOC_LINK_NONE,
.len = 0,
};
d->latest = 0;
}
void doc_fini(struct doc *d) {
buf_free(&d->txt);
buf_free(&d->lnk);
}
void doc_new_line(struct doc *d) {
size_t here = d->latest, there = d->txt.sz;
buf_grow(&d->txt, sizeof(struct doc_line));
*(struct doc_line *)&d->txt.buf[there] = (struct doc_line) {
.prev = there - here,
.link = DOC_LINK_NONE,
.len = 0
};
d->txt.sz += sizeof(struct doc_line);
d->latest = there;
}
void doc_add_line(struct doc *d, const char *s) {
doc_add_text(d, s);
doc_new_line(d);
}
void doc_add_text(struct doc *d, const char *s) {
doc_add_textn(d, s, strlen(s));
}
void doc_add_textn(struct doc *d, const char *s, size_t n) {
buf_grow(&d->txt, n);
memcpy(&d->txt.buf[d->txt.sz], s, n);
struct doc_line *dl = (struct doc_line *)&d->txt.buf[d->latest];
d->txt.sz += n;
dl->len += n;
}
|