From 9a82b94a94bd310db426edd453b0f38c6c8f69f5 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Wed, 24 Aug 2022 04:21:58 +0200 Subject: Introduce xmlNewSAXParserCtxt and htmlNewSAXParserCtxt Add API functions to create a parser context with a custom SAX handler without having to mess with ctxt->sax manually. --- parserInternals.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 8 deletions(-) (limited to 'parserInternals.c') diff --git a/parserInternals.c b/parserInternals.c index c3a14ac0..7e3126c9 100644 --- a/parserInternals.c +++ b/parserInternals.c @@ -1439,16 +1439,19 @@ xmlNewInputFromFile(xmlParserCtxtPtr ctxt, const char *filename) { ************************************************************************/ /** - * xmlInitParserCtxt: - * @ctxt: an XML parser context + * xmlInitSAXParserCtxt: + * @ctxt: XML parser context + * @sax: SAX handlert + * @userData: user data * - * Initialize a parser context + * Initialize a SAX parser context * * Returns 0 in case of success and -1 in case of error */ -int -xmlInitParserCtxt(xmlParserCtxtPtr ctxt) +static int +xmlInitSAXParserCtxt(xmlParserCtxtPtr ctxt, xmlSAXHandlerPtr sax, + void *userData) { xmlParserInputPtr input; @@ -1473,8 +1476,19 @@ xmlInitParserCtxt(xmlParserCtxtPtr ctxt) xmlErrMemory(NULL, "cannot initialize parser context\n"); return(-1); } - else + if (sax == NULL) { + memset(ctxt->sax, 0, sizeof(xmlSAXHandler)); xmlSAXVersion(ctxt->sax, 2); + ctxt->userData = ctxt; + } else { + if (sax->initialized == XML_SAX2_MAGIC) { + memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler)); + } else { + memset(ctxt->sax, 0, sizeof(xmlSAXHandler)); + memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1)); + } + ctxt->userData = userData ? userData : ctxt; + } ctxt->maxatts = 0; ctxt->atts = NULL; @@ -1572,7 +1586,6 @@ xmlInitParserCtxt(xmlParserCtxtPtr ctxt) ctxt->spaceMax = 10; ctxt->spaceTab[0] = -1; ctxt->space = &ctxt->spaceTab[0]; - ctxt->userData = ctxt; ctxt->myDoc = NULL; ctxt->wellFormed = 1; ctxt->nsWellFormed = 1; @@ -1624,6 +1637,24 @@ xmlInitParserCtxt(xmlParserCtxtPtr ctxt) return(0); } +/** + * xmlInitParserCtxt: + * @ctxt: an XML parser context + * + * DEPRECATED: Internal function which will be made private in a future + * version. + * + * Initialize a parser context + * + * Returns 0 in case of success and -1 in case of error + */ + +int +xmlInitParserCtxt(xmlParserCtxtPtr ctxt) +{ + return(xmlInitSAXParserCtxt(ctxt, NULL, NULL)); +} + /** * xmlFreeParserCtxt: * @ctxt: an XML parser context @@ -1720,6 +1751,22 @@ xmlFreeParserCtxt(xmlParserCtxtPtr ctxt) xmlParserCtxtPtr xmlNewParserCtxt(void) +{ + return(xmlNewSAXParserCtxt(NULL, NULL)); +} + +/** + * xmlNewSAXParserCtxt: + * @sax: SAX handler + * @userData: user data + * + * Allocate and initialize a new SAX parser context. + * + * Returns the xmlParserCtxtPtr or NULL + */ + +xmlParserCtxtPtr +xmlNewSAXParserCtxt(xmlSAXHandlerPtr sax, void *userData) { xmlParserCtxtPtr ctxt; @@ -1729,7 +1776,7 @@ xmlNewParserCtxt(void) return(NULL); } memset(ctxt, 0, sizeof(xmlParserCtxt)); - if (xmlInitParserCtxt(ctxt) < 0) { + if (xmlInitSAXParserCtxt(ctxt, sax, userData) < 0) { xmlFreeParserCtxt(ctxt); return(NULL); } -- cgit v1.2.1