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
|
#include <HtmlParser.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
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", (int)html_parser->tag_name.size, html_parser->tag_name.data);
break;
case HTML_PARSE_TAG_END:
printf("tag end: %.*s\n", (int)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;
}
html_parser_parse(file_data, filesize, html_parse_callback, NULL);
free(file_data);
return 0;
}
|