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
00048 #include <pro/uxml.h>
00049
00054
00062 UXML_NODE *UxmlNodeCreate(char *name)
00063 {
00064 UXML_NODE *node;
00065 size_t nlen;
00066
00067 if ((node = malloc(sizeof(UXML_NODE))) != NULL) {
00068 memset(node, 0, sizeof(UXML_NODE));
00069 nlen = strlen(name) + 1;
00070 if ((node->xmln_name = malloc(nlen)) != NULL) {
00071 memcpy(node->xmln_name, name, nlen);
00072 }
00073 }
00074 return node;
00075 }
00076
00086 int UxmlNodeAddAttrib(UXML_NODE * node, char *name, char *value)
00087 {
00088 UXML_ATTRIB *attr;
00089 UXML_ATTRIB *ap;
00090 size_t len;
00091
00092 if ((attr = malloc(sizeof(UXML_ATTRIB))) == NULL) {
00093 return -1;
00094 }
00095 attr->xmla_next = NULL;
00096 len = strlen(name) + 1;
00097 attr->xmla_name = malloc(len);
00098 memcpy(attr->xmla_name, name, len);
00099 len = strlen(value) + 1;
00100 attr->xmla_value = malloc(len);
00101 memcpy(attr->xmla_value, value, len);
00102
00103 if (node->xmln_attribs == NULL) {
00104 node->xmln_attribs = attr;
00105 } else {
00106 ap = node->xmln_attribs;
00107 for (;;) {
00108 if (ap->xmla_next == NULL) {
00109 ap->xmla_next = attr;
00110 break;
00111 }
00112 ap = ap->xmla_next;
00113 }
00114 }
00115 return 0;
00116 }
00117
00118 static void UxmlNodeDestroy(UXML_NODE * node)
00119 {
00120 UXML_ATTRIB *ap;
00121 UXML_ATTRIB *attr;
00122
00123 if (node) {
00124 if (node->xmln_name) {
00125 free(node->xmln_name);
00126 }
00127 ap = node->xmln_attribs;
00128 while (ap) {
00129 attr = ap;
00130 ap = ap->xmla_next;
00131 if (attr->xmla_name) {
00132 free(attr->xmla_name);
00133 }
00134 if (attr->xmla_value) {
00135 free(attr->xmla_value);
00136 }
00137 free(attr);
00138 }
00139 free(node);
00140 }
00141 }
00142
00151 UXML_NODE *UxmlTreeAddSibling(UXML_NODE * node, UXML_NODE * sibling)
00152 {
00153 for (;;) {
00154 if (node->xmln_next == NULL) {
00155 node->xmln_next = sibling;
00156 sibling->xmln_parent = node->xmln_parent;
00157 break;
00158 }
00159 node = node->xmln_next;
00160 }
00161 return sibling;
00162 }
00163
00172 UXML_NODE *UxmlTreeAddChild(UXML_NODE * node, UXML_NODE * child)
00173 {
00174 if (node->xmln_child == NULL) {
00175 node->xmln_child = child;
00176 child->xmln_parent = node;
00177 } else {
00178 UxmlTreeAddSibling(node->xmln_child, child);
00179 }
00180 return child;
00181 }
00182
00188 void UxmlTreeDestroy(UXML_NODE * node)
00189 {
00190 UXML_NODE *np = node;
00191
00192 while (np) {
00193 node = np;
00194 np = np->xmln_next;
00195 if (node->xmln_child) {
00196 UxmlTreeDestroy(node->xmln_child);
00197 }
00198 UxmlNodeDestroy(node);
00199 }
00200 }
00201