commit 3d70083dd7ff283b8be1c4910e0547a17b45922f from: Omar Polo date: Fri Mar 12 13:44:53 2021 UTC reuse append and set_buf across our parsers commit - 191b4ca2b0a77bb4183b5edb495eba913e4ece75 commit + 3d70083dd7ff283b8be1c4910e0547a17b45922f blob - ba73cda40354a59a46adacc8d078e3f30d1c11bf blob + 8371a0e037984882553fd699538e3d7429606f40 --- Makefile.am +++ Makefile.am @@ -6,6 +6,7 @@ telescope_SOURCES = compat.h \ gemtext.c \ mime.c \ pages.c \ + parser.c \ telescope.c \ telescope.h \ textplain.c \ blob - 03d8232b9f564226f4e87f976583af28f6853846 blob + 78d151f4dba52503568224e8c42654f315aba34d --- gemtext.c +++ gemtext.c @@ -333,41 +333,6 @@ detect_line_type(const char *buf, size_t len, int in_p return LINE_TEXT; } -static inline int -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; -} - -static inline int -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; -} - static int gemtext_parse(struct parser *p, const char *buf, size_t size) { @@ -379,7 +344,7 @@ gemtext_parse(struct parser *p, const char *buf, size_ b = buf; len = size; } else { - if (!append(p, buf, size)) + if (!parser_append(p, buf, size)) return 0; b = p->buf; len = p->len; @@ -407,7 +372,7 @@ gemtext_parse(struct parser *p, const char *buf, size_ } } - return set_buf(p, b, len); + return parser_set_buf(p, b, len); } static int blob - b610237928efb87711a84d3dad895aafce7697a2 blob + f0a975bc4d3ac0d40f4a0171b99724564690bdcf --- telescope.h +++ telescope.h @@ -129,6 +129,10 @@ extern const char *about_new; #define UNKNOWN_TYPE_OR_CSET 3 extern const char *err_pages[70]; +/* parser.c */ +int parser_append(struct parser*, const char*, size_t); +int parser_set_buf(struct parser*, const char*, size_t); + /* telescope.c */ void load_about_url(struct tab*, const char*); void load_gemini_url(struct tab*, const char*); blob - 31b215e86619b56b436715dd6068216520a1f479 blob + 7d1a8c4e2fe2b9cafda95959913abca95ec7f6d4 --- textplain.c +++ textplain.c @@ -65,41 +65,6 @@ textplain_initparser(struct parser *p) emit_line(p, LINE_PRE_START, NULL, 0); } -static inline int -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; -} - -static inline int -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; -} - static int textplain_parse(struct parser *p, const char *buf, size_t size) { @@ -111,7 +76,7 @@ textplain_parse(struct parser *p, const char *buf, siz b = buf; len = size; } else { - if (!append(p, buf, size)) + if (!parser_append(p, buf, size)) return 0; b = p->buf; len = p->len; @@ -136,7 +101,7 @@ textplain_parse(struct parser *p, const char *buf, siz } } - return set_buf(p, b, len); + return parser_set_buf(p, b, len); } static int