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

📄 mcbxml.c

📁 解析xml为树状结构
💻 C
📖 第 1 页 / 共 5 页
字号:
		pText->lpszValue = NULL;
	}

}/* McbDeleteText */

/**
 ****************************************************************************
 * <P> Delete memory associated with a clear (unformatted) node.   </P>
 *
 * @methodName  McbDeleteClear
 *
 * @param       *pClear		
 *
 * @return      void
 *
 * @exception   none
 *
 * @author      Martyn C Brown
 *
 * @changeHistory  
 *	20th August    	2001	 - 	(V1.0) Creation (MCB)
 ****************************************************************************
 */
void McbDeleteClear(McbXMLClear *pClear)
{
	assert(pClear);

	if (pClear->lpszValue)
	{
		free(pClear->lpszValue);
		pClear->lpszValue = NULL;
	}

}/* McbDeleteClear */

/**
 ****************************************************************************
 * <P> Delete a given node.  </P>
 *
 * @methodName  McbDeleteNode
 *
 * @param       *pEntry		
 *
 * @return      void
 *
 * @exception   none
 *
 * @author      Martyn C Brown
 *
 * @changeHistory  
 *	20th August    	2001	 - 	(V1.0) Creation (MCB)
 ****************************************************************************
 */
void McbDeleteNode(McbXMLNode *pEntry)
{
	if (pEntry)
	{
		if (pEntry->type == eNodeEmpty)
		{
			return;
		}

       /*
        *********************************************************************
        * Delete the appropriate node
        *********************************************************************
        */
		switch(pEntry->type)
		{
		case eNodeAttribute:
			McbDeleteAttribute(pEntry->node.pAttrib);
			break;
			
		case eNodeElement:
			McbDeleteElement(pEntry->node.pElement);
			break;

		case eNodeText:
			McbDeleteText(pEntry->node.pText);
			break;

		case eNodeClear:
			McbDeleteClear(pEntry->node.pClear);
			break;

		default:
			assert(TRUE);
		}	

		free(pEntry->node.pAttrib);
		pEntry->type = eNodeEmpty;
	}

}/* McbDeleteNode */

/**
 ****************************************************************************
 * <P> Delete an element and all it's contained nodes.  </P>
 *
 * @methodName  McbDeleteElement
 *
 * @param       *pEntry		
 *
 * @return      void
 *
 * @exception   none
 *
 * @author      Martyn C Brown
 *
 * @changeHistory  
 *	20th August    	2001	 - 	(V1.0) Creation (MCB)
 ****************************************************************************
 */
void McbDeleteElement(McbXMLElement *pEntry)
{
	int n;

	assert(pEntry);

   /*
    *************************************************************************
    * Delete each node (this may recurse)
    *************************************************************************
    */
	for(n = 0; n<pEntry->nSize; n++)
	{
		McbDeleteNode(&pEntry->pEntries[n]);
	}

   /*
    *************************************************************************
    * Cleanup the list of nodes
    *************************************************************************
    */
	pEntry->nMax = 0;
	pEntry->nSize = 0;

	free(pEntry->pEntries);
	pEntry->pEntries = NULL;
	
   /*
    *************************************************************************
    * Delete the name of the element
    *************************************************************************
    */
	if (pEntry->lpszName)
	{
		free(pEntry->lpszName);
		pEntry->lpszName = NULL;
	}

	/* free(pEntry); */

}/* McbDeleteElement */

/**
 ****************************************************************************
 * <P> Attach nodes from one list to another.  </P>
 *
 * @methodName  McbAttachNodes
 *
 * @param       *pDst		
 * @param       *pSrc		
 * @param       nNum		
 *
 * @return      void
 *
 * @exception   none
 *
 * @author      Martyn C Brown
 *
 * @changeHistory  
 *	14th August    	2001	 - 	(V1.0) Creation (MCB)
 ****************************************************************************
 */
void McbAttachNodes(McbXMLNode *pDst, McbXMLNode *pSrc, int nNum)
{
	int n;

	for (n=0; n<nNum; n++)
	{
		pDst[n] = pSrc[n];
		pSrc[n].type = eNodeEmpty;
	}

}/* McbAttachNodes */

/**
 ****************************************************************************
 * <P> Reserve memory for additional nodes.  </P>
 *
 * @methodName  McbAllocNodes
 *
 * @param       *pEntry		
 * @param       nGrowBy		
 *
 * @return      void
 *
 * @exception   none
 *
 * @author      Martyn C Brown
 *
 * @changeHistory  
 *	15th August    	2001	 - 	(V1.0) Creation (MCB)
 ****************************************************************************
 */
void McbAllocNodes(McbXMLElement *pEntry, int nGrowBy)
{
	int nMax;
	McbXMLNode * pNew;

	assert(pEntry);
	assert(nGrowBy > 0);

   /*
    *************************************************************************
    * Allocate storage for new nodes
    *************************************************************************
    */
    nMax = pEntry->nMax += nGrowBy;
    pNew = malloc(sizeof(McbXMLNode) * nMax);

   /*
    *************************************************************************
    * Attach old entries to the new storage
    *************************************************************************
    */
    McbAttachNodes(pNew, pEntry->pEntries, pEntry->nSize);

	if (pEntry->pEntries)
	{
		free(pEntry->pEntries);
	}

    pEntry->pEntries = pNew;

}/* McbAllocNodes */

/**
 ****************************************************************************
 * <P> Add an element node to the element.  </P>
 *
 * @methodName  McbAddElement
 *
 * @param       *pEntry		
 * @param       lpszName		
 * @param       nIsDeclaration
 * @param       nGrowBy		
 *
 * @return      McbXMLElement *
 *
 * @exception   none
 *
 * @author      Martyn C Brown
 *
 * @changeHistory  
 *	20th August    	2001	 - 	(V1.0) Creation (MCB)
 ****************************************************************************
 */
McbXMLElement * McbAddElement(McbXMLElement *pEntry, LPTSTR lpszName, 
							 int nIsDeclaration, int nGrowBy)
{
	McbXMLNode *pNode;
	McbXMLElement * pElement;

	assert(pEntry);
	assert(nGrowBy > 0);

   /*
    *************************************************************************
    * Check we have enough storage
    *************************************************************************
    */
	if (pEntry->nSize == pEntry->nMax)
	{
		McbAllocNodes(pEntry, nGrowBy);
	}

   /*
    *************************************************************************
    * Obtain the new node, set the type and initialise it.
    *************************************************************************
    */
	pNode = &pEntry->pEntries[pEntry->nSize];
	pNode->type = eNodeElement;

	pElement = malloc(sizeof(McbXMLElement));
	pNode->node.pElement = pElement;

	McbInitElement(pElement, pEntry, lpszName, nIsDeclaration);

   /*
    *************************************************************************
    * Increment the number of contained nodes
    *************************************************************************
    */
	pEntry->nSize++;

   /*
    *************************************************************************
    * Return the new element.
    *************************************************************************
    */
	return pElement;

}/* McbAddElement */

/**
 ****************************************************************************
 * <P> Add an attribute to an element.  </P>
 *
 * @methodName  McbAddAttribute
 *
 * @param       *pEntry		
 * @param       lpszName		
 * @param       lpszValue		
 * @param       nGrowBy		
 *
 * @return      McbXMLAttribute * 
 *
 * @exception   none
 *
 * @author      Martyn C Brown
 *
 * @changeHistory  
 *	14th August    	2001	 - 	(V1.0) Creation (MCB)
 ****************************************************************************
 */
McbXMLAttribute * McbAddAttribute(McbXMLElement *pEntry, LPTSTR lpszName, 
					LPTSTR lpszValue, int nGrowBy)
{
	McbXMLNode *pNode;
	McbXMLAttribute *pAttr;

	assert(pEntry);
	assert(nGrowBy > 0);

   /*
    *************************************************************************
    * Check we have enough storage
    *************************************************************************
    */
	if (pEntry->nSize == pEntry->nMax)
	{
		McbAllocNodes(pEntry, nGrowBy);
	}

   /*
    *************************************************************************
    * Obtain the new node, set the type and initialise it.
    *************************************************************************
    */
	pNode = &pEntry->pEntries[pEntry->nSize];
	pNode->type = eNodeAttribute;

	pAttr = malloc(sizeof(McbXMLAttribute));
	pNode->node.pAttrib = pAttr;

	pAttr->lpszName = lpszName;
	pAttr->lpszValue = lpszValue;

   /*
    *************************************************************************
    * Increment the number of contained nodes
    *************************************************************************
    */
	pEntry->nSize++;

   /*
    *************************************************************************
    * Return the new attribute.
    *************************************************************************
    */
	return pAttr;

}/* McbAddAttribute */

/**
 ****************************************************************************
 * <P> Add text to the element.  </P>
 *
 * @methodName  * McbAddText
 *
 * @param       *pEntry		
 * @param       lpszValue		
 * @param       nGrowBy		
 *
 * @return      McbXMLText
 *
 * @exception   none
 *
 * @author      Martyn C Brown
 *
 * @changeHistory  
 *	20th August    	2001	 - 	(V1.0) Creation (MCB)
 ****************************************************************************
 */
McbXMLText * McbAddText(McbXMLElement *pEntry, LPTSTR lpszValue, int nGrowBy)
{
	McbXMLNode *pNode;
	McbXMLText *pText;

	assert(pEntry);
	assert(nGrowBy > 0);

   /*
    *************************************************************************
    * Check we have enough storage
    *************************************************************************
    */
	if (pEntry->nSize == pEntry->nMax)
	{
		McbAllocNodes(pEntry, nGrowBy);
	}

   /*
    *************************************************************************
    * Obtain the new node, set the type and initialise it.
    *************************************************************************
    */
	pNode = &pEntry->pEntries[pEntry->nSize];
	pNode->type = eNodeText;

	pText = malloc(sizeof(McbXMLText));
	pNode->node.pText = pText;
	pText->lpszValue = lpszValue;

   /*
    *************************************************************************
    * Increment the number of contained nodes
    *************************************************************************
    */
	pEntry->nSize++;

   /*
    *************************************************************************
    * Return the new attribute.
    *************************************************************************
    */
	return pText;

}/* McbAddText */

/**
 ****************************************************************************
 * <P> Add clear (unformatted) to the element.  </P>
 *

⌨️ 快捷键说明

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