Re: [xml] PATCH: implement xmlSaveToBuffer()



Hi Daniel,

thanks for your reply. I've checked my code and was actually passing in an xmlOutputBufferPtr, not an xmlBufferPtr! That's why it worked in my tests. See the (Pyrex/lxml) code below:

           buffer = tree.xmlAllocOutputBuffer(encoder)
           ctxt = tree.xmlSaveToBuffer(buffer, encoding, options)
           tree.xmlSaveDoc(ctxt, self._c_doc)
           tree.xmlSaveFlush(ctxt)
           s = dumpOutputBuffer(buffer, encoding)
           file.write(s)
           tree.xmlSaveClose(ctxt)

Can we just change the xmlSaveToBuffer() prototype to accept an xmlOutputBuffer as per the updated patch attached? Or is there something which makes it better to have an interface with an xmlBuffer?

The attached patch also comments in and updates the prototype in xmlsave.h.

Regards,
Geert


Daniel Veillard wrote:
On Sun, Nov 06, 2005 at 06:07:52PM +0100, Geert Jansen wrote:
Hi,

the attached patch implement the function xmlSaveToBuffer() which was documented but not implemented.

Can the patch be applied?

  Sounds wrong. You are using an xmlBufferPtr while ret->buf is expected
to be an xmlOutputBufferPtr. As a result if someone tries to free the returned
value with the xmlFreeSaveCtxt() call it will crash or corrupt memory.
If you compile with gcc you get a warning about this:
    xmlsave.c: In function `xmlSaveToBuffer':
    xmlsave.c:1489: warning: assignment from incompatible pointer type
That patch really can't be applied as-is, I really can't see how it would
have worked in any test either. Instead generate an xmlOutputBufferPtr
from the passed xmlBufferPtr and check that the passed buffer is not freed
when xmlFreeSaveCtxt is called, that should work correctly.

  Sorry, no, but thanks !

Daniel


diff -ur libxml2-2.6.22.orig/include/libxml/xmlsave.h libxml2-2.6.22/include/libxml/xmlsave.h
--- libxml2-2.6.22.orig/include/libxml/xmlsave.h        2005-09-12 17:34:25.000000000 +0200
+++ libxml2-2.6.22/include/libxml/xmlsave.h     2005-11-06 20:10:42.000000000 +0100
@@ -45,14 +45,12 @@
                xmlSaveToFilename       (const char *filename,
                                         const char *encoding,
                                         int options);
-/******
-  Not yet implemented.
 
 XMLPUBFUN xmlSaveCtxtPtr XMLCALL
-               xmlSaveToBuffer         (xmlBufferPtr buffer,
+               xmlSaveToBuffer         (xmlOutputBufferPtr buffer,
                                         const char *encoding,
                                         int options);
- ******/
+
 XMLPUBFUN xmlSaveCtxtPtr XMLCALL
                xmlSaveToIO             (xmlOutputWriteCallback iowrite,
                                         xmlOutputCloseCallback ioclose,
diff -ur libxml2-2.6.22.orig/xmlsave.c libxml2-2.6.22/xmlsave.c
--- libxml2-2.6.22.orig/xmlsave.c       2005-09-12 17:35:07.000000000 +0200
+++ libxml2-2.6.22/xmlsave.c    2005-11-06 20:01:21.000000000 +0100
@@ -1473,13 +1473,18 @@
  * with the encoding and the options given
  *
  * Returns a new serialization context or NULL in case of error.
+ */
+
 xmlSaveCtxtPtr
-xmlSaveToBuffer(xmlBufferPtr buffer, const char *encoding, int options)
+xmlSaveToBuffer(xmlOutputBufferPtr buffer, const char *encoding, int options)
 {
-    TODO
-    return(NULL);
+    xmlSaveCtxtPtr ret;
+
+    ret = xmlNewSaveCtxt(encoding, options);
+    if (ret == NULL) return(NULL);
+    ret->buf = buffer;
+    return(ret);
 }
- */
 
 /**
  * xmlSaveToIO:


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]