diff options
author | Nick Wellnhofer <wellnhofer@aevum.de> | 2022-08-24 04:21:58 +0200 |
---|---|---|
committer | Nick Wellnhofer <wellnhofer@aevum.de> | 2022-08-24 14:07:55 +0200 |
commit | 9a82b94a94bd310db426edd453b0f38c6c8f69f5 (patch) | |
tree | 955b518c012765ccd2f77fb6ef7ff3a3da2276d4 /parserInternals.c | |
parent | 0a04db19fca3433a0eb7b562d394a0b0695d277d (diff) | |
download | libxml2-9a82b94a94bd310db426edd453b0f38c6c8f69f5.tar.gz |
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.
Diffstat (limited to 'parserInternals.c')
-rw-r--r-- | parserInternals.c | 63 |
1 files changed, 55 insertions, 8 deletions
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; @@ -1625,6 +1638,24 @@ xmlInitParserCtxt(xmlParserCtxtPtr ctxt) } /** + * 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 * @@ -1721,6 +1752,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; ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt)); @@ -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); } |