⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 node.c

📁 upnpdom main part of upnp
💻 C
字号:
/////////////////////////////////////////////////////////////////////////////// Copyright (c) 2000 Intel Corporation// All rights reserved.//// Redistribution and use in source and binary forms, with or without// modification, are permitted provided that the following conditions are met://// * Redistributions of source code must retain the above copyright notice,// this list of conditions and the following disclaimer.// * Redistributions in binary form must reproduce the above copyright notice,// this list of conditions and the following disclaimer in the documentation// and/or other materials provided with the distribution.// * Neither name of Intel Corporation nor the names of its contributors// may be used to endorse or promote products derived from this software// without specific prior written permission.//// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.///////////////////////////////////////////////////////////////////////////////	$Revision: 1.1.2.7 $//	$Date: 2004/08/16 03:17:47 $#include "../../inc/tools/config.h"#if EXCLUDE_DOM == 0#include "../../inc/upnpdom/Node.h"#include "../../inc/upnpdom/DOMException.h"void Node_Node_0(Node *pNode){	pNode->nact=NULL;	pNode->ownerNode=pNode;}//The destructor function checks to see if no other node is referencing its node act//Only then it destroys its nodeact.#if 0void Node_Node_free(Node *pNode){	if(pNode->nact !=NULL)	{		pNode->nact->RefCount--;		if(pNode->nact->RefCount==0)//check for children and delete recursively..		{			NodeAct_deleteNodeAct(pNode->nact);			free(pNode->nact);		}	}	pNode->nact=NULL;}#endif//Returns the nodename of this node//User has to free the memorychar*	Node_getNodeName(Node *pNode){	if(pNode->nact !=NULL)	{		char *retNodeName=NULL;		if(!pNode->nact->NA_NodeName)			return NULL;		retNodeName = (char *) malloc (strlen(pNode->nact->NA_NodeName)+1);		if(!retNodeName)		{			DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)			Handle_DOMException(INSUFFICIENT_MEMORY);			//throw DOMException(DOMException::INSUFFICIENT_MEMORY);		}		strcpy(retNodeName, pNode->nact->NA_NodeName);		return retNodeName;	}	else	{		Handle_DOMException(NO_SUCH_NODE);		//throw DOMException(DOMException::NO_SUCH_NODE);		return NULL;	}}//Returns the nodeValue of this node//User has to free the memorychar*	Node_getNodeValue(Node *pNode){	if(pNode->nact !=NULL)	{		char *retNodeValue=NULL;		if(!pNode->nact->NA_NodeValue)			return NULL;		retNodeValue = (char *) malloc (strlen(pNode->nact->NA_NodeValue)+1);		if(!retNodeValue)		{			DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)			Handle_DOMException(INSUFFICIENT_MEMORY);			//throw DOMException(DOMException::INSUFFICIENT_MEMORY);		}		strcpy(retNodeValue, pNode->nact->NA_NodeValue);		return retNodeValue;	}	else	{		Handle_DOMException(NO_SUCH_NODE);		//throw DOMException(DOMException::NO_SUCH_NODE);		return NULL;	}}//Sets the nodeValue of this node if the node is present//If the node is not found throws an exceptionint	Node_setNodeValue(Node *pNode, char *newNodeValue){	if(pNode->nact!=NULL)	{		if(pNode->nact->NA_NodeValue !=NULL)			free(pNode->nact->NA_NodeValue);		pNode->nact->NA_NodeValue =NULL;			pNode->nact->NA_NodeValue=(char *) malloc (strlen(newNodeValue)+1);		if(!pNode->nact->NA_NodeValue)		{			DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)			//throw DOMException(DOMException::INSUFFICIENT_MEMORY);			Handle_DOMException(INSUFFICIENT_MEMORY);				return -1;		}		strcpy(pNode->nact->NA_NodeValue, newNodeValue);	}	else	{		return -1;		Handle_DOMException(NO_SUCH_NODE);		//throw DOMException(DOMException::NO_SUCH_NODE);	}	return 0;}//Gets the NodeType of this nodeunsigned short Node_getNodeType(Node *pNode){	if(pNode->nact!=NULL)		return(pNode->nact->NA_NodeType);	else	{		Handle_DOMException(NO_SUCH_NODE);		//throw DOMException(DOMException::NO_SUCH_NODE);		return 0;	}}//Returns the parent node of the node under reference//To see if it actually returned NULL use isNull() functionNode	*Node_getParentNode(Node *pNode){	Node *returnNode;	returnNode = (Node *) malloc (sizeof(Node) + 1);	Node_Node_0(returnNode);	if(!returnNode)		{DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)}	returnNode->nact=pNode->nact->ParentNode;	if(returnNode->nact!=NULL)		returnNode->nact->RefCount++;	return returnNode;}//Returns the first child of the node under reference//To see if it actually returned NULL use isNull() functionNode	*Node_getFirstChild(Node *pNode){	Node *returnNode;        returnNode = (Node *) malloc (sizeof(Node) + 1);	Node_Node_0(returnNode);	//returnNode = new Node();	if(!returnNode)		{DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)}	returnNode->nact=pNode->nact->FirstChild;	if(Node_isNull(returnNode))		return returnNode;	returnNode->nact->RefCount++;	return returnNode;}//Returns the last child of the node under reference//To see if it actually returned NULL use isNull() functionNode	*Node_getLastChild(Node *pNode){	Node *returnNode;        returnNode = (Node *) malloc (sizeof(Node) + 1);	Node_Node_0(returnNode);	//returnNode = new Node();	if(!returnNode)		{DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)}	returnNode->nact=pNode->nact->LastChild;	if(Node_isNull(returnNode))		return returnNode;	returnNode->nact->RefCount++;	return returnNode;}//Returns the previous sibling of the node under reference//To see if it actually returned NULL use isNull() functionNode	*Node_getPreviousSibling(Node *pNode){	Node *returnNode;        returnNode = (Node *) malloc (sizeof(Node) + 1);	Node_Node_0(returnNode);	//returnNode = new Node();	if(!returnNode)		{DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)}	returnNode->nact=pNode->nact->PrevSibling;	if(Node_isNull(returnNode))		return returnNode;	returnNode->nact->RefCount++;	return returnNode;}//Returns the next sibling of the node under reference//To see if it actually returned NULL use isNull() functionNode	*Node_getNextSibling(Node *pNode){	Node *returnNode;        returnNode = (Node *) malloc (sizeof(Node) + 1);	Node_Node_0(returnNode);	//returnNode = new Node();	if(!returnNode)		{DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)}	returnNode->nact=pNode->nact->NextSibling;	if(Node_isNull(returnNode))		return returnNode;	returnNode->nact->RefCount++;	return returnNode;}//Gets the owner Document//To see if it actually returned NULL use isNull() functionstruct strDocument	*Node_getOwnerDocument(Node *pNode, struct strDocument *pDocument){	struct strDocument *returnDoc;	struct strNode *p;	returnDoc = (struct strDocument *) malloc (sizeof(struct strDocument) + 1);	Document_Document_0(returnDoc, pNode);	//returnDoc = new Document;	if(!returnDoc)		{DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)}	p = (struct strNode *)returnDoc;	p->nact= pNode->nact->OwnerNode;	p->nact->RefCount++;	returnDoc = (struct strDocument *)p;	return returnDoc;}void Node_createNode(struct strNode *returnNode, enum NODE_TYPE nt, char *NodeName, char *NodeValue){	returnNode = (struct strNode *) malloc (sizeof(struct strNode) + 1);	if(!returnNode)		{DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)}	returnNode->nact= (struct strNodeAct *) malloc (sizeof(struct strNodeAct) + 1);	NodeAct_NodeAct_4(returnNode->nact, nt, NodeName, NodeValue, returnNode);	if(!(returnNode->nact))		{DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)}		returnNode->nact->RefCount++;}Node	*Node_insertBefore(Node *pNode, Node *newChild, Node *refChild){	Node *returnNode;        returnNode = (Node *) malloc (sizeof(Node) + 1);	Node_Node_0(returnNode);	//returnNode = new Node();	if(!returnNode)		Handle_DOMException(INSUFFICIENT_MEMORY);		//throw DOMException(DOMException::INSUFFICIENT_MEMORY);	//try	//{	if(!Node_insertBefore((Node *)pNode->nact, (Node *)newChild->nact, (Node *)refChild->nact)) 	//}	//{		Handle_DOMException(NOT_FOUND_ERR_DOM);	//}	//catch (DOMException& toCatch)	//{	//	throw DOMException( toCatch.code);	//}	returnNode->nact=newChild->nact;	return returnNode;}Node	*Node_replaceChild(Node *pNode, Node *newChild, Node *oldChild){	Node *returnNode;	returnNode = (Node *) malloc (sizeof(Node) + 1);	Node_Node_0(returnNode);	if(!returnNode)	{		DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)		Handle_DOMException(INSUFFICIENT_MEMORY);		//throw DOMException(DOMException::INSUFFICIENT_MEMORY);	}	//try	//{	if(!Node_replaceChild((Node *)pNode->nact, (Node *)newChild->nact, (Node *)oldChild->nact)) 	{		Handle_DOMException(NOT_FOUND_ERR_DOM);	}	//}	//catch (DOMException& toCatch)	//{	//	throw DOMException( toCatch.code);	//}	newChild->nact=oldChild->nact;	returnNode->nact=newChild->nact;	return returnNode;}Node	*Node_removeChild(Node *pNode, Node *oldChild){	Node *returnNode;	returnNode = (Node *) malloc (sizeof(Node) + 1);	Node_Node_0(returnNode);	if(!returnNode)	{		DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)		Handle_DOMException(INSUFFICIENT_MEMORY);		//throw DOMException(DOMException::INSUFFICIENT_MEMORY);	}	//try	//{	if (! Node_removeChild((Node *)pNode->nact, (Node *)oldChild->nact))	{		Handle_DOMException(NOT_FOUND_ERR_DOM);	}		//}	//catch (DOMException& toCatch)	//{	//	throw DOMException( toCatch.code);	//}	returnNode->nact=oldChild->nact;	return returnNode;}	Node *Node_appendChild(Node *pNode, Node *newChild){	Node *returnNode;	returnNode = (Node *) malloc (sizeof(Node) + 1);   //new Node();	Node_Node_0(returnNode);	if(!returnNode)		{DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)}	Node_appendChild((Node *)pNode->nact, (Node *)newChild->nact);	returnNode->nact=newChild->nact;	return returnNode;}Node *Node_cloneNode(Node *pNode, short int deep){	Node *returnNode;	returnNode = (Node *) malloc (sizeof(Node) + 1);  //new Node();	Node_Node_0(returnNode);	if(!returnNode)		{DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)}	returnNode->nact=(struct strNodeAct *)Node_cloneNode((Node *)pNode->nact, deep);	return returnNode;}struct strNodeList *Node_getChildNodes(Node *pNode){	struct strNodeAct *na;	struct strNodeList *returnNodeList;	returnNodeList= (struct strNodeList *) malloc (sizeof(struct strNodeList) + 1);	if(!returnNodeList)		{DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)}	returnNodeList->ownerNodeList=returnNodeList;	na=pNode->nact->FirstChild;	while(na !=NULL)	{		NodeList_addToInternalList(returnNodeList, na);		na=na->NextSibling;	}	return returnNodeList;}struct strNamedNodeMap *Node_getAttributes(Node *pNode){	struct strNamedNodeMap *returnNamedNodeMap;	returnNamedNodeMap= (struct strNamedNodeMap *) malloc (sizeof(struct strNamedNodeMap) + 1);	if(!returnNamedNodeMap)		{DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)}	returnNamedNodeMap->ownerNamedNodeMap=returnNamedNodeMap;	returnNamedNodeMap->ofWho=pNode;	return returnNamedNodeMap;}int Node_hasChildNodes(Node *pNode){	return(pNode->nact->FirstChild!=NULL);}short int Node_isNull(Node *pNode){    return pNode->nact == NULL;}//Call this function whenever a node is returned back to you//Also one can use this function in place of delete when created on the heap.void	Node_deleteNode(Node *pNode){	free(pNode->ownerNode);	pNode->nact=NULL;}/***Node & Node::operator = (const Node &other){    if (this->nact != other.nact)    {        this->nact = other.nact;		if(this->nact !=NULL)			this->nact->RefCount++;    }	this->ownerNode=other.ownerNode;    return *this;};****/Node *Node_Node_operator_eq(Node *pNode, const Node *other){    if (pNode->nact != other->nact)    {        pNode->nact = other->nact;                if(pNode->nact !=NULL)                        pNode->nact->RefCount++;    }        pNode->ownerNode=other->ownerNode;    return pNode;}void Node_SearchList(Node *n, char *tagname, struct strNodeList *lst, short int ignorePrefix) {    struct strNode *child = Node_getFirstChild(n);    Node *temp;    while (!Node_isNull(child)) {        temp=Node_getNextSibling(child);        char *name;        char *name2;        if (Node_getNodeType(child)==ELEMENT_NODE){            name=Node_getNodeName(child);            if (ignorePrefix) {                name2=strchr(name,':');                if (name2==NULL) name2=name;                else name2++;            }            else name2=name;            if (strcmp(tagname,name2)==0||strcmp(tagname,"*")==0)                NodeList_addToInternalList(lst, child->nact);            free(name);            }        Node_SearchList(child, tagname, lst, ignorePrefix);        child = temp;    }    if(Node_isNull(child))    	Node_deleteNode(child);	if(Node_getNodeType(n)!=DOCUMENT_NODE)		Node_deleteNode(n);}#endif		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -