commit 191b4ca2b0a77bb4183b5edb495eba913e4ece75 from: Omar Polo date: Fri Mar 12 13:44:34 2021 UTC move some parser utils to its own file commit - 5b2bf7046904e24781da7d031eb868e86c1a1341 commit + 191b4ca2b0a77bb4183b5edb495eba913e4ece75 blob - /dev/null blob + e4e8a8cad0f95bcae2845f9672e6b231cbcbd851 (mode 644) --- /dev/null +++ parser.c @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2021 Omar Polo + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "telescope.h" + +#include +#include + +int +parser_append(struct parser *p, const char *buf, size_t len) +{ + size_t newlen; + char *t; + + newlen = len + p->len; + if ((t = calloc(1, newlen)) == NULL) + return 0; + memcpy(t, p->buf, p->len); + memcpy(t + p->len, buf, len); + free(p->buf); + p->buf = t; + p->len = newlen; + return 1; +} + +int +parser_set_buf(struct parser *p, const char *buf, size_t len) +{ + free(p->buf); + p->buf = NULL; + + if (len == 0) { + p->len = 0; + return 1; + } + + if ((p->buf = calloc(1, len)) == NULL) + return 0; + memcpy(p->buf, buf, len); + p->len = len; + return 1; +} +