1 #include <CspCompileRunTime.h>
2 #line 80 "C:/home/wini/develop/WebServer/delivery/examples/hangman/html/hangman.html"
3 #include <stdlib.h>
4 #include <time.h>
5 #include <assert.h>
6 #include <string.h>
7 #include <ctype.h>
8 #include <HttpServerLib.h>
9
10 struct Word
11 {
12 const char* clue;
13 const char* answer;
14 };
15
16 const Word wordDB[] = {
17 {"action", "drool"},
18 {"action", "forecast"},
19 {"action", "forget"},
20 {"action", "jump"},
21 {"action", "panic"},
22 {"action", "quash"},
23 {"action", "snore"},
24 {"action", "talk"},
25 {"action", "walk"},
26 {"activity", "boating"},
27 {"activity", "repair"},
28 {"activity", "ruffle"},
29 {"activity", "sleep"},
30 {"activity", "surfing"},
31 {"animal", "aardvark"},
32 {"animal", "adder"},
33 {"animal", "beaver"},
34 {"animal", "bison"},
35 {"animal", "cardinal"},
36 {"animal", "cariboo"},
37 {"animal", "cat"},
38 {"animal", "donkey"},
39 {"animal", "elephant"},
40 {"animal", "koala"},
41 {"animal", "otter"},
42 {"animal", "rabbit"},
43 {"animal", "raccoon"},
44 {"animal", "rodent"},
45 {"animal", "shark"},
46 {"animal", "sheep"},
47 {"animal", "squirrel"},
48 {"animal", "tiger"},
49 {"antique", "china"},
50 {"band", "nirvana"},
51 {"band", "rush"},
52 {"band", "ventures"},
53 {"creature", "elf"},
54 {"creature", "hobbit"},
55 {"creature", "mummy"},
56 {"creature", "troll"},
57 {"creature", "unicorn"},
58 {"creature", "vampire"},
59 {"creature", "warewolf"},
60 {"drink", "cola"},
61 {"drink", "juice"},
62 {"event", "drama"},
63 {"event", "mirage"},
64 {"event", "siesta"},
65 {"event", "stampede"},
66 {"food", "apple"},
67 {"food", "butter"},
68 {"food", "caesar salad"},
69 {"food", "cashew"},
70 {"food", "celery"},
71 {"food", "cheese"},
72 {"food", "chestnut"},
73 {"food", "cream"},
74 {"food", "donut"},
75 {"food", "grape"},
76 {"food", "honey"},
77 {"food", "icecream"},
78 {"food", "limburger"},
79 {"food", "milk"},
80 {"food", "noodle"},
81 {"food", "orange"},
82 {"food", "pasta"},
83 {"food", "pizza"},
84 {"food", "potato"},
85 {"food", "spice"},
86 {"food", "sugar"},
87 {"food", "tomatoes"},
88 {"food", "turnip"},
89 {"game", "cards"},
90 {"game", "golf"},
91 {"game", "pacman"},
92 {"icon", "barbie"},
93 {"icon", "frosty"},
94 {"icon", "grinch"},
95 {"icon", "gumby"},
96 {"icon", "lucy"},
97 {"icon", "popeye"},
98 {"icon", "snoopy"},
99 {"icon", "waldo"},
100 {"invention", "velcro"},
101 {"person", "enemy"},
102 {"person", "giant"},
103 {"person", "hero"},
104 {"person", "husband"},
105 {"person", "kennedy"},
106 {"person", "nixon"},
107 {"person", "youngster"},
108 {"place", "amsterdam"},
109 {"place", "athens"},
110 {"place", "beach"},
111 {"place", "bob"},
112 {"place", "buffalo"},
113 {"place", "calgary"},
114 {"place", "canada"},
115 {"place", "detroit"},
116 {"place", "downtown"},
117 {"place", "england"},
118 {"place", "gorge"},
119 {"place", "graceland"},
120 {"place", "hotel"},
121 {"place", "hotel"},
122 {"place", "india"},
123 {"place", "japan"},
124 {"place", "land"},
125 {"place", "library"},
126 {"place", "london"},
127 {"place", "norway"},
128 {"place", "oslo"},
129 {"place", "ottawa"},
130 {"place", "quadrant"},
131 {"place", "restaurant"},
132 {"place", "rink"},
133 {"place", "toronto"},
134 {"place", "victoria"},
135 {"place", "village"},
136 {"place", "zoo"},
137 {"plant", "daffodil"},
138 {"plant", "eggplant"},
139 {"plant", "ivy"},
140 {"plant", "spinach"},
141 {"plant", "tulip"},
142 {"profession", "astronaut"},
143 {"profession", "bard"},
144 {"profession", "hermit"},
145 {"profession", "lawyer"},
146 {"profession", "sorcerer"},
147 {"state", "alaska"},
148 {"subject", "geography"},
149 {"taste", "bitter"},
150 {"taste", "salty"},
151 {"taste", "sour"},
152 {"taste", "sweet"},
153 {"tool", "anvil"},
154 {"tool", "buffer"},
155 {"tool", "hook"},
156 {"tool", "knife"},
157 {"tool", "lasso"},
158 {"tool", "pickaxe"},
159 {"tool", "pliers"},
160 {"tool", "plow"},
161 {"tool", "screwdriver"},
162 {"tool", "wrench"},
163 {"toy", "kite"},
164 {"toy", "slinky"},
165 {"trouble", "tribble"}
166 };
167
168
169 typedef struct hangman_html0BC2{
170 void init_(U32 nameOffset, U32 b0, U32 b1, U32 b2, U32 b3, U32 b4, U32 b5, U32 b6, U32 b7, U32 b8, U32 b9, U32 b10, U32 b11, U32 b12, U32 b13, U32 b14, U32 b15);
171 void cspDestroy();
172 void service(HttpRequest* request, HttpResponse* response);
173 HttpPage page;
174 CspReader* data;
175 HttpDiskBlock blocks[16];
176 }hangman_html0BC2;
177 static char hangman_html0BC2_buf[sizeof(hangman_html0BC2) + 13];
178
179 void hangman_html0BC2::cspDestroy()
180 {
181 }
182
183
184 void hangman_html0BC2::service(HttpRequest* request, HttpResponse* response)
185 {
186 if(!request) { cspDestroy(); return; }
187 #line 253 "C:/home/wini/develop/WebServer/delivery/examples/hangman/html/hangman.html"
188 unsigned int i;
189 const char* ch=0;
190 const Word* word=0;
191 int wordDbIndex;
192 const char* guess=0;
193 char* newGuess=0;
194 int noOfUsedGues = 0;
195 HttpParameterIterator fIter(request);
196 /* Extract data from form in the above html */
197 for( ; fIter.hasMoreElements() ; fIter.nextElement())
198 {
199 if( ! strcmp(fIter.getName(), "guess") )
200 guess = fIter.getValue();
201 else if( ! strcmp(fIter.getName(), "ch") )
202 ch = fIter.getValue();
203 else if( ! strcmp(fIter.getName(), "wordDbIndex") )
204 {
205 wordDbIndex = atoi(fIter.getValue());
206 word = &wordDB[wordDbIndex];
207 }
208 else if( ! strcmp(fIter.getName(), "noOfUsedGues") )
209 noOfUsedGues = atoi(fIter.getValue());
210 }
211 if(word) /* Should be true unless first time or a new game.
212 * This works since the form in the html above is only
213 * emitted if we are playing a game
214 */
215 {
216 bool chCorrect=false;
217 assert(guess);
218 newGuess = httpStrdup(guess);
219 assert(ch);
220 for(i = 0 ; i < strlen(word->answer) ; i++)
221 {
222 if(tolower(word->answer[i]) == tolower(*ch))
223 {
224 newGuess[i] = *ch;
225 chCorrect = true;
226 }
227 }
228 if( ! httpStrCaseCmp(newGuess, word->answer) )
229 noOfUsedGues = 7; /* 7 signals "You Won!!!" */
230 else if(chCorrect == false)
231 noOfUsedGues++;
232 }
233 else /* First time or new game */
234 {
235 wordDbIndex = rand() % (sizeof(wordDB) / sizeof(wordDB[0]));
236 word = &wordDB[wordDbIndex];
237 newGuess = (char*)httpMalloc(strlen(word->answer)+1);
238 memset(newGuess, '_', strlen(word->answer));
239 newGuess[strlen(word->answer)] = 0;
240 }
241
242 HttpResponse_setDefaultHeaders(response);
243 if(httpWriteSection(this->data, response, this->blocks[0].offset, this->blocks[0].size)) goto L_epilogue;
244 #line 19 "C:/home/wini/develop/WebServer/delivery/examples/hangman/html/hangman.html"
245 for(i = 0; i < strlen(newGuess) ; i++)
246 response->printf("%c ", newGuess[i]);
247
248 if(httpWriteSection(this->data, response, this->blocks[1].offset, this->blocks[1].size)) goto L_epilogue;
249 #line 26 "C:/home/wini/develop/WebServer/delivery/examples/hangman/html/hangman.html"
250 HttpResponse_printf(response, "%d", noOfUsedGues);
251 if(httpWriteSection(this->data, response, this->blocks[2].offset, this->blocks[2].size)) goto L_epilogue;
252 #line 28 "C:/home/wini/develop/WebServer/delivery/examples/hangman/html/hangman.html"
253 if(noOfUsedGues == 6 || noOfUsedGues == 7)
254 {
255
256 if(httpWriteSection(this->data, response, this->blocks[3].offset, this->blocks[3].size)) goto L_epilogue;
257 #line 33 "C:/home/wini/develop/WebServer/delivery/examples/hangman/html/hangman.html"
258 HttpResponse_printf(response, "%s",noOfUsedGues == 6 ? "Game over." : "You Won!!!");
259 if(httpWriteSection(this->data, response, this->blocks[4].offset, this->blocks[4].size)) goto L_epilogue;
260 #line 38 "C:/home/wini/develop/WebServer/delivery/examples/hangman/html/hangman.html"
261 HttpResponse_printf(response, "%s",word->answer);
262 if(httpWriteSection(this->data, response, this->blocks[5].offset, this->blocks[5].size)) goto L_epilogue;
263 #line 43 "C:/home/wini/develop/WebServer/delivery/examples/hangman/html/hangman.html"
264 }
265 else
266 {
267 if(noOfUsedGues == 0)
268 {
269
270 if(httpWriteSection(this->data, response, this->blocks[6].offset, this->blocks[6].size)) goto L_epilogue;
271 #line 51 "C:/home/wini/develop/WebServer/delivery/examples/hangman/html/hangman.html"
272 }
273 else
274 {
275
276 if(httpWriteSection(this->data, response, this->blocks[7].offset, this->blocks[7].size)) goto L_epilogue;
277 #line 55 "C:/home/wini/develop/WebServer/delivery/examples/hangman/html/hangman.html"
278 HttpResponse_printf(response, "%d ", 6-noOfUsedGues);
279 if(httpWriteSection(this->data, response, this->blocks[8].offset, this->blocks[8].size)) goto L_epilogue;
280 #line 57 "C:/home/wini/develop/WebServer/delivery/examples/hangman/html/hangman.html"
281 }
282
283 if(httpWriteSection(this->data, response, this->blocks[9].offset, this->blocks[9].size)) goto L_epilogue;
284 #line 59 "C:/home/wini/develop/WebServer/delivery/examples/hangman/html/hangman.html"
285 HttpResponse_printf(response, "%s",word->clue);
286 if(httpWriteSection(this->data, response, this->blocks[10].offset, this->blocks[10].size)) goto L_epilogue;
287 #line 62 "C:/home/wini/develop/WebServer/delivery/examples/hangman/html/hangman.html"
288 HttpResponse_printf(response, "%d", wordDbIndex);
289 if(httpWriteSection(this->data, response, this->blocks[11].offset, this->blocks[11].size)) goto L_epilogue;
290 #line 63 "C:/home/wini/develop/WebServer/delivery/examples/hangman/html/hangman.html"
291 HttpResponse_printf(response, "%s",newGuess);
292 if(httpWriteSection(this->data, response, this->blocks[12].offset, this->blocks[12].size)) goto L_epilogue;
293 #line 64 "C:/home/wini/develop/WebServer/delivery/examples/hangman/html/hangman.html"
294 HttpResponse_printf(response, "%d", noOfUsedGues);
295 if(httpWriteSection(this->data, response, this->blocks[13].offset, this->blocks[13].size)) goto L_epilogue;
296 #line 65 "C:/home/wini/develop/WebServer/delivery/examples/hangman/html/hangman.html"
297 for(i = 'A'; i <= 'Z'; i++)
298 response->printf("\t\t<input type='submit' name='ch' value='%c'/>\n",i);
299
300 if(httpWriteSection(this->data, response, this->blocks[14].offset, this->blocks[14].size)) goto L_epilogue;
301 #line 70 "C:/home/wini/develop/WebServer/delivery/examples/hangman/html/hangman.html"
302 }
303
304 if(httpWriteSection(this->data, response, this->blocks[15].offset, this->blocks[15].size)) goto L_epilogue;
305 L_epilogue:
306 #line 308 "C:/home/wini/develop/WebServer/delivery/examples/hangman/html/hangman.html"
307 httpFree(newGuess);
308
309 ;
310 }
311
312 static void hangman_html0BC2_service(HttpPage* o,HttpRequest* request, HttpResponse* response)
313 {
314 ((hangman_html0BC2*)hangman_html0BC2_buf)->service(request, response);
315 }
316
317 void hangman_html0BC2::init_(U32 nameOffset, U32 b0, U32 b1, U32 b2, U32 b3, U32 b4, U32 b5, U32 b6, U32 b7, U32 b8, U32 b9, U32 b10, U32 b11, U32 b12, U32 b13, U32 b14, U32 b15)
318 {
319 HttpDynamicMemPage_loadAndInit((HttpPage*)hangman_html0BC2_buf, this->data, sizeof(hangman_html0BC2), hangman_html0BC2_service, nameOffset, 13);
320 this->blocks[0].offset = b0;
321 this->blocks[0].size = 605;
322 this->blocks[1].offset = b1;
323 this->blocks[1].size = 86;
324 this->blocks[2].offset = b2;
325 this->blocks[2].size = 42;
326 this->blocks[3].offset = b3;
327 this->blocks[3].size = 72;
328 this->blocks[4].offset = b4;
329 this->blocks[4].size = 132;
330 this->blocks[5].offset = b5;
331 this->blocks[5].size = 113;
332 this->blocks[6].offset = b6;
333 this->blocks[6].size = 79;
334 this->blocks[7].offset = b7;
335 this->blocks[7].size = 26;
336 this->blocks[8].offset = b8;
337 this->blocks[8].size = 28;
338 this->blocks[9].offset = b9;
339 this->blocks[9].size = 43;
340 this->blocks[10].offset = b10;
341 this->blocks[10].size = 125;
342 this->blocks[11].offset = b11;
343 this->blocks[11].size = 57;
344 this->blocks[12].offset = b12;
345 this->blocks[12].size = 64;
346 this->blocks[13].offset = b13;
347 this->blocks[13].size = 16;
348 this->blocks[14].offset = b14;
349 this->blocks[14].size = 25;
350 this->blocks[15].offset = b15;
351 this->blocks[15].size = 131;
352 #line 247 "C:/home/wini/develop/WebServer/delivery/examples/hangman/html/hangman.html"
353 /* Seed the random-number generator with current time so that
354 * the numbers will be different every time we run.
355 */
356 srand((unsigned)time(0));
357
358 }
359
360 #ifdef __cplusplus
361 extern "C"
362 #endif
363 void __hangman_html0BC2_consructor(HttpDir* dir, CspReader* data, U32 nameOffset, U32 b0, U32 b1, U32 b2, U32 b3, U32 b4, U32 b5, U32 b6, U32 b7, U32 b8, U32 b9, U32 b10, U32 b11, U32 b12, U32 b13, U32 b14, U32 b15)
364 {
365 ((hangman_html0BC2*)hangman_html0BC2_buf)->data = data;
366 ((hangman_html0BC2*)hangman_html0BC2_buf)->init_(nameOffset, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15);
367 HttpDir_insertPage(dir, (HttpPage*)hangman_html0BC2_buf);
368 }