1 /*
2  * Copyright (c) 2002, Jon Travis
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <assert.h>
30 
31 #include "ekhtml.h"
32 
33 #define MAGIC_DOODIE 0xf9d33bc1
34 
35 typedef struct {
36     unsigned int n_starttags;
37     unsigned int n_endtags;
38     unsigned int n_comments;
39     unsigned int n_data;
40     unsigned int magic_doodie;
41     unsigned int only_parse;
42 } tester_cbdata;
43 
handle_starttag_way(void * cbdata,ekhtml_string_t * tag,ekhtml_attr_t * attrs)44 static void handle_starttag_way(void *cbdata, ekhtml_string_t *tag,
45                                 ekhtml_attr_t *attrs)
46 {
47     printf("GOT WAY START!\n");
48 }
49 
handle_starttag(void * cbdata,ekhtml_string_t * tag,ekhtml_attr_t * attrs)50 static void handle_starttag(void *cbdata, ekhtml_string_t *tag,
51 			    ekhtml_attr_t *attrs)
52 {
53     ekhtml_attr_t *attr;
54     tester_cbdata *tdata = cbdata;
55 
56     assert(tdata->magic_doodie == MAGIC_DOODIE);
57     tdata->n_starttags++;
58     if(tdata->only_parse)
59         return;
60 
61     printf("START: \"%.*s\"\n", tag->len, tag->str);
62     for(attr=attrs; attr; attr=attr->next) {
63         printf("ATTRIBUTE: \"%.*s\" = ", attr->name.len, attr->name.str);
64         if(!attr->isBoolean)
65             printf("\"%.*s\"\n", attr->val.len, attr->val.str);
66         else
67             printf("\"%.*s\"\n", attr->name.len, attr->name.str);
68     }
69 }
70 
handle_endtag(void * cbdata,ekhtml_string_t * str)71 static void handle_endtag(void *cbdata, ekhtml_string_t *str){
72     tester_cbdata *tdata = cbdata;
73 
74     assert(tdata->magic_doodie == MAGIC_DOODIE);
75     tdata->n_endtags++;
76     if(tdata->only_parse)
77         return;
78 
79     printf("END: \"%.*s\"\n", str->len, str->str);
80 }
81 
handle_comment(void * cbdata,ekhtml_string_t * str)82 static void handle_comment(void *cbdata, ekhtml_string_t *str){
83     tester_cbdata *tdata = cbdata;
84 
85     assert(tdata->magic_doodie == MAGIC_DOODIE);
86     tdata->n_comments++;
87     if(tdata->only_parse)
88         return;
89 
90     printf("COMMENT: \"%.*s\"\n", str->len, str->str);
91 }
92 
handle_data(void * cbdata,ekhtml_string_t * str)93 static void handle_data(void *cbdata, ekhtml_string_t *str){
94     tester_cbdata *tdata = cbdata;
95 
96     assert(tdata->magic_doodie == MAGIC_DOODIE);
97     tdata->n_data++;
98     if(tdata->only_parse)
99         return;
100 
101     fwrite(str->str, str->len, 1, stdout);
102 }
103 
main(int argc,char * argv[])104 int main(int argc, char *argv[]){
105     tester_cbdata cbdata;
106     ekhtml_parser_t *ekparser;
107     char *buf;
108     size_t nbuf;
109     int feedsize;
110 
111     if(argc < 2){
112         fprintf(stderr, "Syntax: %s <feedsize> [1|0 (to print debug)]\n",
113                 argv[0]);
114         return -1;
115     }
116 
117     feedsize = atoi(argv[1]);
118 
119     ekparser = ekhtml_parser_new(NULL);
120 
121     cbdata.n_starttags  = 0;
122     cbdata.n_endtags    = 0;
123     cbdata.n_comments   = 0;
124     cbdata.n_data       = 0;
125     cbdata.magic_doodie = MAGIC_DOODIE;
126     cbdata.only_parse   = argc == 3;
127 
128     ekhtml_parser_datacb_set(ekparser, handle_data);
129     ekhtml_parser_commentcb_set(ekparser, handle_comment);
130     ekhtml_parser_startcb_add(ekparser, "WAY", handle_starttag_way);
131     ekhtml_parser_startcb_add(ekparser, NULL, handle_starttag);
132     ekhtml_parser_endcb_add(ekparser, NULL, handle_endtag);
133     ekhtml_parser_cbdata_set(ekparser, &cbdata);
134     buf = malloc(feedsize);
135 
136     while((nbuf = fread(buf, 1, feedsize, stdin))){
137         ekhtml_string_t str;
138 
139         str.str = buf;
140         str.len = nbuf;
141         ekhtml_parser_feed(ekparser, &str);
142         ekhtml_parser_flush(ekparser, 0);
143     }
144     ekhtml_parser_flush(ekparser, 1);
145     ekhtml_parser_destroy(ekparser);
146     free(buf);
147 
148     if(argc == 3){
149         fprintf(stderr,
150                 "# starttags: %u\n"
151                 "# endtags:   %u\n"
152                 "# comments:  %u\n"
153                 "# data:      %u\n", cbdata.n_starttags,
154                 cbdata.n_endtags, cbdata.n_comments, cbdata.n_data);
155     }
156 
157     return 0;
158 }
159