From c4573f3289e429b939da0b5ac6d4a950ce72466a Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Sun, 16 Aug 2015 20:07:46 +0200 Subject: 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. --- xml.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'xml.c') diff --git a/xml.c b/xml.c index 8530916..f7691cb 100644 --- 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); } -- cgit v1.2.3