2 1ac119fb 2024-01-23 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 1ac119fb 2024-01-23 op * Permission to use, copy, modify, and distribute this software for any
5 1ac119fb 2024-01-23 op * purpose with or without fee is hereby granted, provided that the above
6 1ac119fb 2024-01-23 op * copyright notice and this permission notice appear in all copies.
8 1ac119fb 2024-01-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 1ac119fb 2024-01-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 1ac119fb 2024-01-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 1ac119fb 2024-01-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 1ac119fb 2024-01-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 1ac119fb 2024-01-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 1ac119fb 2024-01-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1ac119fb 2024-01-23 op * A streaming text/plain "parser."
21 b66e7d4d 2024-02-06 op #include "compat.h"
23 1ac119fb 2024-01-23 op #include <stdlib.h>
24 1ac119fb 2024-01-23 op #include <string.h>
26 1ac119fb 2024-01-23 op #include "parser.h"
28 1ac119fb 2024-01-23 op static int textplain_parse(struct parser*, const char*, size_t);
29 1ac119fb 2024-01-23 op static int textplain_foreach_line(struct parser*, const char*, size_t);
30 1ac119fb 2024-01-23 op static int textplain_free(struct parser*);
32 1ac119fb 2024-01-23 op static inline int
33 1ac119fb 2024-01-23 op emit_line(struct parser *p, const char *line, size_t len)
35 1ac119fb 2024-01-23 op struct line *l;
37 1ac119fb 2024-01-23 op if ((l = calloc(1, sizeof(*l))) == NULL)
40 1ac119fb 2024-01-23 op l->type = LINE_TEXT;
42 1ac119fb 2024-01-23 op if (len != 0) {
43 1ac119fb 2024-01-23 op if ((l->line = calloc(1, len+1)) == NULL) {
48 1ac119fb 2024-01-23 op memcpy(l->line, line, len);
51 1ac119fb 2024-01-23 op TAILQ_INSERT_TAIL(&p->head, l, lines);
57 1ac119fb 2024-01-23 op textplain_initparser(struct parser *p)
59 1ac119fb 2024-01-23 op memset(p, 0, sizeof(*p));
61 1ac119fb 2024-01-23 op p->name = "text/plain";
62 1ac119fb 2024-01-23 op p->parse = &textplain_parse;
63 1ac119fb 2024-01-23 op p->free = &textplain_free;
65 1ac119fb 2024-01-23 op TAILQ_INIT(&p->head);
69 1ac119fb 2024-01-23 op textplain_parse(struct parser *p, const char *buf, size_t size)
71 1ac119fb 2024-01-23 op return parser_foreach_line(p, buf, size, textplain_foreach_line);
75 1ac119fb 2024-01-23 op textplain_foreach_line(struct parser *p, const char *line, size_t linelen)
77 1ac119fb 2024-01-23 op return emit_line(p, line, linelen);
81 1ac119fb 2024-01-23 op textplain_free(struct parser *p)
83 1ac119fb 2024-01-23 op if (p->len != 0)
84 1ac119fb 2024-01-23 op return emit_line(p, p->buf, p->len);