From d5e4755ee765a5cd69daac00cf04db25cae5fed8 Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Thu, 8 Sep 2016 20:43:52 +0200 Subject: Implement first version of parsing --- yxml.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 1 deletion(-) diff --git a/yxml.c b/yxml.c index f575a8d..271bc4a 100644 --- a/yxml.c +++ b/yxml.c @@ -1,12 +1,112 @@ +#include #include #include +#include #include "yxmllib.h" #define BUFSIZE 8096 +typedef struct buffer { + int len; + char *buffer; +} buffer; + +buffer *buffer_new() { + // allocate struct and buffer in one go + buffer *buf = malloc(sizeof(buffer)+BUFSIZE); + buf->len = 0; + + return buf; +} + +buffer *buffer_reset(buffer *buf) { + buf->buffer = memset(buf->buffer, 0, BUFSIZE); + buf->len = 0; + + return buf; +} + +buffer *buffer_append(buffer *buf, char *data) { + int i; + + for (i = buf->len; data; i++) { + buf->buffer[i] = data[i]; + } + + return buf; +} + int process(char *fn, yxml_t *state) { - yxml_init(state, state+1, BUFSIZE); + size_t filelen; + char *parsebuffer; + int inelement; + buffer *contentbuf; + + FILE* f = fopen(fn, "rb"); + if (!f) { + fprintf(stderr, "Could not open %s.", fn); + perror("Error:"); + return 1; + } + + // get file size + fseek(f, 0L, SEEK_END); + filelen = ftell(f); + rewind(f); + + parsebuffer = calloc(1, filelen+1); + if (!parsebuffer) { + fprintf(stderr, "Could not allocate memory for file %s.\n", fn); + fclose(f); + return 1; + } + + if (fread(parsebuffer, filelen, 1 , f) != 1) { + fprintf(stderr, "Could not read file into memory for file %s.\n", fn); + free(parsebuffer); + fclose(f); + return 1; + } + + fclose(f); + + inelement = 0; + contentbuf = buffer_new(); + if (!contentbuf) { + fprintf(stderr, "Could not allocate memory for the element content buffer.\n"); + fclose(f); + return 1; + } + + for (; *parsebuffer; parsebuffer++) { + yxml_init(state, state+1, BUFSIZE); + yxml_ret_t r = yxml_parse(state, *parsebuffer); + if(r < 0) { + return 1; + } + switch(r) { + case YXML_ELEMSTART: + if (!strcmp(state->elem, "article-title")) { + inelement = 1; + } + break; + + case YXML_CONTENT: + if (inelement) { + buffer_append(contentbuf, state->data); + } + break; + + case YXML_ELEMEND: + if (!strcmp(state->elem, "article-title")) { + inelement = 0; + } + printf("%s\n", contentbuf->buffer); + buffer_reset(contentbuf); + break; + } + } return 0; } -- cgit v1.2.3