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

📄 runtest.c

📁 xml开源解析代码.版本为libxml2-2.6.29,可支持GB3212.网络消息发送XML时很有用.
💻 C
📖 第 1 页 / 共 5 页
字号:
static voidhtmlcdataDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len){    unsigned char output[40];    int inlen = len, outlen = 30;    htmlEncodeEntities(output, &outlen, ch, &inlen, 0);    output[outlen] = 0;    fprintf(SAXdebug, "SAX.cdata(%s, %d)\n", output, len);}static xmlSAXHandler debugHTMLSAXHandlerStruct = {    internalSubsetDebug,    isStandaloneDebug,    hasInternalSubsetDebug,    hasExternalSubsetDebug,    resolveEntityDebug,    getEntityDebug,    entityDeclDebug,    notationDeclDebug,    attributeDeclDebug,    elementDeclDebug,    unparsedEntityDeclDebug,    setDocumentLocatorDebug,    startDocumentDebug,    endDocumentDebug,    htmlstartElementDebug,    endElementDebug,    referenceDebug,    htmlcharactersDebug,    ignorableWhitespaceDebug,    processingInstructionDebug,    commentDebug,    warningDebug,    errorDebug,    fatalErrorDebug,    getParameterEntityDebug,    htmlcdataDebug,    externalSubsetDebug,    1,    NULL,    NULL,    NULL,    NULL};static xmlSAXHandlerPtr debugHTMLSAXHandler = &debugHTMLSAXHandlerStruct;#endif /* LIBXML_HTML_ENABLED */#ifdef LIBXML_SAX1_ENABLED/** * saxParseTest: * @filename: the file to parse * @result: the file with expected result * @err: the file with error messages * * Parse a file using the SAX API and check for errors. * * Returns 0 in case of success, an error code otherwise */static intsaxParseTest(const char *filename, const char *result,             const char *err ATTRIBUTE_UNUSED,             int options) {    int ret;    char *temp;    nb_tests++;    temp = resultFilename(filename, "", ".res");    if (temp == NULL) {        fprintf(stderr, "out of memory\n");        fatalError();    }    SAXdebug = fopen(temp, "wb");    if (SAXdebug == NULL) {        fprintf(stderr, "Failed to write to %s\n", temp);	free(temp);	return(-1);    }    /* for SAX we really want the callbacks though the context handlers */    xmlSetStructuredErrorFunc(NULL, NULL);    xmlSetGenericErrorFunc(NULL, testErrorHandler);#ifdef LIBXML_HTML_ENABLED    if (options & XML_PARSE_HTML) {	htmlSAXParseFile(filename, NULL, emptySAXHandler, NULL);	ret = 0;    } else#endif    ret = xmlSAXUserParseFile(emptySAXHandler, NULL, filename);    if (ret == XML_WAR_UNDECLARED_ENTITY) {        fprintf(SAXdebug, "xmlSAXUserParseFile returned error %d\n", ret);        ret = 0;    }    if (ret != 0) {        fprintf(stderr, "Failed to parse %s\n", filename);	return(1);    }#ifdef LIBXML_HTML_ENABLED    if (options & XML_PARSE_HTML) {	htmlSAXParseFile(filename, NULL, debugHTMLSAXHandler, NULL);	ret = 0;    } else#endif    if (options & XML_PARSE_SAX1) {	ret = xmlSAXUserParseFile(debugSAXHandler, NULL, filename);    } else {	ret = xmlSAXUserParseFile(debugSAX2Handler, NULL, filename);    }    if (ret == XML_WAR_UNDECLARED_ENTITY) {        fprintf(SAXdebug, "xmlSAXUserParseFile returned error %d\n", ret);        ret = 0;    }    fclose(SAXdebug);    if (compareFiles(temp, result)) {        fprintf(stderr, "Got a difference for %s\n", filename);        ret = 1;    } else    unlink(temp);    free(temp);        /* switch back to structured error handling */    xmlSetGenericErrorFunc(NULL, NULL);    xmlSetStructuredErrorFunc(NULL, testStructuredErrorHandler);    return(ret);}#endif/************************************************************************ *									* *		Parse to tree based tests				* *									* ************************************************************************//** * oldParseTest: * @filename: the file to parse * @result: the file with expected result * @err: the file with error messages: unused * * Parse a file using the old xmlParseFile API, then serialize back * reparse the result and serialize again, then check for deviation * in serialization. * * Returns 0 in case of success, an error code otherwise */static intoldParseTest(const char *filename, const char *result,             const char *err ATTRIBUTE_UNUSED,	     int options ATTRIBUTE_UNUSED) {    xmlDocPtr doc;    char *temp;    int res = 0;    nb_tests++;    /*     * base of the test, parse with the old API     */#ifdef LIBXML_SAX1_ENABLED    doc = xmlParseFile(filename);#else    doc = xmlReadFile(filename, NULL, 0);#endif    if (doc == NULL)        return(1);    temp = resultFilename(filename, "", ".res");    if (temp == NULL) {        fprintf(stderr, "out of memory\n");        fatalError();    }    xmlSaveFile(temp, doc);    if (compareFiles(temp, result)) {        res = 1;    }    xmlFreeDoc(doc);    /*     * Parse the saved result to make sure the round trip is okay     */#ifdef LIBXML_SAX1_ENABLED    doc = xmlParseFile(temp);#else    doc = xmlReadFile(temp, NULL, 0);#endif    if (doc == NULL)        return(1);    xmlSaveFile(temp, doc);    if (compareFiles(temp, result)) {        res = 1;    }    xmlFreeDoc(doc);    unlink(temp);    free(temp);    return(res);}#ifdef LIBXML_PUSH_ENABLED/** * pushParseTest: * @filename: the file to parse * @result: the file with expected result * @err: the file with error messages: unused * * Parse a file using the Push API, then serialize back * to check for content. * * Returns 0 in case of success, an error code otherwise */static intpushParseTest(const char *filename, const char *result,             const char *err ATTRIBUTE_UNUSED,	     int options) {    xmlParserCtxtPtr ctxt;    xmlDocPtr doc;    const char *base;    int size, res;    int cur = 0;    nb_tests++;    /*     * load the document in memory and work from there.     */    if (loadMem(filename, &base, &size) != 0) {        fprintf(stderr, "Failed to load %s\n", filename);	return(-1);    }    #ifdef LIBXML_HTML_ENABLED    if (options & XML_PARSE_HTML)	ctxt = htmlCreatePushParserCtxt(NULL, NULL, base + cur, 4, filename,	                                XML_CHAR_ENCODING_NONE);    else#endif    ctxt = xmlCreatePushParserCtxt(NULL, NULL, base + cur, 4, filename);    xmlCtxtUseOptions(ctxt, options);    cur += 4;    while (cur < size) {        if (cur + 1024 >= size) {#ifdef LIBXML_HTML_ENABLED	    if (options & XML_PARSE_HTML)		htmlParseChunk(ctxt, base + cur, size - cur, 1);	    else#endif	    xmlParseChunk(ctxt, base + cur, size - cur, 1);	    break;	} else {#ifdef LIBXML_HTML_ENABLED	    if (options & XML_PARSE_HTML)		htmlParseChunk(ctxt, base + cur, 1024, 0);	    else#endif	    xmlParseChunk(ctxt, base + cur, 1024, 0);	    cur += 1024;	}    }    doc = ctxt->myDoc;#ifdef LIBXML_HTML_ENABLED    if (options & XML_PARSE_HTML)        res = 1;    else#endif    res = ctxt->wellFormed;    xmlFreeParserCtxt(ctxt);    free((char *)base);    if (!res) {	xmlFreeDoc(doc);	fprintf(stderr, "Failed to parse %s\n", filename);	return(-1);    }#ifdef LIBXML_HTML_ENABLED    if (options & XML_PARSE_HTML)	htmlDocDumpMemory(doc, (xmlChar **) &base, &size);    else#endif    xmlDocDumpMemory(doc, (xmlChar **) &base, &size);    xmlFreeDoc(doc);    res = compareFileMem(result, base, size);    if ((base == NULL) || (res != 0)) {	if (base != NULL)	    xmlFree((char *)base);        fprintf(stderr, "Result for %s failed\n", filename);	return(-1);    }    xmlFree((char *)base);    if (err != NULL) {	res = compareFileMem(err, testErrors, testErrorsSize);	if (res != 0) {	    fprintf(stderr, "Error for %s failed\n", filename);	    return(-1);	}    }    return(0);}#endif/** * memParseTest: * @filename: the file to parse * @result: the file with expected result * @err: the file with error messages: unused * * Parse a file using the old xmlReadMemory API, then serialize back * reparse the result and serialize again, then check for deviation * in serialization. * * Returns 0 in case of success, an error code otherwise */static intmemParseTest(const char *filename, const char *result,             const char *err ATTRIBUTE_UNUSED,	     int options ATTRIBUTE_UNUSED) {    xmlDocPtr doc;    const char *base;    int size, res;    nb_tests++;    /*     * load and parse the memory     */    if (loadMem(filename, &base, &size) != 0) {        fprintf(stderr, "Failed to load %s\n", filename);	return(-1);    }        doc = xmlReadMemory(base, size, filename, NULL, 0);    unloadMem(base);    if (doc == NULL) {        return(1);    }    xmlDocDumpMemory(doc, (xmlChar **) &base, &size);    xmlFreeDoc(doc);    res = compareFileMem(result, base, size);    if ((base == NULL) || (res != 0)) {	if (base != NULL)	    xmlFree((char *)base);        fprintf(stderr, "Result for %s failed\n", filename);	return(-1);    }    xmlFree((char *)base);    return(0);}/** * noentParseTest: * @filename: the file to parse * @result: the file with expected result * @err: the file with error messages: unused * * Parse a file with entity resolution, then serialize back * reparse the result and serialize again, then check for deviation * in serialization. * * Returns 0 in case of success, an error code otherwise */static intnoentParseTest(const char *filename, const char *result,               const char *err  ATTRIBUTE_UNUSED,	       int options) {    xmlDocPtr doc;    char *temp;    int res = 0;    nb_tests++;    /*     * base of the test, parse with the old API     */    doc = xmlReadFile(filename, NULL, options);    if (doc == NULL)        return(1);    temp = resultFilename(filename, "", ".res");    if (temp == NULL) {        fprintf(stderr, "Out of memory\n");        fatalError();    }    xmlSaveFile(temp, doc);    if (compareFiles(temp, result)) {        res = 1;    }    xmlFreeDoc(doc);    /*     * Parse the saved result to make sure the round trip is okay     */    doc = xmlReadFile(filename, NULL, options);    if (doc == NULL)        return(1);    xmlSaveFile(temp, doc);    if (compareFiles(temp, result)) {        res = 1;    }    xmlFreeDoc(doc);    unlink(temp);    free(temp);    return(res);}/** * errParseTest: * @filename: the file to parse * @result: the file with expected result * @err: the file with error messages * * Parse a file using the xmlReadFile API and check for errors. * * Returns 0 in case of success, an error code otherwise */static interrParseTest(const char *filename, const char *result, const char *err,             int options) {    xmlDocPtr doc;    const char *base = NULL;    int size, res = 0;    nb_tests++;#ifdef LIBXML_HTML_ENABLED    if (options & XML_PARSE_HTML) {        doc = htmlReadFile(filename, NULL, options);    } else#endif#ifdef LIBXML_XINCLUDE_ENABLED    if (options & XML_PARSE_XINCLUDE) {	doc = xmlReadFile(filename, NULL, options);	xmlXIncludeProcessFlags(doc, options);    } else#endif    {	xmlGetWarningsDefaultValue = 1;	doc = xmlReadFile(filename, NULL, options);    }    xmlGetWarningsDefaultValue = 0;    if (result) {	if (doc == NULL) {	    base = "";	    size = 0;	} else {#ifdef LIBXML_HTML_ENABLED	    if (options & XML_PARSE_HTML) {		htmlDocDumpMemory(doc, (xmlChar **) &base, &size);	    } else#endif	    xmlDocDumpMemory(doc, (xmlChar **) &base, &size);	}	res = compareFileMem(result, base, size);    }    if (doc != NULL) {	if (base != NULL)	    xmlFree((char *)base);	xmlFreeDoc(doc);    }    if (res != 0) {        fprintf(stderr, "Result for %s failed\n", filename);	return(-1);    }    if (err != NULL) {	res = compareFileMem(err, testErrors, testErrorsSize);	if (res != 0) {	    fprintf(stderr, "Error for %s failed\n", filename);	    return(-1);	}    } else if (options & XML_PARSE_DTDVALID) {        if (testErrorsSize != 0)	    fprintf(stderr, "Validation for %s failed\n", filename);    }    return(0);}#ifdef LIBXML_READER_ENABLED/************************************************************************ *									* *		Reader based tests					* *									* ************************************************************************/static void processNode(FILE *out, xmlTextReaderPtr reader) {    const xmlChar *name, *value;    int type, empty;    type = xmlTextReaderNodeType(reader);    empty = xmlTextReaderIsEmptyElement(reader);    name = xmlTextReaderConstName(reader);    if (name == NULL)	name = BAD_CAST "--";    value = xmlTextReaderConstValue(reader);        fprintf(out, "%d %d %s %d %d", 	    xmlTextReaderDepth(reader),	    type,	    name,	    empty,	    xmlTextReaderHasValue(reader));    if (value == NULL)	fprintf(out, "\n");    else {	fprintf(out, " %s\n", value);    }}static intstreamProcessTest(const char *filename, const char *result, const char *err,                  xmlTextReaderPtr reader, const char *rng) {    int ret;    char *temp = NULL;    FILE *t = NULL;    if (reader == NULL)        return(-1);    nb_tests++;    if (result != NULL) {	temp = resultFilename(filename, "", ".res");	if (temp == NULL) {	    fprintf(stderr, "Out of memory\n");	    fatalError();	}	t = fopen(temp, "wb");	if (t == NULL) {	    fprintf(stderr, "Can't open temp file %s\n", temp);	    free(temp);	    return(-1);	}    }#ifdef LIBXML_SCHEMAS_ENABLED

⌨️ 快捷键说明

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