Re: [xml] PATCH: implement xmlSaveToBuffer()



Daniel Veillard wrote:
Any reason why this couldn't or shouldn't be done the same way xmlNewTextWriterMemory is done? Would only require the addition of write and close callbacks and not require any of the other new stuff from the patch.

  Right, while Geert patch is correct, an approach where you reuse the
I/O callback to fill up a buffer looks cleaner to me.
OK -- please see the attached patch.

Regards,
Geert
diff -ur libxml2-2.6.22.orig/include/libxml/xmlIO.h libxml2-2.6.22/include/libxml/xmlIO.h
--- libxml2-2.6.22.orig/include/libxml/xmlIO.h  2005-07-27 22:59:37.000000000 +0200
+++ libxml2-2.6.22/include/libxml/xmlIO.h       2005-11-07 22:22:17.000000000 +0100
@@ -232,6 +232,10 @@
                                         xmlCharEncodingHandlerPtr encoder);
 
 XMLPUBFUN xmlOutputBufferPtr XMLCALL
+       xmlOutputBufferCreateBuffer     (xmlBufferPtr buffer,
+                                        xmlCharEncodingHandlerPtr encoder);
+
+XMLPUBFUN xmlOutputBufferPtr XMLCALL
        xmlOutputBufferCreateFd         (int fd,
                                         xmlCharEncodingHandlerPtr encoder);
 
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 22:49:37.000000000 +0100
@@ -45,14 +45,12 @@
                xmlSaveToFilename       (const char *filename,
                                         const char *encoding,
                                         int options);
-/******
-  Not yet implemented.
 
 XMLPUBFUN xmlSaveCtxtPtr XMLCALL
                xmlSaveToBuffer         (xmlBufferPtr buffer,
                                         const char *encoding,
                                         int options);
- ******/
+
 XMLPUBFUN xmlSaveCtxtPtr XMLCALL
                xmlSaveToIO             (xmlOutputWriteCallback iowrite,
                                         xmlOutputCloseCallback ioclose,
diff -ur libxml2-2.6.22.orig/xmlIO.c libxml2-2.6.22/xmlIO.c
--- libxml2-2.6.22.orig/xmlIO.c 2005-08-05 15:53:39.000000000 +0200
+++ libxml2-2.6.22/xmlIO.c      2005-11-07 22:37:38.000000000 +0100
@@ -860,6 +860,42 @@
     return(ret);
 }
 
+#ifdef LIBXML_OUTPUT_ENABLED
+/**
+ * xmlBufferWrite:
+ * @context:  the xmlBuffer
+ * @buffer:  the data to write
+ * @len:  number of bytes to write
+ *
+ * Write @len bytes from @buffer to the xml buffer
+ *
+ * Returns the number of bytes written
+ */
+static int
+xmlBufferWrite (void * context, const char * buffer, int len) {
+    int ret;
+
+    ret = xmlBufferAdd((xmlBufferPtr) context, (const xmlChar *) buffer, len);
+    if (ret != 0)
+        return -1;
+    return(len);
+}
+
+/**
+ * xmlBufferClose:
+ * @context:  the xmlBuffer
+ *
+ * Close a buffer
+ *
+ * Returns 0 or -1 in case of error
+ */
+static int
+xmlBufferClose (void * context) {
+    (void) context;
+    return(0);
+}
+#endif
+
 #ifdef HAVE_ZLIB_H
 /************************************************************************
  *                                                                     *
@@ -2436,6 +2472,33 @@
 
     return(ret);
 }
+
+/**
+ * xmlOutputBufferCreateBuffer:
+ * @buffer:  a xmlBufferPtr
+ * @encoder:  the encoding converter or NULL
+ *
+ * Create a buffered output for the progressive saving to a xmlBuffer
+ *
+ * Returns the new parser output or NULL
+ */
+xmlOutputBufferPtr
+xmlOutputBufferCreateBuffer(xmlBufferPtr buffer,
+                            xmlCharEncodingHandlerPtr encoder) {
+    xmlOutputBufferPtr ret;
+
+    if (buffer == NULL) return(NULL);
+
+    ret = xmlAllocOutputBuffer(encoder);
+    if (ret != NULL) {
+        ret->context = buffer;
+        ret->writecallback = xmlBufferWrite;
+        ret->closecallback = xmlBufferClose;
+    }
+
+    return(ret);
+}
+
 #endif /* LIBXML_OUTPUT_ENABLED */
 
 /**
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 23:06:09.000000000 +0100
@@ -1473,13 +1473,36 @@
  * 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)
 {
-    TODO
-    return(NULL);
+    xmlSaveCtxtPtr ret;
+    xmlOutputBufferPtr out_buff;
+    xmlCharEncodingHandlerPtr handler;
+
+    ret = xmlNewSaveCtxt(encoding, options);
+    if (ret == NULL) return(NULL);
+
+    if (encoding != NULL) {
+        handler = xmlFindCharEncodingHandler(encoding);
+        if (handler == NULL) {
+            xmlFree(ret);
+            return(NULL);
+        }
+    } else
+        handler = NULL;
+    out_buff = xmlOutputBufferCreateBuffer(buffer, handler);
+    if (out_buff == NULL) {
+        xmlFree(ret);
+        if (handler) xmlCharEncCloseFunc(handler);
+        return(NULL);
+    }
+
+    ret->buf = out_buff;
+    return(ret);
 }
- */
 
 /**
  * xmlSaveToIO:


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