uxmltree.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <sys/types.h>
00044
00045 #include <stdlib.h>
00046 #include <string.h>
00047 #include <memdebug.h>
00048
00049 #include <pro/uxml.h>
00050
00055
00063 UXML_NODE *UxmlNodeCreate(char *name)
00064 {
00065 UXML_NODE *node;
00066 size_t nlen;
00067
00068 if ((node = malloc(sizeof(UXML_NODE))) != NULL) {
00069 memset(node, 0, sizeof(UXML_NODE));
00070 nlen = strlen(name) + 1;
00071 if ((node->xmln_name = malloc(nlen)) != NULL) {
00072 memcpy(node->xmln_name, name, nlen);
00073 }
00074 }
00075 return node;
00076 }
00077
00087 int UxmlNodeAddAttrib(UXML_NODE * node, char *name, char *value)
00088 {
00089 UXML_ATTRIB *attr;
00090 UXML_ATTRIB *ap;
00091 size_t len;
00092
00093 if ((attr = malloc(sizeof(UXML_ATTRIB))) == NULL) {
00094 return -1;
00095 }
00096 attr->xmla_next = NULL;
00097 len = strlen(name) + 1;
00098 attr->xmla_name = malloc(len);
00099 memcpy(attr->xmla_name, name, len);
00100 len = strlen(value) + 1;
00101 attr->xmla_value = malloc(len);
00102 memcpy(attr->xmla_value, value, len);
00103
00104 if (node->xmln_attribs == NULL) {
00105 node->xmln_attribs = attr;
00106 } else {
00107 ap = node->xmln_attribs;
00108 for (;;) {
00109 if (ap->xmla_next == NULL) {
00110 ap->xmla_next = attr;
00111 break;
00112 }
00113 ap = ap->xmla_next;
00114 }
00115 }
00116 return 0;
00117 }
00118
00119 static void UxmlNodeDestroy(UXML_NODE * node)
00120 {
00121 UXML_ATTRIB *ap;
00122 UXML_ATTRIB *attr;
00123
00124 if (node) {
00125 if (node->xmln_name) {
00126 free(node->xmln_name);
00127 }
00128 ap = node->xmln_attribs;
00129 while (ap) {
00130 attr = ap;
00131 ap = ap->xmla_next;
00132 if (attr->xmla_name) {
00133 free(attr->xmla_name);
00134 }
00135 if (attr->xmla_value) {
00136 free(attr->xmla_value);
00137 }
00138 free(attr);
00139 }
00140 free(node);
00141 }
00142 }
00143
00152 UXML_NODE *UxmlTreeAddSibling(UXML_NODE * node, UXML_NODE * sibling)
00153 {
00154 for (;;) {
00155 if (node->xmln_next == NULL) {
00156 node->xmln_next = sibling;
00157 sibling->xmln_parent = node->xmln_parent;
00158 break;
00159 }
00160 node = node->xmln_next;
00161 }
00162 return sibling;
00163 }
00164
00173 UXML_NODE *UxmlTreeAddChild(UXML_NODE * node, UXML_NODE * child)
00174 {
00175 if (node->xmln_child == NULL) {
00176 node->xmln_child = child;
00177 child->xmln_parent = node;
00178 } else {
00179 UxmlTreeAddSibling(node->xmln_child, child);
00180 }
00181 return child;
00182 }
00183
00189 void UxmlTreeDestroy(UXML_NODE * node)
00190 {
00191 UXML_NODE *np = node;
00192
00193 while (np) {
00194 node = np;
00195 np = np->xmln_next;
00196 if (node->xmln_child) {
00197 UxmlTreeDestroy(node->xmln_child);
00198 }
00199 UxmlNodeDestroy(node);
00200 }
00201 }
00202