1
0
Fork 0

* 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)
This commit is contained in:
ehofman 2008-07-21 12:32:22 +00:00
parent 83c2f96153
commit d015bfe72f
5 changed files with 139 additions and 90 deletions

View file

@ -1,3 +1,10 @@
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
@ -9,7 +16,7 @@
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
@ -17,7 +24,7 @@
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

View file

@ -7,8 +7,8 @@ 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
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.
@ -25,9 +25,8 @@ 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;
@ -63,9 +62,9 @@ void *xmlCopyNode(void *, const char *);
# 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);
@ -75,8 +74,14 @@ void *xmlCopyNode(void *, const char *);
# 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.
@ -90,6 +95,7 @@ void *xmlGetNextElement(const void *, void *, const char *);
long int xmlGetInt(void *);
double xmlGetDouble(void *);
char *xmlGetString(void *);
size_t xmlCopyString(void *, char *, const size_t);
int xmlCompareString(const void *, const char *);
#
@ -103,5 +109,5 @@ 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 *);

View file

@ -76,7 +76,7 @@ static char *__xmlSkipComment(const char *, size_t);
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) { \
@ -135,7 +135,7 @@ xmlClose(void *id)
}
void *
xmlCopyNode(void *id, char *path)
xmlCopyNode(const void *id, char *path)
{
struct _xml_id *xsid = 0;
@ -170,7 +170,7 @@ xmlCopyNode(void *id, char *path)
}
void *
xmlGetNode(void *id, char *path)
xmlGetNode(const void *id, char *path)
{
struct _xml_id *xsid = 0;
@ -201,23 +201,25 @@ xmlGetNode(void *id, char *path)
}
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;
@ -227,7 +229,10 @@ xmlCopyNodeName(void *id, const char *buf, size_t len)
slen = len-1;
if (slen > xid->nlen) slen = xid->nlen;
memcpy((char *)buf, xid->name, slen);
*((char *)buf + slen) = 0;
}
return slen;
}
void *
@ -316,7 +321,7 @@ xmlCompareNodeString(const void *id, const char *path, const char *s)
}
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;
@ -348,7 +353,7 @@ xmlGetNodeString(void *id, const char *path)
}
char *
xmlGetString(void *id)
xmlGetString(const void *id)
{
struct _xml_id *xid = (struct _xml_id *)id;
char *str = 0;
@ -381,7 +386,7 @@ xmlGetString(void *id)
}
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;
@ -413,7 +418,7 @@ xmlCopyString(void *id, char *buf, size_t len)
}
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;
@ -451,7 +456,7 @@ xmlCopyNodeString(void *id, const char *path, char *buffer, size_t buflen)
}
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;
@ -476,7 +481,7 @@ xmlGetNodeInt(void *id, const char *path)
}
long int
xmlGetInt(void *id)
xmlGetInt(const void *id)
{
struct _xml_id *xid = (struct _xml_id *)id;
long int li = 0;
@ -491,7 +496,7 @@ xmlGetInt(void *id)
}
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;
@ -516,7 +521,7 @@ xmlGetNodeDouble(void *id, const char *path)
}
double
xmlGetDouble(void *id)
xmlGetDouble(const void *id)
{
struct _xml_id *xid = (struct _xml_id *)id;
double d = 0.0;
@ -532,7 +537,7 @@ xmlGetDouble(void *id)
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;
@ -575,7 +580,7 @@ xmlGetNumNodes(void *id, const char *path)
}
void *
xmlMarkId(void *id)
xmlMarkId(const void *id)
{
struct _xml_id *xmid = 0;
@ -585,7 +590,6 @@ xmlMarkId(void *id)
if (xmid)
{
memcpy(xmid, id, sizeof(struct _xml_id));
xmid->nlen = 0;
}
}
@ -661,6 +665,7 @@ __xmlGetNode(const char *start, size_t *len, char **name, size_t *rlen, int *nod
{
char *new, *cur, *ne, *ret = 0;
size_t restlen, elementlen;
size_t retlen = 0;
int found, num;
void *element;
@ -715,6 +720,7 @@ __xmlGetNode(const char *start, size_t *len, char **name, size_t *rlen, int *nod
new = __xml_memncasecmp(cur, &restlen, &element, &elementlen);
if (new)
{
retlen = elementlen;
if (found == num ) ret = new+1;
}
else
@ -722,7 +728,6 @@ __xmlGetNode(const char *start, size_t *len, char **name, size_t *rlen, int *nod
new = cur+elementlen;
if (new >= ne) return 0;
element = (char *)*name;
elementlen = *rlen;
}
/* restlen -= new-cur; not necessary because of __xml_memncasecmp */
@ -774,7 +779,7 @@ __xmlGetNode(const char *start, size_t *len, char **name, size_t *rlen, int *nod
else return 0;
}
*rlen = elementlen;
*rlen = retlen;
*nodenum = found;
return ret;
@ -881,7 +886,7 @@ __xml_memmem(const void *haystack, size_t haystacklen,
#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;
@ -903,7 +908,6 @@ __xml_memncasecmp(void *haystack, size_t *haystacklen,
while ((hs < he) && (*hs != ' ') && (*hs != '>')) hs++;
*needle = (void *)haystack;
*needlelen = hs - (char *)haystack;
while ((hs < he) && (*hs != '>')) hs++;
rptr = hs;
}

View file

@ -41,16 +41,16 @@ void *xmlOpen(const char *);
*
* @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
@ -62,25 +62,25 @@ void *xmlGetNode(const void *, const char *);
* 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
@ -88,9 +88,9 @@ char *xmlGetNodeName(void *);
* @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
@ -99,10 +99,10 @@ size_t xmlCopyNodeName(void *, const char *, size_t);
* @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
@ -115,22 +115,22 @@ void *xmlGetNodeNum(const void *, void *, const char *, int);
/**
* 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
@ -138,50 +138,50 @@ int xmlCompareString(const void *, const char *);
* @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 *);
@ -189,37 +189,45 @@ 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 */

View file

@ -1,4 +1,5 @@
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
#include <string.h>
@ -166,13 +167,11 @@ void walk_the_tree(size_t num, void *xid, char *tree)
{
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);
@ -182,19 +181,44 @@ void walk_the_tree(size_t num, void *xid, char *tree)
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");
}