diff --git a/ChangeLog b/ChangeLog index e4ae3377..cd208e30 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Sat Nov 25 01:21:01 CET 2000 Daniel Veillard + + * tree.[ch] xmlIO.c: added xmlDocDumpMemoryEnc() from John Kroll + * error.c: applied fix suggested by "Leo Davidson" + Sat Nov 25 00:24:49 CET 2000 Daniel Veillard * HTMLparser.c: some fixes on auto-open of html/head/body diff --git a/error.c b/error.c index 806cdad5..cf76016f 100644 --- a/error.c +++ b/error.c @@ -62,10 +62,11 @@ void *xmlGenericErrorContext = NULL; */ 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 b2838e1e..5e09f64f 100644 --- a/include/libxml/tree.h +++ b/include/libxml/tree.h @@ -628,6 +628,10 @@ int xmlReconciliateNs (xmlDocPtr doc, 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 708ff015..4e3f44a4 100644 --- a/tree.c +++ b/tree.c @@ -5593,6 +5593,96 @@ xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int *size) { xmlBufferFree(buf); } +/** + * 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 b2838e1e..5e09f64f 100644 --- a/tree.h +++ b/tree.h @@ -628,6 +628,10 @@ int xmlReconciliateNs (xmlDocPtr doc, 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 31cd9133..dec8694d 100644 --- a/xmlIO.c +++ b/xmlIO.c @@ -836,7 +836,8 @@ xmlOutputBufferClose(xmlOutputBufferPtr out) { if (out == NULL) return(-1); - xmlOutputBufferFlush(out); + if (out->writecallback != NULL) + xmlOutputBufferFlush(out); if (out->closecallback != NULL) { out->closecallback(out->context); }