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 */


    /*
     * 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))
        {
            /*
             * Free the old value as needed...
             */

            if (attr->value)
                PhFree(attr->value);

            attr->value = value;

            return (0);
        }

    /*
     * Add a new attribute...
     */

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

    if (!attr)
    {
        mxml_error("Unable to allocate memory for attribute '%s' in element %s!",
                   name, node->value.element.name);
        return (-1);
    }

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

    if ((attr->name = PhDuplicateBytesZSafe((char *)name)) == NULL)
    {
        mxml_error("Unable to allocate memory for attribute '%s' in element %s!",
                   name, node->value.element.name);
        return (-1);
    }

    attr->value = value;

    node->value.element.num_attrs ++;

    return (0);
}
Beispiel #2
0
mxml_index_t *				/* O - New index */
mxmlIndexNew(mxml_node_t *node,		/* I - XML node tree */
             const char  *element,	/* I - Element to index or @code NULL@ for all */
             const char  *attr)		/* I - Attribute to index or @code NULL@ for none */
{
  mxml_index_t	*ind;			/* New index */
  mxml_node_t	*current,		/* Current node in index */
  		**temp;			/* Temporary node pointer array */


 /*
  * Range check input...
  */

#ifdef DEBUG
  printf("mxmlIndexNew(node=%p, element=\"%s\", attr=\"%s\")\n",
         node, element ? element : "(null)", attr ? attr : "(null)");
#endif /* DEBUG */

  if (!node)
    return (NULL);

 /*
  * Create a new index...
  */

  if ((ind = PhAllocateExSafe(sizeof(mxml_index_t), HEAP_ZERO_MEMORY)) == NULL)
  {
    mxml_error("Unable to allocate %d bytes for index - %s",
               sizeof(mxml_index_t), strerror(errno));
    return (NULL);
  }

  if (attr)
    ind->attr = PhDuplicateBytesZSafe((char *)attr);

  if (!element && !attr)
    current = node;
  else
    current = mxmlFindElement(node, node, element, attr, NULL, MXML_DESCEND);

  while (current)
  {
    if (ind->num_nodes >= ind->alloc_nodes)
    {
      if (!ind->alloc_nodes)
        temp = PhAllocateSafe(64 * sizeof(mxml_node_t *));
      else
        temp = PhReAllocateSafe(ind->nodes, (ind->alloc_nodes + 64) * sizeof(mxml_node_t *));

      if (!temp)
      {
       /*
        * Unable to allocate memory for the index, so abort...
	*/

        mxml_error("Unable to allocate %d bytes for index: %s",
	           (ind->alloc_nodes + 64) * sizeof(mxml_node_t *),
		   strerror(errno));

        mxmlIndexDelete(ind);
	return (NULL);
      }

      ind->nodes       = temp;
      ind->alloc_nodes += 64;
    }

    ind->nodes[ind->num_nodes ++] = current;

    current = mxmlFindElement(current, node, element, attr, NULL, MXML_DESCEND);
  }

 /*
  * Sort nodes based upon the search criteria...
  */

#ifdef DEBUG
  {
    int i;				/* Looping var */


    printf("%d node(s) in index.\n\n", ind->num_nodes);

    if (attr)
    {
      printf("Node      Address   Element         %s\n", attr);
      puts("--------  --------  --------------  ------------------------------");

      for (i = 0; i < ind->num_nodes; i ++)
	printf("%8d  %-8p  %-14.14s  %s\n", i, ind->nodes[i],
	       ind->nodes[i]->value.element.name,
	       mxmlElementGetAttr(ind->nodes[i], attr));
    }
    else
    {
      puts("Node      Address   Element");
      puts("--------  --------  --------------");

      for (i = 0; i < ind->num_nodes; i ++)
	printf("%8d  %-8p  %s\n", i, ind->nodes[i],
	       ind->nodes[i]->value.element.name);
    }

    putchar('\n');
  }
#endif /* DEBUG */

  if (ind->num_nodes > 1)
    index_sort(ind, 0, ind->num_nodes - 1);

#ifdef DEBUG
  {
    int i;				/* Looping var */


    puts("After sorting:\n");

    if (attr)
    {
      printf("Node      Address   Element         %s\n", attr);
      puts("--------  --------  --------------  ------------------------------");

      for (i = 0; i < ind->num_nodes; i ++)
	printf("%8d  %-8p  %-14.14s  %s\n", i, ind->nodes[i],
	       ind->nodes[i]->value.element.name,
	       mxmlElementGetAttr(ind->nodes[i], attr));
    }
    else
    {
      puts("Node      Address   Element");
      puts("--------  --------  --------------");

      for (i = 0; i < ind->num_nodes; i ++)
	printf("%8d  %-8p  %s\n", i, ind->nodes[i],
	       ind->nodes[i]->value.element.name);
    }

    putchar('\n');
  }
#endif /* DEBUG */

 /*
  * Return the new index...
  */

  return (ind);
}