diff options
-rw-r--r-- | yxml.c | 23 |
1 files changed, 15 insertions, 8 deletions
@@ -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; |