From 38987f3f5a3919ac81ba419e05ac8610c269faff Mon Sep 17 00:00:00 2001 From: wrmr Date: Sat, 9 Nov 2024 02:04:33 -0500 Subject: rename buf_t to str_t, and consolidate str.c/h with strv.c/h --- buf.c | 41 ----------------------------------------- buf.h | 19 ------------------- doc.c | 16 ++++++++-------- doc.h | 5 ++--- nav.c | 6 +++--- net.c | 12 ++++++------ net.h | 2 +- parse.c | 8 ++++---- parse.h | 4 ++-- str.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ str.h | 30 ++++++++++++++++++++++++++++++ strv.c | 18 ------------------ strv.h | 14 -------------- 13 files changed, 116 insertions(+), 119 deletions(-) delete mode 100644 buf.c delete mode 100644 buf.h create mode 100644 str.c create mode 100644 str.h delete mode 100644 strv.c delete mode 100644 strv.h diff --git a/buf.c b/buf.c deleted file mode 100644 index 5575bbd..0000000 --- a/buf.c +++ /dev/null @@ -1,41 +0,0 @@ -#include -#include -#include "buf.h" -#include "err.h" - -void buf_init(buf_t *b, size_t n) { - b->buf = calloc(1, n); - if (!b->buf) { - efatal("buf_init"); - } - b->cap = n; - b->sz = 0; -} - -/* does NOT change sz! just makes room */ -void buf_grow(buf_t *b, size_t n) { - size_t sz = b->sz + n; - size_t c = b->cap; - if (sz > c) { - while (sz > c) c <<= 1; - char *p = realloc(b->buf, c); - if (!p) efatal("buf_grow"); - b->buf = p; - b->cap = c; - } -} - -void buf_cat(buf_t *b, strv_t s) { - buf_grow(b, s.n); - memcpy(&b->buf[b->sz], s.s, s.n); - b->sz += s.n; -} - -void buf_catc(buf_t *b, char c) { - buf_grow(b, 1); - b->buf[b->sz++] = c; -} - -void buf_free(buf_t *b) { - free(b->buf); -} diff --git a/buf.h b/buf.h deleted file mode 100644 index e7fb6b7..0000000 --- a/buf.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef BUF_H -#define BUF_H - -#include -#include "strv.h" - -typedef struct buf { - size_t sz, cap; - char *buf; -} buf_t; - -void buf_init(buf_t *, size_t); -void buf_grow(buf_t *, size_t); -void buf_free(buf_t *); - -void buf_cat(buf_t *b, strv_t s); -void buf_catc(buf_t *b, char c); - -#endif diff --git a/doc.c b/doc.c index aa18369..a8e7975 100644 --- a/doc.c +++ b/doc.c @@ -8,8 +8,8 @@ /* initialization / destruction */ void doc_init(struct doc *d) { - buf_init(&d->txt, sizeof(struct doc_line)); - buf_init(&d->lnk, 1); + str_init(&d->txt, sizeof(struct doc_line)); + str_init(&d->lnk, 1); d->txt.sz = sizeof(struct doc_line); *(struct doc_line *)d->txt.buf = (struct doc_line) { .prev = 0, @@ -21,14 +21,14 @@ void doc_init(struct doc *d) { } void doc_fini(struct doc *d) { - buf_free(&d->txt); - buf_free(&d->lnk); + str_free(&d->txt); + str_free(&d->lnk); } /* line creation */ void doc_new_line(struct doc *d) { - buf_grow(&d->txt, sizeof(struct doc_line)); + str_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, @@ -44,7 +44,7 @@ void doc_add_line(struct doc *d, strv_t s) { } void doc_add_text(struct doc *d, strv_t s) { - buf_grow(&d->txt, s.n); + str_grow(&d->txt, s.n); memcpy(&d->txt.buf[d->txt.sz], s.s, s.n); struct doc_line *dl = (struct doc_line *)&d->txt.buf[d->latest]; d->txt.sz += s.n; @@ -52,8 +52,8 @@ void doc_add_text(struct doc *d, strv_t s) { } unsigned short doc_add_link(struct doc *d, strv_t url) { - buf_cat(&d->lnk, url); - buf_catc(&d->lnk, 0); + str_cat(&d->lnk, url); + str_catc(&d->lnk, 0); return d->linkc++; } diff --git a/doc.h b/doc.h index 2a9fe58..5df443e 100644 --- a/doc.h +++ b/doc.h @@ -1,8 +1,7 @@ #ifndef DOC_H #define DOC_H -#include "buf.h" -#include "strv.h" +#include "str.h" #define DOC_LINK_NONE 0xffff @@ -19,7 +18,7 @@ enum doc_type { }; struct doc { - buf_t txt, lnk; + str_t txt, lnk; size_t latest; unsigned short linkc; }; diff --git a/nav.c b/nav.c index 18b21db..8a520ac 100644 --- a/nav.c +++ b/nav.c @@ -100,7 +100,7 @@ void nav_redraw(struct nav_state *ns) { int nav_to(struct nav_state *ns, const char *url) { enum doc_type doct; struct addr adr; - buf_t buf; + str_t buf; if (net_addr(url, &adr, ns->prot_default)) { return -1; } @@ -110,11 +110,11 @@ int nav_to(struct nav_state *ns, const char *url) { } struct doc d; if (parse_doc(doct, &d, &buf)) { - buf_free(&buf); + str_free(&buf); return -1; } nav_push(ns, d); - buf_free(&buf); + str_free(&buf); nav_redraw(ns); return 0; } diff --git a/net.c b/net.c index 0d6604e..87fe659 100644 --- a/net.c +++ b/net.c @@ -129,12 +129,12 @@ static int fetch_file(const struct addr *adr, struct buf *buf, enum doc_type *do perr("file not found"); return -1; } - buf_init(buf, 1024); + str_init(buf, 1024); char b[256]; while (fgets(b, sizeof b, f)) { - buf_cat(buf, strv(b)); + str_cat(buf, strv(b)); } - buf_catc(buf, 0); + str_catc(buf, 0); fclose(f); *doct = DOC_PLAIN; return 0; @@ -173,18 +173,18 @@ static int fetch_gopher(const struct addr *adr, struct buf *buf, enum doc_type * perr("write failure"); return -1; } - buf_init(buf, 64); + str_init(buf, 64); char inbuf[256]; for (;;) { ssize_t n = read(s, inbuf, sizeof inbuf); if (n < 0) { perr("read error"); - buf_free(buf); + str_free(buf); close(s); return -1; } if (!n) break; - buf_cat(buf, (strv_t) { inbuf, n }); + str_cat(buf, (strv_t) { inbuf, n }); } close(s); return 0; diff --git a/net.h b/net.h index a2ad569..fa1ac87 100644 --- a/net.h +++ b/net.h @@ -1,7 +1,7 @@ #ifndef NET_H #define NET_H -#include "buf.h" +#include "str.h" #include "doc.h" #define HOST_MAX 255 diff --git a/parse.c b/parse.c index 40f7a25..afa8008 100644 --- a/parse.c +++ b/parse.c @@ -2,7 +2,7 @@ #include #include "parse.h" -#include "strv.h" +#include "str.h" #include "err.h" int isurlch(char c) { @@ -23,7 +23,7 @@ void parse_plain_url(struct doc *d, struct doc_line *l, size_t i) { l->link = doc_add_link(d, url); } -int parse_plain(struct doc *d, const buf_t *b) { +int parse_plain(struct doc *d, const str_t *b) { doc_init(d); strv_t ln, buf = (strv_t) { b->buf, b->sz }; while (strv_split(&buf, '\n', &ln)) { @@ -80,7 +80,7 @@ int parse_gophermap_line(struct doc *d, strv_t ln) { return 0; } -int parse_gophermap(struct doc *d, const buf_t *b) { +int parse_gophermap(struct doc *d, const str_t *b) { doc_init(d); strv_t ln, buf = { b->buf, b->sz }; while (strv_split(&buf, '\n', &ln)) { @@ -90,7 +90,7 @@ int parse_gophermap(struct doc *d, const buf_t *b) { return 0; } -int parse_doc(enum doc_type type, struct doc *d, const buf_t *b) { +int parse_doc(enum doc_type type, struct doc *d, const str_t *b) { switch (type) { case DOC_PLAIN: return parse_plain(d, b); diff --git a/parse.h b/parse.h index 460c275..c6776e6 100644 --- a/parse.h +++ b/parse.h @@ -2,8 +2,8 @@ #define PARSE_H #include "doc.h" -#include "buf.h" +#include "str.h" -int parse_doc(enum doc_type type, struct doc *d, const buf_t *b); +int parse_doc(enum doc_type type, struct doc *d, const str_t *b); #endif diff --git a/str.c b/str.c new file mode 100644 index 0000000..10d64e1 --- /dev/null +++ b/str.c @@ -0,0 +1,60 @@ +#include +#include +#include "str.h" +#include "err.h" + +/* strings */ + +void str_init(str_t *b, size_t n) { + b->buf = calloc(1, n); + if (!b->buf) { + efatal("str_init"); + } + b->cap = n; + b->sz = 0; +} + +/* does NOT change sz! just makes room */ +void str_grow(str_t *b, size_t n) { + size_t sz = b->sz + n; + size_t c = b->cap; + if (sz > c) { + while (sz > c) c <<= 1; + char *p = realloc(b->buf, c); + if (!p) efatal("str_grow"); + b->buf = p; + b->cap = c; + } +} + +void str_cat(str_t *b, strv_t s) { + str_grow(b, s.n); + memcpy(&b->buf[b->sz], s.s, s.n); + b->sz += s.n; +} + +void str_catc(str_t *b, char c) { + str_grow(b, 1); + b->buf[b->sz++] = c; +} + +void str_free(str_t *b) { + free(b->buf); +} + +/* string views */ + +strv_t strv(const char *s) { + return (strv_t) { s, strlen(s) }; +} + +int strv_split(strv_t *src, int chr, strv_t *dest) { + char *c = memchr(src->s, chr, src->n); + *dest = (strv_t) { + src->s, + c ? c - src->s : src->n + }; + src->s = c ? c + 1 : &src->s[src->n]; + src->n -= dest->n + !!c; + return dest->n > 0; +} diff --git a/str.h b/str.h new file mode 100644 index 0000000..fe56d98 --- /dev/null +++ b/str.h @@ -0,0 +1,30 @@ +#ifndef BUF_H +#define BUF_H + +#include + +/* string views */ + +typedef struct { + const char *s; + size_t n; +} strv_t; + +strv_t strv(const char *s); +int strv_split(strv_t *src, int chr, strv_t *dest); + +/* strings */ + +typedef struct buf { + size_t sz, cap; + char *buf; +} str_t; + +void str_init(str_t *, size_t); +void str_grow(str_t *, size_t); +void str_free(str_t *); + +void str_cat(str_t *b, strv_t s); +void str_catc(str_t *b, char c); + +#endif diff --git a/strv.c b/strv.c deleted file mode 100644 index 0d37a6f..0000000 --- a/strv.c +++ /dev/null @@ -1,18 +0,0 @@ -#include - -#include "strv.h" - -strv_t strv(const char *s) { - return (strv_t) { s, strlen(s) }; -} - -int strv_split(strv_t *src, int chr, strv_t *dest) { - char *c = memchr(src->s, chr, src->n); - *dest = (strv_t) { - src->s, - c ? c - src->s : src->n - }; - src->s = c ? c + 1 : &src->s[src->n]; - src->n -= dest->n + !!c; - return dest->n > 0; -} diff --git a/strv.h b/strv.h deleted file mode 100644 index 1718711..0000000 --- a/strv.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef STRV_H -#define STRV_H - -#include - -typedef struct { - const char *s; - size_t n; -} strv_t; - -strv_t strv(const char *s); -int strv_split(strv_t *src, int chr, strv_t *dest); - -#endif -- cgit 1.4.1-2-gfad0