2 ba49fdf9 2021-07-30 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 ba49fdf9 2021-07-30 op * Permission to use, copy, modify, and distribute this software for any
5 ba49fdf9 2021-07-30 op * purpose with or without fee is hereby granted, provided that the above
6 ba49fdf9 2021-07-30 op * copyright notice and this permission notice appear in all copies.
8 ba49fdf9 2021-07-30 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 ba49fdf9 2021-07-30 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 ba49fdf9 2021-07-30 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ba49fdf9 2021-07-30 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 ba49fdf9 2021-07-30 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ba49fdf9 2021-07-30 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 ba49fdf9 2021-07-30 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 ba49fdf9 2021-07-30 op * A streaming text/x-patch parser
21 ba49fdf9 2021-07-30 op #include <stdlib.h>
22 ba49fdf9 2021-07-30 op #include <string.h>
24 ba49fdf9 2021-07-30 op #include "telescope.h"
25 ba49fdf9 2021-07-30 op #include "parser.h"
27 ba49fdf9 2021-07-30 op static int tpatch_parse(struct parser *, const char *, size_t);
28 ba49fdf9 2021-07-30 op static int tpatch_emit_line(struct parser *, const char *, size_t);
29 ba49fdf9 2021-07-30 op static int tpatch_foreach_line(struct parser *, const char *, size_t);
30 ba49fdf9 2021-07-30 op static int tpatch_free(struct parser *);
33 ba49fdf9 2021-07-30 op textpatch_initparser(struct parser *p)
35 ba49fdf9 2021-07-30 op memset(p, 0, sizeof(*p));
37 ba49fdf9 2021-07-30 op p->name = "text/x-patch";
38 ba49fdf9 2021-07-30 op p->parse = &tpatch_parse;
39 ba49fdf9 2021-07-30 op p->free = &tpatch_free;
41 ba49fdf9 2021-07-30 op p->flags = PARSER_IN_PATCH_HDR;
43 78894e73 2021-08-12 op TAILQ_INIT(&p->head);
47 ba49fdf9 2021-07-30 op tpatch_parse(struct parser *p, const char *buf, size_t size)
49 ba49fdf9 2021-07-30 op return parser_foreach_line(p, buf, size, tpatch_foreach_line);
53 ba49fdf9 2021-07-30 op tpatch_emit_line(struct parser *p, const char *line, size_t linelen)
55 ba49fdf9 2021-07-30 op struct line *l;
57 ba49fdf9 2021-07-30 op if ((l = calloc(1, sizeof(*l))) == NULL)
60 ba49fdf9 2021-07-30 op if (p->flags & PARSER_IN_PATCH_HDR)
61 ba49fdf9 2021-07-30 op l->type = LINE_PATCH_HDR;
63 ba49fdf9 2021-07-30 op l->type = LINE_PATCH;
65 ba49fdf9 2021-07-30 op if (linelen != 0) {
66 ba49fdf9 2021-07-30 op if ((l->line = calloc(1, linelen+1)) == NULL) {
71 ba49fdf9 2021-07-30 op memcpy(l->line, line, linelen);
73 ba49fdf9 2021-07-30 op if (!(p->flags & PARSER_IN_PATCH_HDR))
74 ba49fdf9 2021-07-30 op switch (*l->line) {
76 ba49fdf9 2021-07-30 op l->type = LINE_PATCH_ADD;
79 ba49fdf9 2021-07-30 op l->type = LINE_PATCH_DEL;
82 ba49fdf9 2021-07-30 op l->type = LINE_PATCH_HUNK_HDR;
86 ba49fdf9 2021-07-30 op if (has_prefix(l->line, "+++"))
87 ba49fdf9 2021-07-30 op p->flags &= ~PARSER_IN_PATCH_HDR;
90 32ac17a4 2021-08-12 op TAILQ_INSERT_TAIL(&p->head, l, lines);
96 ba49fdf9 2021-07-30 op tpatch_foreach_line(struct parser *p, const char *line, size_t linelen)
98 ba49fdf9 2021-07-30 op return tpatch_emit_line(p, line, linelen);
102 ba49fdf9 2021-07-30 op tpatch_free(struct parser *p)
104 ba49fdf9 2021-07-30 op if (p->len != 0)
105 ba49fdf9 2021-07-30 op return tpatch_emit_line(p, p->buf, p->len);