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

Re: [xml] function consolidation



Daniel Veillard wrote:


 good :-) Then send the CVs diff to be sure we came to the same conclusion :-)
Here's the latest. Added the xmlFree(name) to xmlNewNodeEatName as well when it fails.

Rob
Index: tree.c
===================================================================
RCS file: /cvs/gnome/gnome-xml/tree.c,v
retrieving revision 1.339
diff -c -r1.339 tree.c
*** tree.c	28 Feb 2005 10:28:20 -0000	1.339
--- tree.c	27 Mar 2005 11:02:56 -0000
***************
*** 1721,1759 ****
  }
  #endif /* LIBXML_TREE_ENABLED */
  
! #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
!     defined(LIBXML_SCHEMAS_ENABLED)
! /**
!  * xmlNewProp:
!  * @node:  the holding node
!  * @name:  the name of the attribute
!  * @value:  the value of the attribute
!  *
!  * Create a new property carried by a node.
!  * Returns a pointer to the attribute
!  */
! xmlAttrPtr
! xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
      xmlAttrPtr cur;
      xmlDocPtr doc = NULL;
  
!     if (name == NULL) {
! #ifdef DEBUG_TREE
!         xmlGenericError(xmlGenericErrorContext,
! 		"xmlNewProp : name == NULL\n");
! #endif
! 	return(NULL);
!     }
!     if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
! 	return(NULL);
  
      /*
       * Allocate a new property and fill the fields.
       */
      cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
      if (cur == NULL) {
! 	xmlTreeErrMemory("building attribute");
! 	return(NULL);
      }
      memset(cur, 0, sizeof(xmlAttr));
      cur->type = XML_ATTRIBUTE_NODE;
--- 1721,1746 ----
  }
  #endif /* LIBXML_TREE_ENABLED */
  
! static xmlAttrPtr xmlNewPropInternal(xmlNodePtr node, xmlNsPtr ns, 
!            const xmlChar *name, const xmlChar *value, int eatname) {
      xmlAttrPtr cur;
      xmlDocPtr doc = NULL;
  
!     if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) {
! 		if (eatname == 1)
! 			xmlFree((xmlChar *) name);
! 		return(NULL);
! 	}
  
      /*
       * Allocate a new property and fill the fields.
       */
      cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
      if (cur == NULL) {
! 		if (eatname == 1)
! 			xmlFree((xmlChar *) name);
! 		xmlTreeErrMemory("building attribute");
! 		return(NULL);
      }
      memset(cur, 0, sizeof(xmlAttr));
      cur->type = XML_ATTRIBUTE_NODE;
***************
*** 1763,1772 ****
  	doc = node->doc;
  	cur->doc = doc;
      }
!     if ((doc != NULL) && (doc->dict != NULL))
!         cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
!     else
! 	cur->name = xmlStrdup(name);
      if (value != NULL) {
  	xmlChar *buffer;
  	xmlNodePtr tmp;
--- 1750,1765 ----
  	doc = node->doc;
  	cur->doc = doc;
      }
! 	cur->ns = ns;
! 
! 	if (eatname == 0) {
! 		if ((doc != NULL) && (doc->dict != NULL))
! 			cur->name = (xmlChar *) xmlDictLookup(doc->dict, name, -1);
! 		else
! 			cur->name = xmlStrdup(name);
! 	} else
! 		cur->name = name;
! 
      if (value != NULL) {
  	xmlChar *buffer;
  	xmlNodePtr tmp;
***************
*** 1777,1783 ****
  	tmp = cur->children;
  	while (tmp != NULL) {
  	    tmp->parent = (xmlNodePtr) cur;
- 	    tmp->doc = doc;
  	    if (tmp->next == NULL)
  		cur->last = tmp;
  	    tmp = tmp->next;
--- 1770,1775 ----
***************
*** 1804,1809 ****
--- 1796,1826 ----
  	xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
      return(cur);
  }
+ 
+ #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_HTML_ENABLED) || \
+     defined(LIBXML_SCHEMAS_ENABLED)
+ /**
+  * xmlNewProp:
+  * @node:  the holding node
+  * @name:  the name of the attribute
+  * @value:  the value of the attribute
+  *
+  * Create a new property carried by a node.
+  * Returns a pointer to the attribute
+  */
+ xmlAttrPtr
+ xmlNewProp(xmlNodePtr node, const xmlChar *name, const xmlChar *value) {
+ 
+     if (name == NULL) {
+ #ifdef DEBUG_TREE
+         xmlGenericError(xmlGenericErrorContext,
+ 		"xmlNewProp : name == NULL\n");
+ #endif
+ 	return(NULL);
+     }
+ 
+ 	return xmlNewPropInternal(node, NULL, name, value, 0);
+ }
  #endif /* LIBXML_TREE_ENABLED */
  
  /**
***************
*** 1819,1826 ****
  xmlAttrPtr
  xmlNewNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name,
             const xmlChar *value) {
-     xmlAttrPtr cur;
-     xmlDocPtr doc = NULL;
  
      if (name == NULL) {
  #ifdef DEBUG_TREE
--- 1836,1841 ----
***************
*** 1830,1891 ****
  	return(NULL);
      }
  
!     /*
!      * Allocate a new property and fill the fields.
!      */
!     cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
!     if (cur == NULL) {
! 	xmlTreeErrMemory("building attribute");
! 	return(NULL);
!     }
!     memset(cur, 0, sizeof(xmlAttr));
!     cur->type = XML_ATTRIBUTE_NODE;
! 
!     cur->parent = node; 
!     if (node != NULL) {
! 	doc = node->doc;
! 	cur->doc = doc;
!     }
!     cur->ns = ns;
!     if ((doc != NULL) && (doc->dict != NULL))
! 	cur->name = xmlDictLookup(doc->dict, name, -1);
!     else
! 	cur->name = xmlStrdup(name);
!     if (value != NULL) {
! 	xmlChar *buffer;
! 	xmlNodePtr tmp;
! 
! 	buffer = xmlEncodeEntitiesReentrant(doc, value);
! 	cur->children = xmlStringGetNodeList(doc, buffer);
! 	cur->last = NULL;
! 	tmp = cur->children;
! 	while (tmp != NULL) {
! 	    tmp->parent = (xmlNodePtr) cur;
! 	    if (tmp->next == NULL)
! 		cur->last = tmp;
! 	    tmp = tmp->next;
! 	}
! 	xmlFree(buffer);
!     }
! 
!     /*
!      * Add it at the end to preserve parsing order ...
!      */
!     if (node != NULL) {
! 	if (node->properties == NULL) {
! 	    node->properties = cur;
! 	} else {
! 	    xmlAttrPtr prev = node->properties;
! 
! 	    while (prev->next != NULL) prev = prev->next;
! 	    prev->next = cur;
! 	    cur->prev = prev;
! 	}
!     }
! 
!     if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
! 	xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
!     return(cur);
  }
  
  /**
--- 1845,1851 ----
  	return(NULL);
      }
  
!     return xmlNewPropInternal(node, ns, name, value, 0);
  }
  
  /**
***************
*** 1901,1908 ****
  xmlAttrPtr
  xmlNewNsPropEatName(xmlNodePtr node, xmlNsPtr ns, xmlChar *name,
             const xmlChar *value) {
-     xmlAttrPtr cur;
-     xmlDocPtr doc = NULL;
  
      if (name == NULL) {
  #ifdef DEBUG_TREE
--- 1861,1866 ----
***************
*** 1912,1970 ****
  	return(NULL);
      }
  
!     /*
!      * Allocate a new property and fill the fields.
!      */
!     cur = (xmlAttrPtr) xmlMalloc(sizeof(xmlAttr));
!     if (cur == NULL) {
! 	xmlTreeErrMemory("building attribute");
! 	return(NULL);
!     }
!     memset(cur, 0, sizeof(xmlAttr));
!     cur->type = XML_ATTRIBUTE_NODE;
! 
!     cur->parent = node; 
!     if (node != NULL) {
! 	doc = node->doc;
! 	cur->doc = doc;
!     }
!     cur->ns = ns;
!     cur->name = name;
!     if (value != NULL) {
! 	xmlChar *buffer;
! 	xmlNodePtr tmp;
! 
! 	buffer = xmlEncodeEntitiesReentrant(doc, value);
! 	cur->children = xmlStringGetNodeList(doc, buffer);
! 	cur->last = NULL;
! 	tmp = cur->children;
! 	while (tmp != NULL) {
! 	    tmp->parent = (xmlNodePtr) cur;
! 	    if (tmp->next == NULL)
! 		cur->last = tmp;
! 	    tmp = tmp->next;
! 	}
! 	xmlFree(buffer);
!     }
! 
!     /*
!      * Add it at the end to preserve parsing order ...
!      */
!     if (node != NULL) {
! 	if (node->properties == NULL) {
! 	    node->properties = cur;
! 	} else {
! 	    xmlAttrPtr prev = node->properties;
! 
! 	    while (prev->next != NULL) prev = prev->next;
! 	    prev->next = cur;
! 	    cur->prev = prev;
! 	}
!     }
! 
!     if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
! 	xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
!     return(cur);
  }
  
  /**
--- 1870,1876 ----
  	return(NULL);
      }
  
! 	return xmlNewPropInternal(node, ns, name, value, 1);
  }
  
  /**
***************
*** 2250,2255 ****
--- 2156,2162 ----
       */
      cur = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
      if (cur == NULL) {
+ 	xmlFree(name);
  	xmlTreeErrMemory("building node");
  	return(NULL);
      }
***************
*** 6311,6327 ****
      while (prop != NULL) {
          if ((xmlStrEqual(prop->name, name)) &&
  	    (prop->ns == NULL)) {
! 	    if (prev == NULL) {
! 		node->properties = prop->next;
! 		if (prop->next != NULL)
! 		    prop->next->prev = NULL;
! 	    } else {
! 		prev->next = prop->next;
! 		if (prop->next != NULL)
! 		    prop->next->prev = NULL;
! 	    }
! 	    prop->next = NULL;
! 	    prop->prev = NULL;
  	    xmlFreeProp(prop);
  	    return(0);
  	}
--- 6218,6224 ----
      while (prop != NULL) {
          if ((xmlStrEqual(prop->name, name)) &&
  	    (prop->ns == NULL)) {
! 		xmlUnlinkNode((xmlNodePtr) prop);
  	    xmlFreeProp(prop);
  	    return(0);
  	}
***************
*** 6354,6370 ****
      while (prop != NULL) {
          if ((xmlStrEqual(prop->name, name)) &&
  	    (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) {
! 	    if (prev == NULL) {
! 		node->properties = prop->next;
! 		if (prop->next != NULL)
! 		    prop->next->prev = NULL;
! 	    } else {
! 		prev->next = prop->next;
! 		if (prop->next != NULL)
! 		    prop->next->prev = NULL;
! 	    }
! 	    prop->next = NULL;
! 	    prop->prev = NULL;
  	    xmlFreeProp(prop);
  	    return(0);
  	}
--- 6251,6257 ----
      while (prop != NULL) {
          if ((xmlStrEqual(prop->name, name)) &&
  	    (prop->ns != NULL) && (xmlStrEqual(prop->ns->href, ns->href))) {
! 	    xmlUnlinkNode((xmlNodePtr) prop);
  	    xmlFreeProp(prop);
  	    return(0);
  	}


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