mxml-attr.c 7.76 KB
Newer Older
Michael R Sweet's avatar
Michael R Sweet committed
1
/*
Michael Sweet's avatar
Michael Sweet committed
2
 * Attribute support code for Mini-XML, a small XML file parsing library.
Michael R Sweet's avatar
Michael R Sweet committed
3
 *
Michael Sweet's avatar
Michael Sweet committed
4
 * Copyright 2003-2017 by Michael R Sweet.
Michael R Sweet's avatar
Michael R Sweet committed
5
 *
6 7 8 9 10
 * These coded instructions, statements, and computer programs are the
 * property of Michael R Sweet and are protected by Federal copyright
 * law.  Distribution and use rights are outlined in the file "COPYING"
 * which should have been included with this file.  If this file is
 * missing or damaged, see the license at:
Michael R Sweet's avatar
Michael R Sweet committed
11
 *
Michael Sweet's avatar
Michael Sweet committed
12
 *     https://michaelrsweet.github.io/mxml
Michael R Sweet's avatar
Michael R Sweet committed
13 14 15 16 17 18
 */

/*
 * Include necessary headers...
 */

19
#include "config.h"
20
#include "mxml.h"
Michael R Sweet's avatar
Michael R Sweet committed
21 22


23 24 25 26 27 28 29 30
/*
 * Local functions...
 */

static int	mxml_set_attr(mxml_node_t *node, const char *name,
		              char *value);


Michael R Sweet's avatar
Michael R Sweet committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
/*
 * 'mxmlElementDeleteAttr()' - Delete an attribute.
 *
 * @since Mini-XML 2.4@
 */

void
mxmlElementDeleteAttr(mxml_node_t *node,/* I - Element */
                      const char  *name)/* I - Attribute name */
{
  int		i;			/* Looping var */
  mxml_attr_t	*attr;			/* Cirrent attribute */


#ifdef DEBUG
  fprintf(stderr, "mxmlElementDeleteAttr(node=%p, name=\"%s\")\n",
          node, name ? name : "(null)");
#endif /* DEBUG */

 /*
  * Range check input...
  */

  if (!node || node->type != MXML_ELEMENT || !name)
    return;

 /*
  * Look for the attribute...
  */

  for (i = node->value.element.num_attrs, attr = node->value.element.attrs;
       i > 0;
       i --, attr ++)
  {
#ifdef DEBUG
    printf("    %s=\"%s\"\n", attr->name, attr->value);
#endif /* DEBUG */

    if (!strcmp(attr->name, name))
    {
     /*
      * Delete this attribute...
      */

      free(attr->name);
      free(attr->value);

      i --;
      if (i > 0)
        memmove(attr, attr + 1, i * sizeof(mxml_attr_t));

      node->value.element.num_attrs --;
83 84 85

      if (node->value.element.num_attrs == 0)
        free(node->value.element.attrs);
Michael R Sweet's avatar
Michael R Sweet committed
86 87 88 89 90 91
      return;
    }
  }
}


Michael R Sweet's avatar
Michael R Sweet committed
92 93
/*
 * 'mxmlElementGetAttr()' - Get an attribute.
94
 *
95
 * This function returns @code NULL@ if the node is not an element or the
96
 * named attribute does not exist.
Michael R Sweet's avatar
Michael R Sweet committed
97 98
 */

99
const char *				/* O - Attribute value or @code NULL@ */
Michael R Sweet's avatar
Michael R Sweet committed
100 101 102
mxmlElementGetAttr(mxml_node_t *node,	/* I - Element node */
                   const char  *name)	/* I - Name of attribute */
{
Michael R Sweet's avatar
Michael R Sweet committed
103
  int		i;			/* Looping var */
Michael R Sweet's avatar
Michael R Sweet committed
104 105 106
  mxml_attr_t	*attr;			/* Cirrent attribute */


Michael R Sweet's avatar
Michael R Sweet committed
107 108 109 110 111
#ifdef DEBUG
  fprintf(stderr, "mxmlElementGetAttr(node=%p, name=\"%s\")\n",
          node, name ? name : "(null)");
#endif /* DEBUG */

Michael R Sweet's avatar
Michael R Sweet committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125
 /*
  * Range check input...
  */

  if (!node || node->type != MXML_ELEMENT || !name)
    return (NULL);

 /*
  * Look for the attribute...
  */

  for (i = node->value.element.num_attrs, attr = node->value.element.attrs;
       i > 0;
       i --, attr ++)
Michael R Sweet's avatar
Michael R Sweet committed
126 127 128 129 130
  {
#ifdef DEBUG
    printf("    %s=\"%s\"\n", attr->name, attr->value);
#endif /* DEBUG */

Michael R Sweet's avatar
Michael R Sweet committed
131
    if (!strcmp(attr->name, name))
Michael R Sweet's avatar
Michael R Sweet committed
132 133 134 135
    {
#ifdef DEBUG
      printf("    Returning \"%s\"!\n", attr->value);
#endif /* DEBUG */
Michael R Sweet's avatar
Michael R Sweet committed
136
      return (attr->value);
Michael R Sweet's avatar
Michael R Sweet committed
137 138
    }
  }
Michael R Sweet's avatar
Michael R Sweet committed
139 140 141 142 143

 /*
  * Didn't find attribute, so return NULL...
  */

Michael R Sweet's avatar
Michael R Sweet committed
144 145 146 147
#ifdef DEBUG
  puts("    Returning NULL!\n");
#endif /* DEBUG */

Michael R Sweet's avatar
Michael R Sweet committed
148 149 150 151
  return (NULL);
}


152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
/*
 * 'mxmlElementGetAttrByIndex()' - Get an element attribute by index.
 *
 * The index ("idx") is 0-based.  @code NULL@ is returned if the specified index
 * is out of range.
 *
 * @since Mini-XML 2.11@
 */

const char *                            /* O - Attribute value */
mxmlElementGetAttrByIndex(
    mxml_node_t *node,                  /* I - Node */
    int         idx,                    /* I - Attribute index, starting at 0 */
    const char  **name)                 /* O - Attribute name */
{
  if (!node || node->type != MXML_ELEMENT || idx < 0 || idx >= node->value.element.num_attrs)
    return (NULL);

  if (name)
    *name = node->value.element.attrs[idx].name;

  return (node->value.element.attrs[idx].value);
}


/*
 * 'mxmlElementGetAttrCount()' - Get the number of element attributes.
 *
 * @since Mini-XML 2.11@
 */

int                                     /* O - Number of attributes */
mxmlElementGetAttrCount(
    mxml_node_t *node)                  /* I - Node */
{
  if (node && node->type == MXML_ELEMENT)
    return (node->value.element.num_attrs);
  else
    return (0);
}


Michael R Sweet's avatar
Michael R Sweet committed
194 195
/*
 * 'mxmlElementSetAttr()' - Set an attribute.
196 197 198 199 200
 *
 * If the named attribute already exists, the value of the attribute
 * is replaced by the new string value. The string value is copied
 * into the element node. This function does nothing if the node is
 * not an element.
Michael R Sweet's avatar
Michael R Sweet committed
201 202 203 204 205 206 207
 */

void
mxmlElementSetAttr(mxml_node_t *node,	/* I - Element node */
                   const char  *name,	/* I - Name of attribute */
                   const char  *value)	/* I - Attribute value */
{
208
  char	*valuec;			/* Copy of value */
Michael R Sweet's avatar
Michael R Sweet committed
209 210


Michael R Sweet's avatar
Michael R Sweet committed
211 212 213 214 215
#ifdef DEBUG
  fprintf(stderr, "mxmlElementSetAttr(node=%p, name=\"%s\", value=\"%s\")\n",
          node, name ? name : "(null)", value ? value : "(null)");
#endif /* DEBUG */

Michael R Sweet's avatar
Michael R Sweet committed
216 217 218 219
 /*
  * Range check input...
  */

220
  if (!node || node->type != MXML_ELEMENT || !name)
Michael R Sweet's avatar
Michael R Sweet committed
221 222
    return;

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  if (value)
    valuec = strdup(value);
  else
    valuec = NULL;

  if (mxml_set_attr(node, name, valuec))
    free(valuec);
}


/*
 * 'mxmlElementSetAttrf()' - Set an attribute with a formatted value.
 *
 * If the named attribute already exists, the value of the attribute
 * is replaced by the new formatted string. The formatted string value is
 * copied into the element node. This function does nothing if the node
 * is not an element.
240 241
 *
 * @since Mini-XML 2.3@
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
 */

void
mxmlElementSetAttrf(mxml_node_t *node,	/* I - Element node */
                    const char  *name,	/* I - Name of attribute */
                    const char  *format,/* I - Printf-style attribute value */
		    ...)		/* I - Additional arguments as needed */
{
  va_list	ap;			/* Argument pointer */
  char		*value;			/* Value */


#ifdef DEBUG
  fprintf(stderr,
          "mxmlElementSetAttrf(node=%p, name=\"%s\", format=\"%s\", ...)\n",
          node, name ? name : "(null)", format ? format : "(null)");
#endif /* DEBUG */

 /*
  * Range check input...
  */

  if (!node || node->type != MXML_ELEMENT || !name || !format)
    return;

 /*
  * Format the value...
  */

  va_start(ap, format);
  value = _mxml_vstrdupf(format, ap);
  va_end(ap);

275
  if (!value)
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    mxml_error("Unable to allocate memory for attribute '%s' in element %s!",
               name, node->value.element.name);
  else if (mxml_set_attr(node, name, value))
    free(value);
}


/*
 * 'mxml_set_attr()' - Set or add an attribute name/value pair.
 */

static int				/* O - 0 on success, -1 on failure */
mxml_set_attr(mxml_node_t *node,	/* I - Element node */
              const char  *name,	/* I - Attribute name */
              char        *value)	/* I - Attribute value */
{
  int		i;			/* Looping var */
  mxml_attr_t	*attr;			/* New attribute */


Michael R Sweet's avatar
Michael R Sweet committed
296 297 298 299 300 301 302 303 304 305
 /*
  * Look for the attribute...
  */

  for (i = node->value.element.num_attrs, attr = node->value.element.attrs;
       i > 0;
       i --, attr ++)
    if (!strcmp(attr->name, name))
    {
     /*
306
      * Free the old value as needed...
Michael R Sweet's avatar
Michael R Sweet committed
307 308
      */

309 310
      if (attr->value)
        free(attr->value);
Michael R Sweet's avatar
Michael R Sweet committed
311

312
      attr->value = value;
Michael R Sweet's avatar
Michael R Sweet committed
313

314
      return (0);
Michael R Sweet's avatar
Michael R Sweet committed
315 316 317
    }

 /*
318
  * Add a new attribute...
Michael R Sweet's avatar
Michael R Sweet committed
319 320 321 322 323 324 325 326 327 328
  */

  if (node->value.element.num_attrs == 0)
    attr = malloc(sizeof(mxml_attr_t));
  else
    attr = realloc(node->value.element.attrs,
                   (node->value.element.num_attrs + 1) * sizeof(mxml_attr_t));

  if (!attr)
  {
Michael R Sweet's avatar
Michael R Sweet committed
329 330
    mxml_error("Unable to allocate memory for attribute '%s' in element %s!",
               name, node->value.element.name);
331
    return (-1);
Michael R Sweet's avatar
Michael R Sweet committed
332 333 334 335 336
  }

  node->value.element.attrs = attr;
  attr += node->value.element.num_attrs;

337
  if ((attr->name = strdup(name)) == NULL)
Michael R Sweet's avatar
Michael R Sweet committed
338
  {
Michael R Sweet's avatar
Michael R Sweet committed
339 340
    mxml_error("Unable to allocate memory for attribute '%s' in element %s!",
               name, node->value.element.name);
341
    return (-1);
Michael R Sweet's avatar
Michael R Sweet committed
342
  }
343 344 345

  attr->value = value;

Michael R Sweet's avatar
Michael R Sweet committed
346
  node->value.element.num_attrs ++;
347 348

  return (0);
Michael R Sweet's avatar
Michael R Sweet committed
349
}