| 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>cxx . . $ `<file>/include/gutenprint/util.h`/ |
| 27 | #include <gutenprint/mxml.h>cxx . . $ `<file>/include/gutenprint/mxml.h`/ |
| 28 | #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:411:9`! |
| 29 | #include "config.h"cxx . . $ `<file>/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_mxmlElementGetAttrcxx . . $ stp_mxmlElementGetAttr(42933e0d82869bb1).(stp_mxml_node_tcxx . . $ 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_tcxx . . $ stp_mxml_attr_t# *attr; /* Cirrent attribute */ |
| 46 | |
| 47 | |
| 48 | /* |
| 49 | * Range check input... |
| 50 | */ |
| 51 | |
| 52 | if (!node || node->typecxx . . $ stp_mxml_node_s#type. != STP_MXML_ELEMENTcxx . . $ STP_MXML_ELEMENT. || !name) |
| 53 | return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 54 | |
| 55 | /* |
| 56 | * Look for the attribute... |
| 57 | */ |
| 58 | |
| 59 | for (i = node->valuecxx . . $ stp_mxml_node_s#value..elementcxx . . $ stp_mxml_value_u#element..num_attrscxx . . $ stp_mxml_value_s#num_attrs., attr = node->valuecxx . . $ stp_mxml_node_s#value..elementcxx . . $ stp_mxml_value_u#element..attrscxx . . $ stp_mxml_value_s#attrs.; |
| 60 | i > 0; |
| 61 | i --, attr ++) |
| 62 | if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(attr->namecxx . . $ stp_mxml_attr_s#name., name)) |
| 63 | return (attr->valuecxx . . $ stp_mxml_attr_s#value.); |
| 64 | |
| 65 | /* |
| 66 | * Didn't find attribute, so return NULL... |
| 67 | */ |
| 68 | |
| 69 | return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 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_mxmlElementSetAttrcxx . . $ stp_mxmlElementSetAttr(22374eb3aa38e502).(stp_mxml_node_tcxx . . $ 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_tcxx . . $ stp_mxml_attr_t# *attr; /* New attribute */ |
| 89 | |
| 90 | |
| 91 | /* |
| 92 | * Range check input... |
| 93 | */ |
| 94 | |
| 95 | if (!node || node->typecxx . . $ stp_mxml_node_s#type. != STP_MXML_ELEMENTcxx . . $ STP_MXML_ELEMENT. || !name || !value) |
| 96 | return; |
| 97 | |
| 98 | /* |
| 99 | * Look for the attribute... |
| 100 | */ |
| 101 | |
| 102 | for (i = node->valuecxx . . $ stp_mxml_node_s#value..elementcxx . . $ stp_mxml_value_u#element..num_attrscxx . . $ stp_mxml_value_s#num_attrs., attr = node->valuecxx . . $ stp_mxml_node_s#value..elementcxx . . $ stp_mxml_value_u#element..attrscxx . . $ stp_mxml_value_s#attrs.; |
| 103 | i > 0; |
| 104 | i --, attr ++) |
| 105 | if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(attr->namecxx . . $ stp_mxml_attr_s#name., name)) |
| 106 | { |
| 107 | /* |
| 108 | * Replace the attribute value and return... |
| 109 | */ |
| 110 | |
| 111 | freecxx . . $ free(5d22bdacc48458e8).(attr->valuecxx . . $ stp_mxml_attr_s#value.); |
| 112 | |
| 113 | attr->valuecxx . . $ stp_mxml_attr_s#value. = stp_strdupcxx . . $ stp_strdup(a8b8e32347182212).(value); |
| 114 | |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Attribute not found, so add a new one... |
| 120 | */ |
| 121 | |
| 122 | if (node->valuecxx . . $ stp_mxml_node_s#value..elementcxx . . $ stp_mxml_value_u#element..num_attrscxx . . $ stp_mxml_value_s#num_attrs. == 0) |
| 123 | attr = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(stp_mxml_attr_tcxx . . $ stp_mxml_attr_t#)); |
| 124 | else |
| 125 | attr = realloccxx . . $ realloc(417ab160f6baadc6).(node->valuecxx . . $ stp_mxml_node_s#value..elementcxx . . $ stp_mxml_value_u#element..attrscxx . . $ stp_mxml_value_s#attrs., |
| 126 | (node->valuecxx . . $ stp_mxml_node_s#value..elementcxx . . $ stp_mxml_value_u#element..num_attrscxx . . $ stp_mxml_value_s#num_attrs. + 1) * sizeof(stp_mxml_attr_tcxx . . $ stp_mxml_attr_t#)); |
| 127 | |
| 128 | if (!attr) |
| 129 | { |
| 130 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Unable to allocate memory for attribute '%s' in element %s!\n", |
| 131 | name, node->valuecxx . . $ stp_mxml_node_s#value..elementcxx . . $ stp_mxml_value_u#element..namecxx . . $ stp_mxml_value_s#name.); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | node->valuecxx . . $ stp_mxml_node_s#value..elementcxx . . $ stp_mxml_value_u#element..attrscxx . . $ stp_mxml_value_s#attrs. = attr; |
| 136 | attr += node->valuecxx . . $ stp_mxml_node_s#value..elementcxx . . $ stp_mxml_value_u#element..num_attrscxx . . $ stp_mxml_value_s#num_attrs.; |
| 137 | |
| 138 | attr->namecxx . . $ stp_mxml_attr_s#name. = stp_strdupcxx . . $ stp_strdup(a8b8e32347182212).(name); |
| 139 | attr->valuecxx . . $ stp_mxml_attr_s#value. = stp_strdupcxx . . $ stp_strdup(a8b8e32347182212).(value); |
| 140 | |
| 141 | if (!attr->namecxx . . $ stp_mxml_attr_s#name. || !attr->valuecxx . . $ stp_mxml_attr_s#value.) |
| 142 | { |
| 143 | if (attr->namecxx . . $ stp_mxml_attr_s#name.) |
| 144 | freecxx . . $ free(5d22bdacc48458e8).(attr->namecxx . . $ stp_mxml_attr_s#name.); |
| 145 | |
| 146 | if (attr->valuecxx . . $ stp_mxml_attr_s#value.) |
| 147 | freecxx . . $ free(5d22bdacc48458e8).(attr->valuecxx . . $ stp_mxml_attr_s#value.); |
| 148 | |
| 149 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Unable to allocate memory for attribute '%s' in element %s!\n", |
| 150 | name, node->valuecxx . . $ stp_mxml_node_s#value..elementcxx . . $ stp_mxml_value_u#element..namecxx . . $ stp_mxml_value_s#name.); |
| 151 | |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | node->valuecxx . . $ stp_mxml_node_s#value..elementcxx . . $ stp_mxml_value_u#element..num_attrscxx . . $ stp_mxml_value_s#num_attrs. ++; |
| 156 | } |