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 #include "config.h"
00029 #include "mxml.h"
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 const char *
00040 mxmlElementGetAttr(mxml_node_t *node,
00041 const char *name)
00042 {
00043 int i;
00044 mxml_attr_t *attr;
00045
00046
00047 #ifdef DEBUG
00048 fprintf(stderr, "mxmlElementGetAttr(node=%p, name=\"%s\")\n",
00049 node, name ? name : "(null)");
00050 #endif
00051
00052
00053
00054
00055
00056 if (!node || node->type != MXML_ELEMENT || !name)
00057 return (NULL);
00058
00059
00060
00061
00062
00063 for (i = node->value.element.num_attrs, attr = node->value.element.attrs;
00064 i > 0;
00065 i --, attr ++)
00066 if (!strcmp(attr->name, name))
00067 return (attr->value);
00068
00069
00070
00071
00072
00073 return (NULL);
00074 }
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 void
00087 mxmlElementSetAttr(mxml_node_t *node,
00088 const char *name,
00089 const char *value)
00090 {
00091 int i;
00092 mxml_attr_t *attr;
00093
00094
00095 #ifdef DEBUG
00096 fprintf(stderr, "mxmlElementSetAttr(node=%p, name=\"%s\", value=\"%s\")\n",
00097 node, name ? name : "(null)", value ? value : "(null)");
00098 #endif
00099
00100
00101
00102
00103
00104 if (!node || node->type != MXML_ELEMENT || !name)
00105 return;
00106
00107
00108
00109
00110
00111 for (i = node->value.element.num_attrs, attr = node->value.element.attrs;
00112 i > 0;
00113 i --, attr ++)
00114 if (!strcmp(attr->name, name))
00115 {
00116
00117
00118
00119
00120 if (attr->value)
00121 free(attr->value);
00122
00123 if (value)
00124 {
00125 if ((attr->value = strdup(value)) == NULL)
00126 mxml_error("Unable to allocate memory for attribute '%s' in element %s!",
00127 name, node->value.element.name);
00128 }
00129 else
00130 attr->value = NULL;
00131
00132 return;
00133 }
00134
00135
00136
00137
00138
00139 if (node->value.element.num_attrs == 0)
00140 attr = malloc(sizeof(mxml_attr_t));
00141 else
00142 attr = realloc(node->value.element.attrs,
00143 (node->value.element.num_attrs + 1) * sizeof(mxml_attr_t));
00144
00145 if (!attr)
00146 {
00147 mxml_error("Unable to allocate memory for attribute '%s' in element %s!",
00148 name, node->value.element.name);
00149 return;
00150 }
00151
00152 node->value.element.attrs = attr;
00153 attr += node->value.element.num_attrs;
00154
00155 attr->name = strdup(name);
00156 if (value)
00157 attr->value = strdup(value);
00158 else
00159 attr->value = NULL;
00160
00161 if (!attr->name || (!attr->value && value))
00162 {
00163 if (attr->name)
00164 free(attr->name);
00165
00166 if (attr->value)
00167 free(attr->value);
00168
00169 mxml_error("Unable to allocate memory for attribute '%s' in element %s!",
00170 name, node->value.element.name);
00171
00172 return;
00173 }
00174
00175 node->value.element.num_attrs ++;
00176 }
00177
00178
00179
00180
00181