things
This commit is contained in:
parent
f3253acc2d
commit
f820c978fc
3 changed files with 61 additions and 36 deletions
2
Makefile
2
Makefile
|
|
@ -13,7 +13,7 @@ SO = .so
|
||||||
all: libxmllib$(SO)
|
all: libxmllib$(SO)
|
||||||
|
|
||||||
format:
|
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)
|
example: example.c libxmllib$(SO)
|
||||||
$(CC) -o $@ example.c -I include -Wl,-R. -L. -lxmllib
|
$(CC) -o $@ example.c -I include -Wl,-R. -L. -lxmllib
|
||||||
|
|
|
||||||
25
example.c
25
example.c
|
|
@ -1,28 +1,37 @@
|
||||||
#include <xmllib.h>
|
#include <xmllib.h>
|
||||||
|
|
||||||
void recursive(xmllib_node_t* node, int indent){
|
void recursive(xmllib_node_t* node, int indent) {
|
||||||
int i;
|
int i;
|
||||||
xmllib_node_t* n;
|
xmllib_node_t* n;
|
||||||
for(i = 0; i < indent; i++) printf(" ");
|
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("<!--%s-->\n", node->text);
|
||||||
|
}
|
||||||
|
|
||||||
n = node->first_child;
|
n = node->first_child;
|
||||||
while(n != NULL){
|
while(n != NULL) {
|
||||||
recursive(n, indent + 2);
|
recursive(n, indent + 2);
|
||||||
n = n->next;
|
n = n->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(node->name != NULL && node->type == XMLLIB_NODE_NODE) {
|
||||||
|
for(i = 0; i < indent; i++) printf(" ");
|
||||||
|
printf("</%s>\n", node->name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv){
|
int main(int argc, char** argv) {
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for(i = 1; i < argc; i++){
|
for(i = 1; i < argc; i++) {
|
||||||
xmllib_t* h = xmllib_open_file(argv[i]);
|
xmllib_t* h = xmllib_open_file(argv[i]);
|
||||||
if(h != NULL){
|
if(h != NULL) {
|
||||||
printf("%s:\n", argv[i]);
|
printf("%s:\n", argv[i]);
|
||||||
if(xmllib_parse(h)){
|
if(xmllib_parse(h)) {
|
||||||
if(h->root != NULL) recursive(h->root, 2);
|
if(h->root != NULL) recursive(h->root, 2);
|
||||||
}else{
|
} else {
|
||||||
printf(" Parse error\n");
|
printf(" Parse error\n");
|
||||||
}
|
}
|
||||||
xmllib_close(h);
|
xmllib_close(h);
|
||||||
|
|
|
||||||
48
src/core.c
48
src/core.c
|
|
@ -99,11 +99,24 @@ int xmllib_parse(xmllib_t* handle) {
|
||||||
|
|
||||||
if(STATE == STATE_INITIAL) {
|
if(STATE == STATE_INITIAL) {
|
||||||
/* parsed tag */
|
/* parsed tag */
|
||||||
|
xmllib_node_t* n = NULL;
|
||||||
|
xmllib_node_t* targ = NULL;
|
||||||
|
|
||||||
if(node[0] == '!') {
|
if(node[0] == '!') {
|
||||||
/* special tag */
|
/* special tag */
|
||||||
if(strlen(node) > 1 && node[1] == '-') {
|
if(strlen(node) > 1 && node[1] == '-') {
|
||||||
/* comment */
|
/* 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 {
|
} else {
|
||||||
/* doctype or something - we ignore this for now */
|
/* doctype or something - we ignore this for now */
|
||||||
}
|
}
|
||||||
|
|
@ -122,10 +135,10 @@ int xmllib_parse(xmllib_t* handle) {
|
||||||
|
|
||||||
nest_level--;
|
nest_level--;
|
||||||
} else {
|
} 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;
|
if(handle->root == NULL) handle->root = n;
|
||||||
|
|
||||||
n->type = XMLLIB_NODE_NODE;
|
n->type = XMLLIB_NODE_NODE;
|
||||||
|
|
@ -151,6 +164,20 @@ int xmllib_parse(xmllib_t* handle) {
|
||||||
n->root = handle->root;
|
n->root = handle->root;
|
||||||
n->parent = current;
|
n->parent = current;
|
||||||
|
|
||||||
|
if(node[strlen(node) - 1] != '/') {
|
||||||
|
/* non self-closing tag */
|
||||||
|
targ = n;
|
||||||
|
nest_level++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(nest_level < 0) ERROR;
|
||||||
|
if(nest_level == 0) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(n != NULL) {
|
||||||
|
xmllib_node_t* last = NULL;
|
||||||
|
|
||||||
n->first_child = NULL;
|
n->first_child = NULL;
|
||||||
|
|
||||||
if(current != NULL && current->first_child != NULL) {
|
if(current != NULL && current->first_child != NULL) {
|
||||||
|
|
@ -165,23 +192,12 @@ int xmllib_parse(xmllib_t* handle) {
|
||||||
n->prev->next = n;
|
n->prev->next = n;
|
||||||
}
|
}
|
||||||
|
|
||||||
current = n;
|
if(current != NULL && current->first_child == NULL) {
|
||||||
|
current->first_child = 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 {
|
|
||||||
nest_level++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(nest_level < 0) ERROR;
|
if(targ != NULL) current = targ;
|
||||||
if(nest_level == 0) break;
|
|
||||||
}
|
|
||||||
|
|
||||||
free(node);
|
free(node);
|
||||||
node = NULL;
|
node = NULL;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue