summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvan Jegen <s.jegen@gmail.com>2016-09-08 21:08:51 +0200
committerSilvan Jegen <s.jegen@gmail.com>2016-09-08 21:08:51 +0200
commitd35da988e8cfb7dbee3d2f0a587b0718e5f1bfbf (patch)
tree85eb7bbc946752f5e13c91a41560d7676144e56e
parentd5e4755ee765a5cd69daac00cf04db25cae5fed8 (diff)
Move parser init out of the parsing loop
-rw-r--r--yxml.c23
1 files 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;