Re: about xmlReadMemory()

nicolas bats via xml <[email protected]> Wed, 3 Mar 2021 11:27:50 +0100
Newsgroups gmane.comp.gnome.lib.xml.general
Message-ID <CAPb79oUVrJ_4KFT8fA7qoFRrv8w3GkTVVZiUg5KguJp+cQyWSw@mail.gmail.com>
--===============9075537196922693361==
Content-Type: multipart/alternative; boundary="0000000000002e6c0805bc9f4e86"

--0000000000002e6c0805bc9f4e86
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

thank you so much Nick,
it's crystal clear now :)

++
nicolas

Le mer. 3 mars 2021 =C3=A0 11:12, Nick Wellnhofer <[email protected]> a =
=C3=A9crit :

> 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 =3D vcontext;
>
>      if (context->remaining < len)
>          len =3D context->remaining;
>      memcpy(buffer, context->ptr, len);
>      context->ptr +=3D len;
>      context->remaining -=3D len;
>
>      return len;
> }
>
> xmlDocPtr
> myReadMemory(const char *buffer, size_t size, const char *URL,
>               const char *encoding, int options) {
>      myContext context;
>
>      context.ptr =3D buffer;
>      context.remaining =3D size;
>
>      return xmlReadIO(myReadCallback, NULL, &context, URL, encoding,
> options);
> }
>

--0000000000002e6c0805bc9f4e86
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">thank you so much Nick,<div>it&#39;s crystal clear now :)<=
/div><div><br></div><div>++<br></div><div>nicolas</div></div><br><div class=
=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">Le=C2=A0mer. 3 mars =
2021 =C3=A0=C2=A011:12, Nick Wellnhofer &lt;<a href=3D"mailto:wellnhofer@ae=
vum.de">[email protected]</a>&gt; a =C3=A9crit=C2=A0:<br></div><blockquot=
e class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px s=
olid rgb(204,204,204);padding-left:1ex">On 03/03/2021 09:30, nicolas bats w=
rote:<br>
&gt; Hi Nick,<br>
&gt; I&#39;ve experimented with xmlReadIO and it&#39;s cool.<br>
&gt; this message just to check I&#39;m doing right:<br>
&gt; -I register an xmlInputReadCallback of type: size_t myCallback(void* c=
ontext, <br>
&gt; char* buffer, int length)<br>
&gt; -I do my stuff in the callback and if data I use exceed the length of =
the <br>
&gt; buffer, I realloc it.<br>
&gt; Is this schema good?<br>
&gt; Do I need to set size_t as the return type of myCallback?<br>
<br>
No, the read callback is supposed to fill the buffer with up to &#39;length=
&#39; <br>
bytes. Try something like:<br>
<br>
typedef struct {<br>
=C2=A0 =C2=A0 =C2=A0const char *ptr;<br>
=C2=A0 =C2=A0 =C2=A0size_t remaining;<br>
} myContext;<br>
<br>
static int<br>
myReadCallback(void *vcontext, char *buffer, int len) {<br>
=C2=A0 =C2=A0 =C2=A0myContext *context =3D vcontext;<br>
<br>
=C2=A0 =C2=A0 =C2=A0if (context-&gt;remaining &lt; len)<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0len =3D context-&gt;remaining;<br>
=C2=A0 =C2=A0 =C2=A0memcpy(buffer, context-&gt;ptr, len);<br>
=C2=A0 =C2=A0 =C2=A0context-&gt;ptr +=3D len;<br>
=C2=A0 =C2=A0 =C2=A0context-&gt;remaining -=3D len;<br>
<br>
=C2=A0 =C2=A0 =C2=A0return len;<br>
}<br>
<br>
xmlDocPtr<br>
myReadMemory(const char *buffer, size_t size, const char *URL,<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 const char *encoding, int =
options) {<br>
=C2=A0 =C2=A0 =C2=A0myContext context;<br>
<br>
=C2=A0 =C2=A0 =C2=A0context.ptr =3D buffer;<br>
=C2=A0 =C2=A0 =C2=A0context.remaining =3D size;<br>
<br>
=C2=A0 =C2=A0 =C2=A0return xmlReadIO(myReadCallback, NULL, &amp;context, UR=
L, encoding, options);<br>
}<br>
</blockquote></div>

--0000000000002e6c0805bc9f4e86--

--===============9075537196922693361==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
[email protected]
https://mail.gnome.org/mailman/listinfo/xml

--===============9075537196922693361==--