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 151 152 153 154 155 156 157 158 159
|
#include <inttypes.h>
#include <stdio.h>
#include <parserutils/input/inputstream.h>
#include <hubbub/hubbub.h>
#include "utils/utils.h"
#include "tokeniser/tokeniser.h"
#include "testutils.h"
static hubbub_error token_handler(const hubbub_token *token, void *pw);
int main(int argc, char **argv)
{
parserutils_inputstream *stream;
hubbub_tokeniser *tok;
hubbub_tokeniser_optparams params;
FILE *fp;
size_t len;
#define CHUNK_SIZE (4096)
uint8_t buf[CHUNK_SIZE];
if (argc != 2) {
printf("Usage: %s <filename>\n", argv[0]);
return 1;
}
assert(parserutils_inputstream_create("UTF-8", 0, NULL,
&stream) == PARSERUTILS_OK);
assert(hubbub_tokeniser_create(stream, &tok) == HUBBUB_OK);
params.token_handler.handler = token_handler;
params.token_handler.pw = NULL;
assert(hubbub_tokeniser_setopt(tok, HUBBUB_TOKENISER_TOKEN_HANDLER,
¶ms) == HUBBUB_OK);
fp = fopen(argv[1], "rb");
if (fp == NULL) {
printf("Failed opening %s\n", argv[1]);
return 1;
}
fseek(fp, 0, SEEK_END);
len = ftell(fp);
fseek(fp, 0, SEEK_SET);
while (len > 0) {
ssize_t bytes_read = fread(buf, 1, CHUNK_SIZE, fp);
if (bytes_read < 1)
break;
assert(parserutils_inputstream_append(stream,
buf, bytes_read) == PARSERUTILS_OK);
len -= bytes_read;
assert(hubbub_tokeniser_run(tok) == HUBBUB_OK);
}
assert(len == 0);
fclose(fp);
hubbub_tokeniser_destroy(tok);
parserutils_inputstream_destroy(stream);
printf("PASS\n");
return 0;
}
hubbub_error token_handler(const hubbub_token *token, void *pw)
{
static const char *token_names[] = {
"DOCTYPE", "START TAG", "END TAG",
"COMMENT", "CHARACTERS", "EOF"
};
size_t i;
UNUSED(pw);
printf("%s: ", token_names[token->type]);
switch (token->type) {
case HUBBUB_TOKEN_DOCTYPE:
printf("'%.*s' %sids:\n",
(int) token->data.doctype.name.len,
token->data.doctype.name.ptr,
token->data.doctype.force_quirks ?
"(force-quirks) " : "");
if (token->data.doctype.public_missing)
printf("\tpublic: missing\n");
else
printf("\tpublic: '%.*s'\n",
(int) token->data.doctype.public_id.len,
token->data.doctype.public_id.ptr);
if (token->data.doctype.system_missing)
printf("\tsystem: missing\n");
else
printf("\tsystem: '%.*s'\n",
(int) token->data.doctype.system_id.len,
token->data.doctype.system_id.ptr);
break;
case HUBBUB_TOKEN_START_TAG:
printf("'%.*s' %s%s\n",
(int) token->data.tag.name.len,
token->data.tag.name.ptr,
(token->data.tag.self_closing) ?
"(self-closing) " : "",
(token->data.tag.n_attributes > 0) ?
"attributes:" : "");
for (i = 0; i < token->data.tag.n_attributes; i++) {
printf("\t'%.*s' = '%.*s'\n",
(int) token->data.tag.attributes[i].name.len,
token->data.tag.attributes[i].name.ptr,
(int) token->data.tag.attributes[i].value.len,
token->data.tag.attributes[i].value.ptr);
}
break;
case HUBBUB_TOKEN_END_TAG:
printf("'%.*s' %s%s\n",
(int) token->data.tag.name.len,
token->data.tag.name.ptr,
(token->data.tag.self_closing) ?
"(self-closing) " : "",
(token->data.tag.n_attributes > 0) ?
"attributes:" : "");
for (i = 0; i < token->data.tag.n_attributes; i++) {
printf("\t'%.*s' = '%.*s'\n",
(int) token->data.tag.attributes[i].name.len,
token->data.tag.attributes[i].name.ptr,
(int) token->data.tag.attributes[i].value.len,
token->data.tag.attributes[i].value.ptr);
}
break;
case HUBBUB_TOKEN_COMMENT:
printf("'%.*s'\n", (int) token->data.comment.len,
token->data.comment.ptr);
break;
case HUBBUB_TOKEN_CHARACTER:
printf("'%.*s'\n", (int) token->data.character.len,
token->data.character.ptr);
break;
case HUBBUB_TOKEN_EOF:
printf("\n");
break;
}
return HUBBUB_OK;
}
|