📄 xmlnode.c
字号:
for(step = parent->firstchild; step != NULL; step = xmlnode_get_nextsibling(step)) { if(xmlnode_get_type(step) != NTYPE_TAG) continue; if(j_strcmp(xmlnode_get_name(step),str) != 0) continue; ret = xmlnode_get_tag(step, slash); if(ret != NULL) { free(str); return ret; } } free(str); return NULL;}/* return the cdata from any tag */char *xmlnode_get_tag_data(xmlnode parent, const char *name){ xmlnode tag; tag = xmlnode_get_tag(parent, name); if(tag == NULL) return NULL; return xmlnode_get_data(tag);}void xmlnode_put_attrib(xmlnode owner, const char* name, const char* value){ xmlnode attrib; if(owner == NULL || name == NULL || value == NULL) return; /* If there are no existing attributs, allocate a new one to start the list */ if (owner->firstattrib == NULL) { attrib = _xmlnode_new(owner->p, name, NTYPE_ATTRIB); owner->firstattrib = attrib; owner->lastattrib = attrib; } else { attrib = _xmlnode_search(owner->firstattrib, name, NTYPE_ATTRIB); if(attrib == NULL) { attrib = _xmlnode_append_sibling(owner->lastattrib, name, NTYPE_ATTRIB); owner->lastattrib = attrib; } } /* Update the value of the attribute */ attrib->data_sz = strlen(value); attrib->data = pstrdup(owner->p, value);}char* xmlnode_get_attrib(xmlnode owner, const char* name){ xmlnode attrib; if (owner != NULL && owner->firstattrib != NULL) { attrib = _xmlnode_search(owner->firstattrib, name, NTYPE_ATTRIB); if (attrib != NULL) return (char*)attrib->data; } return NULL;}void xmlnode_put_vattrib(xmlnode owner, const char* name, void *value){ xmlnode attrib; if (owner != NULL) { attrib = _xmlnode_search(owner->firstattrib, name, NTYPE_ATTRIB); if (attrib == NULL) { xmlnode_put_attrib(owner, name, ""); attrib = _xmlnode_search(owner->firstattrib, name, NTYPE_ATTRIB); } if (attrib != NULL) attrib->firstchild = (xmlnode)value; }}void* xmlnode_get_vattrib(xmlnode owner, const char* name){ xmlnode attrib; if (owner != NULL && owner->firstattrib != NULL) { attrib = _xmlnode_search(owner->firstattrib, name, NTYPE_ATTRIB); if (attrib != NULL) return (void*)attrib->firstchild; } return NULL;}xmlnode xmlnode_get_firstattrib(xmlnode parent){ if (parent != NULL) return parent->firstattrib; return NULL;}xmlnode xmlnode_get_firstchild(xmlnode parent){ if (parent != NULL) return parent->firstchild; return NULL;}xmlnode xmlnode_get_lastchild(xmlnode parent){ if (parent != NULL) return parent->lastchild; return NULL;}xmlnode xmlnode_get_nextsibling(xmlnode sibling){ if (sibling != NULL) return sibling->next; return NULL;}xmlnode xmlnode_get_prevsibling(xmlnode sibling){ if (sibling != NULL) return sibling->prev; return NULL;}xmlnode xmlnode_get_parent(xmlnode node){ if (node != NULL) return node->parent; return NULL;}char* xmlnode_get_name(xmlnode node){ if (node != NULL) return node->name; return NULL;}char* xmlnode_get_data(xmlnode node){ if(xmlnode_get_type(node) == NTYPE_TAG) /* loop till we find a CDATA in the children */ for(node = xmlnode_get_firstchild(node); node != NULL; node = xmlnode_get_nextsibling(node)) if(xmlnode_get_type(node) == NTYPE_CDATA) break; if(node == NULL) return NULL; /* check for a dirty node w/ unassembled cdata chunks */ if(xmlnode_get_type(node->next) == NTYPE_CDATA) _xmlnode_merge(node); return node->data;}int xmlnode_get_datasz(xmlnode node){ if(xmlnode_get_type(node) != NTYPE_CDATA) return 0; /* check for a dirty node w/ unassembled cdata chunks */ if(xmlnode_get_type(node->next) == NTYPE_CDATA) _xmlnode_merge(node); return node->data_sz;}int xmlnode_get_type(xmlnode node){ if (node != NULL) return node->type; return NTYPE_UNDEF;}int xmlnode_has_children(xmlnode node){ if ((node != NULL) && (node->firstchild != NULL)) return 1; return 0;}int xmlnode_has_attribs(xmlnode node){ if ((node != NULL) && (node->firstattrib != NULL)) return 1; return 0;}pool xmlnode_pool(xmlnode node){ if (node != NULL) return node->p; return (pool)NULL;}void xmlnode_hide(xmlnode child){ xmlnode parent; if(child == NULL || child->parent == NULL) return; parent = child->parent; /* first fix up at the child level */ _xmlnode_hide_sibling(child); /* next fix up at the parent level */ if(parent->firstchild == child) parent->firstchild = child->next; if(parent->lastchild == child) parent->lastchild = child->prev;}void xmlnode_hide_attrib(xmlnode parent, const char *name){ xmlnode attrib; if(parent == NULL || parent->firstattrib == NULL || name == NULL) return; attrib = _xmlnode_search(parent->firstattrib, name, NTYPE_ATTRIB); if(attrib == NULL) return; /* first fix up at the child level */ _xmlnode_hide_sibling(attrib); /* next fix up at the parent level */ if(parent->firstattrib == attrib) parent->firstattrib = attrib->next; if(parent->lastattrib == attrib) parent->lastattrib = attrib->prev;}/* * xmlnode2str -- convert given xmlnode tree into a string * * parameters * node -- pointer to the xmlnode structure * * results * a pointer to the created string * or NULL if it was unsuccessfull */char *xmlnode2str(xmlnode node){ return spool_print(_xmlnode2spool(node));}/* * xmlnode2tstr -- convert given xmlnode tree into a newline terminated string * * parameters * node -- pointer to the xmlnode structure * * results * a pointer to the created string * or NULL if it was unsuccessfull */char* xmlnode2tstr(xmlnode node){ spool s = _xmlnode2spool(node); if (s != NULL) spool_add(s, "\n"); return spool_print(s);}/* loop through both a and b comparing everything, attribs, cdata, children, etc */int xmlnode_cmp(xmlnode a, xmlnode b){ int ret = 0; while(1) { if(a == NULL && b == NULL) return 0; if(a == NULL || b == NULL) return -1; if(xmlnode_get_type(a) != xmlnode_get_type(b)) return -1; switch(xmlnode_get_type(a)) { case NTYPE_ATTRIB: ret = j_strcmp(xmlnode_get_name(a), xmlnode_get_name(b)); if(ret != 0) return -1; ret = j_strcmp(xmlnode_get_data(a), xmlnode_get_data(b)); if(ret != 0) return -1; break; case NTYPE_TAG: ret = j_strcmp(xmlnode_get_name(a), xmlnode_get_name(b)); if(ret != 0) return -1; ret = xmlnode_cmp(xmlnode_get_firstattrib(a), xmlnode_get_firstattrib(b)); if(ret != 0) return -1; ret = xmlnode_cmp(xmlnode_get_firstchild(a), xmlnode_get_firstchild(b)); if(ret != 0) return -1; break; case NTYPE_CDATA: ret = j_strcmp(xmlnode_get_data(a), xmlnode_get_data(b)); if(ret != 0) return -1; } a = xmlnode_get_nextsibling(a); b = xmlnode_get_nextsibling(b); }}xmlnode xmlnode_insert_tag_node(xmlnode parent, xmlnode node){ xmlnode child; child = xmlnode_insert_tag(parent, xmlnode_get_name(node)); if (xmlnode_has_attribs(node)) xmlnode_insert_node(child, xmlnode_get_firstattrib(node)); if (xmlnode_has_children(node)) xmlnode_insert_node(child, xmlnode_get_firstchild(node)); return child;}/* places copy of node and node's siblings in parent */void xmlnode_insert_node(xmlnode parent, xmlnode node){ if(node == NULL || parent == NULL) return; while(node != NULL) { switch(xmlnode_get_type(node)) { case NTYPE_ATTRIB: xmlnode_put_attrib(parent, xmlnode_get_name(node), xmlnode_get_data(node)); break; case NTYPE_TAG: xmlnode_insert_tag_node(parent, node); break; case NTYPE_CDATA: xmlnode_insert_cdata(parent, xmlnode_get_data(node), xmlnode_get_datasz(node)); } node = xmlnode_get_nextsibling(node); }}/* produce full duplicate of x with a new pool, x must be a tag! */xmlnode xmlnode_dup(xmlnode x){ xmlnode x2; if(x == NULL) return NULL; x2 = xmlnode_new_tag(xmlnode_get_name(x)); if (xmlnode_has_attribs(x)) xmlnode_insert_node(x2, xmlnode_get_firstattrib(x)); if (xmlnode_has_children(x)) xmlnode_insert_node(x2, xmlnode_get_firstchild(x)); return x2;}xmlnode xmlnode_dup_pool(pool p, xmlnode x){ xmlnode x2; if(x == NULL) return NULL; x2 = xmlnode_new_tag_pool(p, xmlnode_get_name(x)); if (xmlnode_has_attribs(x)) xmlnode_insert_node(x2, xmlnode_get_firstattrib(x)); if (xmlnode_has_children(x)) xmlnode_insert_node(x2, xmlnode_get_firstchild(x)); return x2;}xmlnode xmlnode_wrap(xmlnode x,const char *wrapper){ xmlnode wrap; if(x==NULL||wrapper==NULL) return NULL; wrap=xmlnode_new_tag_pool(xmlnode_pool(x),wrapper); if(wrap==NULL) return NULL; wrap->firstchild=x; wrap->lastchild=x; x->parent=wrap; return wrap;}void xmlnode_free(xmlnode node){ if(node == NULL) return; pool_free(node->p);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -