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