From b656ebe4566e3e1e858f32585c07e77e9c92577d Mon Sep 17 00:00:00 2001 From: Daniel Veillard Date: Fri, 22 Sep 2000 13:51:48 +0000 Subject: [PATCH] Cleanup, patch from Wayne Davison: - xmlIO.h tree.h: made xmlNodeDump() and xmlNodeDumpOutput() public - parser.[ch] nanohttp.c HTMLtree.c HTMLparser.c tree.c: applied and modified slightly Wayne Davison patch adding xmlStrcasecmp and related function, fixing xmlStrncmp(), and associated cleanup - result/HTML/entities.html.sax: updating result Daniel --- ChangeLog | 8 +++ HTMLparser.c | 28 ++------- HTMLtree.c | 28 +++------ include/libxml/parser.h | 7 +++ include/libxml/tree.h | 6 +- include/libxml/xmlIO.h | 6 ++ nanohttp.c | 24 ++------ parser.c | 128 +++++++++++++++++++++++++++++++++++++--- parser.h | 7 +++ tree.c | 21 +++---- tree.h | 6 +- xmlIO.h | 6 ++ 12 files changed, 191 insertions(+), 84 deletions(-) diff --git a/ChangeLog b/ChangeLog index c4cac365..4de7aca2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Fri Sep 22 14:17:53 CEST 2000 Daniel Veillard + + * xmlIO.h tree.h: made xmlNodeDump() and xmlNodeDumpOutput() public + * parser.[ch] nanohttp.c HTMLtree.c HTMLparser.c tree.c: applied and + modified slightly Wayne Davison patch adding xmlStrcasecmp and + related function, fixing xmlStrncmp(), and associated cleanup + * result/HTML/entities.html.sax: updating result + Tue Sep 19 14:20:10 CEST 2000 Daniel Veillard * uri.c: applied patch for URI escaping from Wayne Davison diff --git a/HTMLparser.c b/HTMLparser.c index a2527666..9799b1bd 100644 --- a/HTMLparser.c +++ b/HTMLparser.c @@ -598,7 +598,7 @@ htmlInitAutoClose(void) { /** * htmlTagLookup: - * @tag: The tag name + * @tag: The tag name in lowercase * * Lookup the HTML tag in the ElementTable * @@ -2663,19 +2663,11 @@ htmlCheckEncoding(htmlParserCtxtPtr ctxt, const xmlChar *attvalue) { if ((ctxt == NULL) || (attvalue == NULL)) return; - encoding = xmlStrstr(attvalue, BAD_CAST"charset="); - if (encoding == NULL) - encoding = xmlStrstr(attvalue, BAD_CAST"Charset="); - if (encoding == NULL) - encoding = xmlStrstr(attvalue, BAD_CAST"CHARSET="); + encoding = xmlStrcasestr(attvalue, BAD_CAST"charset="); if (encoding != NULL) { encoding += 8; } else { - encoding = xmlStrstr(attvalue, BAD_CAST"charset ="); - if (encoding == NULL) - encoding = xmlStrstr(attvalue, BAD_CAST"Charset ="); - if (encoding == NULL) - encoding = xmlStrstr(attvalue, BAD_CAST"CHARSET ="); + encoding = xmlStrcasestr(attvalue, BAD_CAST"charset ="); if (encoding != NULL) encoding += 9; } @@ -2757,18 +2749,10 @@ htmlCheckMeta(htmlParserCtxtPtr ctxt, const xmlChar **atts) { att = atts[i++]; while (att != NULL) { value = atts[i++]; - if ((value != NULL) && - ((!xmlStrcmp(att, BAD_CAST"http-equiv")) || - (!xmlStrcmp(att, BAD_CAST"Http-Equiv")) || - (!xmlStrcmp(att, BAD_CAST"HTTP-EQUIV"))) && - ((!xmlStrcmp(value, BAD_CAST"Content-Type")) || - (!xmlStrcmp(value, BAD_CAST"content-type")) || - (!xmlStrcmp(value, BAD_CAST"CONTENT-TYPE")))) + if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"http-equiv")) + && (!xmlStrcasecmp(value, BAD_CAST"Content-Type"))) http = 1; - else if ((value != NULL) && - ((!xmlStrcmp(att, BAD_CAST"content")) || - (!xmlStrcmp(att, BAD_CAST"Content")) || - (!xmlStrcmp(att, BAD_CAST"CONTENT")))) + else if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"content"))) content = value; att = atts[i++]; } diff --git a/HTMLtree.c b/HTMLtree.c index 2d3b8a49..6ab29eac 100644 --- a/HTMLtree.c +++ b/HTMLtree.c @@ -113,17 +113,11 @@ found_meta: #else value = xmlBufferContent(attr->children->content); #endif - if (((!xmlStrcmp(attr->name, BAD_CAST"http-equiv")) || - (!xmlStrcmp(attr->name, BAD_CAST"Http-Equiv")) || - (!xmlStrcmp(attr->name, BAD_CAST"HTTP-EQUIV"))) && - ((!xmlStrcmp(value, BAD_CAST"Content-Type")) || - (!xmlStrcmp(value, BAD_CAST"content-type")) || - (!xmlStrcmp(value, BAD_CAST"CONTENT-TYPE")))) + if ((!xmlStrcasecmp(attr->name, BAD_CAST"http-equiv")) + && (!xmlStrcasecmp(value, BAD_CAST"Content-Type"))) http = 1; - else if ((value != NULL) && - ((!xmlStrcmp(attr->name, BAD_CAST"content")) || - (!xmlStrcmp(attr->name, BAD_CAST"Content")) || - (!xmlStrcmp(attr->name, BAD_CAST"CONTENT")))) + else if ((value != NULL) + && (!xmlStrcasecmp(attr->name, BAD_CAST"content"))) content = value; if ((http != 0) && (content != NULL)) goto found_content; @@ -294,17 +288,11 @@ found_meta: #else value = xmlBufferContent(attr->children->content); #endif - if (((!xmlStrcmp(attr->name, BAD_CAST"http-equiv")) || - (!xmlStrcmp(attr->name, BAD_CAST"Http-Equiv")) || - (!xmlStrcmp(attr->name, BAD_CAST"HTTP-EQUIV"))) && - ((!xmlStrcmp(value, BAD_CAST"Content-Type")) || - (!xmlStrcmp(value, BAD_CAST"content-type")) || - (!xmlStrcmp(value, BAD_CAST"CONTENT-TYPE")))) + if ((!xmlStrcasecmp(attr->name, BAD_CAST"http-equiv")) + && (!xmlStrcasecmp(value, BAD_CAST"Content-Type"))) http = 1; - else if ((value != NULL) && - ((!xmlStrcmp(attr->name, BAD_CAST"content")) || - (!xmlStrcmp(attr->name, BAD_CAST"Content")) || - (!xmlStrcmp(attr->name, BAD_CAST"CONTENT")))) + else if ((value != NULL) + && (!xmlStrcasecmp(attr->name, BAD_CAST"content"))) content = value; if ((http != 0) && (content != NULL)) break; diff --git a/include/libxml/parser.h b/include/libxml/parser.h index 988d895c..7e0a37ab 100644 --- a/include/libxml/parser.h +++ b/include/libxml/parser.h @@ -335,11 +335,18 @@ const xmlChar * xmlStrchr (const xmlChar *str, xmlChar val); const xmlChar * xmlStrstr (const xmlChar *str, xmlChar *val); +const xmlChar * xmlStrcasestr (const xmlChar *str, + xmlChar *val); int xmlStrcmp (const xmlChar *str1, const xmlChar *str2); int xmlStrncmp (const xmlChar *str1, const xmlChar *str2, int len); +int xmlStrcasecmp (const xmlChar *str1, + const xmlChar *str2); +int xmlStrncasecmp (const xmlChar *str1, + const xmlChar *str2, + int len); int xmlStrlen (const xmlChar *str); xmlChar * xmlStrcat (xmlChar *cur, const xmlChar *add); diff --git a/include/libxml/tree.h b/include/libxml/tree.h index e4cf730b..27834bb9 100644 --- a/include/libxml/tree.h +++ b/include/libxml/tree.h @@ -13,7 +13,6 @@ #include #include - #ifdef __cplusplus extern "C" { #endif @@ -625,6 +624,11 @@ void xmlElemDump (FILE *f, xmlNodePtr cur); int xmlSaveFile (const char *filename, xmlDocPtr cur); +void xmlNodeDump (xmlBufferPtr buf, + xmlDocPtr doc, + xmlNodePtr cur, + int level, + int format); /* This one is exported from xmlIO.h diff --git a/include/libxml/xmlIO.h b/include/libxml/xmlIO.h index 6ef91fd0..e7b5f768 100644 --- a/include/libxml/xmlIO.h +++ b/include/libxml/xmlIO.h @@ -150,6 +150,12 @@ int xmlRegisterOutputCallbacks (xmlOutputMatchCallback match, int xmlSaveFileTo (xmlOutputBuffer *buf, xmlDocPtr cur, const char *encoding); +void xmlNodeDumpOutput (xmlOutputBufferPtr buf, + xmlDocPtr doc, + xmlNodePtr cur, + int level, + int format, + const char *encoding); #ifdef __cplusplus } #endif diff --git a/nanohttp.c b/nanohttp.c index 4c63f371..52a6b59d 100644 --- a/nanohttp.c +++ b/nanohttp.c @@ -62,10 +62,13 @@ #endif #include +#include /* for xmlStr(n)casecmp() */ #include #ifdef STANDALONE #define DEBUG_HTTP +#define xmlStrncasecmp(a, b, n) strncasecmp((char *)a, (char *)b, n) +#define xmlStrcasecmpi(a, b) strcasecmp((char *)a, (char *)b) #endif #define XML_NANO_HTTP_MAX_REDIR 10 @@ -523,38 +526,23 @@ xmlNanoHTTPScanAnswer(xmlNanoHTTPCtxtPtr ctxt, const char *line) { } if ((*cur != 0) && (*cur != ' ') && (*cur != '\t')) return; ctxt->returnValue = ret; - } else if (!strncmp(line, "Content-Type:", 13)) { + } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Content-Type:", 13)) { cur += 13; while ((*cur == ' ') || (*cur == '\t')) cur++; if (ctxt->contentType != NULL) xmlFree(ctxt->contentType); ctxt->contentType = xmlMemStrdup(cur); - } else if (!strncmp(line, "ContentType:", 12)) { + } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"ContentType:", 12)) { cur += 12; if (ctxt->contentType != NULL) return; while ((*cur == ' ') || (*cur == '\t')) cur++; ctxt->contentType = xmlMemStrdup(cur); - } else if (!strncmp(line, "content-type:", 13)) { - cur += 13; - if (ctxt->contentType != NULL) return; - while ((*cur == ' ') || (*cur == '\t')) cur++; - ctxt->contentType = xmlMemStrdup(cur); - } else if (!strncmp(line, "contenttype:", 12)) { - cur += 12; - if (ctxt->contentType != NULL) return; - while ((*cur == ' ') || (*cur == '\t')) cur++; - ctxt->contentType = xmlMemStrdup(cur); - } else if (!strncmp(line, "Location:", 9)) { + } else if (!xmlStrncasecmp(BAD_CAST line, BAD_CAST"Location:", 9)) { cur += 9; while ((*cur == ' ') || (*cur == '\t')) cur++; if (ctxt->location != NULL) xmlFree(ctxt->location); ctxt->location = xmlMemStrdup(cur); - } else if (!strncmp(line, "location:", 9)) { - cur += 9; - if (ctxt->location != NULL) return; - while ((*cur == ' ') || (*cur == '\t')) cur++; - ctxt->location = xmlMemStrdup(cur); } } diff --git a/parser.c b/parser.c index 6f97fb07..b9abb73a 100644 --- a/parser.c +++ b/parser.c @@ -1014,10 +1014,10 @@ xmlStrcmp(const xmlChar *str1, const xmlChar *str2) { if (str1 == NULL) return(-1); if (str2 == NULL) return(1); do { - tmp = *str1++ - *str2++; + tmp = *str1++ - *str2; if (tmp != 0) return(tmp); - } while ((*str1 != 0) && (*str2 != 0)); /* non input consuming */ - return (*str1 - *str2); + } while (*str2++ != 0); + return 0; } /** @@ -1036,16 +1036,99 @@ xmlStrncmp(const xmlChar *str1, const xmlChar *str2, int len) { register int tmp; if (len <= 0) return(0); - if ((str1 == NULL) && (str2 == NULL)) return(0); + if (str1 == str2) return(0); if (str1 == NULL) return(-1); if (str2 == NULL) return(1); do { - tmp = *str1++ - *str2++; + tmp = *str1++ - *str2; + if (tmp != 0 || --len == 0) return(tmp); + } while (*str2++ != 0); + return 0; +} + +static xmlChar casemap[256] = { + 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, + 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F, + 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17, + 0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F, + 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27, + 0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F, + 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37, + 0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F, + 0x40,0x61,0x62,0x63,0x64,0x65,0x66,0x67, + 0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F, + 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77, + 0x78,0x79,0x7A,0x7B,0x5C,0x5D,0x5E,0x5F, + 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67, + 0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F, + 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77, + 0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F, + 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, + 0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, + 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97, + 0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, + 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7, + 0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, + 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7, + 0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, + 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7, + 0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, + 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7, + 0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, + 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7, + 0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, + 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7, + 0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF +}; + +/** + * xmlStrcasecmp: + * @str1: the first xmlChar * + * @str2: the second xmlChar * + * + * a strcasecmp for xmlChar's + * + * Returns the integer result of the comparison + */ + +int +xmlStrcasecmp(const xmlChar *str1, const xmlChar *str2) { + register int tmp; + + if (str1 == str2) return(0); + if (str1 == NULL) return(-1); + if (str2 == NULL) return(1); + do { + tmp = casemap[*str1++] - casemap[*str2]; if (tmp != 0) return(tmp); - len--; - if (len <= 0) return(0); - } while ((*str1 != 0) && (*str2 != 0)); /* non input consuming */ - return (*str1 - *str2); + } while (*str2++ != 0); + return 0; +} + +/** + * xmlStrncasecmp: + * @str1: the first xmlChar * + * @str2: the second xmlChar * + * @len: the max comparison length + * + * a strncasecmp for xmlChar's + * + * Returns the integer result of the comparison + */ + +int +xmlStrncasecmp(const xmlChar *str1, const xmlChar *str2, int len) { + register int tmp; + + if (len <= 0) return(0); + if (str1 == str2) return(0); + if (str1 == NULL) return(-1); + if (str2 == NULL) return(1); + do { + tmp = casemap[*str1++] - casemap[*str2]; + if (tmp != 0 || --len == 0) return(tmp); + } while (*str2++ != 0); + return 0; } /** @@ -1096,6 +1179,33 @@ xmlStrstr(const xmlChar *str, xmlChar *val) { return(NULL); } +/** + * xmlStrcasestr: + * @str: the xmlChar * array (haystack) + * @val: the xmlChar to search (needle) + * + * a case-ignoring strstr for xmlChar's + * + * Returns the xmlChar * for the first occurence or NULL. + */ + +const xmlChar * +xmlStrcasestr(const xmlChar *str, xmlChar *val) { + int n; + + if (str == NULL) return(NULL); + if (val == NULL) return(NULL); + n = xmlStrlen(val); + + if (n == 0) return(str); + while (*str != 0) { /* non input consuming */ + if (casemap[*str] == casemap[*val]) + if (!xmlStrncasecmp(str, val, n)) return(str); + str++; + } + return(NULL); +} + /** * xmlStrsub: * @str: the xmlChar * array (haystack) diff --git a/parser.h b/parser.h index 988d895c..7e0a37ab 100644 --- a/parser.h +++ b/parser.h @@ -335,11 +335,18 @@ const xmlChar * xmlStrchr (const xmlChar *str, xmlChar val); const xmlChar * xmlStrstr (const xmlChar *str, xmlChar *val); +const xmlChar * xmlStrcasestr (const xmlChar *str, + xmlChar *val); int xmlStrcmp (const xmlChar *str1, const xmlChar *str2); int xmlStrncmp (const xmlChar *str1, const xmlChar *str2, int len); +int xmlStrcasecmp (const xmlChar *str1, + const xmlChar *str2); +int xmlStrncasecmp (const xmlChar *str1, + const xmlChar *str2, + int len); int xmlStrlen (const xmlChar *str); xmlChar * xmlStrcat (xmlChar *cur, const xmlChar *add); diff --git a/tree.c b/tree.c index 9e6a6a6a..b261e560 100644 --- a/tree.c +++ b/tree.c @@ -2770,21 +2770,16 @@ xmlNodeGetBase(xmlDocPtr doc, xmlNodePtr cur) { cur = cur->next; continue; } - if ((!xmlStrcmp(cur->name, BAD_CAST "html")) || - (!xmlStrcmp(cur->name, BAD_CAST "HTML"))) { + if (!xmlStrcasecmp(cur->name, BAD_CAST "html")) { cur = cur->children; continue; } - if ((!xmlStrcmp(cur->name, BAD_CAST "head")) || - (!xmlStrcmp(cur->name, BAD_CAST "HEAD"))) { + if (!xmlStrcasecmp(cur->name, BAD_CAST "head")) { cur = cur->children; continue; } - if ((!xmlStrcmp(cur->name, BAD_CAST "base")) || - (!xmlStrcmp(cur->name, BAD_CAST "BASE"))) { - base = xmlGetProp(cur, BAD_CAST "href"); - if (base != NULL) return(base); - return(xmlGetProp(cur, BAD_CAST "HREF")); + if (!xmlStrcasecmp(cur->name, BAD_CAST "base")) { + return(xmlGetProp(cur, BAD_CAST "href")); } cur = cur->next; } @@ -4311,7 +4306,7 @@ xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) { * * ************************************************************************/ -static void +void xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, int format); static void @@ -4556,7 +4551,7 @@ xmlNodeListDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, * * Dump an XML node, recursive behaviour,children are printed too. */ -static void +void xmlNodeDump(xmlBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, int format) { int i; @@ -4800,7 +4795,7 @@ xmlDocContentDump(xmlBufferPtr buf, xmlDocPtr cur) { * * ************************************************************************/ -static void +void xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, int format, const char *encoding); static void @@ -5049,7 +5044,7 @@ xmlNodeListDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, * * Dump an XML node, recursive behaviour,children are printed too. */ -static void +void xmlNodeDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlNodePtr cur, int level, int format, const char *encoding) { int i; diff --git a/tree.h b/tree.h index e4cf730b..27834bb9 100644 --- a/tree.h +++ b/tree.h @@ -13,7 +13,6 @@ #include #include - #ifdef __cplusplus extern "C" { #endif @@ -625,6 +624,11 @@ void xmlElemDump (FILE *f, xmlNodePtr cur); int xmlSaveFile (const char *filename, xmlDocPtr cur); +void xmlNodeDump (xmlBufferPtr buf, + xmlDocPtr doc, + xmlNodePtr cur, + int level, + int format); /* This one is exported from xmlIO.h diff --git a/xmlIO.h b/xmlIO.h index 6ef91fd0..e7b5f768 100644 --- a/xmlIO.h +++ b/xmlIO.h @@ -150,6 +150,12 @@ int xmlRegisterOutputCallbacks (xmlOutputMatchCallback match, int xmlSaveFileTo (xmlOutputBuffer *buf, xmlDocPtr cur, const char *encoding); +void xmlNodeDumpOutput (xmlOutputBufferPtr buf, + xmlDocPtr doc, + xmlNodePtr cur, + int level, + int format, + const char *encoding); #ifdef __cplusplus } #endif