From f35ab314bf8b41dea011c9acddc641340229c56a Mon Sep 17 00:00:00 2001 From: ehofman Date: Mon, 30 Jun 2008 12:00:40 +0000 Subject: [PATCH] Remove the last unnecessary alloc from the library --- utils/xmlgrep/ChangeLog | 5 +++++ utils/xmlgrep/xml.c | 24 ++++++++++++++---------- utils/xmlgrep/xmlgrep.c | 2 +- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/utils/xmlgrep/ChangeLog b/utils/xmlgrep/ChangeLog index b04370df3..2f950bc13 100644 --- a/utils/xmlgrep/ChangeLog +++ b/utils/xmlgrep/ChangeLog @@ -1,3 +1,8 @@ +30-06-2008: + * some small changes; fix some typo's en fix a small memory leak + * update the documentation in README + * remove the last unnecessary alloc + 29-06-2008: * rename xmlGet(Int/Double/String) to xmlGetNode(Int/Double/String) * add new xmlGet(Int/Double/String) functions diff --git a/utils/xmlgrep/xml.c b/utils/xmlgrep/xml.c index ccc1ccf82..fc1c040ce 100644 --- a/utils/xmlgrep/xml.c +++ b/utils/xmlgrep/xml.c @@ -1,5 +1,5 @@ -/* Copyright (c) 2007, 2008 by Adalin B.V. - * Copyright (c) 2007, 2008 by Erik Hofman +/* Copyright (c) 2007,2008 by Adalin B.V. + * Copyright (c) 2007,2008 by Erik Hofman * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -60,7 +60,6 @@ void simple_unmmap(SIMPLE_UNMMAP *); #include #include - struct _xml_id { char *start; @@ -124,6 +123,7 @@ xmlCopyNode(void *id, char *path) char *ptr, *p; size_t rlen; + rlen = strlen(path); ptr = __xmlGetNode(xid->start, xid->len, path, &rlen); if (ptr) { @@ -155,6 +155,7 @@ xmlGetNode(void *id, char *path) size_t rlen; char *ptr; + rlen = strlen(path); ptr = __xmlGetNode(xid->start, xid->len, path, &rlen); if (ptr) { @@ -183,6 +184,7 @@ xmlGetNextElement(const void *pid, void *id, char *path) if (xid->len < xpid->len) xid->start += xid->len; nlen = xpid->len - (xid->start - xpid->start); + rlen = strlen(path); ptr = __xmlGetNode(xid->start, nlen, path, &rlen); if (ptr) { @@ -220,6 +222,7 @@ xmlCompareNodeString(const void *id, const char *path, const char *s) size_t rlen; char *str; + rlen = strlen(path); str = __xmlGetNode(xid->start, xid->len, path, &rlen); if (str) ret = strncasecmp(str, s, rlen); } @@ -303,6 +306,7 @@ xmlCopyString(void *id, const char *path, char *buffer, unsigned int buflen) char *str; *buffer = 0; + rlen = strlen(path); str = __xmlGetNode(xid->start, xid->len, path, &rlen); if (str) { @@ -337,6 +341,7 @@ xmlGetNodeInt(void *id, const char *path) unsigned int rlen; char *str; + rlen = strlen(path); str = __xmlGetNode(xid->start, xid->len, path, &rlen); if (str) li = strtol(str, (char **)NULL, 10); } @@ -367,6 +372,7 @@ xmlGetNodeDouble(void *id, const char *path) unsigned int rlen; char *str; + rlen = strlen(path); str = __xmlGetNode(xid->start, xid->len, path, &rlen); if (str) d = strtod(str, (char **)NULL); @@ -406,13 +412,8 @@ xmlGetNumElements(void *id, const char *path) nname = strrchr(pathname, '/'); if (nname) { - unsigned int plen = nname-pathname; - - pname = calloc(1, plen+1); - memcpy(pname, path, plen); - + clen = nname-pathname; p = __xmlGetNode(xid->start, xid->len, pname, &clen); - free(pname); } else { @@ -454,6 +455,7 @@ __xmlCopyNode(char *start, size_t len, const char *path) char *p, *ret = 0; size_t rlen; + rlen = strlen(path); p = __xmlGetNode(start, len, path, &rlen); if (p && rlen) { @@ -482,7 +484,7 @@ __xmlGetNode(char *start, size_t len, const char *path, size_t *rlen) if (!ptr) { last_node = 1; - ptr = name+strlen(name); + ptr = name + *rlen; } plen = ptr - name; @@ -529,7 +531,9 @@ __xmlGetNode(char *start, size_t len, const char *path, size_t *rlen) } } else + { ret = __xmlGetNode(cur, len, ptr+1, rlen); + } } } } diff --git a/utils/xmlgrep/xmlgrep.c b/utils/xmlgrep/xmlgrep.c index 139f1ae8d..a75b1f8cb 100644 --- a/utils/xmlgrep/xmlgrep.c +++ b/utils/xmlgrep/xmlgrep.c @@ -45,7 +45,7 @@ show_help () void free_and_exit(int i) { - if (_root != _static_root) free(_root); + if (_root && _root != _static_root) free(_root); if (_element) free(_element); if (_value) free(_value); if (_print) free(_print);