| 1 | /* |
| 2 | * Attribute support code for mini-XML, a small XML-like file parsing library. |
| 3 | * |
| 4 | * Copyright 2003 by Michael Sweet. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Library General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * Contents: |
| 17 | * |
| 18 | * stp_mxmlElementGetAttr() - Get an attribute. |
| 19 | * stp_mxmlElementSetAttr() - Set an attribute. |
| 20 | */ |
| 21 | |
| 22 | /* |
| 23 | * Include necessary headers... |
| 24 | */ |
| 25 | |
| 26 | #include <gutenprint/util.h> |
| 27 | #include <gutenprint/mxml.h> |
| 28 | #ifdef HAVE_CONFIG_H |
| 29 | #include "config.h" |
| 30 | #endif |
| 31 | |
| 32 | |
| 33 | /* |
| 34 | * 'stp_mxmlElementGetAttr()' - Get an attribute. |
| 35 | * |
| 36 | * This function returns NULL if the node is not an element or the |
| 37 | * named attribute does not exist. |
| 38 | */ |
| 39 | |
| 40 | const char * /* O - Attribute value or NULL */ |
| 41 | stp_mxmlElementGetAttr(stp_mxml_node_t *node, /* I - Element node */ |
| 42 | const char *name) /* I - Name of attribute */ |
| 43 | { |
| 44 | int i; /* Looping var */ |
| 45 | stp_mxml_attr_t *attr; /* Cirrent attribute */ |
| 46 | |
| 47 | |
| 48 | /* |
| 49 | * Range check input... |
| 50 | */ |
| 51 | |
| 52 | if (!node || node->type != STP_MXML_ELEMENT || !name) |
| 53 | return (NULL); |
| 54 | |
| 55 | /* |
| 56 | * Look for the attribute... |
| 57 | */ |
| 58 | |
| 59 | for (i = node->value.element.num_attrs, attr = node->value.element.attrs; |
| 60 | i > 0; |
| 61 | i --, attr ++) |
| 62 | if (!strcmp(attr->name, name)) |
| 63 | return (attr->value); |
| 64 | |
| 65 | /* |
| 66 | * Didn't find attribute, so return NULL... |
| 67 | */ |
| 68 | |
| 69 | return (NULL); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /* |
| 74 | * 'stp_mxmlElementSetAttr()' - Set an attribute. |
| 75 | * |
| 76 | * If the named attribute already exists, the value of the attribute |
| 77 | * is replaced by the new string value. The string value is copied |
| 78 | * into the element node. This function does nothing if the node is |
| 79 | * not an element. |
| 80 | */ |
| 81 | |
| 82 | void |
| 83 | stp_mxmlElementSetAttr(stp_mxml_node_t *node, /* I - Element node */ |
| 84 | const char *name, /* I - Name of attribute */ |
| 85 | const char *value) /* I - Attribute value */ |
| 86 | { |
| 87 | int i; /* Looping var */ |
| 88 | stp_mxml_attr_t *attr; /* New attribute */ |
| 89 | |
| 90 | |
| 91 | /* |
| 92 | * Range check input... |
| 93 | */ |
| 94 | |
| 95 | if (!node || node->type != STP_MXML_ELEMENT || !name || !value) |
| 96 | return; |
| 97 | |
| 98 | /* |
| 99 | * Look for the attribute... |
| 100 | */ |
| 101 | |
| 102 | for (i = node->value.element.num_attrs, attr = node->value.element.attrs; |
| 103 | i > 0; |
| 104 | i --, attr ++) |
| 105 | if (!strcmp(attr->name, name)) |
| 106 | { |
| 107 | /* |
| 108 | * Replace the attribute value and return... |
| 109 | */ |
| 110 | |
| 111 | free(attr->value); |
| 112 | |
| 113 | attr->value = stp_strdup(value); |
| 114 | |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Attribute not found, so add a new one... |
| 120 | */ |
| 121 | |
| 122 | if (node->value.element.num_attrs == 0) |
| 123 | attr = malloc(sizeof(stp_mxml_attr_t)); |
| 124 | else |
| 125 | attr = realloc(node->value.element.attrs, |
| 126 | (node->value.element.num_attrs + 1) * sizeof(stp_mxml_attr_t)); |
| 127 | |
| 128 | if (!attr) |
| 129 | { |
| 130 | fprintf(stderr, "Unable to allocate memory for attribute '%s' in element %s!\n", |
| 131 | name, node->value.element.name); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | node->value.element.attrs = attr; |
| 136 | attr += node->value.element.num_attrs; |
| 137 | |
| 138 | attr->name = stp_strdup(name); |
| 139 | attr->value = stp_strdup(value); |
| 140 | |
| 141 | if (!attr->name || !attr->value) |
| 142 | { |
| 143 | if (attr->name) |
| 144 | free(attr->name); |
| 145 | |
| 146 | if (attr->value) |
| 147 | free(attr->value); |
| 148 | |
| 149 | fprintf(stderr, "Unable to allocate memory for attribute '%s' in element %s!\n", |
| 150 | name, node->value.element.name); |
| 151 | |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | node->value.element.num_attrs ++; |
| 156 | } |