summary refs log tree commit diff
path: root/parse.c
diff options
context:
space:
mode:
Diffstat (limited to 'parse.c')
-rw-r--r--parse.c55
1 files changed, 21 insertions, 34 deletions
diff --git a/parse.c b/parse.c
index b2ac019..ab59fb2 100644
--- a/parse.c
+++ b/parse.c
@@ -35,18 +35,12 @@ int parse_plain(struct doc *d, const str_t *b) {
 	return 0;
 }
 
-size_t scatss(char *buf, size_t i, size_t n, strv_t ss) {
-	size_t si = 0;
-	while (i < n && si < ss.n) {
-		buf[i++] = ss.s[si++];
-	}
-	return i;
+int parse_gophermap_line(struct doc *d, strv_t ln) {
+	return 0;
 }
 
-int parse_gophermap_line(struct doc *d, strv_t ln) {
-	if (ln.n == 1 && ln.s[0] == '.') return 1;
-	char url[512] = "gopher://";
-	size_t urln = 9;
+int parse_gophermap(struct doc *d, const str_t *b) {
+	str_t url;
 	struct {
 		char item_type;
 		strv_t dstr;
@@ -54,35 +48,28 @@ int parse_gophermap_line(struct doc *d, strv_t ln) {
 		strv_t host;
 		strv_t port;
 	} bits;
-	bits.item_type = strv_next(&ln);
-	strv_split(&ln, '\t', &bits.dstr);
-	strv_split(&ln, '\t', &bits.sel);
-	strv_split(&ln, '\t', &bits.host);
-	strv_split(&ln, '\t', &bits.port);
-	switch (bits.item_type) {
-	default:
-		urln = scatss(url, urln, sizeof url, bits.host);
-		if (urln < sizeof url) url[urln++] = ':';
-		urln = scatss(url, urln, sizeof url, bits.port);
-		if (urln < sizeof url) url[urln++] = '/';
-		if (urln < sizeof url) url[urln++] = bits.item_type;
-		urln = scatss(url, urln, sizeof url, bits.sel);
-		doc_set_link(d, doc_add_link(d, (strv_t) { url, urln }));
-	case 'i':
-		doc_add_text(d, bits.dstr);
-		doc_new_line(d);
-		break;
-	}
-	return 0;
-}
-
-int parse_gophermap(struct doc *d, const str_t *b) {
 	doc_init(d);
+	str_init(&url, 64);
 	strv_t ln, buf = { b->buf, b->sz };
 	while (strv_split(&buf, '\n', &ln)) {
 		if (ln.n > 0 && ln.s[ln.n - 1] == '\r') ln.n--;
-		if (parse_gophermap_line(d, ln)) break;
+		if (ln.n == 1 && ln.s[0] == '.') break;
+		bits.item_type = strv_next(&ln);
+		strv_split(&ln, '\t', &bits.dstr);
+		strv_split(&ln, '\t', &bits.sel);
+		strv_split(&ln, '\t', &bits.host);
+		strv_split(&ln, '\t', &bits.port);
+		switch (bits.item_type) {
+		default:
+			str_fmt(&url, "%s:%s/%c%s", bits.host, bits.port, bits.item_type, bits.sel);
+			doc_set_link(d, doc_add_link(d, (strv_t) { url.buf, url.sz }));
+		case 'i':
+			doc_add_text(d, bits.dstr);
+			doc_new_line(d);
+			break;
+		}
 	}
+	str_free(&url);
 	return 0;
 }