#include #include #include #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; }