Blame


1 191b4ca2 2021-03-12 op /*
2 191b4ca2 2021-03-12 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 191b4ca2 2021-03-12 op *
4 191b4ca2 2021-03-12 op * Permission to use, copy, modify, and distribute this software for any
5 191b4ca2 2021-03-12 op * purpose with or without fee is hereby granted, provided that the above
6 191b4ca2 2021-03-12 op * copyright notice and this permission notice appear in all copies.
7 191b4ca2 2021-03-12 op *
8 191b4ca2 2021-03-12 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 191b4ca2 2021-03-12 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 191b4ca2 2021-03-12 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 191b4ca2 2021-03-12 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 191b4ca2 2021-03-12 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 191b4ca2 2021-03-12 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 191b4ca2 2021-03-12 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 191b4ca2 2021-03-12 op */
16 191b4ca2 2021-03-12 op
17 191b4ca2 2021-03-12 op #include "telescope.h"
18 191b4ca2 2021-03-12 op
19 191b4ca2 2021-03-12 op #include <stdlib.h>
20 191b4ca2 2021-03-12 op #include <string.h>
21 191b4ca2 2021-03-12 op
22 191b4ca2 2021-03-12 op int
23 191b4ca2 2021-03-12 op parser_append(struct parser *p, const char *buf, size_t len)
24 191b4ca2 2021-03-12 op {
25 191b4ca2 2021-03-12 op size_t newlen;
26 191b4ca2 2021-03-12 op char *t;
27 191b4ca2 2021-03-12 op
28 191b4ca2 2021-03-12 op newlen = len + p->len;
29 191b4ca2 2021-03-12 op if ((t = calloc(1, newlen)) == NULL)
30 191b4ca2 2021-03-12 op return 0;
31 191b4ca2 2021-03-12 op memcpy(t, p->buf, p->len);
32 191b4ca2 2021-03-12 op memcpy(t + p->len, buf, len);
33 191b4ca2 2021-03-12 op free(p->buf);
34 191b4ca2 2021-03-12 op p->buf = t;
35 191b4ca2 2021-03-12 op p->len = newlen;
36 191b4ca2 2021-03-12 op return 1;
37 191b4ca2 2021-03-12 op }
38 191b4ca2 2021-03-12 op
39 191b4ca2 2021-03-12 op int
40 191b4ca2 2021-03-12 op parser_set_buf(struct parser *p, const char *buf, size_t len)
41 191b4ca2 2021-03-12 op {
42 191b4ca2 2021-03-12 op free(p->buf);
43 191b4ca2 2021-03-12 op p->buf = NULL;
44 191b4ca2 2021-03-12 op
45 191b4ca2 2021-03-12 op if (len == 0) {
46 191b4ca2 2021-03-12 op p->len = 0;
47 191b4ca2 2021-03-12 op return 1;
48 191b4ca2 2021-03-12 op }
49 191b4ca2 2021-03-12 op
50 191b4ca2 2021-03-12 op if ((p->buf = calloc(1, len)) == NULL)
51 191b4ca2 2021-03-12 op return 0;
52 191b4ca2 2021-03-12 op memcpy(p->buf, buf, len);
53 191b4ca2 2021-03-12 op p->len = len;
54 191b4ca2 2021-03-12 op return 1;
55 191b4ca2 2021-03-12 op }
56 191b4ca2 2021-03-12 op