summaryrefslogtreecommitdiff
path: root/test/testutils/load.c
blob: 57e07ef1d7c67b31c46d0dd20fc5faaa845aa2f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * 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"
#include "domts.h"

/**
 * 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(const char *file, bool willBeModified)
{
	dom_xml_parser *parser = NULL;
	int handle;
	int readed;
	dom_xml_error error;
	dom_document *ret;
	uint8_t buffer[1024];

	UNUSED(willBeModified);

	parser = dom_xml_parser_create(NULL, NULL, 
			myrealloc, NULL, mymsg, NULL);
	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(const char *file, bool willBeModified)
{
	dom_hubbub_parser *parser = NULL;
	int handle;
	int readed;
	dom_hubbub_error error;
	dom_document *ret;
	uint8_t buffer[1024];

	UNUSED(willBeModified);

	parser = dom_hubbub_parser_create(NULL, true,
			myrealloc, NULL, mymsg, NULL);
	if (parser == NULL) {
		fprintf(stderr, "Can't create Hubbub Parser\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;
}