#include <assert.h>
#include <stdio.h>
#include "libxml/HTMLparser.h"
#include "libxml/debugXML.h"


int main(int argc, char **argv)
{
	FILE *fp;
	size_t chunk;
	char *data;
	htmlParserCtxt *parser;

	if (argc != 3) {
		fprintf(stderr, "Usage: %s chunk-size file\n", argv[0]);
		return 1;
	}

	chunk = atol(argv[1]);
	if (chunk == 0) {
		fprintf(stderr, "chuck-size must be 1 or more\n");
		return 1;
	}

	data = malloc(chunk);
	if (!data) {
		fprintf(stderr, "malloc(%lu) failed\n", chunk);
		return 1;
	}

	fp = fopen(argv[2], "rb");
	if (!fp) {
		perror(argv[2]);
		return 1;
	}

	parser = htmlCreatePushParserCtxt(0, 0, "", 0, 0,
			XML_CHAR_ENCODING_NONE);
	assert(parser);

	while (!feof(fp)) {
		size_t n = fread(data, 1, chunk, fp);
		fprintf(stderr, "htmlParseChunk %lu\n", n);
		htmlParseChunk(parser, data, n, 0);
	}
	fprintf(stderr, "htmlParseChunk 0\n");
	htmlParseChunk(parser, "", 0, 1);

	xmlDebugDumpDocument(stdout, parser->myDoc);
	htmlFreeParserCtxt(parser);

	return 0;
}
