summaryrefslogtreecommitdiff
path: root/xml.c
diff options
context:
space:
mode:
authorHiltjo Posthuma <hiltjo@codemadness.org>2015-07-28 21:56:46 +0200
committerHiltjo Posthuma <hiltjo@codemadness.org>2015-07-28 21:56:46 +0200
commit8e21dabb94089349e4fd5589f9847b29f7a2704c (patch)
tree95ec6d7b16c0c539b4c62eafc72f44d7b97239bc /xml.c
parentb17d0316dc51c254d503e7a095a5883f1848c7f6 (diff)
improve code-style and consistency
Diffstat (limited to 'xml.c')
-rw-r--r--xml.c228
1 files changed, 114 insertions, 114 deletions
diff --git a/xml.c b/xml.c
index 6569e33..79f30b5 100644
--- a/xml.c
+++ b/xml.c
@@ -33,20 +33,20 @@ xmlparser_fd_getnext(XMLParser *x)
ssize_t r;
/* previous read error was set */
- if(x->readerrno)
+ if (x->readerrno)
return EOF;
- if(x->readoffset >= x->readlastbytes) {
+ if (x->readoffset >= x->readlastbytes) {
x->readoffset = 0;
again:
r = read(x->fd, x->readbuf, sizeof(x->readbuf));
- if(r == -1) {
- if(errno == EINTR)
+ if (r == -1) {
+ if (errno == EINTR)
goto again;
x->readerrno = errno;
x->readlastbytes = 0;
return EOF;
- } else if(!r) {
+ } else if (!r) {
return EOF;
}
x->readlastbytes = r;
@@ -66,90 +66,90 @@ xmlparser_parseattrs(XMLParser *x)
size_t namelen = 0, valuelen;
int c, endsep, endname = 0;
- while((c = xmlparser_getnext(x)) != EOF) {
- if(isspace(c)) { /* TODO: simplify endname ? */
- if(namelen)
+ while ((c = xmlparser_getnext(x)) != EOF) {
+ if (isspace(c)) { /* TODO: simplify endname ? */
+ if (namelen)
endname = 1;
continue;
}
- if(c == '?')
+ if (c == '?')
; /* ignore */
- else if(c == '=') {
+ else if (c == '=') {
x->name[namelen] = '\0';
- } else if(namelen && ((endname && isalpha(c)) || (c == '>' || c == '/'))) {
+ } else if (namelen && ((endname && isalpha(c)) || (c == '>' || c == '/'))) {
/* attribute without value */
x->name[namelen] = '\0';
- if(x->xmlattrstart)
+ if (x->xmlattrstart)
x->xmlattrstart(x, x->tag, x->taglen, x->name, namelen);
- if(x->xmlattr)
+ if (x->xmlattr)
x->xmlattr(x, x->tag, x->taglen, x->name, namelen, "", 0);
- if(x->xmlattrend)
+ if (x->xmlattrend)
x->xmlattrend(x, x->tag, x->taglen, x->name, namelen);
endname = 0;
x->name[0] = c;
namelen = 1;
- } else if(namelen && (c == '\'' || c == '"')) {
+ } else if (namelen && (c == '\'' || c == '"')) {
/* attribute with value */
endsep = c; /* c is end separator */
- if(x->xmlattrstart)
+ if (x->xmlattrstart)
x->xmlattrstart(x, x->tag, x->taglen, x->name, namelen);
- for(valuelen = 0; (c = xmlparser_getnext(x)) != EOF;) {
- if(c == '&') { /* entities */
+ for (valuelen = 0; (c = xmlparser_getnext(x)) != EOF;) {
+ if (c == '&') { /* entities */
x->data[valuelen] = '\0';
/* call data function with data before entity if there is data */
- if(valuelen && x->xmlattr)
+ if (valuelen && x->xmlattr)
x->xmlattr(x, x->tag, x->taglen, x->name, namelen, x->data, valuelen);
x->data[0] = c;
valuelen = 1;
- while((c = xmlparser_getnext(x)) != EOF) {
- if(c == endsep)
+ while ((c = xmlparser_getnext(x)) != EOF) {
+ if (c == endsep)
break;
- if(valuelen < sizeof(x->data) - 1)
+ if (valuelen < sizeof(x->data) - 1)
x->data[valuelen++] = c;
else {
/* TODO: entity too long? this should be very strange. */
x->data[valuelen] = '\0';
- if(x->xmlattr)
+ if (x->xmlattr)
x->xmlattr(x, x->tag, x->taglen, x->name, namelen, x->data, valuelen);
valuelen = 0;
break;
}
- if(c == ';') {
+ if (c == ';') {
x->data[valuelen] = '\0';
- if(x->xmlattrentity)
+ if (x->xmlattrentity)
x->xmlattrentity(x, x->tag, x->taglen, x->name, namelen, x->data, valuelen);
valuelen = 0;
break;
}
}
- } else if(c != endsep) {
- if(valuelen < sizeof(x->data) - 1) {
+ } else if (c != endsep) {
+ if (valuelen < sizeof(x->data) - 1) {
x->data[valuelen++] = c;
} else {
x->data[valuelen] = '\0';
- if(x->xmlattr)
+ if (x->xmlattr)
x->xmlattr(x, x->tag, x->taglen, x->name, namelen, x->data, valuelen);
x->data[0] = c;
valuelen = 1;
}
}
- if(c == endsep) {
+ if (c == endsep) {
x->data[valuelen] = '\0';
- if(x->xmlattr)
+ if (x->xmlattr)
x->xmlattr(x, x->tag, x->taglen, x->name, namelen, x->data, valuelen);
- if(x->xmlattrend)
+ if (x->xmlattrend)
x->xmlattrend(x, x->tag, x->taglen, x->name, namelen);
break;
}
}
namelen = 0;
endname = 0;
- } else if(namelen < sizeof(x->name) - 1) {
+ } else if (namelen < sizeof(x->name) - 1) {
x->name[namelen++] = c;
}
- if(c == '>') {
+ if (c == '>') {
break;
- } else if(c == '/') {
+ } else if (c == '/') {
x->isshorttag = 1;
namelen = 0;
x->name[0] = '\0';
@@ -165,22 +165,22 @@ xmlparser_parsecomment(XMLParser *x)
char tmp[4];
int c;
- if(x->xmlcommentstart)
+ if (x->xmlcommentstart)
x->xmlcommentstart(x);
- while((c = xmlparser_getnext(x)) != EOF) {
- if(c == end[i]) {
- if(end[++i] == '\0') { /* end */
+ while ((c = xmlparser_getnext(x)) != EOF) {
+ if (c == end[i]) {
+ if (end[++i] == '\0') { /* end */
x->data[datalen] = '\0';
- if(x->xmlcomment)
+ if (x->xmlcomment)
x->xmlcomment(x, x->data, datalen);
- if(x->xmlcommentend)
+ if (x->xmlcommentend)
x->xmlcommentend(x);
return;
}
- } else if(i) {
- if(x->xmlcomment) {
+ } else if (i) {
+ if (x->xmlcomment) {
x->data[datalen] = '\0';
- if(datalen)
+ if (datalen)
x->xmlcomment(x, x->data, datalen);
memcpy(tmp, end, i);
tmp[i] = '\0';
@@ -189,11 +189,11 @@ xmlparser_parsecomment(XMLParser *x)
i = 0;
x->data[0] = c;
datalen = 1;
- } else if(datalen < sizeof(x->data) - 1) {
+ } else if (datalen < sizeof(x->data) - 1) {
x->data[datalen++] = c;
} else {
x->data[datalen] = '\0';
- if(x->xmlcomment)
+ if (x->xmlcomment)
x->xmlcomment(x, x->data, datalen);
x->data[0] = c;
datalen = 1;
@@ -209,22 +209,22 @@ xmlparser_parsecdata(XMLParser *x)
char tmp[4];
int c;
- if(x->xmlcdatastart)
+ if (x->xmlcdatastart)
x->xmlcdatastart(x);
- while((c = xmlparser_getnext(x)) != EOF) {
- if(c == end[i]) {
- if(end[++i] == '\0') { /* end */
+ while ((c = xmlparser_getnext(x)) != EOF) {
+ if (c == end[i]) {
+ if (end[++i] == '\0') { /* end */
x->data[datalen] = '\0';
- if(x->xmlcdata)
+ if (x->xmlcdata)
x->xmlcdata(x, x->data, datalen);
- if(x->xmlcdataend)
+ if (x->xmlcdataend)
x->xmlcdataend(x);
return;
}
- } else if(i) {
+ } else if (i) {
x->data[datalen] = '\0';
- if(x->xmlcdata) {
- if(datalen)
+ if (x->xmlcdata) {
+ if (datalen)
x->xmlcdata(x, x->data, datalen);
memcpy(tmp, end, i);
tmp[i] = '\0';
@@ -233,11 +233,11 @@ xmlparser_parsecdata(XMLParser *x)
i = 0;
x->data[0] = c;
datalen = 1;
- } else if(datalen < sizeof(x->data) - 1) {
+ } else if (datalen < sizeof(x->data) - 1) {
x->data[datalen++] = c;
} else {
x->data[datalen] = '\0';
- if(x->xmlcdata)
+ if (x->xmlcdata)
x->xmlcdata(x, x->data, datalen);
x->data[0] = c;
datalen = 1;
@@ -248,19 +248,19 @@ xmlparser_parsecdata(XMLParser *x)
int
xml_codepointtoutf8(uint32_t cp, uint32_t *utf)
{
- if(cp >= 0x10000) {
+ if (cp >= 0x10000) {
/* 4 bytes */
*utf = 0xf0808080 | ((cp & 0xfc0000) << 6) |
((cp & 0x3f000) << 4) | ((cp & 0xfc0) << 2) |
(cp & 0x3f);
return 4;
- } else if(cp >= 0x00800) {
+ } else if (cp >= 0x00800) {
/* 3 bytes */
*utf = 0xe08080 |
((cp & 0x3f000) << 4) | ((cp & 0xfc0) << 2) |
(cp & 0x3f);
return 3;
- } else if(cp >= 0x80) {
+ } else if (cp >= 0x80) {
/* 2 bytes */
*utf = 0xc080 |
((cp & 0xfc0) << 2) | (cp & 0x3f);
@@ -276,16 +276,16 @@ xml_namedentitytostr(const char *e, char *buf, size_t bufsiz)
size_t i;
/* buffer is too small */
- if(bufsiz < 2)
+ if (bufsiz < 2)
return -1;
/* doesn't start with &: can't match */
- if(*e != '&')
+ if (*e != '&')
return 0;
- for(i = 0; sizeof(entities) / sizeof(*entities); i++) {
+ for (i = 0; sizeof(entities) / sizeof(*entities); i++) {
/* NOTE: compares max 6 chars */
- if(!strncasecmp(e, entities[i].entity, 6)) {
+ if (!strncasecmp(e, entities[i].entity, 6)) {
buf[0] = entities[i].c;
buf[1] = '\0';
return 1;
@@ -302,28 +302,28 @@ xml_numericentitytostr(const char *e, char *buf, size_t bufsiz)
char *end;
/* buffer is too small */
- if(bufsiz < 5)
+ if (bufsiz < 5)
return -1;
/* not a numeric entity */
- if(!(e[0] == '&' && e[1] == '#'))
+ if (!(e[0] == '&' && e[1] == '#'))
return 0;
/* e[1] == '#', numeric / hexadecimal entity */
e += 2; /* skip "&#" */
errno = 0;
/* hex (16) or decimal (10) */
- if(*e == 'x')
+ if (*e == 'x')
l = strtoul(e + 1, &end, 16);
else
l = strtoul(e, &end, 10);
/* invalid value or not a well-formed entity */
- if(errno != 0 || (*end != '\0' && *end != ';'))
+ if (errno != 0 || (*end != '\0' && *end != ';'))
return 0;
- if(!(len = xml_codepointtoutf8(l, &cp)))
+ if (!(len = xml_codepointtoutf8(l, &cp)))
return 0;
/* make string */
- for(b = 0; b < len; b++)
+ for (b = 0; b < len; b++)
buf[b] = (cp >> (8 * (len - 1 - b))) & 0xff;
buf[len] = '\0';
return (ssize_t)len;
@@ -335,13 +335,13 @@ ssize_t
xml_entitytostr(const char *e, char *buf, size_t bufsiz)
{
/* buffer is too small */
- if(bufsiz < 5)
+ if (bufsiz < 5)
return -1;
/* doesn't start with & */
- if(e[0] != '&')
+ if (e[0] != '&')
return 0;
/* named entity */
- if(e[1] != '#')
+ if (e[1] != '#')
return xml_namedentitytostr(e, buf, bufsiz);
else /* numeric entity */
return xml_numericentitytostr(e, buf, bufsiz);
@@ -353,26 +353,26 @@ xmlparser_parse(XMLParser *x)
int c, ispi;
size_t datalen, tagdatalen, taglen;
- while((c = xmlparser_getnext(x)) != EOF && c != '<'); /* skip until < */
+ while ((c = xmlparser_getnext(x)) != EOF && c != '<'); /* skip until < */
- while(c != EOF) {
- if(c == '<') { /* parse tag */
- if((c = xmlparser_getnext(x)) == EOF)
+ while (c != EOF) {
+ if (c == '<') { /* parse tag */
+ if ((c = xmlparser_getnext(x)) == EOF)
return;
x->tag[0] = '\0';
x->taglen = 0;
- if(c == '!') { /* cdata and comments */
- for(tagdatalen = 0; (c = xmlparser_getnext(x)) != EOF;) {
- if(tagdatalen <= strlen("[CDATA[")) /* if(d < sizeof(x->data)) */
+ if (c == '!') { /* cdata and comments */
+ for (tagdatalen = 0; (c = xmlparser_getnext(x)) != EOF;) {
+ if (tagdatalen <= strlen("[CDATA[")) /* if (d < sizeof(x->data)) */
x->data[tagdatalen++] = c; /* TODO: prevent overflow */
- if(c == '>')
+ if (c == '>')
break;
- else if(c == '-' && tagdatalen == strlen("--") &&
+ else if (c == '-' && tagdatalen == strlen("--") &&
(x->data[0] == '-')) { /* comment */
xmlparser_parsecomment(x);
break;
- } else if(c == '[') {
- if(tagdatalen == strlen("[CDATA[") &&
+ } else if (c == '[') {
+ if (tagdatalen == strlen("[CDATA[") &&
x->data[1] == 'C' && x->data[2] == 'D' &&
x->data[3] == 'A' && x->data[4] == 'T' &&
x->data[5] == 'A' && x->data[6] == '[') { /* CDATA */
@@ -382,92 +382,92 @@ xmlparser_parse(XMLParser *x)
} else {
/* TODO ? */
/* markup declaration section */
- while((c = xmlparser_getnext(x)) != EOF && c != ']');
+ while ((c = xmlparser_getnext(x)) != EOF && c != ']');
#endif
}
}
}
} else { /* normal tag (open, short open, close), processing instruction. */
- if(isspace(c))
- while((c = xmlparser_getnext(x)) != EOF && isspace(c));
- if(c == EOF)
+ if (isspace(c))
+ while ((c = xmlparser_getnext(x)) != EOF && isspace(c));
+ if (c == EOF)
return;
x->tag[0] = c;
ispi = (c == '?') ? 1 : 0;
x->isshorttag = ispi;
taglen = 1;
- while((c = xmlparser_getnext(x)) != EOF) {
- if(c == '/') /* TODO: simplify short tag? */
+ while ((c = xmlparser_getnext(x)) != EOF) {
+ if (c == '/') /* TODO: simplify short tag? */
x->isshorttag = 1; /* short tag */
- else if(c == '>' || isspace(c)) {
+ else if (c == '>' || isspace(c)) {
x->tag[taglen] = '\0';
- if(x->tag[0] == '/') { /* end tag, starts with </ */
+ if (x->tag[0] == '/') { /* end tag, starts with </ */
x->taglen = --taglen; /* len -1 because of / */
- if(taglen && x->xmltagend)
+ if (taglen && x->xmltagend)
x->xmltagend(x, &(x->tag)[1], x->taglen, 0);
} else {
x->taglen = taglen;
/* start tag */
- if(x->xmltagstart)
+ if (x->xmltagstart)
x->xmltagstart(x, x->tag, x->taglen);
- if(isspace(c))
+ if (isspace(c))
xmlparser_parseattrs(x);
- if(x->xmltagstartparsed)
+ if (x->xmltagstartparsed)
x->xmltagstartparsed(x, x->tag, x->taglen, x->isshorttag);
}
/* call tagend for shortform or processing instruction */
- if((x->isshorttag || ispi) && x->xmltagend)
+ if ((x->isshorttag || ispi) && x->xmltagend)
x->xmltagend(x, x->tag, x->taglen, 1);
break;
- } else if(taglen < sizeof(x->tag) - 1)
+ } else if (taglen < sizeof(x->tag) - 1)
x->tag[taglen++] = c;
}
}
} else {
/* parse tag data */
datalen = 0;
- if(x->xmldatastart)
+ if (x->xmldatastart)
x->xmldatastart(x);
- while((c = xmlparser_getnext(x)) != EOF) {
- if(c == '&') {
- if(datalen) {
+ while ((c = xmlparser_getnext(x)) != EOF) {
+ if (c == '&') {
+ if (datalen) {
x->data[datalen] = '\0';
- if(x->xmldata)
+ if (x->xmldata)
x->xmldata(x, x->data, datalen);
}
x->data[0] = c;
datalen = 1;
- while((c = xmlparser_getnext(x)) != EOF) {
- if(c == '<')
+ while ((c = xmlparser_getnext(x)) != EOF) {
+ if (c == '<')
break;
- if(datalen < sizeof(x->data) - 1)
+ if (datalen < sizeof(x->data) - 1)
x->data[datalen++] = c;
- if(isspace(c))
+ if (isspace(c))
break;
- else if(c == ';') {
+ else if (c == ';') {
x->data[datalen] = '\0';
- if(x->xmldataentity)
+ if (x->xmldataentity)
x->xmldataentity(x, x->data, datalen);
datalen = 0;
break;
}
}
- } else if(c != '<') {
- if(datalen < sizeof(x->data) - 1) {
+ } else if (c != '<') {
+ if (datalen < sizeof(x->data) - 1) {
x->data[datalen++] = c;
} else {
x->data[datalen] = '\0';
- if(x->xmldata)
+ if (x->xmldata)
x->xmldata(x, x->data, datalen);
x->data[0] = c;
datalen = 1;
}
}
- if(c == '<') {
+ if (c == '<') {
x->data[datalen] = '\0';
- if(x->xmldata && datalen)
+ if (x->xmldata && datalen)
x->xmldata(x, x->data, datalen);
- if(x->xmldataend)
+ if (x->xmldataend)
x->xmldataend(x);
break;
}