summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvan Jegen <s.jegen@gmail.com>2016-09-08 20:43:52 +0200
committerSilvan Jegen <s.jegen@gmail.com>2016-09-08 20:43:52 +0200
commitd5e4755ee765a5cd69daac00cf04db25cae5fed8 (patch)
tree4c83f95df2d5dc0c868295a1da4d0aa1392a82d8
parentd986cd307bd8e85d96624cd53828854e1bad20ae (diff)
Implement first version of parsing
-rw-r--r--yxml.c102
1 files changed, 101 insertions, 1 deletions
diff --git a/yxml.c b/yxml.c
index f575a8d..271bc4a 100644
--- a/yxml.c
+++ b/yxml.c
@@ -1,12 +1,112 @@
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#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;
}