Fixes/enhancements: - tree.[ch] xmlIO.c: added xmlDocDumpMemoryEnc() from

Fixes/enhancements:
- tree.[ch] xmlIO.c: added xmlDocDumpMemoryEnc() from John Kroll
- error.c: applied fix suggested by "Leo Davidson" <leo@ox.compsoc.net>
Daniel
diff --git a/ChangeLog b/ChangeLog
index e4ae337..cd208e3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Sat Nov 25 01:21:01 CET 2000 Daniel Veillard <Daniel.Veillard@w3.org>
+
+	* tree.[ch] xmlIO.c: added xmlDocDumpMemoryEnc() from John Kroll
+	* error.c: applied fix suggested by "Leo Davidson" <leo@ox.compsoc.net>
+
 Sat Nov 25 00:24:49 CET 2000 Daniel Veillard <Daniel.Veillard@w3.org>
 
 	* HTMLparser.c: some fixes on auto-open of html/head/body
diff --git a/error.c b/error.c
index 806cdad..cf76016 100644
--- a/error.c
+++ b/error.c
@@ -62,10 +62,11 @@
  */
 void
 xmlSetGenericErrorFunc(void *ctx, xmlGenericErrorFunc handler) {
-    if (ctx != NULL)
-	xmlGenericErrorContext = ctx;
+    xmlGenericErrorContext = ctx;
     if (handler != NULL)
 	xmlGenericError = handler;
+    else
+	xmlGenericError = xmlGenericErrorDefaultFunc;
 }
 
 /************************************************************************
diff --git a/include/libxml/tree.h b/include/libxml/tree.h
index b2838e1..5e09f64 100644
--- a/include/libxml/tree.h
+++ b/include/libxml/tree.h
@@ -628,6 +628,10 @@
 void		xmlDocDumpMemory	(xmlDocPtr cur,
 					 xmlChar**mem,
 					 int *size);
+void		xmlDocDumpMemoryEnc	(xmlDocPtr out_doc,
+					 xmlChar **doc_txt_ptr,
+					 int * doc_txt_len,
+					 const char *txt_encoding);
 int		xmlDocDump		(FILE *f,
 					 xmlDocPtr cur);
 void		xmlElemDump		(FILE *f,
diff --git a/tree.c b/tree.c
index 708ff01..4e3f44a 100644
--- a/tree.c
+++ b/tree.c
@@ -5594,6 +5594,96 @@
 }
 
 /**
+ * xmlDocDumpMemoryEnc:
+ * @out_doc:  Document to generate XML text from
+ * @doc_txt_ptr:  Memory pointer for allocated XML text
+ * @doc_txt_len:  Length of the generated XML text
+ * @txt_encoding:  Character encoding to use when generating XML text
+ *
+ * Dump the current DOM tree into memory using the character encoding specified
+ * by the caller.  Note it is up to the caller of this function to free the
+ * allocated memory.
+ */
+
+void
+xmlDocDumpMemoryEnc(xmlDocPtr out_doc, xmlChar **doc_txt_ptr,
+	            int * doc_txt_len, const char * txt_encoding) {
+    int                         dummy = 0;
+
+    xmlCharEncoding             doc_charset;
+    xmlOutputBufferPtr          out_buff = NULL;
+    xmlCharEncodingHandlerPtr   conv_hdlr = NULL;
+
+    if (doc_txt_len == NULL) {
+        doc_txt_len = &dummy;   /*  Continue, caller just won't get length */
+    }
+
+    if (doc_txt_ptr == NULL) {
+        *doc_txt_len = 0;
+        xmlGenericError(xmlGenericErrorContext,
+                    "xmlDocDumpMemoryEnc:  Null return buffer pointer.");
+        return;
+    }
+
+    *doc_txt_ptr = NULL;
+    *doc_txt_len = 0;
+
+    if (out_doc == NULL) {
+        /*  No document, no output  */
+        xmlGenericError(xmlGenericErrorContext,
+                "xmlDocDumpMemoryEnc:  Null DOM tree document pointer.\n");
+        return;
+    }
+
+    /*
+     *  Validate the encoding value, if provided.
+     *  This logic is copied from xmlSaveFileEnc.
+     */
+
+    if (txt_encoding != NULL) {
+        doc_charset = xmlParseCharEncoding(txt_encoding);
+
+        if (out_doc->charset != XML_CHAR_ENCODING_UTF8) {
+            xmlGenericError(xmlGenericErrorContext,
+                    "xmlDocDumpMemoryEnc:  Source document not in UTF8\n");
+            return;
+
+        } else if (doc_charset != XML_CHAR_ENCODING_UTF8) {
+            conv_hdlr = xmlFindCharEncodingHandler(txt_encoding);
+            if ( conv_hdlr == NULL ) {
+                xmlGenericError(xmlGenericErrorContext,
+                                "%s:  %s %s '%s'\n",
+                                "xmlDocDumpMemoryEnc",
+                                "Failed to identify encoding handler for",
+                                "character set",
+                                txt_encoding);
+                return;
+            }
+        }
+    }
+
+    if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) {
+        xmlGenericError(xmlGenericErrorContext,
+                "xmlDocDumpMemoryEnc:  Failed to allocate output buffer.\n");
+        return;
+    }
+
+    xmlDocContentDumpOutput(out_buff, out_doc, txt_encoding);
+    *doc_txt_len = out_buff->buffer->use;
+    *doc_txt_ptr = xmlStrndup(out_buff->buffer->content, *doc_txt_len);
+    (void)xmlOutputBufferClose(out_buff);
+
+    if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) {
+        *doc_txt_len = 0;
+        xmlGenericError(xmlGenericErrorContext,
+                "xmlDocDumpMemoryEnc:  %s\n",
+                "Failed to allocate memory for document text representation.");
+    }
+
+    return;
+}
+
+/**
  * xmlGetDocCompressMode:
  * @doc:  the document
  *
diff --git a/tree.h b/tree.h
index b2838e1..5e09f64 100644
--- a/tree.h
+++ b/tree.h
@@ -628,6 +628,10 @@
 void		xmlDocDumpMemory	(xmlDocPtr cur,
 					 xmlChar**mem,
 					 int *size);
+void		xmlDocDumpMemoryEnc	(xmlDocPtr out_doc,
+					 xmlChar **doc_txt_ptr,
+					 int * doc_txt_len,
+					 const char *txt_encoding);
 int		xmlDocDump		(FILE *f,
 					 xmlDocPtr cur);
 void		xmlElemDump		(FILE *f,
diff --git a/xmlIO.c b/xmlIO.c
index 31cd913..dec8694 100644
--- a/xmlIO.c
+++ b/xmlIO.c
@@ -836,7 +836,8 @@
 
     if (out == NULL)
         return(-1);
-    xmlOutputBufferFlush(out);
+    if (out->writecallback != NULL)
+	xmlOutputBufferFlush(out);
     if (out->closecallback != NULL) {
 	out->closecallback(out->context);
     }