diff options
author | Silvan Jegen <s.jegen@gmail.com> | 2016-09-08 21:19:26 +0200 |
---|---|---|
committer | Silvan Jegen <s.jegen@gmail.com> | 2016-09-08 21:19:26 +0200 |
commit | 041b3508e1f9bee3990c779ea42f917d4ecdc136 (patch) | |
tree | 864df16ae54ab834264661694dbbc34acad56386 | |
parent | d35da988e8cfb7dbee3d2f0a587b0718e5f1bfbf (diff) |
Allocate buffer struct the right way
-rw-r--r-- | yxml.c | 19 |
1 files changed, 9 insertions, 10 deletions
@@ -13,9 +13,9 @@ typedef struct buffer { } buffer; buffer *buffer_new() { - // allocate struct and buffer in one go - buffer *buf = malloc(sizeof(buffer)+BUFSIZE); - if (!buf) { + buffer *buf = malloc(sizeof(buffer)); + buf->buffer = malloc(BUFSIZE); + if (!buf || !buf->buffer) { fprintf(stderr, "Could not allocate memory for buffer struct.\n"); exit(1); } @@ -32,12 +32,10 @@ buffer *buffer_reset(buffer *buf) { } buffer *buffer_append(buffer *buf, char *data) { - int i; - - for (i = buf->len; data; i++) { - buf->buffer[i] = data[i]; + for (int i = 0; data[i]; i++) { + buf->buffer[buf->len] = data[i]; + buf->len++; } - buf->len += i+1; return buf; } @@ -82,9 +80,10 @@ int process(char *fn, yxml_t *state) { yxml_init(state, state+1, BUFSIZE); for (; *parsebuffer; parsebuffer++) { - //fprintf(stderr, "pb: %c\n", *parsebuffer); yxml_ret_t r = yxml_parse(state, *parsebuffer); - fprintf(stderr, "parse_state: %d\n", r); + + //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); |