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
|
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "doc.h"
#include "err.h"
/* initialization / destruction */
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;
d->linkc = 0;
}
void doc_fini(struct doc *d) {
buf_free(&d->txt);
buf_free(&d->lnk);
}
/* line creation */
void doc_new_line(struct doc *d) {
buf_grow(&d->txt, sizeof(struct doc_line));
*(struct doc_line *)&d->txt.buf[d->txt.sz] = (struct doc_line) {
.prev = ((struct doc_line *)&d->txt.buf[d->latest])->len,
.link = DOC_LINK_NONE,
.len = 0
};
d->latest = d->txt.sz;
d->txt.sz += sizeof(struct doc_line);
}
void doc_add_line(struct doc *d, strv_t s) {
doc_add_textn(d, s.s, s.n);
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;
}
unsigned short doc_add_link(struct doc *d, const char *url) {
buf_cat(&d->lnk, url, strlen(url) + 1);
return d->linkc++;
}
void doc_set_link(struct doc *d, unsigned short lnk) {
struct doc_line *l = doc_line_at(d, d->latest);
l->link = lnk;
}
/* line navigation */
struct doc_line *doc_line_at(struct doc *d, size_t ofs) {
return (struct doc_line *)&d->txt.buf[ofs];
}
int doc_line_nextp(struct doc *d, size_t ofs) {
return ofs + doc_line_at(d, ofs)->len + sizeof(struct doc_line) < d->txt.sz;
}
int doc_line_prevp(struct doc *d, size_t ofs) {
return ofs >= doc_line_at(d, ofs)->prev + sizeof(struct doc_line);
}
int doc_line_prev(struct doc *d, size_t *ofs) {
size_t n = doc_line_at(d, *ofs)->prev + sizeof(struct doc_line);
if (*ofs >= n) {
*ofs -= n;
return 0;
} else {
return -1;
}
}
int doc_line_next(struct doc *d, size_t *ofs) {
size_t n = doc_line_at(d, *ofs)->len + sizeof(struct doc_line);
if (*ofs + n < d->txt.sz) {
*ofs += n;
return 0;
} else {
return -1;
}
}
const char *doc_get_link(struct doc *d, unsigned short lnk) {
size_t l = 1;
for (size_t i = 0; i < d->lnk.sz; i++) {
if (l == lnk) return &d->lnk.buf[i];
if (d->lnk.buf[i] == 0) l++;
}
return NULL;
}
|