/home/dko/projects/mobilec/tags/MobileC-v1.10.2/MobileC-v1.10.2/src/mxml-2.2.2/mxml-attr.c

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

Generated on Fri Jul 11 17:59:45 2008 for Mobile-C by  doxygen 1.5.4