--- ray/src/common/ezxml.c 2011/06/01 00:29:40 2.2 +++ ray/src/common/ezxml.c 2016/03/06 01:13:17 2.9 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: ezxml.c,v 2.2 2011/06/01 00:29:40 greg Exp $"; +static const char RCSid[] = "$Id: ezxml.c,v 2.9 2016/03/06 01:13:17 schorsch Exp $"; #endif /* ezxml.c * @@ -25,6 +25,10 @@ static const char RCSid[] = "$Id: ezxml.c,v 2.2 2011/0 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#if defined(_WIN32) || defined(_WIN64) +#define EZXML_NOMMAP +#endif + #include #include #include @@ -38,6 +42,8 @@ static const char RCSid[] = "$Id: ezxml.c,v 2.2 2011/0 #include #include "ezxml.h" +#include "platform.h" + #define EZXML_WS "\t\r\n " /* whitespace */ #define EZXML_ERRL 128 /* maximum error string length */ @@ -67,6 +73,14 @@ ezxml_t ezxml_child(ezxml_t xml, const char *name) return xml; } +/* returns the given tag's character content or empty string if none */ +char *ezxml_txt(ezxml_t xml) +{ + if (xml == NULL) + return ""; + return xml->txt; +} + /* returns the Nth tag with the same name in the same subsection or NULL if not */ /* found */ ezxml_t ezxml_idx(ezxml_t xml, int idx) @@ -143,7 +157,7 @@ ezxml_t ezxml_err(ezxml_root_t root, char *s, const ch char *t, fmt[EZXML_ERRL]; for (t = root->s; t < s; t++) if (*t == '\n') line++; - snprintf(fmt, EZXML_ERRL, "[error near line %d]: %s", line, err); + sprintf(fmt, "[error near line %d]: %s", line, err); va_start(ap, err); vsnprintf(root->err, EZXML_ERRL, fmt, ap); @@ -163,12 +177,16 @@ char *ezxml_decode(char *s, char **ent, char t) char *e, *r = s, *m = s; long b, c, d, l; - for (; *s; s++) { /* normalize line endings */ - while (*s == '\r') { - *(s++) = '\n'; - if (*s == '\n') memmove(s, (s + 1), strlen(s)); - } - } + for (; *s; s++) /* normalize line endings */ + if (*s == '\r') { + char *s2 = s+1; + do { + while (*s2 == '\r') + ++s2; + *s++ = *s2; + } while (*s2++); + break; + } for (s = r; ; ) { while (*s && *s != '&' && (*s != '%' || t != '%') && !isspace(*s)) s++; @@ -363,8 +381,8 @@ short ezxml_internal_dtd(ezxml_root_t root, char *s, s if (*(s = t + strcspn(t, EZXML_WS ">")) == '>') continue; else *s = '\0'; /* null terminate tag name */ for (i = 0; root->attr[i] && strcmp(n, root->attr[i][0]); i++); - - while (*(n = ++s + strspn(s, EZXML_WS)) && *n != '>') { + ++s; + while (*(n = s + strspn(s, EZXML_WS)) && *n != '>') { if (*(s = n + strcspn(n, EZXML_WS))) *s = '\0'; /* attr name */ else { ezxml_err(root, t, "malformed attr[i][j + 1] = (v) ? ezxml_decode(v, root->ent, *c) : NULL; root->attr[i][j] = n; /* attribute name */ + ++s; } } else if (! strncmp(s, ""); /* comments */ @@ -910,10 +929,28 @@ ezxml_t ezxml_add_child(ezxml_t xml, const char *name, ezxml_t ezxml_set_txt(ezxml_t xml, const char *txt) { if (! xml) return NULL; + if (txt == xml->txt) return xml; if (xml->flags & EZXML_TXTM) free(xml->txt); /* existing txt was malloced */ xml->flags &= ~EZXML_TXTM; xml->txt = (char *)txt; return xml; +} + +/* add text to the current character content, allocating memory as needed (GW) */ +ezxml_t ezxml_add_txt(ezxml_t xml, const char *txt) +{ + int len; + if (! xml) return NULL; + if (!*txt) return xml; + len = strlen(xml->txt) + strlen(txt) + 1; + if (xml->flags & EZXML_TXTM) { + xml->txt = (char *)realloc(xml->txt, len); + } else { + xml->txt = strcpy((char *)malloc(len), xml->txt); + xml->flags |= EZXML_TXTM; + } + strcat(xml->txt, txt); + return xml; } /* Sets the given tag attribute or adds a new attribute if not found. A value */