📄 domcif.cpp
字号:
UpnpNodeList_free(ret); return NULL; } else return((void *)ret);}Upnp_Node UpnpElement_getFirstChild(Upnp_Element OperationElement){ Node *ret = new Node; if(!ret) { DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");) return NULL; } *ret = (*(Element *)OperationElement).getFirstChild(); if(ret->isNull()) { UpnpNode_free(ret); return NULL; } else return((void *)ret);}Upnp_Node UpnpElement_getLastChild(Upnp_Element OperationElement){ Node *ret = new Node; if(!ret) { DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");) return NULL; } *ret = (*(Element *)OperationElement).getLastChild(); if(ret->isNull()) { UpnpNode_free(ret); return NULL; } else return((void *)ret);}Upnp_Node UpnpElement_getPreviousSibling(Upnp_Element OperationElement){ Node *ret = new Node; if(!ret) { DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");) return NULL; } *ret = (*(Element *)OperationElement).getPreviousSibling(); if(ret->isNull()) { UpnpNode_free(ret); return NULL; } else return((void *)ret);}Upnp_Node UpnpElement_getNextSibling(Upnp_Element OperationElement){ Node *ret = new Node; if(!ret) { DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");) return NULL; } *ret = (*(Element *)OperationElement).getNextSibling(); if(ret->isNull()) { UpnpNode_free(ret); return NULL; } else return((void *)ret);}Upnp_NamedNodeMap UpnpElement_getAttributes(Upnp_Element OperationElement){ NamedNodeMap *ret = new NamedNodeMap; if(!ret) { DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");) return NULL; } *ret = (*(Element *)OperationElement).getAttributes(); if(!ret->getLength()) { UpnpNamedNodeMap_free(ret); return NULL; } else return((void *)ret);}Upnp_Document UpnpElement_getOwnerDocument(Upnp_Element OperationElement){ Document *ret = new Document; if(!ret) { DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");) return NULL; } *ret = (*(Element *)OperationElement).getOwnerDocument(); if(ret->isNull()) { UpnpDocument_free(ret); return NULL; } else return((void *)ret);}Upnp_Void UpnpElement_free(Upnp_Element OperationElement){ if(OperationElement == NULL) return; Element *rmv; rmv=(Element *)OperationElement; rmv->deleteElement(); delete rmv; rmv=NULL;}/*******************************************************************************************************************************///Interface NamedNodeMap Implementation/*******************************************************************************************************************************/Upnp_Node UpnpNamedNodeMap_getNamedItem(Upnp_NamedNodeMap OperationNamedNodeMap, Upnp_DOMString name){ Node *ret = new Node; if(!ret) { DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");) return NULL; } *ret = (*(NamedNodeMap *)OperationNamedNodeMap).getNamedItem(name); if(ret->isNull()) { UpnpNode_free(ret); return NULL; } else return((void *)ret);}Upnp_Node UpnpNamedNodeMap_item(Upnp_NamedNodeMap OperationNamedNodeMap, unsigned long index){ Node *ret = new Node; if(!ret) { DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");) return NULL; } *ret = (*(NamedNodeMap *)OperationNamedNodeMap).item(index); if(ret->isNull()) { UpnpNode_free(ret); return NULL; } else return((void *)ret);}Upnp_ULong UpnpNamedNodeMap_getLength(Upnp_NamedNodeMap OperationNamedNodeMap){ return((*(NamedNodeMap *)OperationNamedNodeMap).getLength());}Upnp_Void UpnpNamedNodeMap_free(Upnp_NamedNodeMap OperationNamedNodeMap){ if(OperationNamedNodeMap == NULL) return; NamedNodeMap *rmv; rmv=(NamedNodeMap *)OperationNamedNodeMap; rmv->deleteNamedNodeMap(); delete rmv; rmv=NULL;}/*******************************************************************************************************************************///DOMLevel 1 Implementation End/*******************************************************************************************************************************/Upnp_Void UpnpDOMString_free(Upnp_DOMString in){ if(in ==NULL) return; delete[] in; in=NULL;}Upnp_Void Upnpfree(void * in){ free(in);}Upnp_Document UpnpParseFileAndGetDocument(char* xmlFile){ Document *ret = new Document; if(!ret) { DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");) return NULL; } try { *ret=(*ret).ReadDocumentFileOrBuffer(xmlFile, true); } catch (DOMException& /* toCatch */) {// DBGONLY(printf("%s\n",toCatch.msg)); return NULL; } if(ret->isNull()) { UpnpDocument_free(ret); return NULL; } else return((void *)ret);}Upnp_DOMString UpnpNewPrintDocument(Upnp_Node OperationNode){ if(OperationNode == NULL) return NULL; Upnp_DOMString Upnp_Buff=NULL; if((*(Node *)OperationNode).isNull()) return NULL; if((*(Node *)OperationNode).isNull()) return NULL; DumpDocument(Upnp_Buff, *(Node *)OperationNode,0); return Upnp_Buff;}Upnp_DOMString UpnpPrintDocumentDeEscaped(Upnp_Node OperationNode){ if(OperationNode == NULL) return NULL; Upnp_DOMString Upnp_Buff=NULL; if((*(Node *)OperationNode).isNull()) return NULL; if((*(Node *)OperationNode).isNull()) return NULL; DumpNonEscapedDocument(Upnp_Buff, *(Node *)OperationNode,0); return Upnp_Buff;}Upnp_Void UpnpPrintDocument(Upnp_Node OperationNode, char * Upnp_Buff){ Upnp_DOMString temp=NULL; DumpDocument(temp, *(Node *)OperationNode, 0); strcpy(Upnp_Buff, temp); delete temp;}Upnp_Document UpnpParse_Buffer(char *Buff1){ if(Buff1 == NULL || !strlen(Buff1)) return NULL; Document *ret = new Document; if(!ret) { DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");) return NULL; } try { *ret=(*ret).ReadDocumentFileOrBuffer(Buff1, false); } catch (DOMException& /* toCatch */) {// DBGONLY(printf("%s\n",toCatch.msg)); return NULL; } if(ret->isNull()) { UpnpDocument_free(ret); return NULL; } else return((void *)ret);}void copyToTarget(char*& target, const char* p){ if(target==NULL) { target = (char *)malloc(strlen(p)+1); strcpy(target, p); } else { target = (char *)realloc(target, strlen(target)+strlen(p)+1); strcat(target,p); }}void copy_with_escape( membuffer* buf, const char* p){ int plen = strlen(p); for ( int i = 0; i < plen; i++ ) { switch (p[i]) { case '<': membuffer_append_str( buf, "<" ); break; case '>': membuffer_append_str( buf, ">" ); break; case '&': membuffer_append_str( buf, "&" );break; case '\'': membuffer_append_str( buf, "'" ); break; default: membuffer_append( buf, &p[i], 1 ); } }}//This function takes only a document node//void DumpEscapedDocument(char*& target, Node& n, int loop)void dump_doc(membuffer *buf, Node& n, int loop){ char* nodeName = n.getNodeName(); char* nodeValue = n.getNodeValue(); switch (n.getNodeType()) { case TEXT_NODE: { copy_with_escape( buf, nodeValue ); break; } case PROCESSING_INSTRUCTION_NODE : { membuffer_append_str( buf, "<?" ); membuffer_append_str( buf, nodeName ); membuffer_append_str( buf, " " ); membuffer_append_str( buf, nodeValue ); membuffer_append_str( buf, "?>\n" ); break; } case DOCUMENT_NODE : { Node child=n.getFirstChild(); Node *del; del=&child; Node temp; while(!child.isNull()) { temp = child.getNextSibling(); dump_doc(buf, child,1); child=temp; } (*del).deleteNode(); break; } case ELEMENT_NODE : { // Output the element start tag. membuffer_append_str( buf, "<" ); membuffer_append_str( buf, nodeName ); // Output any attributes on this element NamedNodeMap attributes = n.getAttributes(); int attrCount = attributes.getLength(); for (int i = 0; i < attrCount; i++) { Node attribute = attributes.item(i); char *nn=attribute.getNodeName(); char *nv=attribute.getNodeValue(); membuffer_append_str( buf, " " ); membuffer_append_str( buf, nn ); membuffer_append_str( buf, "=\"" ); copy_with_escape( buf, nv ); membuffer_append_str( buf, "\"" ); delete nn; delete nv; attribute.deleteNode(); } attributes.deleteNamedNodeMap(); // // Test for the presence of children, which includes both // text content and nested elements. // Node child, temp; child = n.getFirstChild(); Node *del=&child;; if (!child.isNull()) { // There are children. Close start-tag, and output children. if(child.getNodeType()!=3) { membuffer_append_str( buf, ">\n" ); } else { membuffer_append_str( buf, ">" ); } while( !child.isNull()) { temp = child.getNextSibling(); dump_doc(buf, child,1); child=temp; } if(child.isNull()) child.deleteNode(); // Done with children. Output the end tag. membuffer_append_str( buf, "</" ); membuffer_append_str( buf, nodeName ); membuffer_append_str( buf, ">\n" ); } else { // // There were no children. Output the short form close of the // element start tag, making it an empty-element tag. // (*del).deleteNode(); membuffer_append_str( buf, "/>\n" ); } break; } default:// DBGONLY(printf("Unrecognized node type = %d\n",n.getNodeType())); break; } //do not delete the topmost node's parent node as it is created by someone else //for now dont delete the document node delete nodeName; delete nodeValue;// if(n.getNodeType()!=DOCUMENT_NODE) if(loop!=0) n.deleteNode();}void DumpDocument(char*& target, Node& n, int loop) { membuffer buf; membuffer_init( &buf ); dump_doc( &buf, n, loop ); target = membuffer_detach( &buf );}//This function takes only a document nodevoid DumpNonEscapedDocument(char*& target, Node& n, int loop){ char* nodeName = n.getNodeName(); char* nodeValue = n.getNodeValue(); switch (n.getNodeType()) { case TEXT_NODE: { copyToTarget(target,nodeValue); break; } case PROCESSING_INSTRUCTION_NODE : { copyToTarget(target,"<?"); copyToTarget(target,nodeName); copyToTarget(target," "); copyToTarget(target,nodeValue); copyToTarget(target,"?>\n"); break; } case DOCUMENT_NODE : { Node child=n.getFirstChild(); Node *del; del=&child; Node temp; while(!child.isNull()) { temp = child.getNextSibling(); DumpDocument(target, child,1); child=temp; } (*del).deleteNode(); break; } case ELEMENT_NODE : { // Output the element start tag. copyToTarget(target,"<"); copyToTarget(target,nodeName); // Output any attributes on this element NamedNodeMap attributes = n.getAttributes(); int attrCount = attributes.getLength(); for (int i = 0; i < attrCount; i++) { Node attribute = attributes.item(i); char *nn=attribute.getNodeName(); char *nv=attribute.getNodeValue(); copyToTarget(target," "); copyToTarget(target,nn); copyToTarget(target,"=\""); copyToTarget(target,nv); copyToTarget(target,"\""); delete nn; delete nv; attribute.deleteNode(); } attributes.deleteNamedNodeMap(); // // Test for the presence of children, which includes both // text content and nested elements. // Node child, temp; child = n.getFirstChild(); Node *del=&child;; if (!child.isNull()) { // There are children. Close start-tag, and output children. if(child.getNodeType()!=3) copyToTarget(target,">\n"); else copyToTarget(target,">"); while( !child.isNull()) { temp = child.getNextSibling(); DumpDocument(target, child,1); child=temp; } if(child.isNull()) child.deleteNode(); // Done with children. Output the end tag. copyToTarget(target,"</"); copyToTarget(target,nodeName); copyToTarget(target,">\n"); } else { // // There were no children. Output the short form close of the // element start tag, making it an empty-element tag. // (*del).deleteNode(); copyToTarget(target,"/>\n"); } break; } default:// DBGONLY(printf("Unrecognized node type = %d\n",n.getNodeType())); break; } //do not delete the topmost node's parent node as it is created by someone else //for now dont delete the document node delete nodeName; delete nodeValue;// if(n.getNodeType()!=DOCUMENT_NODE) if(loop!=0) n.deleteNode();}char *UpnpCloneDOMString(const char *src){ char *ret=NULL; if(src == NULL) return NULL; ret = new char [strlen(src) +1]; //ret = (Upnp_DOMString)malloc(strlen(src)+1); if(!ret) return NULL; strcpy(ret, src); return ret;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -