+21-07-2008
+ * change a number of function parameters to const where appropriate
+ * fix a problem where the wrong node-name length was returned
+ * xmlgrep now also works when only the -e options is specified
+ * fix xmlgrep to show the correct node-name (it reported the parent
+ node-name in the previous version)
+
20-07-2008
* fix __xmlSkipComment to properly find the end of comment tag.
* add the xmlGetNodeName and xmlCopyNodeName functions
for a particular node. this is required for cases where a node with a
particular name is located deeper in a node with the same name;
for example -r /configuration/device/reference/device would fail in the
- previous verion
+ previous version
* rename xmlGetElement to xmlGetNodeNum and add the possibility to request
the nth node with this name
* rename xmlGetNumElements to xmlGetNumNodes
06-07-2008
* reorganize the code to be able to skip comment sections
* depreciate __xmlFindNextElement and use __xmlGetNode instead
- * xmlGetNextElement now returns char* instead of void* for furute use
+ * xmlGetNextElement now returns char* instead of void* for future use
* add preliminary support for wildcards in the search path ('*' and '?')
01-07-2008
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
-a new XML-id, when requesting a string from a node or when a request is made to
-copy a node.
+a new XML-id, when requesting a string from a node, when requesting the node
+name or when a request is made to copy a node into a new memory region.
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.
While it is certainly possible to access every node directly by calling the
xmlGetNode(Int/Double/String) functions, when more than one node need to be
gathered from a parent node it is advised to get the id of the parent node
-and work from there. This is because the XML-id holds the boundaries of the
-(parent)node which limits the searching area resulting in improved searching
-speed.
+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.
{
void *xnid;
char *s;
# Functions to walk the node tree and process them one by one.
# e.g.
# xmid = xmlMarkId(id);
-# num = xmlGetNumElements(xmid);
+# num = xmlGetNumNodes(xmid);
# for (i=0; i<num; i++) {
-# if (xmlGetNextElement(id, xmid, "element") != 0) {
+# if (xmlGetNodeNum(id, xmid, "element", i) != 0) {
# if ((s = xmlGetString(xmid)) != 0) {
# printf("%s\n", s);
# free(s);
# free(xmid);
#
void *xmlMarkId(void *);
-unsigned int xmlGetNumElements(void *, const char *);
-void *xmlGetNextElement(const void *, void *, const char *);
+unsigned int xmlGetNumNodes(void *, const char *);
+void *xmlGetNodeNum(const void *, void *, const char *, int);
+
+#
+# Get the name of the current node
+#
+char *xmlGetNodeName(void *);
+size_t xmlCopyNodeName(void *, const char *, size_t);
#
# These functions work on the current node.
long int xmlGetInt(void *);
double xmlGetDouble(void *);
char *xmlGetString(void *);
+size_t xmlCopyString(void *, char *, const size_t);
int xmlCompareString(const void *, const char *);
#
long int xmlGetNodeInt(void *, const char *);
double xmlGetNodeDouble(void *, const char *);
char *xmlGetNodeString(void *, const char *);
-unsigned xmlCopyNodeString(void *, const char *, char *, const unsigned int);
+size_t xmlCopyNodeString(void *, const char *, char *, const size_t);
int xmlCompareNodeString(const void *, const char *, const char *);
static char *__xmlSkipInfo(const char *, size_t);
static void *__xml_memmem(const void *, size_t, const void *, size_t);
-static void *__xml_memncasecmp(void *, size_t *, void **, size_t *);
+static void *__xml_memncasecmp(const void *, size_t *, void **, size_t *);
static void *__xml_memchr(const void *, int, size_t);
#define PRINT(a, b, c) { \
}
void *
-xmlCopyNode(void *id, char *path)
+xmlCopyNode(const void *id, char *path)
{
struct _xml_id *xsid = 0;
}
void *
-xmlGetNode(void *id, char *path)
+xmlGetNode(const void *id, char *path)
{
struct _xml_id *xsid = 0;
}
char *
-xmlGetNodeName(void *id)
+xmlGetNodeName(const void *id)
{
struct _xml_id *xid = (struct _xml_id *)id;
+ size_t len;
char *ret;
- ret = malloc(xid->nlen+1);
+ len = xid->nlen;
+ ret = malloc(len+1);
if (ret)
{
- memcpy(ret, xid->name, xid->nlen);
- *(ret + xid->nlen) = 0;
+ memcpy(ret, xid->name, len);
+ *(ret + len) = 0;
}
return ret;
}
size_t
-xmlCopyNodeName(void *id, const char *buf, size_t len)
+xmlCopyNodeName(const void *id, const char *buf, size_t len)
{
struct _xml_id *xid = (struct _xml_id *)id;
size_t slen = 0;
slen = len-1;
if (slen > xid->nlen) slen = xid->nlen;
memcpy((char *)buf, xid->name, slen);
+ *((char *)buf + slen) = 0;
}
+
+ return slen;
}
void *
}
char *
-xmlGetNodeString(void *id, const char *path)
+xmlGetNodeString(const void *id, const char *path)
{
struct _xml_id *xid = (struct _xml_id *)id;
char *str = 0;
}
char *
-xmlGetString(void *id)
+xmlGetString(const void *id)
{
struct _xml_id *xid = (struct _xml_id *)id;
char *str = 0;
}
size_t
-xmlCopyString(void *id, char *buf, size_t len)
+xmlCopyString(const void *id, char *buf, size_t len)
{
struct _xml_id *xid = (struct _xml_id *)id;
size_t nlen = 0;
}
size_t
-xmlCopyNodeString(void *id, const char *path, char *buffer, size_t buflen)
+xmlCopyNodeString(const void *id, const char *path, char *buffer, size_t buflen)
{
struct _xml_id *xid = (struct _xml_id *)id;
size_t len = 0;
}
long int
-xmlGetNodeInt(void *id, const char *path)
+xmlGetNodeInt(const void *id, const char *path)
{
struct _xml_id *xid = (struct _xml_id *)id;
long int li = 0;
}
long int
-xmlGetInt(void *id)
+xmlGetInt(const void *id)
{
struct _xml_id *xid = (struct _xml_id *)id;
long int li = 0;
}
double
-xmlGetNodeDouble(void *id, const char *path)
+xmlGetNodeDouble(const void *id, const char *path)
{
struct _xml_id *xid = (struct _xml_id *)id;
double d = 0.0;
}
double
-xmlGetDouble(void *id)
+xmlGetDouble(const void *id)
{
struct _xml_id *xid = (struct _xml_id *)id;
double d = 0.0;
unsigned int
-xmlGetNumNodes(void *id, const char *path)
+xmlGetNumNodes(const void *id, const char *path)
{
struct _xml_id *xid = (struct _xml_id *)id;
unsigned num = 0;
}
void *
-xmlMarkId(void *id)
+xmlMarkId(const void *id)
{
struct _xml_id *xmid = 0;
if (xmid)
{
memcpy(xmid, id, sizeof(struct _xml_id));
- xmid->nlen = 0;
}
}
{
char *new, *cur, *ne, *ret = 0;
size_t restlen, elementlen;
+ size_t retlen = 0;
int found, num;
void *element;
new = __xml_memncasecmp(cur, &restlen, &element, &elementlen);
if (new)
{
+ retlen = elementlen;
if (found == num ) ret = new+1;
}
else
new = cur+elementlen;
if (new >= ne) return 0;
element = (char *)*name;
- elementlen = *rlen;
}
/* restlen -= new-cur; not necessary because of __xml_memncasecmp */
else return 0;
}
- *rlen = elementlen;
+ *rlen = retlen;
*nodenum = found;
return ret;
#define NOCASECMP(a,b) ( ((a)^(b)) & 0xdf )
void *
-__xml_memncasecmp(void *haystack, size_t *haystacklen,
+__xml_memncasecmp(const void *haystack, size_t *haystacklen,
void **needle, size_t *needlelen)
{
void *rptr = 0;
while ((hs < he) && (*hs != ' ') && (*hs != '>')) hs++;
*needle = (void *)haystack;
*needlelen = hs - (char *)haystack;
-
while ((hs < he) && (*hs != '>')) hs++;
rptr = hs;
}
*
* @param xid XML-id
*/
-void xmlClose(const void *);
+void xmlClose(void *);
/**
* Locate a subsection of the xml tree for further processing.
* This adds processing speed since the reuired nodes will only be searched
- * in the subsection.
+ * in the subsection
*
* The memory allocated for the XML-subsection-id has to be freed by the
- * calling program.
+ * calling process
*
* @param xid XML-id
* @param node path to the node containing the subsection
* Copy a subsection of the xml tree for further processing.
* This is useful when it's required to process a section of the XML code
* after the file has been closed. The drawback is the added memory
- * requirements.
+ * requirements
*
* The memory allocated for the XML-subsection-id has to be freed by the
- * calling program.
+ * calling process.
*
* @param xid XML-id
* @param node path to the node containing the subsection
* @return XML-subsection-id for further processing
*/
-void *xmlCopyNode(void *, const char *);
+void *xmlCopyNode(const void *, const char *);
/**
* Return the name of this node.
- * The returned string has to be freed by the calling program.
+ * The returned string has to be freed by the calling process
*
* @param xid XML-id
- * @return a newly alocated string containing the node name.
+ * @return a newly alocated string containing the node name
*/
-char *xmlGetNodeName(void *);
+char *xmlGetNodeName(const void *);
/**
* Copy the name of this node in a pre-allocated buffer
* @param xid XML-id
* @param buffer the buffer to copy the string to
* @param buflen length of the destination buffer
- * @return the length of the node name.
+ * @return the length of the node name
*/
-size_t xmlCopyNodeName(void *, const char *, size_t);
+size_t xmlCopyNodeName(const void *, const char *, size_t);
/**
* Get the number of nodes with the same name from a specified xml path
* @param path path to the xml node
* @return the number count of the nodename
*/
-unsigned int xmlGetNumNodes(void *, const char *);
+unsigned int xmlGetNumNodes(const void *, const char *);
/**
- * Get the nth occurrence of node in the parent node
+ * Get the nth occurrence of node in the parent node.
* The return value should neevr be altered or freed by the caller
*
* @param pid XML-id of the parent node of this node
/**
* Compare the value of this node to a reference string.
- * Comparing is done in a case insensitive way.
+ * Comparing is done in a case insensitive way
*
* @param xid XML-id
- * @param s the string to compare to.
+ * @param str the string to compare to
* @return an integer less than, equal to, ro greater than zero if the value
* of the node is found, respectively, to be less than, to match, or be greater
- * than s
+ * than str
*/
int xmlCompareString(const void *, const char *);
/**
- * Get a string of characters from a specified xml path
+ * Get a string of characters from a specified xml path.
* This function has the advantage of not allocating its own return buffer,
* keeping the memory management to an absolute minimum but the disadvantage
- * is that it's unreliable in multithread environments.
+ * is that it's unreliable in multithread environments
*
* @param xid XML-id
* @param path path to the xml node
* @param buflen length of the destination buffer
* @return the length of the string
*/
-size_t xmlCopyNodeString(void *, const char *, char *, size_t);
+size_t xmlCopyNodeString(const void *, const char *, char *, size_t);
/**
- * Get a string of characters from the current node
- * The returned string has to be freed by the calling program.
+ * Get a string of characters from the current node.
+ * The returned string has to be freed by the calling process
*
* @param xid XML-id
- * @return a newly alocated string containing the contents of the node.
+ * @return a newly alocated string containing the contents of the node
*/
-char *xmlGetString(void *);
+char *xmlGetString(const void *);
/**
- * Get a string of characters from a specified xml path
- * The returned string has to be freed by the calling program.
+ * Get a string of characters from a specified xml path.
+ * The returned string has to be freed by the calling process
*
* @param xid XML-id
* @param path path to the xml node
- * @return a newly alocated string containing the contents of the node.
+ * @return a newly alocated string containing the contents of the node
*/
-char *xmlGetNodeString(void *, const char *);
+char *xmlGetNodeString(const void *, const char *);
/**
- * Get a string of characters from the current node
+ * Get a string of characters from the current node.
* This function has the advantage of not allocating its own return buffer,
* keeping the memory management to an absolute minimum but the disadvantage
- * is that it's unreliable in multithread environments.
+ * is that it's unreliable in multithread environments
*
* @param xid XML-id
* @param buffer the buffer to copy the string to
* @param buflen length of the destination buffer
* @return the length of the string
*/
-size_t xmlCopyString(void *, char *, size_t);
+size_t xmlCopyString(const void *, char *, size_t);
/**
* Compare the value of a node to a reference string.
- * Comparing is done in a case insensitive way.
+ * Comparing is done in a case insensitive way
*
* @param xid XML-id
* @param path path to the xml node to compare to
- * @param s the string to compare to.
+ * @param str the string to compare to
* @return an integer less than, equal to, ro greater than zero if the value
* of the node is found, respectively, to be less than, to match, or be greater
- * than s
+ * than str
*/
int xmlCompareNodeString(const void *, const char *, const char *);
* Get the integer value from the current node
*
* @param xid XML-id
- * @return the contents of the node converted to an integer value.
+ * @return the contents of the node converted to an integer value
*/
-long int xmlGetInt(void *);
+long int xmlGetInt(const void *);
/**
* Get an integer value from a specified xml path
*
* @param xid XML-id
* @param path path to the xml node
- * @return the contents of the node converted to an integer value.
+ * @return the contents of the node converted to an integer value
*/
-long int xmlGetNodeInt(void *, const char *);
+long int xmlGetNodeInt(const void *, const char *);
/**
* Get the double value from the curent node
*
* @param xid XML-id
- * @return the contents of the node converted to a double value.
+ * @return the contents of the node converted to a double value
*/
-double xmlGetDouble(void *);
+double xmlGetDouble(const void *);
/**
* Get a double value from a specified xml path
*
* @param xid XML-id
* @param path path to the xml node
- * @return the contents of the node converted to a double value.
+ * @return the contents of the node converted to a double value
*/
-double xmlGetNodeDouble(void *, const char *);
+double xmlGetNodeDouble(const void *, const char *);
-void *xmlMarkId(void *);
+/**
+ * Create a marker XML-id that starts out with the same settings as the
+ * refference XML-id.
+ * The returned XML-id has to be freed by the calling process
+ *
+ * @param xid reference XML-id
+ * @return a copy of the reference XML-id
+ */
+void *xmlMarkId(const void *);
#endif /* __XML_CONFIG */
#include <stdio.h>
+#include <strings.h>
#include <stdlib.h>
#include <string.h>
{
if (xmlGetNodeNum(xid, xmid, _print, i) != 0)
{
- char *value = xmlGetString(xmid);
- if (value)
- {
- printf("%s: <%s>%s</%s>\n",
- _filenames[num], _print, value, _print);
- free(value);
- }
+ char value[64];
+
+ xmlCopyString(xmid, (char *)&value, 64);
+ printf("%s: <%s>%s</%s>\n",
+ _filenames[num], _print, value, _print);
}
}
free(xmid);
no_elements = xmlGetNumNodes(xmid, _element);
for (i=0; i<no_elements; i++)
{
- if ((xmlGetNodeNum(xid, xmid, _element, i) != 0)
- && (xmlCompareString(xmid, _value) == 0))
+ if (xmlGetNodeNum(xid, xmid, _element, i) != 0)
{
- char *node = xmlGetNodeName(xid);
- printf("%s: <%s>%s</%s>\n",
- _filenames[num], node, _value, node);
- free(node);
+ char nodename[64];
+
+ xmlCopyNodeName(xmid, (char *)&nodename, 64);
+ if (xmlCompareString(xmid, _value) == 0)
+ {
+ printf("%s: <%s>%s</%s>\n",
+ _filenames[num], nodename, _value, nodename);
+ }
}
}
free(xmid);
}
else if (xmid && _element)
{
+ char parentname[64];
+
+ xmlCopyNodeName(xid, (char *)&parentname, 64);
+
+ no_elements = xmlGetNumNodes(xmid, _element);
+ for (i=0; i<no_elements; i++)
+ {
+ if (xmlGetNodeNum(xid, xmid, _element, i) != 0)
+ {
+ char nodename[64];
+
+ xmlCopyNodeName(xmid, (char *)&nodename, 64);
+ if (strncasecmp((char *)&nodename, _element, 64) == 0)
+ {
+ char value[64];
+ xmlCopyString(xmid, (char *)&value, 64);
+ printf("%s: <%s> <%s>%s</%s> </%s>\n",
+ _filenames[num], parentname, nodename, value,
+ nodename, parentname);
+ }
+ }
+ }
}
else printf("Error executing xmlMarkId\n");
}