From 8d8e23320e48f1d8fd98c3c914696f6fe0f7161e Mon Sep 17 00:00:00 2001 From: DEC05EBA Date: Wed, 1 Jan 2020 09:13:18 +0100 Subject: Ignore comments, ignore end tags without a start tag. Fixes tags closing too soon --- tests/main.c | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) (limited to 'tests/main.c') diff --git a/tests/main.c b/tests/main.c index ff1570b..6d84cfa 100644 --- a/tests/main.c +++ b/tests/main.c @@ -1,6 +1,48 @@ +#include +#include #include +#include -int main(int argc, char **argv) { - printf("hello, world!\n"); +char* file_get_content(const char *path, long *filesize) { + FILE *file = fopen(path, "rb"); + if(!file) { + perror(path); + return NULL; + } + + fseek(file, 0, SEEK_END); + *filesize = ftell(file); + fseek(file, 0, SEEK_SET); + + char *data = malloc(*filesize); + fread(data, 1, *filesize, file); + fclose(file); + return data; +} + +static void html_parse_callback(HtmlParser *html_parser, HtmlParseType parse_type, void *userdata_any) { + switch(parse_type) { + case HTML_PARSE_TAG_START: + printf("tag start: %.*s\n", html_parser->tag_name.size, html_parser->tag_name.data); + break; + case HTML_PARSE_TAG_END: + printf("tag end: %.*s\n", html_parser->tag_name.size, html_parser->tag_name.data); + break; + } +} + +int main() { + long filesize; + char *file_data = file_get_content("tests/github.html", &filesize); + if(!file_data) { + fprintf(stderr, "Failed to read from file: tests/github.html\n"); + return 1; + } + + HtmlParser html_parser; + html_parser_init(&html_parser, file_data, filesize, html_parse_callback, NULL); + html_parser_parse(&html_parser); + html_parser_deinit(&html_parser); + free(file_data); return 0; } -- cgit v1.2.3