From 67ca3c0307c873d49327ac10f753049595e54afd Mon Sep 17 00:00:00 2001 From: ehofman Date: Tue, 1 Jul 2008 12:15:46 +0000 Subject: [PATCH] * fix a problem caused by removing the last unnecessary alloc * strip leading-, and trailing spaces from the string before comparing * fix a problem where trailing spaces weren't removed --- utils/xmlgrep/ChangeLog | 13 +++++--- utils/xmlgrep/README | 3 +- utils/xmlgrep/xml.c | 69 +++++++++++++++++++++++++++++------------ 3 files changed, 61 insertions(+), 24 deletions(-) diff --git a/utils/xmlgrep/ChangeLog b/utils/xmlgrep/ChangeLog index 2f950bc13..6af8c6f97 100644 --- a/utils/xmlgrep/ChangeLog +++ b/utils/xmlgrep/ChangeLog @@ -1,5 +1,10 @@ +01-07-2008 + * fix a problem caused by removing the last unnecessary alloc + * strip leading-, and trailing spaces from the string before comparing + * fix a problem where trailing spaces weren't removed + 30-06-2008: - * some small changes; fix some typo's en fix a small memory leak + * some small changes; fix some typo's and fix a small memory leak * update the documentation in README * remove the last unnecessary alloc @@ -11,9 +16,9 @@ * add a README file with short examples of various functions 27-06-2008: - * removed some memorly allocation in xmlGetNode and XMLGetNextElement - * use the filesize for mmap and remove the root node from the xml-id - * rearange xmlGetNode to work with complicated xml files + * removed some memory allocation in xmlGetNode and XMLGetNextElement + * use the file-size for mmap and remove the root node from the xml-id + * rearrange xmlGetNode to work with complicated xml files * add the xmlMarkId function to save the id before using xmlGetNextElement * speed up xmlGetNextId diff --git a/utils/xmlgrep/README b/utils/xmlgrep/README index 81757de9d..4a35d1d0c 100644 --- a/utils/xmlgrep/README +++ b/utils/xmlgrep/README @@ -26,7 +26,8 @@ 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 causing improved searching speed. +(parent)node which limits the searching area resulting in improved searching +speed. { void *xnid; char *s; diff --git a/utils/xmlgrep/xml.c b/utils/xmlgrep/xml.c index 6face341d..6a96fcdf9 100644 --- a/utils/xmlgrep/xml.c +++ b/utils/xmlgrep/xml.c @@ -205,7 +205,17 @@ xmlCompareString(const void *id, const char *s) if (xid && xid->len && s && (strlen(s) > 0)) { - ret = strncasecmp(xid->start, s, xid->len); + char *ps, *pe; + + ps = xid->start; + pe = ps + xid->len; + pe--; + + while ((psps) && isspace(*pe)) pe--; + pe++; + + ret = strncasecmp(ps, s, pe-ps); } return ret; @@ -219,12 +229,21 @@ xmlCompareNodeString(const void *id, const char *path, const char *s) if (xid && xid->len && path && s && (strlen(s) > 0)) { + char *str, *ps, *pe; size_t rlen; - char *str; rlen = strlen(path); str = __xmlGetNode(xid->start, xid->len, path, &rlen); - if (str) ret = strncasecmp(str, s, rlen); + + ps = str; + pe = ps + rlen; + pe--; + + while ((psps) && isspace(*pe)) pe--; + pe++; + + if (str) ret = strncasecmp(pe, s, pe-ps); } return ret; @@ -241,19 +260,20 @@ xmlGetNodeString(void *id, const char *path) str = __xmlCopyNode(xid->start, xid->len, path); if (str) { - char *ps, *pe, *pem; + char *ps, *pe, *pend; int slen; slen = strlen(str); ps = str; - pe = pem = ps+slen; + pend = ps+slen; + pe = pend-1; while ((psps) && isspace(*pe)) pe--; - if (pestr) && slen) memmove(str, ps, slen+1); + if (slen && (ps>str)) memmove(str, ps, slen); else if (!slen) *str = 0; } } @@ -272,20 +292,21 @@ xmlGetString(void *id) str = malloc(xid->len+1); if (str) { - char *ps, *pe, *pem; + char *ps, *pe, *pend; int slen; slen = xid->len; memcpy(str, xid->start, slen); - *(str+slen) = 0; ps = str; - pe = pem = ps+slen; + pend = ps+slen; + pe = pend-1; + *pend = 0; while ((psps) && isspace(*pe)) pe--; - if (pestr) && slen) memmove(str, ps, slen+1); else if (!slen) *str = 0; @@ -404,7 +425,7 @@ xmlGetNumElements(void *id, const char *path) { unsigned int clen; char *p, *pathname; - char *pname, *nname; + char *nname; pathname = (char *)path; if (*path == '/') pathname++; @@ -413,7 +434,7 @@ xmlGetNumElements(void *id, const char *path) if (nname) { clen = nname-pathname; - p = __xmlGetNode(xid->start, xid->len, path, &clen); + p = __xmlGetNode(xid->start, xid->len, pathname, &clen); } else { @@ -471,22 +492,31 @@ __xmlGetNode(char *start, size_t len, const char *path, size_t *rlen) { char *ret = 0; - if (len) + if (len && rlen && *rlen) { char last_node = 0; char *ptr, *name; - int plen; + size_t plen, slen; + slen = *rlen; name = (char *)path; - if (*name == '/') name++; /* skip the leading '/' character */ + if (*name == '/') + { + name++; /* skip the leading '/' character */ + slen--; + } ptr = strchr(name, '/'); if (!ptr) { last_node = 1; - ptr = name + *rlen; + plen = slen; + } + else + { + plen = ptr++ - name; + slen -= (ptr - name); } - plen = ptr - name; if (plen) { @@ -532,7 +562,8 @@ __xmlGetNode(char *start, size_t len, const char *path, size_t *rlen) } else { - ret = __xmlGetNode(cur, len, ptr+1, rlen); + *rlen = slen; + ret = __xmlGetNode(cur, len, ptr, rlen); } } }