-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.c
52 lines (41 loc) · 1.13 KB
/
main.c
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
/*
* html - a simple html parser lacking a better name
* The contents of this file is licensed under the MIT License,
* see the file COPYING or http://opensource.org/licenses/MIT
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "html.h"
#define BUFFSIZE 1024
int main(int argc, char **argv) {
//TODO: dynamic buffer handling to never overflow
FILE *f;
HtmlDocument *doc;
HtmlParseState *parse_state;
char buffer[BUFFSIZE + 1];
const char *token = buffer;
if(argc != 2) {
fprintf(stderr, "usage: html <file.html>\n");
return 1;
}
buffer[BUFFSIZE] = 0;
size_t len = 0;
size_t buffsize = BUFFSIZE;
parse_state = html_parse_begin();
if(!(f = fopen(argv[1], "r"))) {
fprintf(stderr, "error: cannot open file %s\n", argv[1]);
return 1;
}
while(!feof(f)) {
len = fread(buffer + (BUFFSIZE - buffsize), 1, buffsize, f);
token = html_parse_stream(parse_state, buffer + (BUFFSIZE - buffsize), buffer, len);
buffsize = (token - buffer);
memmove(buffer, token, BUFFSIZE - buffsize);
}
doc = html_parse_end(parse_state);
fclose(f);
html_print_dom(doc);
html_free_document(doc);
return 0;
}