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

📄 nodeact.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.9 $//	$Date: 2004/08/16 03:17:47 $#include "../../inc/tools/config.h"#if EXCLUDE_DOM == 0#include "../../inc/upnpdom/NodeAct.h"#include "../../inc/upnpdom/DOMException.h"void NodeAct_NodeAct_4(NodeAct *pNodeAct, enum NODE_TYPE nt, char *NodeName, char *NodeValue, struct strNode *myCreator){	if(NodeName !=NULL)	{		pNodeAct->NA_NodeName=(char *) malloc ( strlen(NodeName)+2 );		if(!pNodeAct->NA_NodeName)		   	{DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)}		strcpy(pNodeAct->NA_NodeName, NodeName);	}	else		pNodeAct->NA_NodeName =NULL;	if(NodeValue !=NULL)	{		pNodeAct->NA_NodeValue=(char *) malloc ( strlen(NodeValue)+2 );		if(!pNodeAct->NA_NodeName)		   	{DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)}		strcpy(pNodeAct->NA_NodeValue, NodeValue);	}	else		pNodeAct->NA_NodeValue =NULL;	pNodeAct->NA_NodeType = nt;	pNodeAct->ParentNode=NULL;	pNodeAct->OwnerNode=pNodeAct;	pNodeAct->NextSibling=NULL;	pNodeAct->PrevSibling=NULL;	pNodeAct->FirstChild=NULL;	pNodeAct->LastChild=NULL;	pNodeAct->FirstAttr=NULL;	pNodeAct->LastAttr=NULL;	pNodeAct->Creator=myCreator;	pNodeAct->RefCount=0;}void NodeAct_NodeAct_2(NodeAct *pNodeAct, NodeAct *other, short int deep){	NodeAct *mykid;	NodeAct_setName(pNodeAct, other->NA_NodeName);   //maybe needing modified again --- ronger	NodeAct_setValue(pNodeAct, other->NA_NodeValue);	pNodeAct->NA_NodeType=other->NA_NodeType;    	pNodeAct->Creator = other->Creator;	pNodeAct->OwnerNode=other->OwnerNode;    	pNodeAct->RefCount=1;    // Need to break the association w/ original kids    	pNodeAct->PrevSibling = NULL;    	pNodeAct->NextSibling = NULL;    	pNodeAct->ParentNode = NULL;    	pNodeAct->FirstChild = NULL;    	pNodeAct->LastChild = NULL;	pNodeAct->FirstAttr = NULL;	pNodeAct->LastAttr = NULL;    // Then, if deep, clone the kids too.    if (deep)    {        for (mykid = other->FirstChild;        mykid != NULL;        mykid = mykid->NextSibling)            NodeAct_appendChild(pNodeAct, NodeAct_cloneNode(pNodeAct, 1));    }}//Returns true if it finds the node in the tree strating from the node under reference//Searches children as well as attribute nodes.short int NodeAct_findNode(NodeAct *pNodeAct, NodeAct *find){	short int notFound=1;		if(find!=NULL)	{		//check the children		NodeAct *na;		na=pNodeAct->FirstChild;		while(na !=NULL)		{			if(na == find)			{				notFound=0;				break;			}			na = na->NextSibling;		}		//check the attributes if the parent is an element		if(pNodeAct->NA_NodeType == ELEMENT_NODE)		{			na = pNodeAct->FirstAttr;			while(na !=NULL)			{				if(na == find)				{					notFound=0;					break;				}				na = na->NextSibling;			}		}		return(!notFound);	}	else return(0);}//Returns true if it finds the node in the whole tree strating from the "from" node//Searches children as well as attribute nodes.short int NodeAct_findNodeFromRef(NodeAct *from, NodeAct *find){	NodeAct *mykid;	static short int Found=0;    for (mykid = from->FirstChild; mykid != NULL; mykid = mykid->NextSibling)	{		NodeAct_findNodeFromRef(from, find);		if(NodeAct_findNode(from, find))		{			Found=1;			return(Found);		}	}	return Found;}void NodeAct_changeOwnerNode(NodeAct *n, NodeAct *newOwner){    NodeAct *mykid;    for (mykid = n->FirstChild; mykid != NULL; mykid = mykid->NextSibling)	{		NodeAct_changeOwnerNode(mykid, newOwner);		mykid->OwnerNode=newOwner;	}}void NodeAct_insertBefore(NodeAct *pNodeAct, NodeAct *newChild, NodeAct *refChild){	if(refChild!=NULL)	{		//Makesure that the ref child is found		//Otherwise raise exception NOT_FOUND_ERR		if(!NodeAct_findNode(pNodeAct, refChild))		{			Handle_DOMException(NOT_FOUND_ERR_DOM);			//throw DOMException(DOMException::NOT_FOUND_ERR);			return;		}		if(NodeAct_findNode(pNodeAct, newChild))		{			NodeAct_removeChild(pNodeAct, newChild);			newChild->NextSibling =NULL;			newChild->PrevSibling =NULL;		}		//Todo: Raise exception for if the node is one of the ancestors, etc..		newChild->RefCount++;		newChild->NextSibling=refChild;		if(refChild->PrevSibling !=NULL)			refChild->PrevSibling->NextSibling=newChild;		newChild->NextSibling=refChild;		refChild->PrevSibling=newChild;		if((newChild->NA_NodeType != ATTRIBUTE_NODE)&&(newChild->PrevSibling==NULL))			pNodeAct->FirstChild=newChild;		else if((newChild->NA_NodeType == ATTRIBUTE_NODE)&&(newChild->PrevSibling==NULL))			pNodeAct->FirstAttr=newChild;		newChild->ParentNode=pNodeAct;		newChild->OwnerNode =pNodeAct->OwnerNode;		//Todo: Atrributes owner node must be changed to newchilds owner node	}	else		NodeAct_appendChild(pNodeAct, newChild);}void NodeAct_replaceChild(NodeAct *pNodeAct, NodeAct *newChild, NodeAct *oldChild){	if(oldChild!=NULL)	{		//Makesure that the ref child is found		//Otherwise raise exception NOT_FOUND_ERR		if(!NodeAct_findNode(pNodeAct, oldChild))		{			Handle_DOMException(NOT_FOUND_ERR_DOM);			//throw DOMException(DOMException::NOT_FOUND_ERR);			return;		}		NodeAct_insertBefore(pNodeAct, newChild, oldChild);		NodeAct_removeChild(pNodeAct, oldChild);	}}void NodeAct_removeChild(NodeAct *pNodeAct, NodeAct *oldChild){	//Makesure that the ref child is found	//Otherwise raise exception NOT_FOUND_ERR	if(!NodeAct_findNode(pNodeAct, oldChild))	{		Handle_DOMException(NOT_FOUND_ERR_DOM);		//throw DOMException(DOMException::NOT_FOUND_ERR);		return;	}	if(oldChild->PrevSibling !=NULL)   		oldChild->PrevSibling->NextSibling=oldChild->NextSibling;   	if(oldChild->NextSibling !=NULL)   		oldChild->NextSibling->PrevSibling=oldChild->PrevSibling;   	if(pNodeAct->FirstChild==oldChild)   		pNodeAct->FirstChild=oldChild->NextSibling;   	if(pNodeAct->LastChild ==oldChild)   		pNodeAct->LastChild=oldChild->PrevSibling;   	oldChild->OwnerNode=oldChild;   	oldChild->ParentNode=NULL;   	oldChild->RefCount=1;}NodeAct * NodeAct_cloneNode(NodeAct *pNodeAct, short int deep){    NodeAct *newnode;    newnode = (NodeAct *) malloc (sizeof(NodeAct) + 1);        NodeAct_NodeAct_2(newnode, pNodeAct, deep);    //newnode = new NodeAct(*this, deep);	if(!newnode)	   	{DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)}    return newnode;}//Appends the child to the node.//If the child exists before its first removed//If the child has children all of them will be appendedvoid NodeAct_appendChild(NodeAct *pNodeAct, NodeAct *newChild){	if(NodeAct_findNodeFromRef(pNodeAct->OwnerNode, newChild))	{		NodeAct_removeChild(newChild->ParentNode, newChild);		newChild->ParentNode=pNodeAct;		newChild->OwnerNode =pNodeAct->OwnerNode;		NodeAct_changeOwnerNode(newChild,pNodeAct->OwnerNode);		newChild->NextSibling =NULL;		newChild->PrevSibling =NULL;	}	else	{		newChild->ParentNode=pNodeAct;		newChild->OwnerNode =pNodeAct->OwnerNode;		NodeAct_changeOwnerNode(newChild,pNodeAct->OwnerNode);		newChild->NextSibling =NULL;		newChild->PrevSibling =NULL;	}	newChild->RefCount++;	if(newChild->NA_NodeType != ATTRIBUTE_NODE)	{		//if this is the first child		if(pNodeAct->FirstChild ==NULL)		{			pNodeAct->FirstChild = newChild;			pNodeAct->LastChild = newChild;		}		else		{			pNodeAct->LastChild->NextSibling=newChild;			newChild->PrevSibling=pNodeAct->LastChild;			pNodeAct->LastChild=newChild;		}	}	else	{		//if this is the first attribute		if(pNodeAct->FirstAttr ==NULL)		{			pNodeAct->FirstAttr = newChild;			pNodeAct->LastAttr = newChild;		}		else		{			pNodeAct->LastAttr->NextSibling=newChild;			newChild->PrevSibling=pNodeAct->LastAttr;			pNodeAct->LastAttr=newChild;		}	}}void NodeAct_setName(NodeAct *pNodeAct, char *n){	if(n!=NULL)	{		pNodeAct->NA_NodeName=(char *) malloc (strlen(n)+1);		if(!pNodeAct->NA_NodeName)		   	{DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)}		strcpy(pNodeAct->NA_NodeName, n);	}	else		pNodeAct->NA_NodeName=NULL;}void NodeAct_setValue(NodeAct *pNodeAct, char *v){	if(v!=NULL)	{		pNodeAct->NA_NodeValue=(char *) malloc (strlen(v)+1);		if(!pNodeAct->NA_NodeValue)		   	{DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");)}		strcpy(pNodeAct->NA_NodeValue, v);	}	else		pNodeAct->NA_NodeValue=NULL;}void NodeAct_NodeAct_free(NodeAct *pNodeAct){	if(pNodeAct->NA_NodeName!=NULL)		free(pNodeAct->NA_NodeName);	if(pNodeAct->NA_NodeValue!=NULL)		free(pNodeAct->NA_NodeValue);//	deleteNodeTree(this);}void NodeAct_deleteNodeAct(NodeAct *pNodeAct, NodeAct *na){	while(pNodeAct->FirstChild!=NULL)//delete all except itself		NodeAct_deleteNodeTree(pNodeAct, na);}void NodeAct_deleteNodeTree(NodeAct *pNodeAct, NodeAct *na){//recurse through the entire tree and delete the leaf	if(na->FirstChild != NULL)	{		na =na->FirstChild;		NodeAct_deleteNodeTree(pNodeAct, na);	}	else	{		NodeAct *nap;		nap=na->ParentNode;		if(nap != NULL)		{			nap->FirstChild=na->NextSibling;//Point the parent to the next sibling			nap->LastChild=NULL;//dont care		}		//delete all the attributes if present in the element node		if(na->NA_NodeType == ELEMENT_NODE)		{			NodeAct *attr;			attr=na->FirstAttr;			while(attr!= NULL){				NodeAct *na1;				na1=attr->NextSibling;				free(attr);				attr =na1;			}			na->FirstAttr=NULL;			na->LastAttr=NULL;		}		if(na!=pNodeAct)				free(na);	}}#endif

⌨️ 快捷键说明

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