summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c59
1 files changed, 55 insertions, 4 deletions
diff --git a/main.c b/main.c
index a886d9b..e883f29 100644
--- a/main.c
+++ b/main.c
@@ -213,24 +213,70 @@ parse_request(Str url, Request *req)
/* documents */
typedef enum {
- DOC_ERROR,
DOC_TEXT,
DOC_GOPHERMAP,
+ DOC_ERROR,
DOC_UNKNOWN
} DocType;
+typedef struct DocLine {
+ struct DocLine *next, *prev;
+ Str s;
+} DocLine;
+
typedef struct {
Arena arena;
DocType type;
Str src;
+ DocLine *head, *tail;
} Doc;
+/* document parsing */
+
+DocLine *
+doc_push_line(Doc *d, Str s)
+{
+ DocLine *n = new(&d->arena, DocLine);
+ n->s = s;
+ n->prev = d->tail;
+ if (d->tail) d->tail->next = n;
+ if (!d->head) d->head = n;
+ d->tail = n;
+ return n;
+}
+
+int
+parse_text(Doc *d)
+{
+ Str s = d->src;
+ while (s.n > 0) {
+ Cut c = str_cut(s, '\n');
+ doc_push_line(d, c.head);
+ s = c.tail;
+ }
+ return 0;
+}
+
int
-doc_parse(Doc *d, Str src, DocType t)
+parse_doc(Doc *d)
{
+ switch (d->type) {
+ case DOC_TEXT:
+ return parse_text(d);
+ case DOC_GOPHERMAP:
+ doc_push_line(d, S("Gophermaps are unimplemented"));
+ return -1;
+ case DOC_ERROR:
+ doc_push_line(d, S("Error"));
+ return -1;
+ default:
+ break;
+ }
+ doc_push_line(d, S("Unknown document type"));
+ return -1;
}
-/* fetching documents */
+/* document fetching */
ssize_t
write_str(int fd, Str s)
@@ -319,7 +365,12 @@ main(void)
Str buf = { 0 };
printf("fetch() -> %d\n", fetch(&buf, req, &scratch, &scratch));
- printf("%.*s\n", (int)buf.n, buf.s);
+ Doc doc = { .arena = scratch, .src = buf };
+ printf("parse() -> %d\n", parse_doc(&doc));
+ int i = 0;
+ for (DocLine *l = doc.head; l && i < 10; (l = l->next), i++) {
+ printf("%d %.*s\n", i, (int)l->s.n, l->s.s);
+ }
addr_fini();
return 0;