home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume36 / formes / part02 / quiz.c < prev    next >
C/C++ Source or Header  |  1993-04-01  |  3KB  |  183 lines

  1.  
  2. /*
  3.  *  Copyright (C) 1992-1993 Jeffrey Chilton
  4.  *
  5.  *  Permission is granted to anyone to make or distribute copies of
  6.  *  this program, in any medium, provided that the copyright notice
  7.  *  and permission notice are preserved, and that the distributor
  8.  *  grants the recipient permission for further redistribution as
  9.  *  permitted by this notice.
  10.  *  
  11.  *  Author's E-mail address:  172-9221@mcimail.com
  12.  *  
  13.  */
  14.  
  15. static char *whatstring = "@(#)quiz.c    2.4 JWC";
  16.  
  17. #include <stdio.h>
  18.  
  19. #include "class.h"
  20. #include "quiz.h"
  21. #include "assoc.h"
  22. #include "collect.h"
  23. #include "quizstep.h"
  24.  
  25. extern int GotRight;
  26.  
  27. /* Quiz_new - Create an empty quiz style */
  28.  
  29. static int matchSymbol();
  30.  
  31. Quiz *
  32. Quiz_new(name)
  33. char *name;
  34. {
  35.     Quiz *self;
  36.  
  37.     self = (Quiz *)malloc(sizeof (Quiz));
  38.     if (!self)
  39.     {
  40.     fprintf(stderr, "Quiz_new: malloc fails\n");
  41.     goto out;
  42.     }
  43.  
  44.     self->symbols = Collection_new(matchSymbol, Association_destroy);
  45.     if (!self->symbols)
  46.     {
  47.     fprintf(stderr, "Quiz_new: Collection_new fails\n");
  48.     self = (Quiz *)0;
  49.     goto out;
  50.     }
  51.  
  52.     self->actions = Collection_new(NULLMATCH, QuizStep_destroy);
  53.     if (!self->actions)
  54.     {
  55.     fprintf(stderr, "Quiz_new: Collection_new fails\n");
  56.     Collection_destroy(self->symbols);
  57.     self = (Quiz *)0;
  58.     goto out;
  59.     }
  60.  
  61.     self->name = ExtendString_newFromString(name);
  62.  
  63. out:
  64.  
  65.     return self;
  66.  
  67. }
  68.  
  69. static int
  70. matchSymbol(symbol, key)
  71. Association *symbol;
  72. char *key;
  73. {
  74.     return strcmp(Association_getKey(symbol), key);
  75. }
  76.  
  77.  
  78. /* Quiz_newFromFile - Create one from file stream */
  79.  
  80. Quiz *
  81. Quiz_newFromFile(name, stream)
  82. char *name;
  83. FILE *stream;
  84. {
  85.     Quiz *self;
  86.     char oneLine[80];
  87.     QuizStep *step;
  88.     int rc;
  89.  
  90.     self = Quiz_new(name);
  91.  
  92.     fgets(oneLine, 80, stream);
  93.     while (TRUE)
  94.     {
  95.     if (oneLine[0] == '\n' || oneLine[0] == '{')
  96.     {
  97.         fgets(oneLine, 80, stream);
  98.         continue;
  99.     }
  100.  
  101.     if (0 == strcmp(oneLine, "}\n"))
  102.     {
  103.         break;
  104.     }
  105.  
  106.     step = QuizStep_newFromString(oneLine, self->symbols);
  107.     if (!step)
  108.     {
  109.         fprintf(stderr, "Quiz_newFromFile: QuizStep_newString fails\n");
  110.         goto out;
  111.     }
  112.  
  113.     rc = Collection_add(self->actions, (char *)step);
  114.     if (rc < PERFECT)
  115.     {
  116.         fprintf(stderr, "Quiz_newFromFile: Collection_add fails\n");
  117.         goto out;
  118.     }
  119.  
  120.     fgets(oneLine, 80, stream);
  121.     }
  122.  
  123. out:
  124.  
  125.     return self;
  126.  
  127. }
  128.  
  129. /* Quiz_perform - make up and ask a question */
  130.  
  131. int
  132. Quiz_perform(self)
  133. Quiz *self;
  134. {
  135.     Collection *ts;
  136.     int rc;
  137.  
  138.     rc = TRUE;
  139.     while (rc)
  140.     {
  141.     ts = Collection_new(NULLMATCH, (void (*)())free);
  142.     if (!ts)
  143.     {
  144.         fprintf(stderr, "Quiz_perform: Collection_new fails\n");
  145.         goto out;
  146.     }
  147.     rc = Collection_do(self->actions, QuizStep_perform, (char *)ts);
  148.     Collection_destroy(ts);
  149.     }
  150.  
  151. out:
  152.  
  153.     return GotRight;
  154.  
  155. }
  156.  
  157. /* Quiz_destroy */
  158.  
  159. void
  160. Quiz_destroy(self)
  161. Quiz *self;
  162. {
  163.     Collection_destroy(self->symbols);
  164.     Collection_destroy(self->actions);
  165.     free(self);
  166. }
  167.  
  168. #ifndef PRODUCTION
  169.  
  170. /* Quiz_display - */
  171.  
  172. void
  173. Quiz_display(self)
  174. Quiz *self;
  175. {
  176.  
  177.     printf("%s (%d symbols):\n", self->name, Collection_size(self->symbols));
  178.     Collection_do(self->actions, QuizStep_display, 0);
  179.  
  180. }
  181.  
  182. #endif
  183.