📄 ospxmlparse.c
字号:
}/**//*-----------------------------------------------------------------------* * OSPPXMLDocGetAttr() - get a single attribute *-----------------------------------------------------------------------*/unsigned /* returns error code */OSPPXMLDocGetAttr( OSPTBFR **ospvBfrAddr, /* buffer containing document */ OSPTXMLENC ospvEncoding, /* character encoding for the document */ OSPTXMLATTR **ospvAttrAddr /* list for attributes */){ unsigned ospvErrCode = OSPC_ERR_NO_ERROR; unsigned char readChar = '\0'; unsigned char quoteChar = '\0'; OSPTXMLATTR *attr = OSPC_OSNULL; OSPTBFR *nameBfr = OSPC_OSNULL; OSPTBFR *valBfr = OSPC_OSNULL; unsigned cnt; if (ospvBfrAddr == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } if (*ospvBfrAddr == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } if (ospvEncoding == ospeXMLEncUnknown) { ospvErrCode = OSPC_ERR_XML_BAD_ENC; } if (ospvAttrAddr == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_XML_NO_ATTR; } if (ospvErrCode == OSPC_ERR_NO_ERROR) { /* set up some of our local variables */ nameBfr = OSPPBfrNew(OSPC_XMLDOC_NAMESIZE); valBfr = OSPPBfrNew(OSPC_XMLDOC_VALUESIZE); /* make sure the initializations went okay */ if ((nameBfr == OSPC_OSNULL) || (valBfr == OSPC_OSNULL)) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } } /* get the name of the attribute */ if (ospvErrCode == OSPC_ERR_NO_ERROR) { ospvErrCode = OSPPXMLDocGetName(ospvBfrAddr, ospvEncoding, &nameBfr); } /* the next character should be an equals sign */ if (ospvErrCode == OSPC_ERR_NO_ERROR) { ospvErrCode = OSPPXMLDocReadChar(ospvBfrAddr, ospvEncoding, &readChar); } if ((ospvErrCode == OSPC_ERR_NO_ERROR) && (readChar != OSPC_XMLDOC_EQUALS)) { ospvErrCode = OSPC_ERR_XML_BAD_ATTR; } /* the next character is either a single or double quote mark */ if (ospvErrCode == OSPC_ERR_NO_ERROR) { ospvErrCode = OSPPXMLDocReadChar(ospvBfrAddr, ospvEncoding, "eChar); } if ((ospvErrCode == OSPC_ERR_NO_ERROR) && (quoteChar != OSPC_XMLDOC_SINGLEQUOTE) && (quoteChar != OSPC_XMLDOC_DOUBLEQUOTE)) { ospvErrCode = OSPC_ERR_XML_BAD_ATTR; } /* now loop on the attribute value - terminate with matching quote */ while (ospvErrCode == OSPC_ERR_NO_ERROR) { ospvErrCode = OSPPXMLDocReadChar(ospvBfrAddr, ospvEncoding, &readChar); if (ospvErrCode == OSPC_ERR_NO_ERROR) { if (readChar == quoteChar) { /* * the matching quote, so we're done. Before we leave, * add the terminating NULL to the buffer. */ cnt = OSPPBfrWriteByte(&valBfr, '\0'); if (cnt != 1) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } break; } /* the character's still a part of the value, add it */ cnt = OSPPBfrWriteByte(&valBfr, readChar); if (cnt != 1) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } } } /* create the attribute object */ if (ospvErrCode == OSPC_ERR_NO_ERROR) { attr = OSPPXMLAttrNew((const unsigned char *)OSPPBfrLinearPtr(nameBfr), (const unsigned char *)OSPPBfrLinearPtr(valBfr)); *ospvAttrAddr = attr; } /* free the buffers we allocated */ if (nameBfr != OSPC_OSNULL) { OSPPBfrDelete(&nameBfr); } if (valBfr != OSPC_OSNULL) { OSPPBfrDelete(&valBfr); } return(ospvErrCode);}/**//*-----------------------------------------------------------------------* * OSPPXMLDocGetContent() - get the content an element *-----------------------------------------------------------------------*/unsigned /* returns error code */OSPPXMLDocGetContent( OSPTBFR **ospvBfrAddr, /* buffer containing document */ OSPTXMLENC ospvEncoding, /* character encoding for the document */ OSPTLIST *ospvChild, /* list for children */ OSPTBFR **ospvValBfr /* place to store value */){ unsigned ospvErrCode = OSPC_ERR_NO_ERROR; unsigned char char1 = '\0'; unsigned char char2 = '\0'; unsigned isComment = OSPC_FALSE; unsigned isPI = OSPC_FALSE; unsigned isCdata = OSPC_FALSE; unsigned cnt; OSPTXMLELEM *elem = OSPC_OSNULL; if (ospvBfrAddr == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } if (*ospvBfrAddr == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } if (ospvEncoding == ospeXMLEncUnknown) { ospvErrCode = OSPC_ERR_XML_BAD_ENC; } if (ospvChild == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_XML_CHILD_NOT_FOUND; } if (ospvValBfr == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_XML_INVALID_ARGS; } if (*ospvValBfr == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_XML_INVALID_ARGS; } /* loop while parsing the content */ while (ospvErrCode == OSPC_ERR_NO_ERROR) { /* first take care of any whitespace */ ospvErrCode = OSPPXMLDocSkipWhite(ospvBfrAddr, ospvEncoding); /* see if there's a comment - if so, skip it */ if (ospvErrCode == OSPC_ERR_NO_ERROR) { ospvErrCode = OSPPXMLDocIsComment(ospvBfrAddr, ospvEncoding, &isComment); } if ((ospvErrCode == OSPC_ERR_NO_ERROR) && isComment) { ospvErrCode = OSPPXMLDocSkipComment(ospvBfrAddr, ospvEncoding); continue; } /* see if there's a processing instruction - if so, skip it */ if (ospvErrCode == OSPC_ERR_NO_ERROR) { ospvErrCode = OSPPXMLDocIsPI(ospvBfrAddr, ospvEncoding, &isPI); } if ((ospvErrCode == OSPC_ERR_NO_ERROR) && isPI) { ospvErrCode = OSPPXMLDocSkipPI(ospvBfrAddr, ospvEncoding); continue; } /* how about a CDATA section? */ if (ospvErrCode == OSPC_ERR_NO_ERROR) { ospvErrCode = OSPPXMLDocIsCdata(ospvBfrAddr, ospvEncoding, &isCdata); } if ((ospvErrCode == OSPC_ERR_NO_ERROR) && isCdata) { ospvErrCode = OSPPXMLDocGetCdata(ospvBfrAddr, ospvEncoding, ospvValBfr); continue; } /* we need a look at the next two characters for several cases */ if (ospvErrCode == OSPC_ERR_NO_ERROR) { OSPPXMLDocPeekCharN(ospvBfrAddr, ospvEncoding, 0, &char1, (int *)&ospvErrCode); } if (ospvErrCode == OSPC_ERR_NO_ERROR) { OSPPXMLDocPeekCharN(ospvBfrAddr, ospvEncoding, 1, &char2, (int *)&ospvErrCode); } /* might this be a child element ? */ if ((ospvErrCode == OSPC_ERR_NO_ERROR) && (char1 == OSPC_XMLDOC_OPEN) && (OSPPXMLIsName1(char2))) { elem = OSPC_OSNULL; ospvErrCode = OSPPXMLDocParseElem(ospvBfrAddr, ospvEncoding, &elem); if (ospvErrCode == OSPC_ERR_NO_ERROR) { OSPPListAppend(ospvChild, elem); /* OSPPXMLElemDelete(elem); */ } continue; } /* is this the end of the element (i.e. the closing tag)? */ if ((ospvErrCode == OSPC_ERR_NO_ERROR) && (char1 == OSPC_XMLDOC_OPEN) && (char2 == OSPC_XMLDOC_TRAILER)) { /* * This looks like the end of the element. We're not being * picky and making sure that the closing tag name matches * the opening tag. */ break; } /* the only thing left is actual data for the element */ if (ospvErrCode == OSPC_ERR_NO_ERROR) { ospvErrCode = OSPPXMLDocReadChar(ospvBfrAddr, ospvEncoding, &char1); if (ospvErrCode == OSPC_ERR_NO_ERROR) { cnt = OSPPBfrWriteByte(ospvValBfr, char1); if (cnt != 1) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } } } } /* before we leave, NULL terminate the value string */ if (ospvErrCode == OSPC_ERR_NO_ERROR) { cnt = OSPPBfrWriteByte(ospvValBfr, '\0'); if (cnt != 1) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } } return(ospvErrCode);}/**//*-----------------------------------------------------------------------* * OSPPXMLDocGetCdata() - get CDATA from an XML document *-----------------------------------------------------------------------*/unsigned /* returns error code */OSPPXMLDocGetCdata( OSPTBFR **ospvBfrAddr, /* buffer containing document */ OSPTXMLENC ospvEncoding, /* character encoding for the document */ OSPTBFR **ospvValBfr /* place to store value */){ unsigned ospvErrCode = OSPC_ERR_NO_ERROR; unsigned char readChar = '\0'; unsigned isCdataEnd = OSPC_FALSE; unsigned cnt; if (ospvBfrAddr == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } if (*ospvBfrAddr == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } if (ospvEncoding == ospeXMLEncUnknown) { ospvErrCode = OSPC_ERR_XML_BAD_ENC; } if (ospvValBfr == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_XML_INVALID_ARGS; } if (*ospvValBfr == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_XML_INVALID_ARGS; } if (ospvErrCode == OSPC_ERR_NO_ERROR) { /* skip past CDATA beginning */ ospvErrCode = OSPPXMLDocSkipPastCdataBeg(ospvBfrAddr, ospvEncoding); } /* loop while looking for the end of the CDATA section */ while (ospvErrCode == OSPC_ERR_NO_ERROR) { /* check to see if we're at the end of CDATA */ if (ospvErrCode == OSPC_ERR_NO_ERROR) { ospvErrCode = OSPPXMLDocIsCdataEnd(ospvBfrAddr, ospvEncoding, &isCdataEnd); } if ((ospvErrCode == OSPC_ERR_NO_ERROR) && isCdataEnd) { /* we're at the end, skip past the CDATA end marker and break */ ospvErrCode = OSPPXMLDocSkipPastCdataEnd(ospvBfrAddr, ospvEncoding); break; } if (ospvErrCode == OSPC_ERR_NO_ERROR) { /* there's more CDATA there, get the next character */ ospvErrCode = OSPPXMLDocReadChar(ospvBfrAddr, ospvEncoding, &readChar); if (ospvErrCode == OSPC_ERR_NO_ERROR) { cnt = OSPPBfrWriteByte(ospvValBfr, readChar); if (cnt != 1) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } } } } return(ospvErrCode);}/**//*-----------------------------------------------------------------------* * OSPPXMLDocGetName() - get the name from and XML document *-----------------------------------------------------------------------*/unsigned /* returns error code */OSPPXMLDocGetName( OSPTBFR **ospvBfrAddr, /* buffer containing document */ OSPTXMLENC ospvEncoding, /* character encoding for the document */ OSPTBFR **ospvNameBfr /* buffer for storing name */){ unsigned ospvErrCode = OSPC_ERR_NO_ERROR; unsigned char readChar = '\0'; unsigned cnt; /* * The only thing particularly noteworthy in this function is that * we use a message buffer to store the extracted name. We do that * because message buffers are elastic, and by using them we don't * have to figure out how big the name is before we start extracting * it. */ if (ospvBfrAddr == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } if (*ospvBfrAddr == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_BUF_EMPTY; } if (ospvEncoding == ospeXMLEncUnknown) { ospvErrCode = OSPC_ERR_XML_BAD_ENC; } if (ospvNameBfr == OSPC_OSNULL) { ospvErrCode = OSPC_ERR_XML_INVALID_ARGS; } if (*ospvNameBfr == OSPC_OSNULL)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -