From 1c732d2e10935529b717864b6fa4296f80edace1 Mon Sep 17 00:00:00 2001 From: Daniel Veillard Date: Sat, 30 Nov 2002 11:22:59 +0000 Subject: [PATCH] code cleanup Daniel * DOCBparser.c HTMLparser.c parser.c valid.c xpath.c: code cleanup Daniel --- ChangeLog | 4 ++ DOCBparser.c | 85 +++++++++++++--------- HTMLparser.c | 85 +++++++++++++--------- parser.c | 195 ++++++++++++++++++++++++++++++++++----------------- valid.c | 93 ++++++++++++------------ xpath.c | 68 +++++++++--------- 6 files changed, 317 insertions(+), 213 deletions(-) diff --git a/ChangeLog b/ChangeLog index f70049a2..4c52e476 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Sat Nov 30 12:19:17 CET 2002 Daniel Veillard + + * DOCBparser.c HTMLparser.c parser.c valid.c xpath.c: code cleanup + Thu Nov 28 12:53:22 CET 2002 Daniel Veillard * uri.c: Johann Richard pointed out some XPointer problems for diff --git a/DOCBparser.c b/DOCBparser.c index 6f381510..33c05343 100644 --- a/DOCBparser.c +++ b/DOCBparser.c @@ -115,42 +115,59 @@ struct _docbElemDesc { * * ************************************************************************/ -/* - * Generic function for accessing stacks in the Parser Context +/** + * docbnamePush: + * @ctxt: a DocBook SGML parser context + * @value: the element name + * + * Pushes a new element name on top of the name stack + * + * Returns 0 in case of error, the index in the stack otherwise */ +static int +docbnamePush(docbParserCtxtPtr ctxt, xmlChar * value) +{ + if (ctxt->nameNr >= ctxt->nameMax) { + ctxt->nameMax *= 2; + ctxt->nameTab = + (xmlChar * *)xmlRealloc(ctxt->nameTab, + ctxt->nameMax * + sizeof(ctxt->nameTab[0])); + if (ctxt->nameTab == NULL) { + xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); + return (0); + } + } + ctxt->nameTab[ctxt->nameNr] = value; + ctxt->name = value; + return (ctxt->nameNr++); +} +/** + * docbnamePop: + * @ctxt: a DocBook SGML parser context + * + * Pops the top element name from the name stack + * + * Returns the name just removed + */ +static xmlChar * +docbnamePop(docbParserCtxtPtr ctxt) +{ + xmlChar *ret; -#define PUSH_AND_POP(scope, type, name) \ -scope int docb##name##Push(docbParserCtxtPtr ctxt, type value) { \ - if (ctxt->name##Nr >= ctxt->name##Max) { \ - ctxt->name##Max *= 2; \ - ctxt->name##Tab = (type *) xmlRealloc(ctxt->name##Tab, \ - ctxt->name##Max * sizeof(ctxt->name##Tab[0])); \ - if (ctxt->name##Tab == NULL) { \ - xmlGenericError(xmlGenericErrorContext, \ - "realloc failed !\n"); \ - return(0); \ - } \ - } \ - ctxt->name##Tab[ctxt->name##Nr] = value; \ - ctxt->name = value; \ - return(ctxt->name##Nr++); \ -} \ -scope type docb##name##Pop(docbParserCtxtPtr ctxt) { \ - type ret; \ - if (ctxt->name##Nr < 0) return(0); \ - ctxt->name##Nr--; \ - if (ctxt->name##Nr < 0) return(0); \ - if (ctxt->name##Nr > 0) \ - ctxt->name = ctxt->name##Tab[ctxt->name##Nr - 1]; \ - else \ - ctxt->name = NULL; \ - ret = ctxt->name##Tab[ctxt->name##Nr]; \ - ctxt->name##Tab[ctxt->name##Nr] = 0; \ - return(ret); \ -} \ - -/* PUSH_AND_POP(static, xmlNodePtr, node) */ -PUSH_AND_POP(static, xmlChar*, name) + if (ctxt->nameNr < 0) + return (0); + ctxt->nameNr--; + if (ctxt->nameNr < 0) + return (0); + if (ctxt->nameNr > 0) + ctxt->name = ctxt->nameTab[ctxt->nameNr - 1]; + else + ctxt->name = NULL; + ret = ctxt->nameTab[ctxt->nameNr]; + ctxt->nameTab[ctxt->nameNr] = 0; + return (ret); +} /* * Macros for accessing the content. Those should be used only by the parser, diff --git a/HTMLparser.c b/HTMLparser.c index c775b89c..e846567c 100644 --- a/HTMLparser.c +++ b/HTMLparser.c @@ -62,42 +62,59 @@ static void htmlParseComment(htmlParserCtxtPtr ctxt); * * ************************************************************************/ -/* - * Generic function for accessing stacks in the Parser Context +/** + * htmlnamePush: + * @ctxt: an HTML parser context + * @value: the element name + * + * Pushes a new element name on top of the name stack + * + * Returns 0 in case of error, the index in the stack otherwise */ +static int +htmlnamePush(htmlParserCtxtPtr ctxt, xmlChar * value) +{ + if (ctxt->nameNr >= ctxt->nameMax) { + ctxt->nameMax *= 2; + ctxt->nameTab = + (xmlChar * *)xmlRealloc(ctxt->nameTab, + ctxt->nameMax * + sizeof(ctxt->nameTab[0])); + if (ctxt->nameTab == NULL) { + xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); + return (0); + } + } + ctxt->nameTab[ctxt->nameNr] = value; + ctxt->name = value; + return (ctxt->nameNr++); +} +/** + * htmlnamePop: + * @ctxt: an HTML parser context + * + * Pops the top element name from the name stack + * + * Returns the name just removed + */ +static xmlChar * +htmlnamePop(htmlParserCtxtPtr ctxt) +{ + xmlChar *ret; -#define PUSH_AND_POP(scope, type, name) \ -scope int html##name##Push(htmlParserCtxtPtr ctxt, type value) { \ - if (ctxt->name##Nr >= ctxt->name##Max) { \ - ctxt->name##Max *= 2; \ - ctxt->name##Tab = (type *) xmlRealloc(ctxt->name##Tab, \ - ctxt->name##Max * sizeof(ctxt->name##Tab[0])); \ - if (ctxt->name##Tab == NULL) { \ - xmlGenericError(xmlGenericErrorContext, \ - "realloc failed !\n"); \ - return(0); \ - } \ - } \ - ctxt->name##Tab[ctxt->name##Nr] = value; \ - ctxt->name = value; \ - return(ctxt->name##Nr++); \ -} \ -scope type html##name##Pop(htmlParserCtxtPtr ctxt) { \ - type ret; \ - if (ctxt->name##Nr <= 0) return(0); \ - ctxt->name##Nr--; \ - if (ctxt->name##Nr < 0) return(0); \ - if (ctxt->name##Nr > 0) \ - ctxt->name = ctxt->name##Tab[ctxt->name##Nr - 1]; \ - else \ - ctxt->name = NULL; \ - ret = ctxt->name##Tab[ctxt->name##Nr]; \ - ctxt->name##Tab[ctxt->name##Nr] = 0; \ - return(ret); \ -} \ - -/* PUSH_AND_POP(static, xmlNodePtr, node) */ -PUSH_AND_POP(static, xmlChar*, name) + if (ctxt->nameNr <= 0) + return (0); + ctxt->nameNr--; + if (ctxt->nameNr < 0) + return (0); + if (ctxt->nameNr > 0) + ctxt->name = ctxt->nameTab[ctxt->nameNr - 1]; + else + ctxt->name = NULL; + ret = ctxt->nameTab[ctxt->nameNr]; + ctxt->nameTab[ctxt->nameNr] = 0; + return (ret); +} /* * Macros for accessing the content. Those should be used only by the parser, diff --git a/parser.c b/parser.c index af569703..d281a7c5 100644 --- a/parser.c +++ b/parser.c @@ -117,47 +117,6 @@ xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt, xmlEntityPtr xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar ** str); -/* - * Generic function for accessing stacks in the Parser Context - */ - -#define PUSH_AND_POP(scope, type, name) \ -scope int name##Push(xmlParserCtxtPtr ctxt, type value) { \ - if (ctxt->name##Nr >= ctxt->name##Max) { \ - ctxt->name##Max *= 2; \ - ctxt->name##Tab = (type *) xmlRealloc(ctxt->name##Tab, \ - ctxt->name##Max * sizeof(ctxt->name##Tab[0])); \ - if (ctxt->name##Tab == NULL) { \ - xmlGenericError(xmlGenericErrorContext, \ - "realloc failed !\n"); \ - return(0); \ - } \ - } \ - ctxt->name##Tab[ctxt->name##Nr] = value; \ - ctxt->name = value; \ - return(ctxt->name##Nr++); \ -} \ -scope type name##Pop(xmlParserCtxtPtr ctxt) { \ - type ret; \ - if (ctxt->name##Nr <= 0) return(0); \ - ctxt->name##Nr--; \ - if (ctxt->name##Nr > 0) \ - ctxt->name = ctxt->name##Tab[ctxt->name##Nr - 1]; \ - else \ - ctxt->name = NULL; \ - ret = ctxt->name##Tab[ctxt->name##Nr]; \ - ctxt->name##Tab[ctxt->name##Nr] = 0; \ - return(ret); \ -} \ - -/** - * inputPop: - * @ctxt: an XML parser context - * - * Pops the top parser input from the input stack - * - * Returns the input just removed - */ /** * inputPush: * @ctxt: an XML parser context @@ -167,31 +126,48 @@ scope type name##Pop(xmlParserCtxtPtr ctxt) { \ * * Returns 0 in case of error, the index in the stack otherwise */ +extern int +inputPush(xmlParserCtxtPtr ctxt, xmlParserInputPtr value) +{ + if (ctxt->inputNr >= ctxt->inputMax) { + ctxt->inputMax *= 2; + ctxt->inputTab = + (xmlParserInputPtr *) xmlRealloc(ctxt->inputTab, + ctxt->inputMax * + sizeof(ctxt->inputTab[0])); + if (ctxt->inputTab == NULL) { + xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); + return (0); + } + } + ctxt->inputTab[ctxt->inputNr] = value; + ctxt->input = value; + return (ctxt->inputNr++); +} /** - * namePop: + * inputPop: * @ctxt: an XML parser context * - * Pops the top element name from the name stack + * Pops the top parser input from the input stack * - * Returns the name just removed - */ -/** - * namePush: - * @ctxt: an XML parser context - * @value: the element name - * - * Pushes a new element name on top of the name stack - * - * Returns 0 in case of error, the index in the stack otherwise - */ -/** - * nodePop: - * @ctxt: an XML parser context - * - * Pops the top element node from the node stack - * - * Returns the node just removed + * Returns the input just removed */ +extern xmlParserInputPtr +inputPop(xmlParserCtxtPtr ctxt) +{ + xmlParserInputPtr ret; + + if (ctxt->inputNr <= 0) + return (0); + ctxt->inputNr--; + if (ctxt->inputNr > 0) + ctxt->input = ctxt->inputTab[ctxt->inputNr - 1]; + else + ctxt->input = NULL; + ret = ctxt->inputTab[ctxt->inputNr]; + ctxt->inputTab[ctxt->inputNr] = 0; + return (ret); +} /** * nodePush: * @ctxt: an XML parser context @@ -201,12 +177,99 @@ scope type name##Pop(xmlParserCtxtPtr ctxt) { \ * * Returns 0 in case of error, the index in the stack otherwise */ -/* - * Those macros actually generate the functions +extern int +nodePush(xmlParserCtxtPtr ctxt, xmlNodePtr value) +{ + if (ctxt->nodeNr >= ctxt->nodeMax) { + ctxt->nodeMax *= 2; + ctxt->nodeTab = + (xmlNodePtr *) xmlRealloc(ctxt->nodeTab, + ctxt->nodeMax * + sizeof(ctxt->nodeTab[0])); + if (ctxt->nodeTab == NULL) { + xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); + return (0); + } + } + ctxt->nodeTab[ctxt->nodeNr] = value; + ctxt->node = value; + return (ctxt->nodeNr++); +} +/** + * nodePop: + * @ctxt: an XML parser context + * + * Pops the top element node from the node stack + * + * Returns the node just removed */ -PUSH_AND_POP(extern, xmlParserInputPtr, input) -PUSH_AND_POP(extern, xmlNodePtr, node) -PUSH_AND_POP(extern, xmlChar*, name) +extern xmlNodePtr +nodePop(xmlParserCtxtPtr ctxt) +{ + xmlNodePtr ret; + + if (ctxt->nodeNr <= 0) + return (0); + ctxt->nodeNr--; + if (ctxt->nodeNr > 0) + ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1]; + else + ctxt->node = NULL; + ret = ctxt->nodeTab[ctxt->nodeNr]; + ctxt->nodeTab[ctxt->nodeNr] = 0; + return (ret); +} +/** + * namePush: + * @ctxt: an XML parser context + * @value: the element name + * + * Pushes a new element name on top of the name stack + * + * Returns 0 in case of error, the index in the stack otherwise + */ +extern int +namePush(xmlParserCtxtPtr ctxt, xmlChar * value) +{ + if (ctxt->nameNr >= ctxt->nameMax) { + ctxt->nameMax *= 2; + ctxt->nameTab = + (xmlChar * *)xmlRealloc(ctxt->nameTab, + ctxt->nameMax * + sizeof(ctxt->nameTab[0])); + if (ctxt->nameTab == NULL) { + xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); + return (0); + } + } + ctxt->nameTab[ctxt->nameNr] = value; + ctxt->name = value; + return (ctxt->nameNr++); +} +/** + * namePop: + * @ctxt: an XML parser context + * + * Pops the top element name from the name stack + * + * Returns the name just removed + */ +extern xmlChar * +namePop(xmlParserCtxtPtr ctxt) +{ + xmlChar *ret; + + if (ctxt->nameNr <= 0) + return (0); + ctxt->nameNr--; + if (ctxt->nameNr > 0) + ctxt->name = ctxt->nameTab[ctxt->nameNr - 1]; + else + ctxt->name = NULL; + ret = ctxt->nameTab[ctxt->nameNr]; + ctxt->nameTab[ctxt->nameNr] = 0; + return (ret); +} static int spacePush(xmlParserCtxtPtr ctxt, int val) { if (ctxt->spaceNr >= ctxt->spaceMax) { diff --git a/valid.c b/valid.c index 4c72bf40..75121a88 100644 --- a/valid.c +++ b/valid.c @@ -32,52 +32,12 @@ xmlGenericError(xmlGenericErrorContext, \ "Unimplemented block at %s:%d\n", \ __FILE__, __LINE__); -/* - * Generic function for accessing stacks in the Validity Context - */ -#define PUSH_AND_POP(scope, type, name) \ -scope int name##VPush(xmlValidCtxtPtr ctxt, type value) { \ - if (ctxt->name##Max <= 0) { \ - ctxt->name##Max = 4; \ - ctxt->name##Tab = (type *) xmlMalloc( \ - ctxt->name##Max * sizeof(ctxt->name##Tab[0])); \ - if (ctxt->name##Tab == NULL) { \ - xmlGenericError(xmlGenericErrorContext, \ - "malloc failed !\n"); \ - ctxt->name##Max = 0; \ - return(0); \ - } \ - } \ - if (ctxt->name##Nr >= ctxt->name##Max) { \ - ctxt->name##Max *= 2; \ - ctxt->name##Tab = (type *) xmlRealloc(ctxt->name##Tab, \ - ctxt->name##Max * sizeof(ctxt->name##Tab[0])); \ - if (ctxt->name##Tab == NULL) { \ - xmlGenericError(xmlGenericErrorContext, \ - "realloc failed !\n"); \ - return(0); \ - } \ - } \ - ctxt->name##Tab[ctxt->name##Nr] = value; \ - ctxt->name = value; \ - return(ctxt->name##Nr++); \ -} \ -scope type name##VPop(xmlValidCtxtPtr ctxt) { \ - type ret; \ - if (ctxt->name##Nr <= 0) return(0); \ - ctxt->name##Nr--; \ - if (ctxt->name##Nr > 0) \ - ctxt->name = ctxt->name##Tab[ctxt->name##Nr - 1]; \ - else \ - ctxt->name = NULL; \ - ret = ctxt->name##Tab[ctxt->name##Nr]; \ - ctxt->name##Tab[ctxt->name##Nr] = 0; \ - return(ret); \ -} \ +#ifndef LIBXML_REGEXP_ENABLED /* - * I use a home made algorithm less complex and easier to + * If regexp are not enabled, it uses a home made algorithm less + * complex and easier to * debug/maintain than a generic NFA -> DFA state based algo. The * only restriction is on the deepness of the tree limited by the * size of the occurs bitfield @@ -110,7 +70,6 @@ typedef struct _xmlValidState { #define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH) #define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1) -#ifndef LIBXML_REGEXP_ENABLED static int vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont, xmlNodePtr node, unsigned char depth, long occurs, @@ -163,7 +122,51 @@ vstateVPop(xmlValidCtxtPtr ctxt) { #endif /* LIBXML_REGEXP_ENABLED */ -PUSH_AND_POP(static, xmlNodePtr, node) +static int +nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value) +{ + if (ctxt->nodeMax <= 0) { + ctxt->nodeMax = 4; + ctxt->nodeTab = + (xmlNodePtr *) xmlMalloc(ctxt->nodeMax * + sizeof(ctxt->nodeTab[0])); + if (ctxt->nodeTab == NULL) { + xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); + ctxt->nodeMax = 0; + return (0); + } + } + if (ctxt->nodeNr >= ctxt->nodeMax) { + ctxt->nodeMax *= 2; + ctxt->nodeTab = + (xmlNodePtr *) xmlRealloc(ctxt->nodeTab, + ctxt->nodeMax * + sizeof(ctxt->nodeTab[0])); + if (ctxt->nodeTab == NULL) { + xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); + return (0); + } + } + ctxt->nodeTab[ctxt->nodeNr] = value; + ctxt->node = value; + return (ctxt->nodeNr++); +} +static xmlNodePtr +nodeVPop(xmlValidCtxtPtr ctxt) +{ + xmlNodePtr ret; + + if (ctxt->nodeNr <= 0) + return (0); + ctxt->nodeNr--; + if (ctxt->nodeNr > 0) + ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1]; + else + ctxt->node = NULL; + ret = ctxt->nodeTab[ctxt->nodeNr]; + ctxt->nodeTab[ctxt->nodeNr] = 0; + return (ret); +} #ifdef DEBUG_VALID_ALGO static void diff --git a/xpath.c b/xpath.c index 28c8a365..6c68d754 100644 --- a/xpath.c +++ b/xpath.c @@ -885,39 +885,6 @@ xmlXPathDebugDumpCompExpr(FILE *output, xmlXPathCompExprPtr comp, * * ************************************************************************/ -/* - * Generic function for accessing stacks in the Parser Context - */ - -#define PUSH_AND_POP(type, name) \ -extern int name##Push(xmlXPathParserContextPtr ctxt, type value) { \ - if (ctxt->name##Nr >= ctxt->name##Max) { \ - ctxt->name##Max *= 2; \ - ctxt->name##Tab = (type *) xmlRealloc(ctxt->name##Tab, \ - ctxt->name##Max * sizeof(ctxt->name##Tab[0])); \ - if (ctxt->name##Tab == NULL) { \ - xmlGenericError(xmlGenericErrorContext, \ - "realloc failed !\n"); \ - return(0); \ - } \ - } \ - ctxt->name##Tab[ctxt->name##Nr] = value; \ - ctxt->name = value; \ - return(ctxt->name##Nr++); \ -} \ -extern type name##Pop(xmlXPathParserContextPtr ctxt) { \ - type ret; \ - if (ctxt->name##Nr <= 0) return(0); \ - ctxt->name##Nr--; \ - if (ctxt->name##Nr > 0) \ - ctxt->name = ctxt->name##Tab[ctxt->name##Nr - 1]; \ - else \ - ctxt->name = NULL; \ - ret = ctxt->name##Tab[ctxt->name##Nr]; \ - ctxt->name##Tab[ctxt->name##Nr] = 0; \ - return(ret); \ -} \ - /** * valuePop: * @ctxt: an XPath evaluation context @@ -926,6 +893,22 @@ extern type name##Pop(xmlXPathParserContextPtr ctxt) { \ * * Returns the XPath object just removed */ +extern xmlXPathObjectPtr +valuePop(xmlXPathParserContextPtr ctxt) +{ + xmlXPathObjectPtr ret; + + if (ctxt->valueNr <= 0) + return (0); + ctxt->valueNr--; + if (ctxt->valueNr > 0) + ctxt->value = ctxt->valueTab[ctxt->valueNr - 1]; + else + ctxt->value = NULL; + ret = ctxt->valueTab[ctxt->valueNr]; + ctxt->valueTab[ctxt->valueNr] = 0; + return (ret); +} /** * valuePush: * @ctxt: an XPath evaluation context @@ -935,7 +918,24 @@ extern type name##Pop(xmlXPathParserContextPtr ctxt) { \ * * returns the number of items on the value stack */ -PUSH_AND_POP(xmlXPathObjectPtr, value) +extern int +valuePush(xmlXPathParserContextPtr ctxt, xmlXPathObjectPtr value) +{ + if (ctxt->valueNr >= ctxt->valueMax) { + ctxt->valueMax *= 2; + ctxt->valueTab = + (xmlXPathObjectPtr *) xmlRealloc(ctxt->valueTab, + ctxt->valueMax * + sizeof(ctxt->valueTab[0])); + if (ctxt->valueTab == NULL) { + xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); + return (0); + } + } + ctxt->valueTab[ctxt->valueNr] = value; + ctxt->value = value; + return (ctxt->valueNr++); +} /** * xmlXPathPopBoolean: