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) { free(attr->value); } attr->value = value; return (0); } /* * Add a new attribute... */ 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) { 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 = strdup(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); }
static int /* O - 0 on success, -1 on error */ myxml_add_char(int ch, /* I - Character to add */ char **bufptr, /* IO - Current position in buffer */ char **buffer, /* IO - Current buffer */ size_t *bufsize) /* IO - Current buffer size */ { char *newbuffer; /* New buffer value */ if (*bufptr >= (*buffer + *bufsize - 4)) { /* * Increase the size of the buffer... */ if (*bufsize < 1024) { (*bufsize) *= 2; } else { (*bufsize) *= 3; (*bufsize) /= 2; } newbuffer = (char *)malloc(*bufsize*sizeof(char)); if(!newbuffer){ free(*buffer); mxml_error("Unable to expand string buffer to %d bytes!", *bufsize); return (-1); } memset(newbuffer,0,*bufsize*sizeof(char)); memcpy(newbuffer,*buffer,*bufptr - *buffer); free(*buffer); *bufptr = newbuffer + (*bufptr - *buffer); *buffer = newbuffer; } if (ch < 128) { /* * Single byte ASCII... */ *(*bufptr)++ = ch; } else if (ch < 2048) { /* * Two-byte UTF-8... */ *(*bufptr)++ = 0xc0 | (ch >> 6); *(*bufptr)++ = 0x80 | (ch & 0x3f); }
int /* O - 0 on success, -1 on failure */ mxmlEntityAddCallback(int (*cb)(const char *name)) /* I - Callback function to add */ { if (num_callbacks < (int)(sizeof(callbacks) / sizeof(callbacks[0]))) { callbacks[num_callbacks] = cb; num_callbacks ++; return (0); } else { mxml_error("Unable to add entity callback!"); return (-1); } }
MINIXML_API int /* O - 0 on success, -1 on failure */ mxmlEntityAddCallback( mxml_entity_cb_t cb) /* I - Callback function to add */ { _mxml_global_t *global = _mxml_global(); /* Global data */ if (global->num_entity_cbs < (int)(sizeof(global->entity_cbs) / sizeof(global->entity_cbs[0]))) { global->entity_cbs[global->num_entity_cbs] = cb; global->num_entity_cbs ++; return (0); } else { mxml_error("Unable to add entity callback!"); return (-1); } }
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); if (!value) 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_index_t* /* O - New index */ mxmlIndexNew(mxml_node_t* node, /* I - XML node tree */ const char* element, /* I - Element to index or NULL for all */ const char* attr) { /* I - Attribute to index or 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 = calloc(1, sizeof(mxml_index_t))) == NULL) { mxml_error("Unable to allocate %d bytes for index - %s", sizeof(mxml_index_t), strerror(errno)); return (NULL); } if (attr) { ind->attr = strdup(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 = malloc(64 * sizeof(mxml_node_t*)); } else { temp = realloc(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); }
/***************************************************************************** * As the other error function takes va_list this is a simply to * enable calling the real error function ****************************************************************************/ static void local_error(CTX_T *data, char *format, ...) { va_list argp; va_start(argp, format); mxml_error(data, format, argp); va_end(argp); }