xemil/example.c

71 lines
1.5 KiB
C
Raw Normal View History

2025-12-23 02:05:29 +09:00
#include <xemil.h>
2025-12-22 18:22:46 +09:00
2025-12-22 19:20:04 +09:00
#define INDENT 2
2025-12-22 20:44:41 +09:00
void recursive(xl_node_t* node, int indent) {
int i;
xl_node_t* n;
2025-12-22 18:22:46 +09:00
for(i = 0; i < indent; i++) printf(" ");
2025-12-22 21:33:45 +09:00
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>\n", node->type == XL_NODE_PROCESS ? "?" : "");
2025-12-22 19:20:04 +09:00
if(node->text != NULL) {
for(i = 0; i < indent + INDENT; i++) printf(" ");
printf("%s\n", node->text);
}
2025-12-22 20:44:41 +09:00
} else if(node->text != NULL && node->type == XL_NODE_COMMENT) {
2025-12-22 18:52:37 +09:00
printf("<!--%s-->\n", node->text);
}
2025-12-22 18:22:46 +09:00
n = node->first_child;
2025-12-22 18:52:37 +09:00
while(n != NULL) {
2025-12-22 19:20:04 +09:00
recursive(n, indent + INDENT);
2025-12-22 18:22:46 +09:00
n = n->next;
}
2025-12-22 18:52:37 +09:00
2025-12-22 20:44:41 +09:00
if(node->name != NULL && node->type == XL_NODE_NODE) {
2025-12-22 18:52:37 +09:00
for(i = 0; i < indent; i++) printf(" ");
printf("</%s>\n", node->name);
}
2025-12-22 18:22:46 +09:00
}
2025-12-22 18:52:37 +09:00
int main(int argc, char** argv) {
2025-12-27 06:05:03 +09:00
int i;
xl_node_t* n;
2025-12-22 18:22:46 +09:00
2025-12-22 18:52:37 +09:00
for(i = 1; i < argc; i++) {
2025-12-23 02:05:29 +09:00
xemil_t* h = xl_open_file(argv[i]);
2025-12-22 18:52:37 +09:00
if(h != NULL) {
2025-12-22 18:22:46 +09:00
printf("%s:\n", argv[i]);
2025-12-22 20:44:41 +09:00
if(xl_parse(h)) {
2025-12-27 06:05:03 +09:00
n = h->pre;
if(n != NULL) {
while(n != NULL) {
recursive(n, INDENT);
n = n->next;
}
}
2025-12-22 19:20:04 +09:00
if(h->root != NULL) recursive(h->root, INDENT);
2025-12-22 18:52:37 +09:00
} else {
2025-12-22 19:20:04 +09:00
int j;
for(j = 0; j < INDENT; j++) printf(" ");
printf("Parse error\n");
2025-12-22 18:22:46 +09:00
}
2025-12-22 20:44:41 +09:00
xl_close(h);
2025-12-22 18:22:46 +09:00
}
}
}