⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 valid.c

📁 SIP 1.5.0源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
/** * xmlAddID: * @ctxt:  the validation context * @doc:  pointer to the document * @value:  the value name * @attr:  the attribute holding the ID * * Register a new id declaration * * Returns NULL if not, othervise the new xmlIDPtr */xmlIDPtr xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,         xmlAttrPtr attr) {    xmlIDPtr ret, cur;    xmlIDTablePtr table;    int i;    if (doc == NULL) {        fprintf(stderr, "xmlAddIDDecl: doc == NULL\n");	return(NULL);    }    if (value == NULL) {        fprintf(stderr, "xmlAddIDDecl: value == NULL\n");	return(NULL);    }    if (attr == NULL) {        fprintf(stderr, "xmlAddIDDecl: attr == NULL\n");	return(NULL);    }    /*     * Create the ID table if needed.     */    table = doc->ids;    if (table == NULL)         table = doc->ids = xmlCreateIDTable();    if (table == NULL) {	fprintf(stderr, "xmlAddID: Table creation failed!\n");        return(NULL);    }    /*     * Validity Check:     * Search the DTD for previous declarations of the ATTLIST     */    for (i = 0;i < table->nb_ids;i++) {        cur = table->table[i];	if (!xmlStrcmp(cur->value, value)) {	    /*	     * The id is already defined in this Dtd.	     */	    VERROR(ctxt->userData, "ID %s already defined\n", value);	    return(NULL);	}    }    /*     * Grow the table, if needed.     */    if (table->nb_ids >= table->max_ids) {        /*	 * need more ids.	 */	table->max_ids *= 2;	table->table = (xmlIDPtr *) 	    xmlRealloc(table->table, table->max_ids *	            sizeof(xmlIDPtr));	if (table->table == NULL) {	    fprintf(stderr, "xmlAddID: out of memory\n");	    return(NULL);	}    }    ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));    if (ret == NULL) {	fprintf(stderr, "xmlAddID: out of memory\n");	return(NULL);    }    table->table[table->nb_ids] = ret;    /*     * fill the structure.     */    ret->value = xmlStrdup(value);    ret->attr = attr;    table->nb_ids++;    return(ret);}/** * xmlFreeID: * @not:  A id * * Deallocate the memory used by an id definition */voidxmlFreeID(xmlIDPtr id) {    if (id == NULL) return;    if (id->value != NULL)	xmlFree((xmlChar *) id->value);    memset(id, -1, sizeof(xmlID));    xmlFree(id);}/** * xmlFreeIDTable: * @table:  An id table * * Deallocate the memory used by an ID hash table. */voidxmlFreeIDTable(xmlIDTablePtr table) {    int i;    if (table == NULL) return;    for (i = 0;i < table->nb_ids;i++) {        xmlFreeID(table->table[i]);    }    xmlFree(table->table);    xmlFree(table);}/** * xmlIsID: * @doc:  the document * @elem:  the element carrying the attribute * @attr:  the attribute * * Determine whether an attribute is of type ID. In case we have Dtd(s) * then this is simple, otherwise we use an heuristic: name ID (upper * or lowercase). * * Returns 0 or 1 depending on the lookup result */intxmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {    if (doc == NULL) return(0);    if (attr == NULL) return(0);    if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {        if (((attr->name[0] == 'I') || (attr->name[0] == 'i')) &&            ((attr->name[1] == 'D') || (attr->name[1] == 'd')) &&	    (attr->name[2] == 0)) return(1);    } else if (doc->type == XML_HTML_DOCUMENT_NODE) {        if ((!xmlStrcmp(BAD_CAST "id", attr->name)) ||	    (!xmlStrcmp(BAD_CAST "name", attr->name)))	    return(1);	return(0);        } else {	xmlAttributePtr attrDecl;	if (elem == NULL) return(0);	attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);	if ((attrDecl == NULL) && (doc->extSubset != NULL))	    attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,	                                 attr->name);        if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))	    return(1);    }    return(0);}/** * xmlRemoveID * @doc:  the document * @attr:  the attribute * * Remove the given attribute from the ID table maintained internally. * * Returns -1 if the lookup failed and 0 otherwise */intxmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {    xmlIDPtr cur;    xmlIDTablePtr table;    int i;    if (doc == NULL) return(-1);    if (attr == NULL) return(-1);    table = doc->ids;    if (table == NULL)         return(-1);    /*     * Search the ID list.     */    for (i = 0;i < table->nb_ids;i++) {        cur = table->table[i];	if (cur->attr == attr) {	    table->nb_ids--;	    memmove(&table->table[i], &table->table[i+1],	            (table->nb_ids - i) * sizeof(xmlIDPtr));	    return(0);	}    }    return(-1);}/** * xmlGetID: * @doc:  pointer to the document * @ID:  the ID value * * Search the attribute declaring the given ID * * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID */xmlAttrPtr xmlGetID(xmlDocPtr doc, const xmlChar *ID) {    xmlIDPtr cur;    xmlIDTablePtr table;    int i;    if (doc == NULL) {        fprintf(stderr, "xmlGetID: doc == NULL\n");	return(NULL);    }    if (ID == NULL) {        fprintf(stderr, "xmlGetID: ID == NULL\n");	return(NULL);    }    table = doc->ids;    if (table == NULL)         return(NULL);    /*     * Search the ID list.     */    for (i = 0;i < table->nb_ids;i++) {        cur = table->table[i];	if (!xmlStrcmp(cur->value, ID)) {	    return(cur->attr);	}    }    return(NULL);}/************************************************************************ *									* *				Refs					* *									* ************************************************************************//** * xmlCreateRefTable: * * create and initialize an empty ref hash table. * * Returns the xmlRefTablePtr just created or NULL in case *                of error. */xmlRefTablePtrxmlCreateRefTable(void) {    xmlRefTablePtr ret;    ret = (xmlRefTablePtr)          xmlMalloc(sizeof(xmlRefTable));    if (ret == NULL) {        fprintf(stderr, "xmlCreateRefTable : xmlMalloc(%ld) failed\n",	        (long)sizeof(xmlRefTable));        return(NULL);    }    ret->max_refs = XML_MIN_NOTATION_TABLE;    ret->nb_refs = 0;    ret->table = (xmlRefPtr *)          xmlMalloc(ret->max_refs * sizeof(xmlRefPtr));    if (ret == NULL) {        fprintf(stderr, "xmlCreateRefTable : xmlMalloc(%ld) failed\n",	        ret->max_refs * (long)sizeof(xmlRef));	xmlFree(ret);        return(NULL);    }    return(ret);}/** * xmlAddRef: * @ctxt:  the validation context * @doc:  pointer to the document * @value:  the value name * @attr:  the attribute holding the Ref * * Register a new ref declaration * * Returns NULL if not, othervise the new xmlRefPtr */xmlRefPtr xmlAddRef(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,         xmlAttrPtr attr) {    xmlRefPtr ret;    xmlRefTablePtr table;    if (doc == NULL) {        fprintf(stderr, "xmlAddRefDecl: doc == NULL\n");	return(NULL);    }    if (value == NULL) {        fprintf(stderr, "xmlAddRefDecl: value == NULL\n");	return(NULL);    }    if (attr == NULL) {        fprintf(stderr, "xmlAddRefDecl: attr == NULL\n");	return(NULL);    }    /*     * Create the Ref table if needed.     */    table = doc->refs;    if (table == NULL)         table = doc->refs = xmlCreateRefTable();    if (table == NULL) {	fprintf(stderr, "xmlAddRef: Table creation failed!\n");        return(NULL);    }    /*     * Grow the table, if needed.     */    if (table->nb_refs >= table->max_refs) {        /*	 * need more refs.	 */	table->max_refs *= 2;	table->table = (xmlRefPtr *) 	    xmlRealloc(table->table, table->max_refs *	            sizeof(xmlRefPtr));	if (table->table == NULL) {	    fprintf(stderr, "xmlAddRef: out of memory\n");	    return(NULL);	}    }    ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));    if (ret == NULL) {	fprintf(stderr, "xmlAddRef: out of memory\n");	return(NULL);    }    table->table[table->nb_refs] = ret;    /*     * fill the structure.     */    ret->value = xmlStrdup(value);    ret->attr = attr;    table->nb_refs++;    return(ret);}/** * xmlFreeRef: * @not:  A ref * * Deallocate the memory used by an ref definition */voidxmlFreeRef(xmlRefPtr ref) {    if (ref == NULL) return;    if (ref->value != NULL)	xmlFree((xmlChar *) ref->value);    memset(ref, -1, sizeof(xmlRef));    xmlFree(ref);}/** * xmlFreeRefTable: * @table:  An ref table * * Deallocate the memory used by an Ref hash table. */voidxmlFreeRefTable(xmlRefTablePtr table) {    int i;    if (table == NULL) return;    for (i = 0;i < table->nb_refs;i++) {        xmlFreeRef(table->table[i]);    }    xmlFree(table->table);    xmlFree(table);}/** * xmlIsRef: * @doc:  the document * @elem:  the element carrying the attribute * @attr:  the attribute * * Determine whether an attribute is of type Ref. In case we have Dtd(s) * then this is simple, otherwise we use an heuristic: name Ref (upper * or lowercase). * * Returns 0 or 1 depending on the lookup result */intxmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {    if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {        return(0);	/*******************        if (((attr->name[0] == 'I') || (attr->name[0] == 'i')) &&            ((attr->name[1] == 'D') || (attr->name[1] == 'd')) &&	    (attr->name[2] == 0)) return(1);	 *******************/    } else {	xmlAttributePtr attrDecl;	attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);	if ((attrDecl == NULL) && (doc->extSubset != NULL))	    attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name,	                                 attr->name);        if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_IDREF))	    return(1);    }    return(0);}/** * xmlRemoveRef * @doc:  the document * @attr:  the attribute * * Remove the given attribute from the Ref table maintained internally. * * Returns -1 if the lookup failed and 0 otherwise */intxmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {    xmlRefPtr cur;    xmlRefTablePtr table;    int i;    if (doc == NULL) return(-1);    if (attr == NULL) return(-1);    table = doc->refs;    if (table == NULL)         return(-1);    /*     * Search the Ref list.     */    for (i = 0;i < table->nb_refs;i++) {        cur = table->table[i];	if (cur->attr == attr) {	    table->nb_refs--;	    memmove(&table->table[i], &table->table[i+1],	            (table->nb_refs - i) * sizeof(xmlRefPtr));	    return(0);	}    }    return(-1);}/** * xmlGetRef: * @doc:  pointer to the document * @Ref:  the Ref value * * Search the next attribute declaring the given Ref * * Returns NULL if not found, otherwise the xmlAttrPtr defining the Ref */xmlAttrPtr xmlGetRef(xmlDocPtr doc, const xmlChar *Ref) {    xmlRefPtr cur;    xmlRefTablePtr table;    int i;    if (doc == NULL) {        fprintf(stderr, "xmlGetRef: doc == NULL\n");	return(NULL);    }    if (Ref == NULL) {        fprintf(stderr, "xmlGetRef: Ref == NULL\n");	return(NULL);    }    table = doc->refs;    if (table == NULL)         return(NULL);    /*     * Search the Ref list.     */    for (i = 0;i < table->nb_refs;i++) {        cur = table->table[i];	if (!xmlStrcmp(cur->value, Ref)) {	    return(cur->attr);	}    }    return(NULL);}/************************************************************************ *									* *		Routines for validity checking				* *									* ************************************************************************//** * xmlGetDtdElementDesc: * @dtd:  a pointer to the DtD to search * @name:  the element name * * Search the Dtd for the description of this element * * returns the xmlElementPtr if found or NULL */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -