/home/dko/projects/mobilec/trunk/src/mxml-2.2.2/mxml-attr.c

Go to the documentation of this file.
00001 /*
00002  * "$Id: mxml-attr.c,v 1.1 2007/05/23 20:43:27 david_ko Exp $"
00003  *
00004  * Attribute support code for Mini-XML, a small XML-like file parsing library.
00005  *
00006  * Copyright 2003-2005 by Michael Sweet.
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Library General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2, or (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * Contents:
00019  *
00020  *   mxmlElementGetAttr() - Get an attribute.
00021  *   mxmlElementSetAttr() - Set an attribute.
00022  */
00023 
00024 /*
00025  * Include necessary headers...
00026  */
00027 
00028 #include "config.h"
00029 #include "mxml.h"
00030 
00031 
00032 /*
00033  * 'mxmlElementGetAttr()' - Get an attribute.
00034  *
00035  * This function returns NULL if the node is not an element or the
00036  * named attribute does not exist.
00037  */
00038 
00039 const char *                            /* O - Attribute value or NULL */
00040 mxmlElementGetAttr(mxml_node_t *node,   /* I - Element node */
00041                    const char  *name)   /* I - Name of attribute */
00042 {
00043   int   i;                              /* Looping var */
00044   mxml_attr_t   *attr;                  /* Cirrent attribute */
00045 
00046 
00047 #ifdef DEBUG
00048   fprintf(stderr, "mxmlElementGetAttr(node=%p, name=\"%s\")\n",
00049           node, name ? name : "(null)");
00050 #endif /* DEBUG */
00051 
00052  /*
00053   * Range check input...
00054   */
00055 
00056   if (!node || node->type != MXML_ELEMENT || !name)
00057     return (NULL);
00058 
00059  /*
00060   * Look for the attribute...
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   * Didn't find attribute, so return NULL...
00071   */
00072 
00073   return (NULL);
00074 }
00075 
00076 
00077 /*
00078  * 'mxmlElementSetAttr()' - Set an attribute.
00079  *
00080  * If the named attribute already exists, the value of the attribute
00081  * is replaced by the new string value. The string value is copied
00082  * into the element node. This function does nothing if the node is
00083  * not an element.
00084  */
00085 
00086 void
00087 mxmlElementSetAttr(mxml_node_t *node,   /* I - Element node */
00088                    const char  *name,   /* I - Name of attribute */
00089                    const char  *value)  /* I - Attribute value */
00090 {
00091   int           i;                      /* Looping var */
00092   mxml_attr_t   *attr;                  /* New attribute */
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 /* DEBUG */
00099 
00100  /*
00101   * Range check input...
00102   */
00103 
00104   if (!node || node->type != MXML_ELEMENT || !name)
00105     return;
00106 
00107  /*
00108   * Look for the attribute...
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       * Replace the attribute value and return...
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   * Attribute not found, so add a new one...
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  * End of "$Id: mxml-attr.c,v 1.1 2007/05/23 20:43:27 david_ko Exp $".
00181  */

Generated on Fri May 16 14:49:54 2008 for Mobile-C by  doxygen 1.5.4