[libxml2] Cleanup function xmlBufResetInput() to set input from Buffer



commit 61551a1eb75bacb32e5209635c0f3459595af54a
Author: Daniel Veillard <veillard redhat com>
Date:   Mon Jul 16 16:28:47 2012 +0800

    Cleanup function xmlBufResetInput() to set input from Buffer
    
    This was scattered in a number of modules, xmlParserInputPtr
    have usually their base, cur and end pointer set from an
    xmlBuf used as input.
    * buf.c buf.h: add a new function implementing this setup
    * parser.c HTMLparser.c catalog.c parserInternals.c xmlreader.c
      use the new function instead of digging into the buffer in
      all those modules

 HTMLparser.c      |   12 +++---------
 buf.c             |   18 ++++++++++++++++++
 buf.h             |    1 +
 catalog.c         |    4 +---
 parser.c          |   13 +++----------
 parserInternals.c |   13 ++++---------
 xmlreader.c       |    4 +---
 7 files changed, 31 insertions(+), 34 deletions(-)
---
diff --git a/HTMLparser.c b/HTMLparser.c
index 4d36617..fdbef80 100644
--- a/HTMLparser.c
+++ b/HTMLparser.c
@@ -3519,9 +3519,7 @@ htmlCheckEncodingDirect(htmlParserCtxtPtr ctxt, const xmlChar *encoding) {
 		             "htmlCheckEncoding: encoder error\n",
 			     NULL, NULL);
 	    }
-	    ctxt->input->base =
-	    ctxt->input->cur = xmlBufContent(ctxt->input->buf->buffer);
-            ctxt->input->end = xmlBufEnd(ctxt->input->buf->buffer);
+            xmlBufResetInput(ctxt->input->buf->buffer, ctxt->input);
 	}
     }
 }
@@ -4906,9 +4904,7 @@ htmlCreateMemoryParserCtxt(const char *buffer, int size) {
 
     input->filename = NULL;
     input->buf = buf;
-    input->cur =
-    input->base = xmlBufContent(input->buf->buffer);
-    input->end = xmlBufEnd(input->buf->buffer);
+    xmlBufResetInput(buf->buffer, input);
 
     inputPush(ctxt, input);
     return(ctxt);
@@ -6106,9 +6102,7 @@ htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax, void *user_data,
 	inputStream->filename = (char *)
 	    xmlCanonicPath((const xmlChar *) filename);
     inputStream->buf = buf;
-    inputStream->cur =
-    inputStream->base = xmlBufContent(buf->buffer);
-    inputStream->end = xmlBufEnd(buf->buffer);
+    xmlBufResetInput(buf->buffer, inputStream);
 
     inputPush(ctxt, inputStream);
 
diff --git a/buf.c b/buf.c
index 520a024..8cf6199 100644
--- a/buf.c
+++ b/buf.c
@@ -1136,3 +1136,21 @@ xmlBufMergeBuffer(xmlBufPtr buf, xmlBufferPtr buffer) {
     xmlBufferFree(buffer);
     return(ret);
 }
+
+/**
+ * xmlBufResetInput:
+ * @buf: an xmlBufPtr
+ * @input: an xmlParserInputPtr
+ *
+ * Update the input to use the current set of pointers from the buffer.
+ *
+ * Returns -1 in case of error, 0 otherwise, in any case @buffer is freed
+ */
+int
+xmlBufResetInput(xmlBufPtr buf, xmlParserInputPtr input) {
+    if ((input == NULL) || (buf == NULL))
+        return(-1);
+    input->base = input->cur = buf->content;
+    input->end = &buf->content[buf->use];
+    return(0);
+}
diff --git a/buf.h b/buf.h
index 4d3afd0..da97e86 100644
--- a/buf.h
+++ b/buf.h
@@ -57,6 +57,7 @@ xmlBufPtr xmlBufFromBuffer(xmlBufferPtr buffer);
 xmlBufferPtr xmlBufBackToBuffer(xmlBufPtr buf);
 int xmlBufMergeBuffer(xmlBufPtr buf, xmlBufferPtr buffer);
 
+int xmlBufResetInput(xmlBufPtr buf, xmlParserInputPtr input);
 #ifdef __cplusplus
 }
 #endif
diff --git a/catalog.c b/catalog.c
index 401e95c..1fac025 100644
--- a/catalog.c
+++ b/catalog.c
@@ -914,9 +914,7 @@ xmlParseCatalogFile(const char *filename) {
 
     inputStream->filename = (char *) xmlCanonicPath((const xmlChar *)filename);
     inputStream->buf = buf;
-    inputStream->cur =
-    inputStream->base = xmlBufContent(buf->buffer);
-    inputStream->end = xmlBufEnd(buf->buffer);
+    xmlBufResetInput(buf->buffer, inputStream);
 
     inputPush(ctxt, inputStream);
     if ((ctxt->directory == NULL) && (directory == NULL))
diff --git a/parser.c b/parser.c
index 78b69c1..d5102c8 100644
--- a/parser.c
+++ b/parser.c
@@ -11925,10 +11925,7 @@ xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax, void *user_data,
 	}
     }
     inputStream->buf = buf;
-    inputStream->cur =
-    inputStream->base = xmlBufContent(inputStream->buf->buffer);
-    inputStream->end = xmlBufEnd(inputStream->buf->buffer);
-
+    xmlBufResetInput(inputStream->buf->buffer, inputStream);
     inputPush(ctxt, inputStream);
 
     /*
@@ -13870,9 +13867,7 @@ xmlCreateMemoryParserCtxt(const char *buffer, int size) {
 
     input->filename = NULL;
     input->buf = buf;
-    input->cur =
-    input->base = xmlBufContent(input->buf->buffer);
-    input->end = xmlBufEnd(input->buf->buffer);
+    xmlBufResetInput(input->buf->buffer, input);
 
     inputPush(ctxt, input);
     return(ctxt);
@@ -14442,9 +14437,7 @@ xmlCtxtResetPush(xmlParserCtxtPtr ctxt, const char *chunk,
         inputStream->filename = (char *)
             xmlCanonicPath((const xmlChar *) filename);
     inputStream->buf = buf;
-    inputStream->cur =
-    inputStream->base = xmlBufContent(buf->buffer);
-    inputStream->end = xmlBufEnd(buf->buffer);
+    xmlBufResetInput(buf->buffer, inputStream);
 
     inputPush(ctxt, inputStream);
 
diff --git a/parserInternals.c b/parserInternals.c
index acae7e5..aae616c 100644
--- a/parserInternals.c
+++ b/parserInternals.c
@@ -1218,9 +1218,7 @@ xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
                 return (-1);
             }
 	    input->buf->rawconsumed += use - xmlBufUse(input->buf->raw);
-            input->base = input->cur = xmlBufContent(input->buf->buffer);
-            input->end = xmlBufEnd(input->buf->buffer);
-
+            xmlBufResetInput(input->buf->buffer, input);
         }
         return (0);
     } else if (input->length == 0) {
@@ -1387,9 +1385,8 @@ xmlNewIOInputStream(xmlParserCtxtPtr ctxt, xmlParserInputBufferPtr input,
     }
     inputStream->filename = NULL;
     inputStream->buf = input;
-    inputStream->cur =
-    inputStream->base = xmlBufContent(inputStream->buf->buffer);
-    inputStream->end = xmlBufEnd(inputStream->buf->buffer);
+    xmlBufResetInput(inputStream->buf->buffer, inputStream);
+
     if (enc != XML_CHAR_ENCODING_NONE) {
         xmlSwitchEncoding(ctxt, enc);
     }
@@ -1542,9 +1539,7 @@ xmlNewInputFromFile(xmlParserCtxtPtr ctxt, const char *filename) {
     if (URI != NULL) xmlFree((char *) URI);
     inputStream->directory = directory;
 
-    inputStream->base =
-    inputStream->cur = xmlBufContent(inputStream->buf->buffer);
-    inputStream->end = xmlBufEnd(inputStream->buf->buffer);
+    xmlBufResetInput(inputStream->buf->buffer, inputStream);
     if ((ctxt->directory == NULL) && (directory != NULL))
         ctxt->directory = (char *) xmlStrdup((const xmlChar *) directory);
     return(inputStream);
diff --git a/xmlreader.c b/xmlreader.c
index be4e44a..767bd05 100644
--- a/xmlreader.c
+++ b/xmlreader.c
@@ -5126,9 +5126,7 @@ xmlTextReaderSetup(xmlTextReaderPtr reader,
 		inputStream->filename = (char *)
 		    xmlCanonicPath((const xmlChar *) URL);
 	    inputStream->buf = buf;
-	    inputStream->cur =
-	    inputStream->base = xmlBufContent(buf->buffer);
-	    inputStream->end = xmlBufEnd(buf->buffer);
+            xmlBufResetInput(buf->buffer, inputStream);
 
 	    inputPush(reader->ctxt, inputStream);
 	    reader->cur = 0;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]