Debian Codegraph @

terps/tads/tads3/gameinfo.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2001, 2002 Michael J. Roberts. All Rights Reserved.
3 *
4 * Please see the accompanying license file, LICENSE.TXT, for information
5 * on using and copying this software.
6 */
7 /*
8 Name
9 gameinfo.cpp - extract and parse tads 2/3 game file information
10 Function
11 Searches a compiled tads 2 game file or a compiled tads 3 image file
12 for a game information resource. If the resource is found, extracts
13 and parses the resource.
14
15 A game information resource is a set of name/value pairs giving
16 information on the game, such as its name, author, and description.
17 Each value is simply a text string. The game information is supplied
18 by the author, and is intended to help archives and users by providing
19 a way to obtain specific information about the game in a structured
20 format directly from the game file itself.
21
22 This implementation is independent of any tads 2 or tads 3 subsystem
23 except osifc, which it uses for portable file I/O and portable byte
24 format conversions. We do depend upon the 'resfind' utility module,
25 which extracts multimedia resources from tads game files.
26
27 For an example of how to use this module, please refer to the TEST
28 definitions at the bottom of this file - this gives a small sample
29 program that loads game information from a file and displays each
30 field on the standard output. For sample build commands, refer to
31 mk_gameinfo.bat, which builds the test program for Win32 console
32 mode using MS Visual C++.
33 Notes
34
35 Modified
36 09/24/01 MJRoberts - Creation
37 */
38
39 #include <string.h>
40 #include <stdlib.h>
41
42 #include "os.h"
43 #include "t3std.h"
44 #include "utf8.h"
45
46 #include "resfind.h"
47 #include "gameinfo.h"
48
49 /* ------------------------------------------------------------------------ */
50 /*
51 * Name/value pair list entry
52 */
53 struct tads_valinfo
54 {
55 const char *name;
56 size_t name_len;
57
58 /* value string */
59 const char *val;
60
61 /* next entry in the list */
62 tads_valinfo *nxt;
63 };
64
65 /* ------------------------------------------------------------------------ */
66 /*
67 * Create
68 */
69 CTadsGameInfo::CTadsGameInfo()
70 {
71 /* no buffer yet */
72 buf_ = 0;
73
74 /* no name/value pairs yet */
75 first_val_ = 0;
76 }
77
78 /* ------------------------------------------------------------------------ */
79 /*
80 * Delete
81 */
82 CTadsGameInfo::~CTadsGameInfo()
83 {
84 /* if we have a buffer, delete it */
85 if (buf_ != 0)
86 osfree(buf_);
87
88 /* free the name/value pair list */
89 free_value_list();
90 }
91
92 /*
93 * Delete the list. If a subclass overrides free_value(), it should call
94 * this routine from its overridden destructor, to ensure that the vtable
95 * still has a reference to the subclass version of free_value() when this
96 * is called.
97 *
98 * Since this routine deletes each entry and leaves us with an empty list,
99 * it's harmless to call this more than once; each subclass can thus
100 * safely call this from its own destructor, even if it thinks its own
101 * superclass or subclasses might call it as well.
102 */
103 void CTadsGameInfo::free_value_list()
104 {
105 while (first_val_ != 0)
106 {
107 /* remember the next one */
108 tads_valinfo *nxt = first_val_->nxt;
109
110 /*
111 * delete this one - delete its value string and then the
112 * structure itself
113 */
114 free_value(first_val_->val);
115 osfree(first_val_);
116
117 /* move on to the next */
118 first_val_ = nxt;
119 }
120 }
121
122 /* ------------------------------------------------------------------------ */
123 /*
124 * Parse a game file and retrieve game information
125 */
126 int CTadsGameInfo::read_from_fp(osfildef *fp)
127 {
128 /* find the GameInfo.txt resource */
129 tads_resinfo resinfo;
130 if (!tads_find_resource_fp(fp, "GameInfo.txt", &resinfo))
131 {
132 /* no GameInfo.txt resource - there's no game information */
133 return FALSE;
134 }
135
136 /* parse the file and return the result code */
137 return parse_file(fp, resinfo.seek_pos, resinfo.siz);
138 }
139
140 /*
141 * Parse a game file and retrieve game information
142 */
143 int CTadsGameInfo::read_from_file(const char *fname)
144 {
145 /* open the file */
146 osfildef *fp;
147 if ((fp = osfoprb(fname, OSFTGAME)) == 0
148 && (fp = osfoprb(fname, OSFTT3IMG)) == 0)
149 {
150 /*
151 * we can't open the file, so we obviously can't parse it to find
152 * game information
153 */
154 return 0;
155 }
156
157 /* parse the file and find the game information */
158 int ret = read_from_fp(fp);
159
160 /* we're done with the file - close it */
161 osfcls(fp);
162
163 /* return the results from the parser */
164 return ret;
165 }
166
167 /* ------------------------------------------------------------------------ */
168 /*
169 * Is this a vertical whitespace character?
170 */
171 static int is_vspace(wchar_t ch)
172 {
173 return ch == '\n' || ch == '\r' || ch == 0x2028;
174 }
175
176 /*
177 * Is this a horizontal whitespace character (i.e., any whitespace
178 * character other than a newline character of some kind)?
179 */
180 static int is_hspace(wchar_t ch)
181 {
182 /*
183 * it's a horizontal whitespace character if it's a whitespace
184 * character and it's not a vertical whitespace character
185 */
186 return is_space(ch) && !is_vspace(ch);
187 }
188
189 /* ------------------------------------------------------------------------ */
190 /*
191 * Skip a newline sequence. We'll skip the longest of the following
192 * sequences we find: CR, LF, CR-LF, LF-CR, 0x2028.
193 */
194 static void skip_newline(utf8_ptr *p, size_t *rem)
195 {
196 /* if there's nothing left, there's nothing to do */
197 if (*rem == 0)
198 return;
199
200 /* check what we have and skip accordingly */
201 switch(p->getch())
202 {
203 case '\n':
204 /*
205 * it's an LF, so skip it and check for a CR; if a CR immediately
206 * follows, consider the entire LF-CR to be a single newline, so
207 * skip the CR as well
208 */
209 p->inc(rem);
210 if (*rem != 0 && p->getch() == '\r')
211 p->inc(rem);
212 break;
213
214 case '\r':
215 /*
216 * it's a CR, so skip it and check for an LF; if an LF immediately
217 * follows, consider the entire CR-LF to be a single newline, so
218 * skip the LF as well
219 */
220 p->inc(rem);
221 if (*rem != 0 && p->getch() == '\n')
222 p->inc(rem);
223 break;
224
225 case 0x2028:
226 /*
227 * it's a Unicode line separator character - this indicates a
228 * newline but cannot be used in combination with any other
229 * characters to form multi-character newline sequences, so simply
230 * skip this one and we're done
231 */
232 p->inc(rem);
233 break;
234
235 default:
236 /* we're not on a newline at all, so there's nothing for us to do */
237 break;
238 }
239 }
240
241 /* ------------------------------------------------------------------------ */
242 /*
243 * Skip to the start of the next line
244 */
245 static void skip_to_next_line(utf8_ptr *p, size_t *rem)
246 {
247 /* find the next line-ending sequence */
248 for ( ; *rem != 0 ; p->inc(rem))
249 {
250 /* get the current character */
251 wchar_t ch = p->getch();
252
253 /* check to see if we've reached a newline yet */
254 if (is_vspace(ch))
255 {
256 /* skip the entire newline sequence */
257 skip_newline(p, rem);
258
259 /* we're done looking for the end of the line */
260 break;
261 }
262 }
263 }
264
265 /* ------------------------------------------------------------------------ */
266 /*
267 * Parse a file
268 */
269 int CTadsGameInfo::parse_file(osfildef *fp, unsigned long res_seek_pos,
270 unsigned long res_size)
271 {
272 /* find the tail of the existing list */
273 tads_valinfo *last_val;
274 for (last_val = first_val_ ; last_val != 0 && last_val->nxt != 0 ;
275 last_val = last_val->nxt) ;
276
277 /* we found the resource - seek to it in the file */
278 if (osfseek(fp, res_seek_pos, OSFSK_SET))
279 return FALSE;
280
281 /*
282 * Allocate a block of memory for loading the game information. The
283 * game information resource is typically fairly small, so for
284 * simplicity we'll just allocate a single block of memory and load
285 * the whole resource into the block. Allocate space for one extra
286 * byte, so that we can ensure we have a newline at the end of the
287 * buffer.
288 */
289 buf_ = (char *)osmalloc(res_size + 1);
290 if (buf_ == 0)
291 return FALSE;
292
293 /* read the data */
294 if (osfrb(fp, buf_, res_size))
295 return FALSE;
296
297 /*
298 * store an extra newline at the end of the buffer, so that we can be
299 * certain that the last line ends in a newline - at worst, this will
300 * add an extra blank line to the end, but since we ignore blank lines
301 * this will do no harm
302 */
303 buf_[res_size++] = '\n';
304
305 /* parse the data */
306 utf8_ptr p(buf_);
307 for (size_t rem = res_size ; rem != 0 ; )
308 {
309 /* skip any leading whitespace */
310 while (rem != 0 && is_space(p.getch()))
311 p.inc(&rem);
312
313 /* if the line starts with '#', it's a comment, so skip it */
314 if (rem != 0 && p.getch() == '#')
315 {
316 /* skip the entire line, and go back for the next one */
317 skip_to_next_line(&p, &rem);
318 continue;
319 }
320
321 /* we must have the start of a name - note it */
322 utf8_ptr name_start = p;
323
324 /* skip ahead to a space or colon */
325 while (rem != 0 && p.getch() != ':' && !is_hspace(p.getch()))
326 p.inc(&rem);
327
328 /* note the length of the name */
329 size_t name_len = p.getptr() - name_start.getptr();
330
331 /* skip any whitespace before the presumed colon */
332 while (rem != 0 && is_hspace(p.getch()))
333 p.inc(&rem);
334
335 /* if we're not at a colon, the line is ill-formed, so skip it */
336 if (rem == 0 || p.getch() != ':')
337 {
338 /* skip the entire line, and go back for the next one */
339 skip_to_next_line(&p, &rem);
340 continue;
341 }
342
343 /* skip the colon and any whitespace immediately after it */
344 for (p.inc(&rem) ; rem != 0 && is_hspace(p.getch()) ; p.inc(&rem)) ;
345
346 /*
347 * Whatever terminated the name, replace it with a null character
348 * - this is safe, since we at least have a colon character to
349 * replace, and we've already skipped it so we can overwrite it
350 * now. A null character in utf-8 is simply a single zero byte,
351 * so we can store our byte directly without worrying about any
352 * utf-8 multi-byte strangeness.
353 */
354 *(name_start.getptr() + name_len) = '\0';
355
356 /* note where the value starts */
357 utf8_ptr val_start = p;
358
359 /* set up to write to the buffer at the current point */
360 utf8_ptr dst = p;
361
362 /*
363 * Now find the end of the value. The value can span multiple
364 * lines; if we find a newline followed by a space, it means that
365 * the next line is a continuation of the current value, and that
366 * the entire sequence of newlines and immediately following
367 * whitespace should be converted to a single space in the value.
368 *
369 * Note that we copy the transformed value directly over the old
370 * version of the value in the buffer. This is safe because the
371 * transformation can only remove characters - we merely collapse
372 * each newline-whitespace sequence into a single space.
373 */
374 while (rem != 0)
375 {
376 /* get this character */
377 wchar_t ch = p.getch();
378
379 /* check for a newline */
380 if (is_vspace(ch))
381 {
382 /*
383 * it's a newline - skip it (and any other characters in
384 * the newline sequence)
385 */
386 skip_newline(&p, &rem);
387
388 /*
389 * if there's no leading whitespace on the next line,
390 * we've reached the end of this value
391 */
392 if (rem == 0 || !is_hspace(p.getch()))
393 {
394 /*
395 * no whitespace -> end of the value - stop scanning
396 * the value
397 */
398 break;
399 }
400
401 /* skip leading whitespace on the line */
402 while (rem != 0 && is_hspace(p.getch()))
403 p.inc(&rem);
404
405 /*
406 * add a single whitespace character to the output for the
407 * entire sequence of the newline plus the leading
408 * whitespace on the line
409 */
410 dst.setch(' ');
411 }
412 else if (ch == 0)
413 {
414 /* change null bytes to spaces */
415 dst.setch(' ');
416
417 /* skip this character */
418 p.inc(&rem);
419 }
420 else
421 {
422 /* it's not a newline - simply copy it out and keep going */
423 dst.setch(ch);
424
425 /* skip this character */
426 p.inc(&rem);
427 }
428 }
429
430 /*
431 * Store a null terminator at the end of the value (it's safe to
432 * write this to the buffer because at worst it'll overwrite the
433 * newline at the end of the last line, which we've already
434 * skipped in the source). Note that we obtain the length of the
435 * value string before storing the null terminator, because we
436 * don't want to count the null byte in the value's length.
437 */
438 dst.setch('\0');
439
440 /*
441 * Create a new value list entry. Point the entry directly into
442 * our buffer, since we're keeping the buffer around as long as
443 * the value list is around.
444 */
445 tads_valinfo *val_info =
446 (tads_valinfo *)osmalloc(sizeof(tads_valinfo));
447 val_info->name = name_start.getptr();
448 val_info->name_len = name_len;
449 val_info->val = store_value(val_start.getptr(),
450 dst.getptr() - val_start.getptr());
451 val_info->nxt = 0;
452
453 /* link the new value at the end of our list */
454 if (last_val != 0)
455 last_val->nxt = val_info;
456 else
457 first_val_ = val_info;
458 last_val = val_info;
459 }
460
461 /* success */
462 return TRUE;
463 }
464
465 /* ------------------------------------------------------------------------ */
466 /*
467 * Look up a a value by name, returning a pointer to the value string.
468 */
469 const char *CTadsGameInfo::get_val(const char *name) const
470 {
471 /* for efficiency, note the length of the name up front */
472 size_t name_len = strlen(name);
473
474 /* scan our list of value entries for the given name */
475 for (tads_valinfo *cur = first_val_ ; cur != 0 ; cur = cur->nxt)
476 {
477 /*
478 * If the name matches, return the value for this entry. Note
479 * that, since the name is always a plain ASCII string, we can
480 * perform a simple byte-by-byte case-insensitive comparison using
481 * memicmp.
482 */
483 if (cur->name_len == name_len
484 && memicmp(cur->name, name, name_len) == 0)
485 {
486 /* it's the one - return its value */
487 return cur->val;
488 }
489 }
490
491 /* didn't find it - return null */
492 return 0;
493 }
494
495 /* ------------------------------------------------------------------------ */
496 /*
497 * Enumerate all name/value pairs
498 */
499 void CTadsGameInfo::enum_values(CTadsGameInfo_enum *cb)
500 {
501 /* invoke the callback for each value in our list */
502 for (tads_valinfo *cur = first_val_ ; cur != 0 ; cur = cur->nxt)
503 cb->tads_enum_game_info(cur->name, cur->val);
504 }
505
506 /* ------------------------------------------------------------------------ */
507 /*
508 * Testing routine. We'll find the game information in a given file, then
509 * display all of the strings. We'll convert the values to the display
510 * character set before showing them.
511 */
512 #ifdef TEST
513 #include "charmap.h"
514 #include "resload.h"
515 #include "vmimage.h" // $$$ for our vmimage substitution
516
517 /*
518 * name/value pair enumeration interface implementation - this is an
519 * implementation for the testing program that merely displays the
520 * name/value pair on the standard output
521 */
522 class CTestEnum: public CTadsGameInfo_enum
523 {
524 public:
525 /*
526 * CTadsGameInfo_enum implementation
527 */
528
529 /* receive a name/value pair in the enumeration */
530 virtual void tads_enum_game_info(const char *name, const char *val)
531 {
532 /* display the name/value pair */
533 printf("%s = %s\n", name, val);
534 }
535 };
536
537 /*
538 * main test program entrypoint
539 */
540 int main(int argc, char **argv)
541 {
542 /* check usage */
543 if (argc != 2)
544 {
545 printf("usage: gameinfo <gamefile>\n");
546 exit(2);
547 }
548
549 /* get the arguments */
550 const char *fname = argv[1];
551
552 /*
553 * set up a game information reader that translates to the default
554 * local display character set
555 */
556 CTadsGameInfo *info = new CTadsGameInfoLocal(argv[0]);
557
558 /* load the game information object, if possible */
559 if (!info->read_from_file(fname))
560 {
561 printf("unable to load game information from file \"%s\"\n", fname);
562 delete info;
563 exit(2);
564 }
565
566 /* enumerate the name/value pairs through our callback implementation */
567 CTestEnum cb;
568 info->enum_values(&cb);
569
570 /* we're done with our game information reader - delete it */
571 delete info;
572
573 /* success */
574 exit(0);
575 return 0;
576 }
577
578 #endif /* TEST */