From d35da988e8cfb7dbee3d2f0a587b0718e5f1bfbf Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Thu, 8 Sep 2016 21:08:51 +0200 Subject: Move parser init out of the parsing loop --- yxml.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/yxml.c b/yxml.c index 271bc4a..26ead86 100644 --- a/yxml.c +++ b/yxml.c @@ -15,6 +15,10 @@ typedef struct buffer { buffer *buffer_new() { // allocate struct and buffer in one go buffer *buf = malloc(sizeof(buffer)+BUFSIZE); + if (!buf) { + fprintf(stderr, "Could not allocate memory for buffer struct.\n"); + exit(1); + } buf->len = 0; return buf; @@ -33,6 +37,7 @@ buffer *buffer_append(buffer *buf, char *data) { for (i = buf->len; data; i++) { buf->buffer[i] = data[i]; } + buf->len += i+1; return buf; } @@ -73,18 +78,19 @@ int process(char *fn, yxml_t *state) { inelement = 0; contentbuf = buffer_new(); - if (!contentbuf) { - fprintf(stderr, "Could not allocate memory for the element content buffer.\n"); - fclose(f); - return 1; - } + + yxml_init(state, state+1, BUFSIZE); for (; *parsebuffer; parsebuffer++) { - yxml_init(state, state+1, BUFSIZE); + //fprintf(stderr, "pb: %c\n", *parsebuffer); yxml_ret_t r = yxml_parse(state, *parsebuffer); + fprintf(stderr, "parse_state: %d\n", r); if(r < 0) { + printf("Parsing error at %s: line %u, byte %lu, offset %lu\n", + fn, state->line, state->byte, state->total); return 1; } + switch(r) { case YXML_ELEMSTART: if (!strcmp(state->elem, "article-title")) { @@ -99,9 +105,10 @@ int process(char *fn, yxml_t *state) { break; case YXML_ELEMEND: - if (!strcmp(state->elem, "article-title")) { - inelement = 0; + if (strcmp(state->elem, "article-title")) { + break; } + inelement = 0; printf("%s\n", contentbuf->buffer); buffer_reset(contentbuf); break; -- cgit v1.2.3