converting the tree module too created a simpler internal error reporting
* tree.c: converting the tree module too
* error.c include/libxml/xmlerror.h: created a simpler internal
error reporting function.
Daniel
diff --git a/ChangeLog b/ChangeLog
index 38228d8..a8d1712 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Wed Oct 8 01:09:05 CEST 2003 Daniel Veillard <daniel@veillard.com>
+
+ * tree.c: converting the tree module too
+ * error.c include/libxml/xmlerror.h: created a simpler internal
+ error reporting function.
+
Tue Oct 7 23:19:39 CEST 2003 Daniel Veillard <daniel@veillard.com>
* error.c include/libxml/xmlerror.h include/libxml/xpath.h
diff --git a/error.c b/error.c
index 59c7037..c996365 100644
--- a/error.c
+++ b/error.c
@@ -518,6 +518,36 @@
}
/**
+ * __xmlSimpleError:
+ * @domain: where the error comes from
+ * @code: the error code
+ * @node: the context node
+ * @extra: extra informations
+ *
+ * Handle an out of memory condition
+ */
+void
+__xmlSimpleError(int domain, int code, xmlNodePtr node,
+ const char *msg, const char *extra)
+{
+
+ if (code == XML_ERR_NO_MEMORY) {
+ if (extra)
+ __xmlRaiseError(NULL, NULL, NULL, node, domain,
+ XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
+ NULL, NULL, 0, 0,
+ "Memory allocation failed : %s\n", extra);
+ else
+ __xmlRaiseError(NULL, NULL, NULL, node, domain,
+ XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
+ NULL, NULL, 0, 0, "Memory allocation failed\n");
+ } else {
+ __xmlRaiseError(NULL, NULL, NULL, node, domain,
+ code, XML_ERR_ERROR, NULL, 0, extra,
+ NULL, NULL, 0, 0, msg, extra);
+ }
+}
+/**
* xmlParserError:
* @ctx: an XML parser context
* @msg: the message to display/transmit
diff --git a/include/libxml/xmlerror.h b/include/libxml/xmlerror.h
index 7c21aec..59a7888 100644
--- a/include/libxml/xmlerror.h
+++ b/include/libxml/xmlerror.h
@@ -27,6 +27,7 @@
typedef enum {
XML_FROM_NONE = 0,
XML_FROM_PARSER, /* The XML parser */
+ XML_FROM_TREE, /* The tree module */
XML_FROM_NAMESPACE, /* The XML Namespace module */
XML_FROM_DTD, /* The XML DTD validation */
XML_FROM_HTML, /* The HTML parser */
@@ -397,7 +398,13 @@
XML_XPTR_SUB_RESOURCE_ERROR,
XML_XPATH_UNDEF_PREFIX_ERROR,
XML_XPATH_ENCODING_ERROR,
- XML_XPATH_INVALID_CHAR_ERROR
+ XML_XPATH_INVALID_CHAR_ERROR,
+ XML_TREE_INVALID_HEX = 1300,
+ XML_TREE_INVALID_DEC,
+ XML_TREE_UNTERMINATED_ENTITY,
+ XML_SAVE_NOT_UTF8 = 1400,
+ XML_SAVE_CHAR_INVALID,
+ XML_SAVE_UNKNOWN_ENCODING
} xmlParserErrors;
/**
@@ -495,6 +502,12 @@
int int2,
const char *msg,
...);
+XMLPUBFUN void XMLCALL
+ __xmlSimpleError (int domain,
+ int code,
+ xmlNodePtr node,
+ const char *msg,
+ const char *extra);
#endif
#ifdef __cplusplus
}
diff --git a/tree.c b/tree.c
index ab79849..b0f55f9 100644
--- a/tree.c
+++ b/tree.c
@@ -44,6 +44,51 @@
/************************************************************************
* *
+ * Tree memory error handler *
+ * *
+ ************************************************************************/
+/**
+ * xmlTreeErrMemory:
+ * @extra: extra informations
+ *
+ * Handle an out of memory condition
+ */
+static void
+xmlTreeErrMemory(const char *extra)
+{
+ __xmlSimpleError(XML_FROM_TREE, XML_ERR_NO_MEMORY, NULL, NULL, extra);
+}
+
+/**
+ * xmlTreeErr:
+ * @code: the error number
+ * @extra: extra informations
+ *
+ * Handle an out of memory condition
+ */
+static void
+xmlTreeErr(int code, xmlNodePtr node, const char *extra)
+{
+ const char *msg = NULL;
+
+ switch(code) {
+ case XML_TREE_INVALID_HEX:
+ msg = "invalid hexadecimal character value";
+ break;
+ case XML_TREE_INVALID_DEC:
+ msg = "invalid decimal character value";
+ break;
+ case XML_TREE_UNTERMINATED_ENTITY:
+ msg = "unterminated entity reference %15s";
+ break;
+ default:
+ msg = "unexpected error number";
+ }
+ __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra);
+}
+
+/************************************************************************
+ * *
* A few static variables and macros *
* *
************************************************************************/
@@ -161,7 +206,10 @@
if ((memory == NULL) || (len < lenn + lenp + 2)) {
ret = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
- if (ret == NULL) return(NULL);
+ if (ret == NULL) {
+ xmlTreeErrMemory("building QName");
+ return(NULL);
+ }
} else {
ret = memory;
}
@@ -220,14 +268,12 @@
*prefix = xmlStrndup(name, len);
if (*prefix == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlSplitQName2 : out of memory!\n");
+ xmlTreeErrMemory("QName split");
return(NULL);
}
ret = xmlStrdup(&name[len + 1]);
if (ret == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlSplitQName2 : out of memory!\n");
+ xmlTreeErrMemory("QName split");
if (*prefix != NULL) {
xmlFree(*prefix);
*prefix = NULL;
@@ -650,8 +696,7 @@
*/
cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
if (cur == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlNewNs : malloc failed\n");
+ xmlTreeErrMemory("building namespace");
return(NULL);
}
memset(cur, 0, sizeof(xmlNs));
@@ -785,8 +830,7 @@
*/
cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
if (cur == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlNewDtd : malloc failed\n");
+ xmlTreeErrMemory("building DTD");
return(NULL);
}
memset(cur, 0 , sizeof(xmlDtd));
@@ -860,8 +904,7 @@
*/
cur = (xmlDtdPtr) xmlMalloc(sizeof(xmlDtd));
if (cur == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlCreateIntSubset : malloc failed\n");
+ xmlTreeErrMemory("building internal subset");
return(NULL);
}
memset(cur, 0, sizeof(xmlDtd));
@@ -1002,8 +1045,7 @@
*/
cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
if (cur == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlNewDoc : malloc failed\n");
+ xmlTreeErrMemory("building doc");
return(NULL);
}
memset(cur, 0, sizeof(xmlDoc));
@@ -1141,8 +1183,8 @@
else if ((tmp >= 'A') && (tmp <= 'F'))
charval = charval * 16 + (tmp - 'A') + 10;
else {
- xmlGenericError(xmlGenericErrorContext,
- "xmlStringGetNodeList: invalid hexadecimal charvalue\n");
+ xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
+ NULL);
charval = 0;
break;
}
@@ -1165,8 +1207,8 @@
if ((tmp >= '0') && (tmp <= '9'))
charval = charval * 10 + (tmp - '0');
else {
- xmlGenericError(xmlGenericErrorContext,
- "xmlStringGetNodeList: invalid decimal charvalue\n");
+ xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
+ NULL);
charval = 0;
break;
}
@@ -1187,10 +1229,8 @@
q = cur;
while ((cur < end) && (*cur != 0) && (*cur != ';')) cur++;
if ((cur >= end) || (*cur == 0)) {
-#ifdef DEBUG_TREE
- xmlGenericError(xmlGenericErrorContext,
- "xmlStringGetNodeList: unterminated entity %30s\n", q);
-#endif
+ xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY, (xmlNodePtr) doc,
+ (const char *) q);
return(ret);
}
if (cur != q) {
@@ -1336,8 +1376,8 @@
else if ((tmp >= 'A') && (tmp <= 'F'))
charval = charval * 16 + (tmp - 'A') + 10;
else {
- xmlGenericError(xmlGenericErrorContext,
- "xmlStringGetNodeList: invalid hexadecimal charvalue\n");
+ xmlTreeErr(XML_TREE_INVALID_HEX, (xmlNodePtr) doc,
+ NULL);
charval = 0;
break;
}
@@ -1354,8 +1394,8 @@
if ((tmp >= '0') && (tmp <= '9'))
charval = charval * 10 + (tmp - '0');
else {
- xmlGenericError(xmlGenericErrorContext,
- "xmlStringGetNodeList: invalid decimal charvalue\n");
+ xmlTreeErr(XML_TREE_INVALID_DEC, (xmlNodePtr) doc,
+ NULL);
charval = 0;
break;
}
@@ -1373,10 +1413,8 @@
q = cur;
while ((*cur != 0) && (*cur != ';')) cur++;
if (*cur == 0) {
-#ifdef DEBUG_TREE
- xmlGenericError(xmlGenericErrorContext,
- "xmlStringGetNodeList: unterminated entity %30s\n", q);
-#endif
+ xmlTreeErr(XML_TREE_UNTERMINATED_ENTITY,
+ (xmlNodePtr) doc, (const char *) q);
return(ret);
}
if (cur != q) {
@@ -1661,8 +1699,7 @@
*/
cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
if (cur == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlNewProp : malloc failed\n");
+ xmlTreeErrMemory("building attribute");
return(NULL);
}
memset(cur, 0, sizeof(xmlAttr));
@@ -1742,8 +1779,7 @@
*/
cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
if (cur == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlNewNsProp : malloc failed\n");
+ xmlTreeErrMemory("building attribute");
return(NULL);
}
memset(cur, 0, sizeof(xmlAttr));
@@ -1822,8 +1858,7 @@
*/
cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
if (cur == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlNewNsPropEatName : malloc failed\n");
+ xmlTreeErrMemory("building attribute");
xmlFree(name);
return(NULL);
}
@@ -1900,8 +1935,7 @@
*/
cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
if (cur == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlNewDocProp : malloc failed\n");
+ xmlTreeErrMemory("building attribute");
return(NULL);
}
memset(cur, 0, sizeof(xmlAttr));
@@ -2050,8 +2084,7 @@
*/
cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
if (cur == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlNewPI : malloc failed\n");
+ xmlTreeErrMemory("building PI");
return(NULL);
}
memset(cur, 0, sizeof(xmlNode));
@@ -2093,8 +2126,7 @@
*/
cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
if (cur == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlNewNode : malloc failed\n");
+ xmlTreeErrMemory("building node");
return(NULL);
}
memset(cur, 0, sizeof(xmlNode));
@@ -2134,8 +2166,7 @@
*/
cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
if (cur == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlNewNode : malloc failed\n");
+ xmlTreeErrMemory("building node");
xmlFree(name);
return(NULL);
}
@@ -2260,8 +2291,7 @@
*/
cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
if (cur == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlNewDocFragment : malloc failed\n");
+ xmlTreeErrMemory("building fragment");
return(NULL);
}
memset(cur, 0, sizeof(xmlNode));
@@ -2291,8 +2321,7 @@
*/
cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
if (cur == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlNewText : malloc failed\n");
+ xmlTreeErrMemory("building text");
return(NULL);
}
memset(cur, 0, sizeof(xmlNode));
@@ -2389,8 +2418,7 @@
*/
cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
if (cur == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlNewCharRef : malloc failed\n");
+ xmlTreeErrMemory("building character reference");
return(NULL);
}
memset(cur, 0, sizeof(xmlNode));
@@ -2431,8 +2459,7 @@
*/
cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
if (cur == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlNewReference : malloc failed\n");
+ xmlTreeErrMemory("building reference");
return(NULL);
}
memset(cur, 0, sizeof(xmlNode));
@@ -2501,8 +2528,7 @@
*/
cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
if (cur == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlNewTextLen : malloc failed\n");
+ xmlTreeErrMemory("building text");
return(NULL);
}
memset(cur, 0, sizeof(xmlNode));
@@ -2553,8 +2579,7 @@
*/
cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
if (cur == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlNewComment : malloc failed\n");
+ xmlTreeErrMemory("building comment");
return(NULL);
}
memset(cur, 0, sizeof(xmlNode));
@@ -2588,8 +2613,7 @@
*/
cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
if (cur == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlNewCDataBlock : malloc failed\n");
+ xmlTreeErrMemory("building CDATA");
return(NULL);
}
memset(cur, 0, sizeof(xmlNode));
@@ -3700,8 +3724,7 @@
*/
ret = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
if (ret == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlStaticCopyNode : malloc failed\n");
+ xmlTreeErrMemory("copying node");
return(NULL);
}
memset(ret, 0, sizeof(xmlNode));
@@ -4088,10 +4111,13 @@
buf_len = 500;
buffer = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
- if (buffer == NULL)
+ if (buffer == NULL) {
+ xmlTreeErrMemory("getting node path");
return (NULL);
+ }
buf = (xmlChar *) xmlMallocAtomic(buf_len * sizeof(xmlChar));
if (buf == NULL) {
+ xmlTreeErrMemory("getting node path");
xmlFree(buffer);
return (NULL);
}
@@ -4243,6 +4269,7 @@
2 * buf_len + xmlStrlen(buffer) + sizeof(nametemp) + 20;
temp = (xmlChar *) xmlRealloc(buffer, buf_len);
if (temp == NULL) {
+ xmlTreeErrMemory("getting node path");
xmlFree(buf);
xmlFree(buffer);
return (NULL);
@@ -4250,6 +4277,7 @@
buffer = temp;
temp = (xmlChar *) xmlRealloc(buf, buf_len);
if (temp == NULL) {
+ xmlTreeErrMemory("getting node path");
xmlFree(buf);
xmlFree(buffer);
return (NULL);
@@ -5144,8 +5172,7 @@
(xmlNsPtr *) xmlMalloc((maxns + 1) *
sizeof(xmlNsPtr));
if (ret == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlGetNsList : out of memory!\n");
+ xmlTreeErrMemory("getting namespace list");
return (NULL);
}
ret[nbns] = NULL;
@@ -5163,8 +5190,7 @@
1) *
sizeof(xmlNsPtr));
if (ret == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlGetNsList : realloc failed!\n");
+ xmlTreeErrMemory("getting namespace list");
return (NULL);
}
}
@@ -5212,8 +5238,7 @@
*/
cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
if (cur == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlSearchNs : malloc failed\n");
+ xmlTreeErrMemory("searching namespace");
return(NULL);
}
memset(cur, 0, sizeof(xmlNs));
@@ -5230,8 +5255,7 @@
*/
doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
if (doc->oldNs == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlSearchNs : malloc failed\n");
+ xmlTreeErrMemory("searching namespace");
return(NULL);
}
memset(doc->oldNs, 0, sizeof(xmlNs));
@@ -5348,8 +5372,7 @@
*/
cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
if (cur == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlSearchNs : malloc failed\n");
+ xmlTreeErrMemory("searching namespace");
return (NULL);
}
memset(cur, 0, sizeof(xmlNs));
@@ -5366,8 +5389,7 @@
*/
doc->oldNs = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
if (doc->oldNs == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlSearchNsByHref : malloc failed\n");
+ xmlTreeErrMemory("searching namespace");
return (NULL);
}
memset(doc->oldNs, 0, sizeof(xmlNs));
@@ -5513,15 +5535,13 @@
oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
sizeof(xmlNsPtr));
if (oldNs == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlReconciliateNs : memory pbm\n");
+ xmlTreeErrMemory("fixing namespaces");
return(-1);
}
newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
sizeof(xmlNsPtr));
if (newNs == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlReconciliateNs : memory pbm\n");
+ xmlTreeErrMemory("fixing namespaces");
xmlFree(oldNs);
return(-1);
}
@@ -5546,16 +5566,14 @@
oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
sizeof(xmlNsPtr));
if (oldNs == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlReconciliateNs : memory pbm\n");
+ xmlTreeErrMemory("fixing namespaces");
xmlFree(newNs);
return(-1);
}
newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
sizeof(xmlNsPtr));
if (newNs == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlReconciliateNs : memory pbm\n");
+ xmlTreeErrMemory("fixing namespaces");
xmlFree(oldNs);
return(-1);
}
@@ -5580,15 +5598,13 @@
oldNs = (xmlNsPtr *) xmlMalloc(sizeCache *
sizeof(xmlNsPtr));
if (oldNs == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlReconciliateNs : memory pbm\n");
+ xmlTreeErrMemory("fixing namespaces");
return(-1);
}
newNs = (xmlNsPtr *) xmlMalloc(sizeCache *
sizeof(xmlNsPtr));
if (newNs == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlReconciliateNs : memory pbm\n");
+ xmlTreeErrMemory("fixing namespaces");
xmlFree(oldNs);
return(-1);
}
@@ -5613,16 +5629,14 @@
oldNs = (xmlNsPtr *) xmlRealloc(oldNs, sizeCache *
sizeof(xmlNsPtr));
if (oldNs == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlReconciliateNs : memory pbm\n");
+ xmlTreeErrMemory("fixing namespaces");
xmlFree(newNs);
return(-1);
}
newNs = (xmlNsPtr *) xmlRealloc(newNs, sizeCache *
sizeof(xmlNsPtr));
if (newNs == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlReconciliateNs : memory pbm\n");
+ xmlTreeErrMemory("fixing namespaces");
xmlFree(oldNs);
return(-1);
}
@@ -6272,8 +6286,7 @@
ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
if (ret == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlBufferCreate : out of memory!\n");
+ xmlTreeErrMemory("creating buffer");
return(NULL);
}
ret->use = 0;
@@ -6281,8 +6294,7 @@
ret->alloc = xmlBufferAllocScheme;
ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
if (ret->content == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlBufferCreate : out of memory!\n");
+ xmlTreeErrMemory("creating buffer");
xmlFree(ret);
return(NULL);
}
@@ -6303,8 +6315,7 @@
ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
if (ret == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlBufferCreate : out of memory!\n");
+ xmlTreeErrMemory("creating buffer");
return(NULL);
}
ret->use = 0;
@@ -6313,8 +6324,7 @@
if (ret->size){
ret->content = (xmlChar *) xmlMallocAtomic(ret->size * sizeof(xmlChar));
if (ret->content == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlBufferCreate : out of memory!\n");
+ xmlTreeErrMemory("creating buffer");
xmlFree(ret);
return(NULL);
}
@@ -6344,8 +6354,7 @@
ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer));
if (ret == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlBufferCreate : out of memory!\n");
+ xmlTreeErrMemory("creating buffer");
return(NULL);
}
ret->use = size;
@@ -6462,7 +6471,10 @@
size = buf->use + len + 100;
newbuf = (xmlChar *) xmlRealloc(buf->content, size);
- if (newbuf == NULL) return(-1);
+ if (newbuf == NULL) {
+ xmlTreeErrMemory("growing buffer");
+ return(-1);
+ }
buf->content = newbuf;
buf->size = size;
return(buf->size - buf->use);
@@ -6592,8 +6604,7 @@
rebuf[buf->use] = 0;
}
if (rebuf == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlBufferResize : out of memory!\n");
+ xmlTreeErrMemory("growing buffer");
return 0;
}
buf->content = rebuf;
@@ -6640,8 +6651,7 @@
needSize = buf->use + len + 2;
if (needSize > buf->size){
if (!xmlBufferResize(buf, needSize)){
- xmlGenericError(xmlGenericErrorContext,
- "xmlBufferAdd : out of memory!\n");
+ xmlTreeErrMemory("growing buffer");
return;
}
}
@@ -6689,8 +6699,7 @@
needSize = buf->use + len + 2;
if (needSize > buf->size){
if (!xmlBufferResize(buf, needSize)){
- xmlGenericError(xmlGenericErrorContext,
- "xmlBufferAddHead : out of memory!\n");
+ xmlTreeErrMemory("growing buffer");
return;
}
}
@@ -6737,8 +6746,7 @@
for (cur = str;*cur != 0;cur++) {
if (buf->use + 10 >= buf->size) {
if (!xmlBufferResize(buf, buf->use+10)){
- xmlGenericError(xmlGenericErrorContext,
- "xmlBufferCCat : out of memory!\n");
+ xmlTreeErrMemory("growing buffer");
return;
}
}
@@ -6829,6 +6837,51 @@
#ifdef LIBXML_OUTPUT_ENABLED
/************************************************************************
* *
+ * Output memory error handler *
+ * *
+ ************************************************************************/
+/**
+ * xmlSaveErrMemory:
+ * @extra: extra informations
+ *
+ * Handle an out of memory condition
+ */
+static void
+xmlSaveErrMemory(const char *extra)
+{
+ __xmlSimpleError(XML_FROM_OUTPUT, XML_ERR_NO_MEMORY, NULL, NULL, extra);
+}
+
+/**
+ * xmlSaveErr:
+ * @code: the error number
+ * @node: the location of the error.
+ * @extra: extra informations
+ *
+ * Handle an out of memory condition
+ */
+static void
+xmlSaveErr(int code, xmlNodePtr node, const char *extra)
+{
+ const char *msg = NULL;
+
+ switch(code) {
+ case XML_SAVE_NOT_UTF8:
+ msg = "string is not in UTF-8";
+ break;
+ case XML_SAVE_CHAR_INVALID:
+ msg = "invalid character value";
+ break;
+ case XML_SAVE_UNKNOWN_ENCODING:
+ msg = "unknown encoding %s";
+ break;
+ default:
+ msg = "unexpected error number";
+ }
+ __xmlSimpleError(XML_FROM_TREE, code, node, msg, extra);
+}
+/************************************************************************
+ * *
* Dumping XML tree content to a simple buffer *
* *
************************************************************************/
@@ -6915,8 +6968,8 @@
if (base != cur)
xmlBufferAdd(buf, base, cur - base);
if (*cur < 0xC0) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlAttrSerializeContent : input not UTF-8\n");
+ xmlSaveErr(XML_SAVE_NOT_UTF8, (xmlNodePtr) attr,
+ NULL);
if (doc != NULL)
doc->encoding =
xmlStrdup(BAD_CAST "ISO-8859-1");
@@ -6949,8 +7002,8 @@
l = 4;
}
if ((l == 1) || (!IS_CHAR(val))) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlAttrSerializeContent : char out of range\n");
+ xmlSaveErr(XML_SAVE_CHAR_INVALID, (xmlNodePtr) attr,
+ NULL);
if (doc != NULL)
doc->encoding =
xmlStrdup(BAD_CAST "ISO-8859-1");
@@ -7031,8 +7084,7 @@
}
outbuf = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
if (outbuf == NULL) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlNodeDump: out of memory!\n");
+ xmlSaveErrMemory("creating buffer");
return (-1);
}
memset(outbuf, 0, (size_t) sizeof(xmlOutputBuffer));
@@ -7086,8 +7138,7 @@
#ifdef LIBXML_HTML_ENABLED
htmlNodeDumpOutput(outbuf, doc, cur, NULL);
#else
- xmlGenericError(xmlGenericErrorContext,
- "HTML support not compiled in\n");
+ xmlSaveErr(XML_ERR_INTERNAL_ERROR, "HTML support not compiled in\n");
#endif /* LIBXML_HTML_ENABLED */
} else
xmlNodeDumpOutput(outbuf, doc, cur, 0, 1, NULL);
@@ -8139,8 +8190,6 @@
if (doc_txt_ptr == NULL) {
*doc_txt_len = 0;
- xmlGenericError(xmlGenericErrorContext,
- "xmlDocDumpFormatMemoryEnc: Null return buffer pointer.");
return;
}
@@ -8149,8 +8198,6 @@
if (out_doc == NULL) {
/* No document, no output */
- xmlGenericError(xmlGenericErrorContext,
- "xmlDocDumpFormatMemoryEnc: Null DOM tree document pointer.\n");
return;
}
@@ -8164,19 +8211,14 @@
if (txt_encoding != NULL) {
conv_hdlr = xmlFindCharEncodingHandler(txt_encoding);
if ( conv_hdlr == NULL ) {
- xmlGenericError(xmlGenericErrorContext,
- "%s: %s %s '%s'\n",
- "xmlDocDumpFormatMemoryEnc",
- "Failed to identify encoding handler for",
- "character set",
- txt_encoding);
+ xmlSaveErr(XML_SAVE_UNKNOWN_ENCODING, (xmlNodePtr) out_doc,
+ txt_encoding);
return;
}
}
if ((out_buff = xmlAllocOutputBuffer(conv_hdlr)) == NULL ) {
- xmlGenericError(xmlGenericErrorContext,
- "xmlDocDumpFormatMemoryEnc: Failed to allocate output buffer.\n");
+ xmlSaveErrMemory("creating buffer");
return;
}
@@ -8193,9 +8235,7 @@
if ((*doc_txt_ptr == NULL) && (*doc_txt_len > 0)) {
*doc_txt_len = 0;
- xmlGenericError(xmlGenericErrorContext,
- "xmlDocDumpFormatMemoryEnc: %s\n",
- "Failed to allocate memory for document text representation.");
+ xmlSaveErrMemory("creating output");
}
return;