2008-06-30 07:30:02 +00:00
|
|
|
This library is specially designed for reading xml configuration files and
|
|
|
|
to be as low on memory management as possible. Modifying or writing xml files
|
|
|
|
is not planned for the future. In most situations being able to gather data
|
|
|
|
by reading an xml file is more than enough and the read-only decision
|
|
|
|
provides a number of advantages over a one-size fits all approach. For isntance
|
|
|
|
the memory footprint can be kept low and the library can be kept simple.
|
|
|
|
|
|
|
|
To achieve these goals the mmap function is used to map the configuration file
|
|
|
|
to a memory region. The only places where memory is allocated is when creating
|
2009-04-16 19:03:22 +00:00
|
|
|
a new XML-id, when requesting a string from a node, when requestiong the node
|
2008-07-21 12:32:22 +00:00
|
|
|
name or when a request is made to copy a node into a new memory region.
|
2008-06-30 07:30:02 +00:00
|
|
|
|
|
|
|
Using this library should be pretty simple for most tasks; just open a file,
|
|
|
|
read every parameter one by one and close the id again.
|
|
|
|
{
|
|
|
|
void *xid;
|
|
|
|
|
|
|
|
xid = xmlOpen("/tmp/file.xml");
|
2009-04-16 19:03:22 +00:00
|
|
|
xpos = xmlNodeGetDouble(xid, "/configuration/x-pos");
|
|
|
|
ypos = xmlNodeGetDouble(xid, "/configuration/y-pos");
|
|
|
|
zpos = xmlNodeGetDouble(xid, "/configuration/z-pos");
|
2008-06-30 07:30:02 +00:00
|
|
|
xmlClose(xid);
|
|
|
|
}
|
|
|
|
|
|
|
|
While it is certainly possible to access every node directly by calling the
|
2009-04-16 19:03:22 +00:00
|
|
|
xmlNodeGet(Int/Double/String) functions, when more than one node need to be
|
2008-06-30 07:30:02 +00:00
|
|
|
gathered from a parent node it is advised to get the id of the parent node
|
2008-07-21 12:32:22 +00:00
|
|
|
and work from there since the XML-id holds the boundaries of the (parent)node
|
|
|
|
which limits the searching area resulting in improved searching speed.
|
2008-06-30 07:30:02 +00:00
|
|
|
{
|
|
|
|
void *xnid;
|
|
|
|
char *s;
|
|
|
|
|
2009-04-16 19:03:22 +00:00
|
|
|
xnid = xmlNodeGet(id, "/configuration/setup/");
|
|
|
|
version = xmlNodeGetDouble(xnid, "version");
|
|
|
|
s = xmlNodeGetString(xnid, "author");
|
2008-06-30 07:30:02 +00:00
|
|
|
if (s) author = s;
|
|
|
|
free(s);
|
|
|
|
free(xnid);
|
|
|
|
}
|
|
|
|
|
|
|
|
Overview of the available functions:
|
|
|
|
-----------------------------------------------------------------------------
|
2008-06-29 13:33:26 +00:00
|
|
|
#
|
|
|
|
# Functions to Open and Close the XML file
|
|
|
|
# e.g.
|
|
|
|
# id = xmlOpen("/tmp/file.xml");
|
|
|
|
# xmlClose(id);
|
|
|
|
#
|
|
|
|
void *xmlOpen(const char *);
|
|
|
|
void xmlClose(const void *);
|
|
|
|
|
|
|
|
#
|
|
|
|
# Get the Id of a node at the specified path
|
|
|
|
# e.g.
|
2009-04-16 19:03:22 +00:00
|
|
|
# xnid = xmlNodeGet(id, "/path/to/specified/node");
|
2008-06-29 13:33:26 +00:00
|
|
|
#
|
2009-04-16 19:03:22 +00:00
|
|
|
void *xmlNodeGet(const void *, const char *);
|
|
|
|
void *xmlNodeCopy(void *, const char *);
|
2008-06-29 13:33:26 +00:00
|
|
|
|
|
|
|
#
|
2008-06-30 07:30:02 +00:00
|
|
|
# Functions to walk the node tree and process them one by one.
|
2008-06-29 13:33:26 +00:00
|
|
|
# e.g.
|
|
|
|
# xmid = xmlMarkId(id);
|
2009-04-16 19:03:22 +00:00
|
|
|
# num = xmlNodeGetNum(xmid);
|
2008-06-29 13:33:26 +00:00
|
|
|
# for (i=0; i<num; i++) {
|
2009-04-16 19:03:22 +00:00
|
|
|
# if (xmlNodeGetPos(id, xmid, "element", i) != 0) {
|
2008-06-29 13:33:26 +00:00
|
|
|
# if ((s = xmlGetString(xmid)) != 0) {
|
|
|
|
# printf("%s\n", s);
|
|
|
|
# free(s);
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
# }
|
2008-06-30 07:30:02 +00:00
|
|
|
# free(xmid);
|
2008-06-29 13:33:26 +00:00
|
|
|
#
|
|
|
|
void *xmlMarkId(void *);
|
2009-04-16 19:03:22 +00:00
|
|
|
unsigned int xmlNodeGetNum(void *, const char *);
|
|
|
|
void *xmlNodeGetPos(const void *, void *, const char *, int);
|
2008-07-21 12:32:22 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Get the name of the current node
|
|
|
|
#
|
2009-04-16 19:03:22 +00:00
|
|
|
char *xmlNodeGetName(void *);
|
|
|
|
size_t xmlNodeCopyName(void *, const char *, size_t);
|
2008-06-29 13:33:26 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# These functions work on the current node.
|
|
|
|
# e.g.
|
2009-04-16 19:03:22 +00:00
|
|
|
# xnid = xmlNodeGet(id, "/path/to/last/node");
|
2008-06-29 13:33:26 +00:00
|
|
|
# i = xmlGetInt(xnid);
|
|
|
|
# or
|
2009-04-16 19:03:22 +00:00
|
|
|
# xnid = xmlNodeGet(id, "/path/to/specified/node");
|
2008-06-29 13:33:26 +00:00
|
|
|
# if (xmlCompareString(xnid, "value") == 0) printf("We have a match!\n");
|
|
|
|
#
|
|
|
|
long int xmlGetInt(void *);
|
|
|
|
double xmlGetDouble(void *);
|
|
|
|
char *xmlGetString(void *);
|
2008-07-21 12:32:22 +00:00
|
|
|
size_t xmlCopyString(void *, char *, const size_t);
|
2008-06-29 13:33:26 +00:00
|
|
|
int xmlCompareString(const void *, const char *);
|
|
|
|
|
|
|
|
#
|
|
|
|
# These functions work on a specified node path
|
|
|
|
# e.g.
|
2009-04-16 19:03:22 +00:00
|
|
|
# d = xmlNodeGetDouble(id, "/path/to/node");
|
2008-06-29 13:33:26 +00:00
|
|
|
# or
|
2009-04-16 19:03:22 +00:00
|
|
|
# xnid = xmlNodeGet(id, "/path/to");
|
|
|
|
# i = xmlNodeGetInt(xnid, "node");
|
2008-06-29 13:33:26 +00:00
|
|
|
#
|
2009-04-16 19:03:22 +00:00
|
|
|
long int xmlNodeGetInt(void *, const char *);
|
|
|
|
double xmlNodeGetDouble(void *, const char *);
|
|
|
|
char *xmlNodeGetString(void *, const char *);
|
|
|
|
size_t xmlNodeCopyString(void *, const char *, char *, const size_t);
|
|
|
|
int xmlNodeCompareString(const void *, const char *, const char *);
|
|
|
|
|
|
|
|
#
|
|
|
|
# These functions work on a specified atribute
|
|
|
|
# e.g.
|
|
|
|
# i = xmlAttributeGetInt(id, "n");
|
|
|
|
#
|
|
|
|
# or
|
|
|
|
# s = xmlNodeGetString(id, "type");
|
|
|
|
# if (s) printf("node is of type '%s'\n", s);
|
|
|
|
# free(s);
|
|
|
|
#
|
|
|
|
long int xmlAttributeGetInt(const void *, const char *);
|
|
|
|
double xmlAttributeGetDouble(const void *, const char *);
|
|
|
|
char *xmlAttributeGetString(const void *, const char *);
|
|
|
|
size_t xmlAttributeCopyString(const void *, const char *, const char *, size_t);
|
|
|
|
int xmlAttributeCompareString(const void *, const char *, const char *);
|