Re: about xmlReadMemory()
Nick Wellnhofer via xml <[email protected]> Wed, 3 Mar 2021 11:12:47 +0100
| Newsgroups | gmane.comp.gnome.lib.xml.general |
|---|---|
| Message-ID | <[email protected]> |
On 03/03/2021 09:30, nicolas bats wrote:
> Hi Nick,
> I've experimented with xmlReadIO and it's cool.
> this message just to check I'm doing right:
> -I register an xmlInputReadCallback of type: size_t myCallback(void* context,
> char* buffer, int length)
> -I do my stuff in the callback and if data I use exceed the length of the
> buffer, I realloc it.
> Is this schema good?
> Do I need to set size_t as the return type of myCallback?
No, the read callback is supposed to fill the buffer with up to 'length'
bytes. Try something like:
typedef struct {
const char *ptr;
size_t remaining;
} myContext;
static int
myReadCallback(void *vcontext, char *buffer, int len) {
myContext *context = vcontext;
if (context->remaining < len)
len = context->remaining;
memcpy(buffer, context->ptr, len);
context->ptr += len;
context->remaining -= len;
return len;
}
xmlDocPtr
myReadMemory(const char *buffer, size_t size, const char *URL,
const char *encoding, int options) {
myContext context;
context.ptr = buffer;
context.remaining = size;
return xmlReadIO(myReadCallback, NULL, &context, URL, encoding, options);
}