📄 mcbxml.c
字号:
int n;
/*
*************************************************************************
* Structure for errors array
*************************************************************************
*/
typedef struct McbErrorList
{
enum McbXMLError err;
LPCTSTR lpszErr;
} McbErrorList;
/*
*************************************************************************
* Static array containing helpful error text.
*************************************************************************
*/
static struct McbErrorList errs[] =
{
{ eXMLErrorNone, _T("No error") },
{ eXMLErrorEmpty, _T("No XML data") },
{ eXMLErrorFirstNotStartTag, _T("First token not start tag") },
{ eXMLErrorMissingTagName, _T("Missing start tag name") },
{ eXMLErrorMissingEndTagName, _T("Missing end tag name") },
{ eXMLErrorNoMatchingQuote, _T("Unmatched quote") },
{ eXMLErrorUnmatchedEndTag, _T("Unmatched end tag") },
{ eXMLErrorUnexpectedToken, _T("Unexpected token found") },
{ eXMLErrorInvalidTag, _T("Invalid tag found") },
{ eXMLErrorNoElements, _T("No elements found") },
{ 0, NULL }
};
/*
*************************************************************************
* Iterate through the list of errors to find a matching error
*************************************************************************
*/
for(n = 0; errs[n].lpszErr; n++)
{
if (errs[n].err == error)
{
lpszErr = errs[n].lpszErr;
break;
}
}
return lpszErr;
}/* McbGetError */
/**
****************************************************************************
* <P> Delete memory associated with a text node. </P>
*
* @methodName McbDeleteText
*
* @param *pText
*
* @return void
*
* @exception none
*
* @author Martyn C Brown
*
* @changeHistory
* 20th August 2001 - (V1.0) Creation (MCB)
****************************************************************************
*/
void McbDeleteText(McbXMLText *pText)
{
assert(pText);
if (pText->lpszValue)
{
free(pText->lpszValue);
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;
//toby add 2006/7/4 10:03
pAttr->lpszName = lpszName;//McbStrdup(lpszName,0);
pAttr->lpszValue = lpszValue;//McbStrdup(lpszValue,0);
/*
*************************************************************************
* Increment the number of contained nodes
*************************************************************************
*/
pEntry->nSize++;
/*
*************************************************************************
* Return the new attribute.
*************************************************************************
*/
return pAttr;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -