libgimpprint API Reference: src/main/mxml-attr.c Source File

Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

# src/main/mxml-attr.c

Go to the documentation of this file.

    00001 /*
    00002  * "$Id: mxml-attr_8c-source.html,v 1.1.1.2 2004/12/22 23:49:05 jlovell Exp $"
    00003  *
    00004  * Attribute support code for mini-XML, a small XML-like file parsing library.
    00005  *
    00006  * Copyright 2003 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  *   stp_mxmlElementGetAttr() - Get an attribute.
    00021  *   stp_mxmlElementSetAttr() - Set an attribute.
    00022  */
    00023
    00024 /*
    00025  * Include necessary headers...
    00026  */
    00027
    00028 #include <gimp-print/mxml.h>
    00029 #include "config.h"
    00030
    00031
    00032 /*
    00033  * 'stp_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 stp_mxmlElementGetAttr(stp_mxml_node_t *node,   /* I - Element node */
    00041                    const char  *name)   /* I - Name of attribute */
    00042 {
    00043   int   i;                              /* Looping var */
    00044   stp_mxml_attr_t       *attr;                  /* Cirrent attribute */
    00045
    00046
    00047  /*
    00048   * Range check input...
    00049   */
    00050
    00051   if (!node || node->type != STP_MXML_ELEMENT || !name)
    00052     return (NULL);
    00053
    00054  /*
    00055   * Look for the attribute...
    00056   */
    00057
    00058   for (i = node->value.element.num_attrs, attr = node->value.element.attrs;
    00059        i > 0;
    00060        i --, attr ++)
    00061     if (!strcmp(attr->name, name))
    00062       return (attr->value);
    00063
    00064  /*
    00065   * Didn't find attribute, so return NULL...
    00066   */
    00067
    00068   return (NULL);
    00069 }
    00070
    00071
    00072 /*
    00073  * 'stp_mxmlElementSetAttr()' - Set an attribute.
    00074  *
    00075  * If the named attribute already exists, the value of the attribute
    00076  * is replaced by the new string value. The string value is copied
    00077  * into the element node. This function does nothing if the node is
    00078  * not an element.
    00079  */
    00080
    00081 void
    00082 stp_mxmlElementSetAttr(stp_mxml_node_t *node,   /* I - Element node */
    00083                    const char  *name,   /* I - Name of attribute */
    00084                    const char  *value)  /* I - Attribute value */
    00085 {
    00086   int           i;                      /* Looping var */
    00087   stp_mxml_attr_t       *attr;                  /* New attribute */
    00088
    00089
    00090  /*
    00091   * Range check input...
    00092   */
    00093
    00094   if (!node || node->type != STP_MXML_ELEMENT || !name || !value)
    00095     return;
    00096
    00097  /*
    00098   * Look for the attribute...
    00099   */
    00100
    00101   for (i = node->value.element.num_attrs, attr = node->value.element.attrs;
    00102        i > 0;
    00103        i --, attr ++)
    00104     if (!strcmp(attr->name, name))
    00105     {
    00106      /*
    00107       * Replace the attribute value and return...
    00108       */
    00109
    00110       free(attr->value);
    00111
    00112       attr->value = strdup(value);
    00113
    00114       return;
    00115     }
    00116
    00117  /*
    00118   * Attribute not found, so add a new one...
    00119   */
    00120
    00121   if (node->value.element.num_attrs == 0)
    00122     attr = malloc(sizeof(stp_mxml_attr_t));
    00123   else
    00124     attr = realloc(node->value.element.attrs,
    00125                    (node->value.element.num_attrs + 1) * sizeof(stp_mxml_attr_t));
    00126
    00127   if (!attr)
    00128   {
    00129     fprintf(stderr, "Unable to allocate memory for attribute '%s' in element %s!\n",
    00130             name, node->value.element.name);
    00131     return;
    00132   }
    00133
    00134   node->value.element.attrs = attr;
    00135   attr += node->value.element.num_attrs;
    00136
    00137   attr->name  = strdup(name);
    00138   attr->value = strdup(value);
    00139
    00140   if (!attr->name || !attr->value)
    00141   {
    00142     if (attr->name)
    00143       free(attr->name);
    00144
    00145     if (attr->value)
    00146       free(attr->value);
    00147
    00148     fprintf(stderr, "Unable to allocate memory for attribute '%s' in element %s!\n",
    00149             name, node->value.element.name);
    00150
    00151     return;
    00152   }
    00153
    00154   node->value.element.num_attrs ++;
    00155 }
    00156
    00157
    00158 /*
    00159  * End of "$Id: mxml-attr_8c-source.html,v 1.1.1.2 2004/12/22 23:49:05 jlovell Exp $".
    00160  */