📄 domcif.c
字号:
rmv=NULL;}/*******************************************************************************************************************************///Interface Element Implementation/*******************************************************************************************************************************/Upnp_NodeList UpnpElement_getElementsByTagName(Upnp_Element OperationElement, Upnp_DOMString name){ struct strNodeList *ret; // = new NodeList; struct strNode *pNode = (struct strNode *) malloc(sizeof(struct strNode) + 1); //NodeList_NodeList_0(); ret = (struct strNodeList *) malloc (sizeof(struct strNodeList) + 1); if(!ret) { DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");) return NULL; } ret = Element_getElementsByTagName(((struct strElement *)OperationElement), pNode, name); if(!NodeList_getLength(ret)) { UpnpNodeList_free(ret); return NULL; } else return((void *)ret);}Upnp_Node UpnpElement_appendChild(Upnp_Element OperationElement, Upnp_Node newChild, Upnp_DOMException *err){ struct strNode *ret; // = new Node; //Node_Node_0(); ret = (struct strNode *) malloc (sizeof(struct strNode) + 1); if(!ret) { DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");) return NULL; } *err =NO_ERR; ret = Node_appendChild(((struct strNode *)OperationElement), ((struct strNode *)newChild)); if (!ret) { Handle_DOMException(NOT_FOUND_ERR_DOM); *err = NOT_FOUND_ERR; } //try //{ // *ret = (*(struct strElement *)OperationElement).appendChild((*(struct strNode *)newChild)); //} //catch(DOMException &tocatch) //{ // *err = (enum Upnp_DOMException)tocatch.code; //} if(Node_isNull(ret)) { UpnpNode_free(ret); return NULL; } else return((void *)ret);} Upnp_Void UpnpElement_free(Upnp_Element OperationElement){ if(OperationElement == NULL) return; struct strElement *rmv; rmv=(struct strElement *)OperationElement; Element_deleteElement(rmv); free(rmv); rmv=NULL;}/*******************************************************************************************************************************///Interface NamedNodeMap Implementation/*******************************************************************************************************************************/Upnp_Void UpnpNamedNodeMap_free(Upnp_NamedNodeMap OperationNamedNodeMap){ if(OperationNamedNodeMap == NULL) return; struct strNamedNodeMap *rmv; rmv=(struct strNamedNodeMap *)OperationNamedNodeMap; NamedNodeMap_deleteNamedNodeMap(rmv); free(rmv); rmv=NULL;}/*******************************************************************************************************************************///DOMLevel 1 Implementation End/*******************************************************************************************************************************/Upnp_Void UpnpDOMString_free(Upnp_DOMString in){ if(in ==NULL) return; //delete[] in; free(in); in=NULL;}Upnp_Void Upnpfree(void * in){ free(in);}Upnp_DOMString UpnpNewPrintDocument(Upnp_Node OperationNode){ if(OperationNode == NULL) return NULL; Upnp_DOMString Upnp_Buff=NULL; if(Node_isNull(((struct strNode *)OperationNode))) return NULL; if(Node_isNull(((struct strNode *)OperationNode))) return NULL; DumpDocument(Upnp_Buff, (struct strNode *)OperationNode,0); return Upnp_Buff;}Upnp_Document UpnpParse_Buffer(char *Buff1){ if(Buff1 == NULL || !strlen(Buff1)) return NULL; //Document_Document_0(); struct strDocument *ret = (struct strDocument *) malloc (sizeof(struct strDocument) + 1); if(!ret) { DBGONLY(UpnpPrintf(UPNP_CRITICAL,DOM,__FILE__,__LINE__,"Insuffecient memory\n");) return NULL; } ret = Document_ReadDocumentFileOrBuffer(Buff1, 1); if (!ret) { Handle_DOMException(NOT_FOUND_ERR_DOM); } //try //{ // *ret=(*ret).ReadDocumentFileOrBuffer(Buff1, false); //} //catch (DOMException& /* toCatch */) //{// DBGONLY(printf("%s\n",toCatch.msg)); // return NULL; //} if(Node_isNull((struct strNode *)ret)) { 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); int i; for (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, struct strNode *n, int loop){ int i; char* nodeName = Node_getNodeName(n); char* nodeValue = Node_getNodeValue(n); switch (Node_getNodeType(n)) { 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 : { struct strNode *child=Node_getFirstChild(n); Node *del; del=child; struct strNode *temp; while(!Node_isNull(child)) { temp = Node_getNextSibling(child); dump_doc(buf, child,1); child=temp; } Node_deleteNode(del); break; } case ELEMENT_NODE : { // Output the element start tag. membuffer_append_str( buf, "<" ); membuffer_append_str( buf, nodeName ); // Output any attributes on this element struct strNamedNodeMap *attributes = Node_getAttributes(n); int attrCount = NamedNodeMap_getLength(attributes); for (i = 0; i < attrCount; i++) { struct strNode *attribute = NamedNodeMap_item(attributes, i); char *nn=Node_getNodeName(attribute); char *nv=Node_getNodeValue(attribute); membuffer_append_str( buf, " " ); membuffer_append_str( buf, nn ); membuffer_append_str( buf, "=\"" ); copy_with_escape( buf, nv ); membuffer_append_str( buf, "\"" ); free(nn); free(nv); Node_deleteNode(attribute); } NamedNodeMap_deleteNamedNodeMap(attributes); // // Test for the presence of children, which includes both // text content and nested elements. // struct strNode *child, *temp; child = Node_getFirstChild(n); struct strNode *del=child;; if (!Node_isNull(child)) { // There are children. Close start-tag, and output children. if(Node_getNodeType(child)!=3) { membuffer_append_str( buf, ">\n" ); } else { membuffer_append_str( buf, ">" ); } while( !Node_isNull(child)) { temp = Node_getNextSibling(child); dump_doc(buf, child,1); child=temp; } if(Node_isNull(child)) Node_deleteNode(child); // 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. // Node_deleteNode(del); 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 free(nodeName); free(nodeValue);// if(n.getNodeType()!=DOCUMENT_NODE) if(loop!=0) Node_deleteNode(n);}void DumpDocument(char* target, struct strNode *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, struct strNode *n, int loop){ char* nodeName = Node_getNodeName(n); char* nodeValue = Node_getNodeValue(n); switch (Node_getNodeType(n)) { 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 : { struct strNode *child=Node_getFirstChild(n); struct strNode *del; del=child; struct strNode *temp; while(!Node_isNull(child)) { temp = Node_getNextSibling(child); DumpDocument(target, child,1); child=temp; } Node_deleteNode(del); break; } case ELEMENT_NODE : { // Output the element start tag. copyToTarget(target,"<"); copyToTarget(target,nodeName); // Output any attributes on this element struct strNamedNodeMap *attributes = Node_getAttributes(n); int attrCount = NamedNodeMap_getLength(attributes); int i; for (i = 0; i < attrCount; i++) { struct strNode *attribute = NamedNodeMap_item(attributes, i); char *nn=Node_getNodeName(attribute); char *nv=Node_getNodeValue(attribute); copyToTarget(target," "); copyToTarget(target,nn); copyToTarget(target,"=\""); copyToTarget(target,nv); copyToTarget(target,"\""); free(nn); free(nv); Node_deleteNode(attribute); } NamedNodeMap_deleteNamedNodeMap(attributes); // // Test for the presence of children, which includes both // text content and nested elements. // struct strNode *child, *temp; child = Node_getFirstChild(n); struct strNode *del=child;; if (!Node_isNull(child)) { // There are children. Close start-tag, and output children. if(Node_getNodeType(child)!=3) copyToTarget(target,">\n"); else copyToTarget(target,">"); while( !Node_isNull(child)) { temp = Node_getNextSibling(child); DumpDocument(target, child,1); child=temp; } if(Node_isNull(child)) Node_deleteNode(child); // 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. // Node_deleteNode(del); 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 free(nodeName); free(nodeValue);// if(n.getNodeType()!=DOCUMENT_NODE) if(loop!=0) Node_deleteNode(n);}char *UpnpCloneDOMString(const char *src){ char *ret=NULL; if(src == NULL) return NULL; ret = (char *) malloc (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 + -