diff options
author | Bo Yang <struggleyb.nku@gmail.com> | 2009-08-11 11:17:23 +0000 |
---|---|---|
committer | Bo Yang <struggleyb.nku@gmail.com> | 2009-08-11 11:17:23 +0000 |
commit | 399da01ae4eb5c5e3e9349bacc2063c946c3d4a1 (patch) | |
tree | 433c8bcde94fc7a6e6f2e5cbf23842a84db98146 /test/testutils/load.c | |
parent | eec057c7437e19b59ca1e698ce548cb56ce37240 (diff) | |
download | libdom-399da01ae4eb5c5e3e9349bacc2063c946c3d4a1.tar.gz libdom-399da01ae4eb5c5e3e9349bacc2063c946c3d4a1.tar.bz2 |
Merge the branches/struggleyb/libdom-remain back to trunk.
svn path=/trunk/dom/; revision=9191
Diffstat (limited to 'test/testutils/load.c')
-rw-r--r-- | test/testutils/load.c | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/test/testutils/load.c b/test/testutils/load.c new file mode 100644 index 0000000..74cc009 --- /dev/null +++ b/test/testutils/load.c @@ -0,0 +1,151 @@ +/* + * This file is part of libdom test suite. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2009 Bo Yang <struggleyb.nku@gmail.com> + */ + +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> + +#include <sys/types.h> +#include <sys/uio.h> +#include <unistd.h> +#include <fcntl.h> + +#include <libwapcaplet/libwapcaplet.h> + +// For parsers +#include <dom/dom.h> +#include <xmlparser.h> +#include <parser.h> +#include <errors.h> + +#include "utils.h" + +extern lwc_context *ctx; + +/** + * Load the file as it is a XML file + * + * \param file The file path + * \param willBeModified Whether this file will be modified, not used + */ +dom_document *load_xml(char *file, bool willBeModified) +{ + dom_xml_parser *parser = NULL; + int handle; + int readed; + dom_xml_error error; + dom_document *ret; + char buffer[1024]; + + UNUSED(willBeModified); + + parser = dom_xml_parser_create(NULL, NULL, + myrealloc, NULL, mymsg, NULL, ctx); + if (parser == NULL) { + fprintf(stderr, "Can't create XMLParser\n"); + return NULL; + } + + handle = open(file, O_RDONLY); + if (handle == -1) { + dom_xml_parser_destroy(parser); + fprintf(stderr, "Can't open test input file: %s\n", file); + return NULL; + } + + readed = read(handle, buffer, 1024); + error = dom_xml_parser_parse_chunk(parser, buffer, readed); + if (error != DOM_XML_OK) { + dom_xml_parser_destroy(parser); + fprintf(stderr, "Parsing errors occur\n"); + return NULL; + } + + while(readed == 1024) { + readed = read(handle, buffer, 1024); + error = dom_xml_parser_parse_chunk(parser, buffer, readed); + if (error != DOM_XML_OK) { + dom_xml_parser_destroy(parser); + fprintf(stderr, "Parsing errors occur\n"); + return NULL; + } + } + + error = dom_xml_parser_completed(parser); + if (error != DOM_XML_OK) { + dom_xml_parser_destroy(parser); + fprintf(stderr, "Parsing error when construct DOM\n"); + return NULL; + } + + ret = dom_xml_parser_get_document(parser); + dom_xml_parser_destroy(parser); + + return ret; +} + +/** + * Load the file as it is a HTML file + * + * \param file The file path + * \param willBeModified Whether this file will be modified, not used + */ +dom_document *load_html(char *file, bool willBeModified) +{ + dom_hubbub_parser *parser = NULL; + int handle; + int readed; + dom_hubbub_error error; + dom_document *ret; + char buffer[1024]; + + UNUSED(willBeModified); + + parser = dom_hubbub_parser_create("data/Aliases", NULL, true, + myrealloc, NULL, mymsg, NULL, ctx); + if (parser == NULL) { + fprintf(stderr, "Can't create XMLParser\n"); + return NULL; + } + + handle = open(file, O_RDONLY); + if (handle == -1) { + dom_hubbub_parser_destroy(parser); + fprintf(stderr, "Can't open test input file: %s\n", file); + return NULL; + } + + readed = read(handle, buffer, 1024); + error = dom_hubbub_parser_parse_chunk(parser, buffer, readed); + if (error != DOM_HUBBUB_OK) { + dom_hubbub_parser_destroy(parser); + fprintf(stderr, "Parsing errors occur\n"); + return NULL; + } + + while(readed == 1024) { + readed = read(handle, buffer, 1024); + error = dom_hubbub_parser_parse_chunk(parser, buffer, readed); + if (error != DOM_HUBBUB_OK) { + dom_hubbub_parser_destroy(parser); + fprintf(stderr, "Parsing errors occur\n"); + return NULL; + } + } + + error = dom_hubbub_parser_completed(parser); + if (error != DOM_HUBBUB_OK) { + dom_hubbub_parser_destroy(parser); + fprintf(stderr, "Parsing error when construct DOM\n"); + return NULL; + } + + ret = dom_hubbub_parser_get_document(parser); + dom_hubbub_parser_destroy(parser); + + return ret; +} |