/* * Extend the existing value for an attribute, appending the given value. */ static void slaxNodeAttribExtend (slax_data_t *sdp, xmlNodePtr nodep, const char *attrib, const char *value, const char *uri) { const xmlChar *uattrib = (const xmlChar *) attrib; xmlChar *current = uri ? xmlGetProp(nodep, uattrib) : xmlGetNsProp(nodep, uattrib, (const xmlChar *) uri); int clen = current ? xmlStrlen(current) + 1 : 0; int vlen = strlen(value) + 1; unsigned char *newp = alloca(clen + vlen); if (newp == NULL) { xmlParserError(sdp->sd_ctxt, "%s:%d: out of memory", sdp->sd_filename, sdp->sd_line); return; } if (clen) { memcpy(newp, current, clen - 1); newp[clen - 1] = ' '; xmlFree(current); } memcpy(newp + clen, value, vlen); if (uri == NULL) xmlSetProp(nodep, uattrib, newp); else { xmlNsPtr nsp = xmlSearchNsByHref(sdp->sd_docp, nodep, (const xmlChar *) uri); xmlSetNsProp(nodep, nsp, uattrib, newp); } }
/* * Find or construct a (possibly temporary) namespace node * for the "func" exslt library and put the given node into * that namespace. We also have to add this as an "extension" * namespace. */ static xmlNsPtr slaxSetNs (slax_data_t *sdp, xmlNodePtr nodep, const char *prefix, const xmlChar *uri, int local) { xmlNsPtr nsp; xmlNodePtr root = xmlDocGetRootElement(sdp->sd_docp); nsp = xmlSearchNs(sdp->sd_docp, root, (const xmlChar *) prefix); if (nsp == NULL) { nsp = xmlNewNs(root, uri, (const xmlChar *) prefix); if (nsp == NULL) { xmlParserError(sdp->sd_ctxt, "%s:%d: out of memory", sdp->sd_filename, sdp->sd_line); return NULL; } /* * Since we added this namespace, we need to add it to the * list of extension prefixes. */ slaxNodeAttribExtend(sdp, root, ATT_EXTENSION_ELEMENT_PREFIXES, prefix, NULL); } if (nodep) { /* Add a distinct namespace to the current node */ nsp = xmlNewNs(nodep, uri, (const xmlChar *) prefix); if (local) nodep->ns = nsp; } return nsp; }
int sylverant_quests_read(const char *filename, sylverant_quest_list_t *rv) { xmlParserCtxtPtr cxt; xmlDoc *doc; xmlNode *n; int irv = 0; /* Clear out the config. */ memset(rv, 0, sizeof(sylverant_quest_list_t)); /* Make sure the file exists and can be read, otherwise quietly bail out */ if(access(filename, R_OK)) { return -1; } /* Create an XML Parsing context */ cxt = xmlNewParserCtxt(); if(!cxt) { debug(DBG_ERROR, "Couldn't create parsing context for config\n"); irv = -2; goto err; } /* Open the configuration file for reading. */ doc = xmlReadFile(filename, NULL, XML_PARSE_DTDVALID); if(!doc) { xmlParserError(cxt, "Error in parsing config"); irv = -3; goto err_cxt; } /* Make sure the document validated properly. */ if(!cxt->valid) { xmlParserValidityError(cxt, "Validity Error parsing config"); irv = -4; goto err_doc; } /* If we've gotten this far, we have a valid document, go through and read everything contained within... */ n = xmlDocGetRootElement(doc); if(!n) { debug(DBG_WARN, "Empty config document\n"); irv = -5; goto err_doc; } /* Make sure the config looks sane. */ if(xmlStrcmp(n->name, XC"quests")) { debug(DBG_WARN, "Quest List does not appear to be the right type\n"); irv = -6; goto err_doc; } n = n->children; while(n) { if(n->type != XML_ELEMENT_NODE) { /* Ignore non-elements. */ n = n->next; continue; } else if(!xmlStrcmp(n->name, XC"category")) { if(handle_category(n, rv)) { irv = -7; goto err_clean; } } else { debug(DBG_WARN, "Invalid Tag %s on line %hu\n", (char *)n->name, n->line); } n = n->next; } /* Cleanup/error handling below... */ err_clean: if(irv < 0) { sylverant_quests_destroy(rv); } err_doc: xmlFreeDoc(doc); err_cxt: xmlFreeParserCtxt(cxt); err: return irv; }