This commit is contained in:
Nishi 2025-12-22 18:52:37 +09:00
commit f820c978fc
Signed by: nishi
GPG key ID: 27EF69B208EB9343
3 changed files with 61 additions and 36 deletions

View file

@ -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

View file

@ -4,13 +4,22 @@ 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) {

View file

@ -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;