final touch running DTD validation on the XmlTextReader added a specific

* valid.c xmlreader.c: final touch running DTD validation
  on the XmlTextReader
* python/tests/Makefile.am python/tests/reader2.py: added a
  specific run based on the examples from test/valid/*.xml
Daniel
diff --git a/ChangeLog b/ChangeLog
index e0ae84a..d1b6b7a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Fri Dec 27 20:35:15 CET 2002 Daniel Veillard <daniel@veillard.com>
+
+	* valid.c xmlreader.c: final touch running DTD validation
+	  on the XmlTextReader
+	* python/tests/Makefile.am python/tests/reader2.py: added a
+	  specific run based on the examples from test/valid/*.xml
+
 Fri Dec 27 15:17:20 CET 2002 Daniel Veillard <daniel@veillard.com>
 
 	* python/libxml.py: added a few predefined xmlTextReader parser
diff --git a/python/tests/Makefile.am b/python/tests/Makefile.am
index c6bef2c..de1ef3b 100644
--- a/python/tests/Makefile.am
+++ b/python/tests/Makefile.am
@@ -20,7 +20,8 @@
     inbuf.py	\
     resolver.py \
     regexp.py	\
-    reader.py
+    reader.py	\
+    reader2.py
 
 XMLS=		\
     tst.xml	\
diff --git a/python/tests/reader2.py b/python/tests/reader2.py
new file mode 100755
index 0000000..975fb00
--- /dev/null
+++ b/python/tests/reader2.py
@@ -0,0 +1,54 @@
+#!/usr/bin/python -u
+#
+# this tests the validation with the XmlTextReader interface
+#
+import sys
+import glob
+import string
+import libxml2
+
+# Memory debug specific
+libxml2.debugMemory(1)
+
+err=""
+expect="""../../test/valid/xlink.xml:450: validity error: ID dt-arc already defined
+	<p><termdef id="dt-arc" term="Arc">
+                                   ^
+../../test/valid/xlink.xml:529: validity error: attribute def line 199 references an unknown ID "dt-xlg"
+<?Pub *0000052575?>
+                   ^
+../../test/valid/rss.xml:172: validity error: Element rss does not carry attribute version
+</rss>
+      ^
+"""
+def callback(ctx, str):
+    global err
+    err = err + "%s" % (str)
+libxml2.registerErrorHandler(callback, "")
+
+valid_files = files = glob.glob("../../test/valid/*.x*")
+for file in valid_files:
+    if string.find(file, "t8") != -1:
+        continue
+    reader = libxml2.newTextReaderFilename(file)
+    #print "%s:" % (file)
+    reader.setParserProp(libxml2.PARSER_VALIDATE, 1)
+    ret = reader.read()
+    while ret == 1:
+        ret = reader.read()
+    if ret != 0:
+        print "Error parsing and validating %s" % (file)
+	#sys.exit(1)
+
+if err != expect:
+    print err
+
+del reader
+
+# Memory debug specific
+libxml2.cleanupParser()
+if libxml2.debugMemory(1) == 0:
+    print "OK"
+else:
+    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
+    libxml2.dumpMemory()
diff --git a/valid.c b/valid.c
index 7fa634c..dcf72d4 100644
--- a/valid.c
+++ b/valid.c
@@ -100,7 +100,7 @@
 vstateVPop(xmlValidCtxtPtr ctxt) {
     xmlElementPtr elemDecl;
 
-    if (ctxt->vstateNr <= 1) return(-1);
+    if (ctxt->vstateNr < 1) return(-1);
     ctxt->vstateNr--;
     elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl;
     ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL;
@@ -250,6 +250,39 @@
     return (ret);
 }
 
+#if 0
+/**
+ * xmlFreeValidCtxt:
+ * @ctxt:  a validation context
+ *
+ * Free the memory allocated for a validation context
+ */
+void 
+xmlFreeValidCtxt(xmlValidCtxtPtr ctxt) {
+    if (ctxt == NULL)
+	return;
+#ifdef LIBXML_REGEXP_ENABLED
+    while (ctxt->vstateNr >= 0)
+	vstateVPop(ctxt);
+    if (ctxt->vstateNr <= 1) return(-1);
+    ctxt->vstateNr--;
+    elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl;
+    ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL;
+    ctxt->vstateTab[ctxt->vstateNr].node = NULL;
+    if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
+	xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec);
+    }
+    ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
+    if (ctxt->vstateNr >= 1)
+	ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1];
+    else
+	ctxt->vstate = NULL;
+    return(ctxt->vstateNr);
+#else /* ! LIBXML_REGEXP_ENABLED */
+#endif /* LIBXML_REGEXP_ENABLED */
+}
+#endif
+
 #ifdef DEBUG_VALID_ALGO
 static void
 xmlValidPrintNode(xmlNodePtr cur) {
diff --git a/xmlreader.c b/xmlreader.c
index 9655624..e36bbf3 100644
--- a/xmlreader.c
+++ b/xmlreader.c
@@ -696,6 +696,12 @@
 	    xmlFreeDoc(reader->ctxt->myDoc);
 	    reader->ctxt->myDoc = NULL;
 	}
+	if ((reader->ctxt->vctxt.vstateTab != NULL) &&
+	    (reader->ctxt->vctxt.vstateMax > 0)){
+	    xmlFree(reader->ctxt->vctxt.vstateTab);
+	    reader->ctxt->vctxt.vstateTab = 0;
+	    reader->ctxt->vctxt.vstateMax = 0;
+	}
 	if (reader->allocs & XML_TEXTREADER_CTXT)
 	    xmlFreeParserCtxt(reader->ctxt);
     }