diff --git a/Makefile b/Makefile index 8f8db43..17f940a 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ SO = .so all: libxmllib$(SO) format: - clang-format --verbose -i `find src include -name "*.c" -or -name "*.h"` + clang-format --verbose -i `find src include -name "*.c" -or -name "*.h"` example.c example: example.c libxmllib$(SO) $(CC) -o $@ example.c -I include -Wl,-R. -L. -lxmllib diff --git a/example.c b/example.c index 7a6bbd8..2b663d8 100644 --- a/example.c +++ b/example.c @@ -1,28 +1,37 @@ #include -void recursive(xmllib_node_t* node, int indent){ - int i; +void recursive(xmllib_node_t* node, int indent) { + int i; xmllib_node_t* n; for(i = 0; i < indent; i++) printf(" "); - printf("%s\n", node->name); - + if(node->name != NULL && node->type == XMLLIB_NODE_NODE) { + printf("<%s>\n", node->name); + } else if(node->text != NULL && node->type == XMLLIB_NODE_COMMENT) { + printf("\n", node->text); + } + n = node->first_child; - while(n != NULL){ + while(n != NULL) { recursive(n, indent + 2); n = n->next; } + + if(node->name != NULL && node->type == XMLLIB_NODE_NODE) { + for(i = 0; i < indent; i++) printf(" "); + printf("\n", node->name); + } } -int main(int argc, char** argv){ +int main(int argc, char** argv) { int i; - for(i = 1; i < argc; i++){ + for(i = 1; i < argc; i++) { xmllib_t* h = xmllib_open_file(argv[i]); - if(h != NULL){ + if(h != NULL) { printf("%s:\n", argv[i]); - if(xmllib_parse(h)){ + if(xmllib_parse(h)) { if(h->root != NULL) recursive(h->root, 2); - }else{ + } else { printf(" Parse error\n"); } xmllib_close(h); diff --git a/src/core.c b/src/core.c index dd7b557..db31d5b 100644 --- a/src/core.c +++ b/src/core.c @@ -99,11 +99,24 @@ int xmllib_parse(xmllib_t* handle) { if(STATE == STATE_INITIAL) { /* parsed tag */ + xmllib_node_t* n = NULL; + xmllib_node_t* targ = NULL; if(node[0] == '!') { /* special tag */ if(strlen(node) > 1 && node[1] == '-') { /* comment */ + + if(strlen(node) > (3 + 2)) { + n = malloc(sizeof(*n)); + + n->type = XMLLIB_NODE_COMMENT; + n->name = NULL; + n->text = malloc(strlen(node) + 1); + + strcpy(n->text, node + 3); + n->text[strlen(n->text) - 2] = 0; + } } else { /* doctype or something - we ignore this for now */ } @@ -122,9 +135,9 @@ int xmllib_parse(xmllib_t* handle) { nest_level--; } else { - xmllib_node_t* n = malloc(sizeof(*n)); - xmllib_node_t* last = NULL; - int i; + int i; + + n = malloc(sizeof(*n)); if(handle->root == NULL) handle->root = n; @@ -151,30 +164,9 @@ int xmllib_parse(xmllib_t* handle) { n->root = handle->root; n->parent = current; - n->first_child = NULL; - - if(current != NULL && current->first_child != NULL) { - last = current->first_child; - while(last->next != NULL) last = last->next; - } - - n->prev = last; - n->next = NULL; - - if(n->prev != NULL) { - n->prev->next = n; - } - - current = n; - - if(current->parent != NULL && current->parent->first_child == NULL) { - current->parent->first_child = n; - } - - if(node[strlen(node) - 1] == '/') { - /* self-closing tag */ - current = current->parent; - } else { + if(node[strlen(node) - 1] != '/') { + /* non self-closing tag */ + targ = n; nest_level++; } } @@ -183,6 +175,30 @@ int xmllib_parse(xmllib_t* handle) { if(nest_level == 0) break; } + if(n != NULL) { + xmllib_node_t* last = NULL; + + n->first_child = NULL; + + if(current != NULL && current->first_child != NULL) { + last = current->first_child; + while(last->next != NULL) last = last->next; + } + + n->prev = last; + n->next = NULL; + + if(n->prev != NULL) { + n->prev->next = n; + } + + if(current != NULL && current->first_child == NULL) { + current->first_child = n; + } + } + + if(targ != NULL) current = targ; + free(node); node = NULL; }