File:  [Public] / XML / testDtd.c
Revision 1.2: download - view: text, annotated - select for diffs
Wed Oct 25 19:26:53 2000 UTC (25 years, 1 month ago) by veillard
Branches: MAIN
CVS tags: LIBXML_2_2_8, HEAD
Message cleanup Jumbo Diff:
- in xmlerror.h : I added the export of an error context type (void *)
     an error handler type xmlGenericErrorFunc
     there is an interface
       xmlSetGenericErrorFunc  (void *ctx, xmlGenericErrorFunc handler);
     to reset the error handling routine and its argument
 (by default it's equivalent to respectively fprintf and stderr.
- in all the c files, all wild accesses to stderr or stdout within
 the library have been replaced to the handler.
- removed slashrdf.c now obsolete
Daniel

/*
 * testDtd.c : a small tester program for Dtd input.
 *
 * See Copyright for the status of this software.
 *
 * Daniel.Veillard@w3.org
 */

#ifdef WIN32
#include "win32config.h"
#else
#include "config.h"
#endif

#include "xmlversion.h"

#include <stdio.h>
#include <string.h>
#include <stdarg.h>


#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/debugXML.h>
#include <libxml/xmlerror.h>

#ifdef LIBXML_DEBUG_ENABLED
static int debug = 0;
#endif
static int noout = 0;
static char *encoding = NULL;

/************************************************************************
 *									*
 *				Debug					*
 *									*
 ************************************************************************/

void parseAndPrintFile(char *filename) {
    xmlDtdPtr dtd = NULL;

    /*
     * build an Dtd tree from a string;
     */
    dtd = xmlParseDTD(NULL, (const xmlChar *) filename);
    if (dtd == NULL) {
        xmlGenericError(xmlGenericErrorContext,
		"Could not parse %s\n", filename);
	return;
    }

    /*
     * print it.
     */
    if (!noout) { 
#ifdef LIBXML_DEBUG_ENABLED
	if (!debug) {
	    if (encoding)
		xmlSaveFileEnc("-", (xmlDocPtr) dtd, encoding);
	    else
		xmlDocDump(stdout, (xmlDocPtr) dtd);
	} else
	    xmlDebugDumpDTD(stdout, dtd);
#else
	if (encoding)
	    xmlSaveFileEnc("-", (xmlDocPtr) dtd, encoding);
	else
	    xmlDocDump(stdout, (xmlDocPtr) dtd);
#endif
    }	

    /*
     * free it.
     */
    xmlFreeDtd(dtd);
}

int main(int argc, char **argv) {
    int i;
    int files = 0;

    for (i = 1; i < argc ; i++) {
#ifdef LIBXML_DEBUG_ENABLED
	if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
	    debug++;
	else
#endif
	if ((!strcmp(argv[i], "-noout")) || (!strcmp(argv[i], "--noout")))
	    noout++;
	else if ((!strcmp(argv[i], "-encode")) ||
	         (!strcmp(argv[i], "--encode"))) {
	    i++;
	    encoding = argv[i];
        }
    }
    for (i = 1; i < argc ; i++) {
	if ((!strcmp(argv[i], "-encode")) ||
	         (!strcmp(argv[i], "--encode"))) {
	    i++;
	    continue;
        }
	if (argv[i][0] != '-') {
	    parseAndPrintFile(argv[i]);
	    files ++;
	}
    }
    if (files == 0) {
	printf("Usage : %s [--debug] [--noout] [--encode xxx] Dtdfiles ...\n",
	       argv[0]);
	printf("\tParse the Dtd files and output the result of the parsing\n");
#ifdef LIBXML_DEBUG_ENABLED
	printf("\t--debug : dump a debug tree of the in-memory Dtd\n");
#endif
	printf("\t--noout : do not print the result\n");
	printf("\t--encode encoding : output in the given encoding\n");
    }
    xmlCleanupParser();
    xmlMemoryDump();

    return(0);
}

Webmaster