mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Turn the XML into something almost parseable.
Of course, now the problem is that the different XML files have different node names, but at least we've turned it into a half-way sane format, and have a nice callback place per value. Soon we could use that to actually fill in useful information. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
parent
fb214b2b39
commit
77ce61644b
2 changed files with 64 additions and 10 deletions
2
Makefile
2
Makefile
|
@ -1,2 +1,2 @@
|
||||||
parse: parse.c
|
parse: parse.c
|
||||||
gcc -g -o parse `xml2-config --cflags` parse.c `xml2-config --libs`
|
gcc -Wall -g -o parse `xml2-config --cflags` parse.c `xml2-config --libs`
|
||||||
|
|
74
parse.c
74
parse.c
|
@ -1,23 +1,77 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <string.h>
|
||||||
#include <libxml/parser.h>
|
#include <libxml/parser.h>
|
||||||
#include <libxml/tree.h>
|
#include <libxml/tree.h>
|
||||||
|
|
||||||
static void show_one_node(int i, xmlNode *node)
|
static const char *nodename(xmlNode *node, char *buf, int len)
|
||||||
{
|
{
|
||||||
static const char indent[] = " ..";
|
/* Don't print out the node name if it is "text" */
|
||||||
|
if (!strcmp(node->name, "text")) {
|
||||||
if (i >= sizeof(indent))
|
node = node->parent;
|
||||||
i = sizeof(indent)-1;
|
if (!node || !node->name)
|
||||||
printf("%.*snode '%s': %s\n", i, indent, node->name, node->content);
|
return "root";
|
||||||
}
|
}
|
||||||
|
|
||||||
static void show(int indent, xmlNode *node)
|
buf += len;
|
||||||
|
*--buf = 0;
|
||||||
|
len--;
|
||||||
|
|
||||||
|
for(;;) {
|
||||||
|
const char *name = node->name;
|
||||||
|
int i = strlen(name);
|
||||||
|
while (--i >= 0) {
|
||||||
|
unsigned char c = name[i];
|
||||||
|
*--buf = tolower(c);
|
||||||
|
if (!--len)
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
node = node->parent;
|
||||||
|
if (!node || !node->name)
|
||||||
|
return buf;
|
||||||
|
*--buf = '.';
|
||||||
|
if (!--len)
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#define MAXNAME 64
|
||||||
|
|
||||||
|
static void show_one_node(xmlNode *node)
|
||||||
|
{
|
||||||
|
int len;
|
||||||
|
const unsigned char *content;
|
||||||
|
char buffer[MAXNAME];
|
||||||
|
const char *name;
|
||||||
|
|
||||||
|
content = node->content;
|
||||||
|
if (!content)
|
||||||
|
return;
|
||||||
|
|
||||||
|
/* Trim whitespace at beginning */
|
||||||
|
while (isspace(*content))
|
||||||
|
content++;
|
||||||
|
|
||||||
|
/* Trim whitespace at end */
|
||||||
|
len = strlen(content);
|
||||||
|
while (len && isspace(content[len-1]))
|
||||||
|
len--;
|
||||||
|
|
||||||
|
if (!len)
|
||||||
|
return;
|
||||||
|
|
||||||
|
name = nodename(node, buffer, sizeof(buffer));
|
||||||
|
|
||||||
|
printf("%s: %.*s\n", name, len, content);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void show(xmlNode *node)
|
||||||
{
|
{
|
||||||
xmlNode *n;
|
xmlNode *n;
|
||||||
|
|
||||||
for (n = node; n; n = n->next) {
|
for (n = node; n; n = n->next) {
|
||||||
show_one_node(indent, n);
|
show_one_node(n);
|
||||||
show(indent+2, n->children);
|
show(n->children);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +85,7 @@ static void parse(const char *filename)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
show(0, xmlDocGetRootElement(doc));
|
show(xmlDocGetRootElement(doc));
|
||||||
xmlFreeDoc(doc);
|
xmlFreeDoc(doc);
|
||||||
xmlCleanupParser();
|
xmlCleanupParser();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue