xemil/example.c
2026-03-09 21:17:39 +09:00

89 lines
2.1 KiB
C

#include <xemil.h>
#define INDENT 2
void recursive(xemil_t* handle, xl_node_t* node, int indent) {
int i;
xl_node_t* n;
for(i = 0; i < indent; i++) printf(" ");
if(node->name != NULL && (node->type == XL_NODE_NODE || node->type == XL_NODE_PROCESS)) {
xl_attribute_t* a;
printf("<%s%s", node->type == XL_NODE_PROCESS ? "?" : "", node->name);
a = node->first_attribute;
while(a != NULL) {
if(a->value == NULL) {
printf(" %s", a->key);
} else {
printf(" %s=\"%s\"", a->key, a->value);
}
a = a->next;
}
printf("%s%s>\n", node->type == XL_NODE_PROCESS ? "?" : "", (node->first_child == NULL && node->text == NULL) ? " /" : "");
if(node->text != NULL && !handle->new_text) {
for(i = 0; i < indent + INDENT; i++) printf(" ");
printf("%s\n", node->text);
}
} else if(node->text != NULL && node->type == XL_NODE_COMMENT) {
printf("<!--%s-->\n", node->text);
} else if(node->text != NULL && node->type == XL_NODE_TEXT) {
printf("%s\n", node->text);
}
n = node->first_child;
while(n != NULL) {
recursive(handle, n, indent + INDENT);
n = n->next;
}
if(node->name != NULL && node->type == XL_NODE_NODE && !(node->first_child == NULL && node->text == NULL && !handle->new_text)) {
for(i = 0; i < indent; i++) printf(" ");
printf("</%s>\n", node->name);
}
}
int main(int argc, char** argv) {
int i;
xl_node_t* n;
for(i = 1; i < argc; i++) {
xemil_t* h = xl_open_file(argv[i]);
h->new_text = 1;
if(h != NULL) {
printf("%s:\n", argv[i]);
if(xl_parse(h)) {
xl_node_t** r;
n = h->pre;
if(n != NULL) {
while(n != NULL) {
// recursive(h, n, INDENT);
n = n->next;
}
}
/* f(h->root != NULL) recursive(h, h->root, INDENT); */
r = xl_get_path(h->root, "book.title");
/* check if book.title dosent exist to prevent SEGFAULT */
if(r != NULL) {
int j;
for(j = 0; r[j] != NULL; j++) {
recursive(h, r[j], INDENT);
}
free(r);
} else {
printf("book.title not found in %s\n", argv[i]);
}
} else {
int j;
for(j = 0; j < INDENT; j++) printf(" ");
printf("Parse error\n");
}
xl_close(h);
}
}
}