25 |
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
26 |
|
*/ |
27 |
|
|
28 |
+ |
#if defined(_WIN32) || defined(_WIN64) |
29 |
+ |
#define EZXML_NOMMAP |
30 |
+ |
#endif |
31 |
+ |
|
32 |
|
#include <stdlib.h> |
33 |
|
#include <stdio.h> |
34 |
|
#include <stdarg.h> |
42 |
|
#include <sys/stat.h> |
43 |
|
#include "ezxml.h" |
44 |
|
|
45 |
< |
#ifdef _WIN32 |
42 |
< |
#include <io.h> |
43 |
< |
#define snprintf printf_s |
44 |
< |
#define read _read |
45 |
< |
#define open _open |
46 |
< |
#define close _close |
47 |
< |
#endif |
45 |
> |
#include "platform.h" |
46 |
|
|
47 |
|
#define EZXML_WS "\t\r\n " /* whitespace */ |
48 |
|
#define EZXML_ERRL 128 /* maximum error string length */ |
73 |
|
return xml; |
74 |
|
} |
75 |
|
|
76 |
+ |
/* returns the given tag's character content or empty string if none */ |
77 |
+ |
char *ezxml_txt(ezxml_t xml) |
78 |
+ |
{ |
79 |
+ |
if (xml == NULL) |
80 |
+ |
return ""; |
81 |
+ |
return xml->txt; |
82 |
+ |
} |
83 |
+ |
|
84 |
|
/* returns the Nth tag with the same name in the same subsection or NULL if not */ |
85 |
|
/* found */ |
86 |
|
ezxml_t ezxml_idx(ezxml_t xml, int idx) |
157 |
|
char *t, fmt[EZXML_ERRL]; |
158 |
|
|
159 |
|
for (t = root->s; t < s; t++) if (*t == '\n') line++; |
160 |
< |
snprintf(fmt, EZXML_ERRL, "[error near line %d]: %s", line, err); |
160 |
> |
sprintf(fmt, "[error near line %d]: %s", line, err); |
161 |
|
|
162 |
|
va_start(ap, err); |
163 |
|
vsnprintf(root->err, EZXML_ERRL, fmt, ap); |
177 |
|
char *e, *r = s, *m = s; |
178 |
|
long b, c, d, l; |
179 |
|
|
180 |
< |
for (; *s; s++) { /* normalize line endings */ |
181 |
< |
while (*s == '\r') { |
182 |
< |
*(s++) = '\n'; |
183 |
< |
if (*s == '\n') memmove(s, (s + 1), strlen(s)); |
184 |
< |
} |
185 |
< |
} |
180 |
> |
for (; *s; s++) /* normalize line endings */ |
181 |
> |
if (*s == '\r') { |
182 |
> |
char *s2 = s+1; |
183 |
> |
do { |
184 |
> |
while (*s2 == '\r') |
185 |
> |
++s2; |
186 |
> |
*s++ = *s2; |
187 |
> |
} while (*s2++); |
188 |
> |
break; |
189 |
> |
} |
190 |
|
|
191 |
|
for (s = r; ; ) { |
192 |
|
while (*s && *s != '&' && (*s != '%' || t != '%') && !isspace(*s)) s++; |
381 |
|
if (*(s = t + strcspn(t, EZXML_WS ">")) == '>') continue; |
382 |
|
else *s = '\0'; /* null terminate tag name */ |
383 |
|
for (i = 0; root->attr[i] && strcmp(n, root->attr[i][0]); i++); |
384 |
< |
|
385 |
< |
while (*(n = ++s + strspn(s, EZXML_WS)) && *n != '>') { |
384 |
> |
++s; |
385 |
> |
while (*(n = s + strspn(s, EZXML_WS)) && *n != '>') { |
386 |
|
if (*(s = n + strcspn(n, EZXML_WS))) *s = '\0'; /* attr name */ |
387 |
|
else { ezxml_err(root, t, "malformed <!ATTLIST"); break; } |
388 |
|
|
423 |
|
root->attr[i][j + 1] = (v) ? ezxml_decode(v, root->ent, *c) |
424 |
|
: NULL; |
425 |
|
root->attr[i][j] = n; /* attribute name */ |
426 |
+ |
++s; |
427 |
|
} |
428 |
|
} |
429 |
|
else if (! strncmp(s, "<!--", 4)) s = strstr(s + 4, "-->"); /* comments */ |
929 |
|
ezxml_t ezxml_set_txt(ezxml_t xml, const char *txt) |
930 |
|
{ |
931 |
|
if (! xml) return NULL; |
932 |
+ |
if (txt == xml->txt) return xml; |
933 |
|
if (xml->flags & EZXML_TXTM) free(xml->txt); /* existing txt was malloced */ |
934 |
|
xml->flags &= ~EZXML_TXTM; |
935 |
|
xml->txt = (char *)txt; |
936 |
|
return xml; |
937 |
+ |
} |
938 |
+ |
|
939 |
+ |
/* add text to the current character content, allocating memory as needed (GW) */ |
940 |
+ |
ezxml_t ezxml_add_txt(ezxml_t xml, const char *txt) |
941 |
+ |
{ |
942 |
+ |
int len; |
943 |
+ |
if (! xml) return NULL; |
944 |
+ |
if (!*txt) return xml; |
945 |
+ |
len = strlen(xml->txt) + strlen(txt) + 1; |
946 |
+ |
if (xml->flags & EZXML_TXTM) { |
947 |
+ |
xml->txt = (char *)realloc(xml->txt, len); |
948 |
+ |
} else { |
949 |
+ |
xml->txt = strcpy((char *)malloc(len), xml->txt); |
950 |
+ |
xml->flags |= EZXML_TXTM; |
951 |
+ |
} |
952 |
+ |
strcat(xml->txt, txt); |
953 |
+ |
return xml; |
954 |
|
} |
955 |
|
|
956 |
|
/* Sets the given tag attribute or adds a new attribute if not found. A value */ |