From 7177fe1589cad14c70e39a95e078d09c7a96dc29 Mon Sep 17 00:00:00 2001 From: jbj Date: Mon, 14 Jun 2004 03:12:22 +0000 Subject: [PATCH] First crack at xml parser object. CVS patchset: 7317 CVS date: 2004/06/14 03:12:22 --- tools/Makefile.am | 4 +- tools/rpmxml.c | 162 ++---------------------------------------------------- tools/rpmxp.c | 146 ++++++++++++++++++++++++++++++++++++++++++++++++ tools/rpmxp.h | 81 +++++++++++++++++++++++++++ 4 files changed, 235 insertions(+), 158 deletions(-) create mode 100644 tools/rpmxp.c create mode 100644 tools/rpmxp.h diff --git a/tools/Makefile.am b/tools/Makefile.am index a809540..209f692 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -17,7 +17,7 @@ INCLUDES = -I. \ -I/usr/include/libxml2 -I$(top_srcdir)/misc -EXTRA_DIST = rpminject.c rpmsort.c rpmxml.c hashtab.h +EXTRA_DIST = rpminject.c rpmsort.c rpmxml.c rpmxp.c rpmxp.h hashtab.h EXTRA_PROGRAMS = convertdb1 rpminject rpmsort @@ -69,7 +69,7 @@ rpmsort_SOURCES = rpmsort.c rpmtool_SOURCES = rpmtool.c #rpmtool_LDFLAGS = $(staticLDFLAGS) -rpmxml_SOURCES = rpmxml.c +rpmxml_SOURCES = rpmxml.c rpmxp.c rpmxml_LDADD = -lxml2 $(PROGRAMS): $(LDADD) diff --git a/tools/rpmxml.c b/tools/rpmxml.c index 95107b3..8ef1109 100644 --- a/tools/rpmxml.c +++ b/tools/rpmxml.c @@ -1,166 +1,16 @@ #include "system.h" -#include +#include #include "debug.h" -typedef struct rpmxp_s * rpmxp; - -struct rpmxp_s { - xmlTextReaderPtr reader; - - xmlChar * name; - xmlChar * value; - int depth; - int nodeType; - int isEmptyElement; - - int n; -}; - -static int _rpmxp_debug = 0; - -static rpmxp rpmxpFree(rpmxp xp) -{ - if (xp != NULL) { - if (xp->value) { - xmlFree(xp->value); - xp->value = NULL; - } - if (xp->name) { - xmlFree(xp->name); - xp->name = NULL; - } - if (xp->reader != NULL) { - xmlFreeTextReader(xp->reader); - xp->reader = NULL; - } - free(xp); - xp = NULL; - } - return xp; -} - -static rpmxp rpmxpNew(const char * fn) -{ - rpmxp xp = calloc(1, sizeof(*xp)); - - if (xp == NULL) - return NULL; - if (fn) { - int xx; - xp->reader = xmlNewTextReaderFilename(fn); - if (xp->reader == NULL) - return rpmxpFree(xp); - xx = xmlTextReaderSetParserProp(xp->reader, XML_PARSER_VALIDATE, 1); - xx = xmlTextReaderSetParserProp(xp->reader, XML_PARSER_SUBST_ENTITIES, 1); - } - xp->name = NULL; - xp->value = NULL; - xp->depth = 0; - xp->nodeType = 0; - xp->isEmptyElement = 0; - xp->n = 2; - return xp; -} - -static int rpmxpRead(rpmxp xp) -{ - return xmlTextReaderRead(xp->reader); -} - -static void rpmxpProcess(rpmxp xp) -{ - xp->name = xmlTextReaderName(xp->reader); - xp->value = xmlTextReaderValue(xp->reader); - xp->depth = xmlTextReaderDepth(xp->reader); - xp->nodeType = xmlTextReaderNodeType(xp->reader); - xp->isEmptyElement = xmlTextReaderIsEmptyElement(xp->reader); - - if (xp->name == NULL) - xp->name = xmlStrdup(BAD_CAST "--"); - -if (_rpmxp_debug) -printf("%d %d %s %d\n", xp->depth, xp->nodeType, xp->name, xp->isEmptyElement); - switch (xp->nodeType) { - case XML_READER_TYPE_ELEMENT: - printf("%*s<%s", (xp->n * xp->depth), "", xp->name); - while (xmlTextReaderMoveToNextAttribute(xp->reader) != 0) { - xmlChar * attrN = xmlTextReaderName(xp->reader); - xmlChar * attrV = xmlTextReaderValue(xp->reader); - printf(" %s", attrN); - if (attrV) { - printf("=\"%s\"", attrV); - xmlFree(attrV); - } - xmlFree(attrN); - } - printf(">"); - if (xp->depth < 2) - printf("\n"); - break; - case XML_READER_TYPE_END_ELEMENT: - if (xp->depth < 2) - printf("%*s", (xp->n * xp->depth), ""); - printf("\n", xp->name); - break; - case XML_READER_TYPE_TEXT: - printf("%s", xp->value); - break; - case XML_READER_TYPE_DOCUMENT_TYPE: - break; - case XML_READER_TYPE_SIGNIFICANT_WHITESPACE: - break; - - case XML_READER_TYPE_NONE: - case XML_READER_TYPE_ATTRIBUTE: - case XML_READER_TYPE_CDATA: - case XML_READER_TYPE_ENTITY_REFERENCE: - case XML_READER_TYPE_ENTITY: - case XML_READER_TYPE_PROCESSING_INSTRUCTION: - case XML_READER_TYPE_COMMENT: - case XML_READER_TYPE_DOCUMENT: - case XML_READER_TYPE_DOCUMENT_FRAGMENT: - case XML_READER_TYPE_NOTATION: - case XML_READER_TYPE_WHITESPACE: - case XML_READER_TYPE_END_ENTITY: - case XML_READER_TYPE_XML_DECLARATION: - default: - printf("%d %d %s %d\n", xp->depth, xp->nodeType, - xp->name, xp->isEmptyElement); - if (xp->value) - printf(" %s", xp->value); - if (xp->depth < 2) - printf("\n"); - break; - } - - - if (xp->value != NULL) { - xmlFree(xp->value); - xp->value = NULL; - } - if (xp->name != NULL) { - xmlFree(xp->name); - xp->name = NULL; - } -} - -static int streamFile(const char * fn) -{ - rpmxp xp = rpmxpNew(fn); - int ret = -1; - - if (xp != NULL) - while ((ret = rpmxpRead(xp)) == 1) - rpmxpProcess(xp); - xp = rpmxpFree(xp); - return ret; -} - int main(int argc, char ** argv) { const char * fn = "time.xml"; + rpmxp xp; int ec = 0; - streamFile(fn); + xp = rpmxpNew(fn); + rpmxpParseFile(xp); + xp = rpmxpFree(xp); + return ec; } diff --git a/tools/rpmxp.c b/tools/rpmxp.c new file mode 100644 index 0000000..fea2c21 --- /dev/null +++ b/tools/rpmxp.c @@ -0,0 +1,146 @@ +#include "system.h" +#define _RPMXP_INTERNAL +#include "rpmxp.h" +#include "debug.h" + +int _rpmxp_debug = 0; + +rpmxp rpmxpFree(rpmxp xp) +{ + if (xp != NULL) { + if (xp->value) { + xmlFree(xp->value); + xp->value = NULL; + } + if (xp->name) { + xmlFree(xp->name); + xp->name = NULL; + } + if (xp->reader != NULL) { + xmlFreeTextReader(xp->reader); + xp->reader = NULL; + } + free(xp); + xp = NULL; + } + return xp; +} + +rpmxp rpmxpNew(const char * fn) +{ + rpmxp xp = calloc(1, sizeof(*xp)); + + if (xp == NULL) + return NULL; + if (fn) { + int xx; + xp->reader = xmlNewTextReaderFilename(fn); + if (xp->reader == NULL) + return rpmxpFree(xp); + xx = xmlTextReaderSetParserProp(xp->reader, XML_PARSER_VALIDATE, 1); + xx = xmlTextReaderSetParserProp(xp->reader, XML_PARSER_SUBST_ENTITIES, 1); + } + xp->name = NULL; + xp->value = NULL; + xp->depth = 0; + xp->nodeType = 0; + xp->isEmptyElement = 0; + xp->n = 2; + return xp; +} + +int rpmxpRead(rpmxp xp) +{ + return xmlTextReaderRead(xp->reader); +} + +int rpmxpProcess(rpmxp xp) +{ + int rc = 0; + + xp->name = xmlTextReaderName(xp->reader); + xp->value = xmlTextReaderValue(xp->reader); + xp->depth = xmlTextReaderDepth(xp->reader); + xp->nodeType = xmlTextReaderNodeType(xp->reader); + xp->isEmptyElement = xmlTextReaderIsEmptyElement(xp->reader); + + if (xp->name == NULL) + xp->name = xmlStrdup(BAD_CAST "--"); + +if (_rpmxp_debug) +printf("%d %d %s %d\n", xp->depth, xp->nodeType, xp->name, xp->isEmptyElement); + switch (xp->nodeType) { + case XML_READER_TYPE_ELEMENT: + printf("%*s<%s", (xp->n * xp->depth), "", xp->name); + while (xmlTextReaderMoveToNextAttribute(xp->reader) != 0) { + xmlChar * attrN = xmlTextReaderName(xp->reader); + xmlChar * attrV = xmlTextReaderValue(xp->reader); + printf(" %s", attrN); + if (attrV) { + printf("=\"%s\"", attrV); + xmlFree(attrV); + } + xmlFree(attrN); + } + printf(">"); + if (xp->depth < 2) + printf("\n"); + break; + case XML_READER_TYPE_END_ELEMENT: + if (xp->depth < 2) + printf("%*s", (xp->n * xp->depth), ""); + printf("\n", xp->name); + break; + case XML_READER_TYPE_TEXT: + printf("%s", xp->value); + break; + case XML_READER_TYPE_DOCUMENT_TYPE: + break; + case XML_READER_TYPE_SIGNIFICANT_WHITESPACE: + break; + + case XML_READER_TYPE_NONE: + case XML_READER_TYPE_ATTRIBUTE: + case XML_READER_TYPE_CDATA: + case XML_READER_TYPE_ENTITY_REFERENCE: + case XML_READER_TYPE_ENTITY: + case XML_READER_TYPE_PROCESSING_INSTRUCTION: + case XML_READER_TYPE_COMMENT: + case XML_READER_TYPE_DOCUMENT: + case XML_READER_TYPE_DOCUMENT_FRAGMENT: + case XML_READER_TYPE_NOTATION: + case XML_READER_TYPE_WHITESPACE: + case XML_READER_TYPE_END_ENTITY: + case XML_READER_TYPE_XML_DECLARATION: + default: + printf("%d %d %s %d\n", xp->depth, xp->nodeType, + xp->name, xp->isEmptyElement); + if (xp->value) + printf(" %s", xp->value); + if (xp->depth < 2) + printf("\n"); + rc = -1; + break; + } + + + if (xp->value != NULL) { + xmlFree(xp->value); + xp->value = NULL; + } + if (xp->name != NULL) { + xmlFree(xp->name); + xp->name = NULL; + } + return rc; +} + +int rpmxpParseFile(rpmxp xp) +{ + int ret = -1; + + if (xp != NULL) + while ((ret = rpmxpRead(xp)) == 1) + rpmxpProcess(xp); + return ret; +} diff --git a/tools/rpmxp.h b/tools/rpmxp.h new file mode 100644 index 0000000..c3b65e6 --- /dev/null +++ b/tools/rpmxp.h @@ -0,0 +1,81 @@ +#ifndef H_RPMXP +#define H_RPMXP + +/** \ingroup rpmdep rpmtrans + * \file tools/rpmxp.h + * Structure(s) used for an xml parser. + */ + +#include + +/** + */ +/*@-exportlocal@*/ +/*@unchecked@*/ +extern int _rpmxp_debug; +/*@=exportlocal@*/ + +/** + */ +typedef struct rpmxp_s * rpmxp; + + +#if defined(_RPMXP_INTERNAL) +/** + * An xml parser. + */ +struct rpmxp_s { + xmlTextReaderPtr reader; + + xmlChar * name; + xmlChar * value; + int depth; + int nodeType; + int isEmptyElement; + + int n; +}; +#endif + +/** +* Destroy an xml parser. + * @param xp rpm xml parser + * @return NULL always + */ +rpmxp rpmxpFree(/*@only@*/ /*@null@*/ rpmxp xp) + /*@modifies xp @*/; + +/** + * Create an xml parser. + * @param fn xml file name + * @return new xml parser + */ +/*@only@*/ +rpmxp rpmxpNew(const char * fn) + /*@*/; + +/** + * Read next item from an xml parser. + * @param xp rpm xml parser + * @return 1 to continue + */ +int rpmxpRead(rpmxp xp) + /*@modifies xp @*/; + +/** + * Parse all elements from an xml parser. + * @param xp rpm xml parser + * @return 0 on success + */ +int rpmxpProcess(rpmxp xp) + /*@modifies xp @*/; + +/** + * Parse all elements from an xml parser. + * @param xp rpm xml parser + * @return 0 on success + */ +int rpmxpParseFile(rpmxp xp) + /*@modifies xp @*/; + +#endif /* H_RPMXP */ -- 2.7.4