Example #1
0
int main(int argc, char *argv[])
{
	tester_cbdata cbdata;
	ekhtml_parser_t *ekparser;
	char *buf;
	size_t nbuf;
	int feedsize;

	if (argc < 2) {
		fprintf(stderr,
			"Syntax: %s <feedsize> [1|0 (to print debug)]\n",
			argv[0]);
		return -1;
	}

	feedsize = atoi(argv[1]);

	ekparser = ekhtml_parser_new(NULL);

	cbdata.n_starttags = 0;
	cbdata.n_endtags = 0;
	cbdata.n_comments = 0;
	cbdata.n_data = 0;
	cbdata.magic_doodie = MAGIC_DOODIE;
	cbdata.only_parse = argc == 3;

	ekhtml_parser_datacb_set(ekparser, handle_data);
	ekhtml_parser_commentcb_set(ekparser, handle_comment);
	ekhtml_parser_startcb_add(ekparser, "h1", handle_starttag_way);
	ekhtml_parser_startcb_add(ekparser, "title", handle_starttag_way);
	ekhtml_parser_startcb_add(ekparser, NULL, handle_starttag);
	//ekhtml_parser_endcb_add(ekparser, "H1", handle_starttag_way);
	ekhtml_parser_endcb_add(ekparser, NULL, handle_endtag);
	ekhtml_parser_cbdata_set(ekparser, &cbdata);
	buf = malloc(feedsize);

	while ((nbuf = fread(buf, 1, feedsize, stdin))) {
		ekhtml_string_t str;

		str.str = buf;
		str.len = nbuf;
		ekhtml_parser_feed(ekparser, &str);
		ekhtml_parser_flush(ekparser, 0);
	}
	ekhtml_parser_flush(ekparser, 1);
	ekhtml_parser_destroy(ekparser);
	free(buf);

	if (argc == 3) {
		fprintf(stderr,
			"# starttags: %u\n"
			"# endtags:   %u\n"
			"# comments:  %u\n"
			"# data:      %u\n", cbdata.n_starttags,
			cbdata.n_endtags, cbdata.n_comments, cbdata.n_data);
	}

	return 0;
}
Example #2
0
void ekhtml_parser_feed(ekhtml_parser_t *parser, ekhtml_string_t *str){
    size_t nfed = 0;
    
    while(nfed != str->len){
        size_t tocopy;
        
        /* First see how much we can fill up our internal buffer */
        tocopy = MIN(parser->nalloced - parser->nbuf, str->len - nfed);
        memcpy(parser->buf + parser->nbuf, str->str + nfed, tocopy);
        nfed         += tocopy;
        parser->nbuf += tocopy;
        if(parser->nalloced == parser->nbuf){
            /* Process the buffer */
            if(!ekhtml_parser_flush(parser, 0)){
                /* If we didn't actually process anything, grow our buffer */
                ekhtml_buffer_grow(parser);
            }
        }
    }
}