From d015bfe72f9a967738482bd33feec450e2e3b513 Mon Sep 17 00:00:00 2001 From: ehofman Date: Mon, 21 Jul 2008 12:32:22 +0000 Subject: [PATCH] * 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) --- utils/xmlgrep/ChangeLog | 11 ++++- utils/xmlgrep/README | 26 +++++++----- utils/xmlgrep/xml.c | 50 +++++++++++----------- utils/xmlgrep/xml.h | 92 ++++++++++++++++++++++------------------- utils/xmlgrep/xmlgrep.c | 50 ++++++++++++++++------ 5 files changed, 139 insertions(+), 90 deletions(-) diff --git a/utils/xmlgrep/ChangeLog b/utils/xmlgrep/ChangeLog index 0b4af6600..73b6864ec 100644 --- a/utils/xmlgrep/ChangeLog +++ b/utils/xmlgrep/ChangeLog @@ -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 diff --git a/utils/xmlgrep/README b/utils/xmlgrep/README index 4a35d1d0c..a23050bcc 100644 --- a/utils/xmlgrep/README +++ b/utils/xmlgrep/README @@ -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; inlen+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; } diff --git a/utils/xmlgrep/xml.h b/utils/xmlgrep/xml.h index 387931f5f..e5b2d10e5 100644 --- a/utils/xmlgrep/xml.h +++ b/utils/xmlgrep/xml.h @@ -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 */ diff --git a/utils/xmlgrep/xmlgrep.c b/utils/xmlgrep/xmlgrep.c index 8288de3a9..90a55e439 100644 --- a/utils/xmlgrep/xmlgrep.c +++ b/utils/xmlgrep/xmlgrep.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -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\n", - _filenames[num], _print, value, _print); - free(value); - } + char value[64]; + + xmlCopyString(xmid, (char *)&value, 64); + printf("%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%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\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 <%s>%s \n", + _filenames[num], parentname, nodename, value, + nodename, parentname); + } + } + } } else printf("Error executing xmlMarkId\n"); } -- 2.39.5