commit c4573f3289e429b939da0b5ac6d4a950ce72466a
parent 3946a701c0d409dd235f2bcb564b5701beb7d775
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Sun, 16 Aug 2015 20:07:46 +0200
xml: change xml_parse_string to xml_parse_buf
In the parser itself allow reading '\0' in the XML itself. Add a length
parameter to specify the buffer size.
Diffstat:
2 files changed, 13 insertions(+), 11 deletions(-)
diff --git a/xml.c b/xml.c
@@ -16,21 +16,23 @@ struct xml_context_fd {
size_t offset;
};
-struct xml_context_string {
- const char *str;
+struct xml_context_buf {
+ const char *buf;
+ size_t len;
+ size_t offset;
};
static int
-xml_getnext_string(XMLParser *x)
+xml_getnext_buf(XMLParser *x)
{
- struct xml_context_string *d = (struct xml_context_string *)x->getnext_data;
+ struct xml_context_buf *d = (struct xml_context_buf *)x->getnext_data;
- if (!*(d->str))
+ if (d->offset >= d->len)
return EOF;
- return (int)*(d->str++);
+ return (int)d->buf[d->offset++];
}
-static int /* like getc(), but do some smart buffering */
+static int /* read from fd with some buffering */
xml_getnext_fd(XMLParser *x)
{
struct xml_context_fd *d = (struct xml_context_fd *)x->getnext_data;
@@ -491,11 +493,11 @@ xml_parse(XMLParser *x)
}
void
-xml_parse_string(XMLParser *x, const char *s)
+xml_parse_buf(XMLParser *x, const char *buf, size_t len)
{
- struct xml_context_string ctx = { .str = s };
+ struct xml_context_buf ctx = { .buf = buf, .len = len };
- x->getnext = xml_getnext_string;
+ x->getnext = xml_getnext_buf;
x->getnext_data = (void *)&ctx;
xml_parse(x);
}
diff --git a/xml.h b/xml.h
@@ -43,5 +43,5 @@ ssize_t xml_namedentitytostr(const char *, char *, size_t);
ssize_t xml_numericetitytostr(const char *, char *, size_t);
void xml_parse(XMLParser *);
+void xml_parse_buf(XMLParser *, const char *, size_t);
void xml_parse_fd(XMLParser *, int);
-void xml_parse_string(XMLParser *, const char *);