home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
misc
/
volume36
/
formes
/
part02
/
quiz.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-04-01
|
3KB
|
183 lines
/*
* Copyright (C) 1992-1993 Jeffrey Chilton
*
* Permission is granted to anyone to make or distribute copies of
* this program, in any medium, provided that the copyright notice
* and permission notice are preserved, and that the distributor
* grants the recipient permission for further redistribution as
* permitted by this notice.
*
* Author's E-mail address: 172-9221@mcimail.com
*
*/
static char *whatstring = "@(#)quiz.c 2.4 JWC";
#include <stdio.h>
#include "class.h"
#include "quiz.h"
#include "assoc.h"
#include "collect.h"
#include "quizstep.h"
extern int GotRight;
/* Quiz_new - Create an empty quiz style */
static int matchSymbol();
Quiz *
Quiz_new(name)
char *name;
{
Quiz *self;
self = (Quiz *)malloc(sizeof (Quiz));
if (!self)
{
fprintf(stderr, "Quiz_new: malloc fails\n");
goto out;
}
self->symbols = Collection_new(matchSymbol, Association_destroy);
if (!self->symbols)
{
fprintf(stderr, "Quiz_new: Collection_new fails\n");
self = (Quiz *)0;
goto out;
}
self->actions = Collection_new(NULLMATCH, QuizStep_destroy);
if (!self->actions)
{
fprintf(stderr, "Quiz_new: Collection_new fails\n");
Collection_destroy(self->symbols);
self = (Quiz *)0;
goto out;
}
self->name = ExtendString_newFromString(name);
out:
return self;
}
static int
matchSymbol(symbol, key)
Association *symbol;
char *key;
{
return strcmp(Association_getKey(symbol), key);
}
/* Quiz_newFromFile - Create one from file stream */
Quiz *
Quiz_newFromFile(name, stream)
char *name;
FILE *stream;
{
Quiz *self;
char oneLine[80];
QuizStep *step;
int rc;
self = Quiz_new(name);
fgets(oneLine, 80, stream);
while (TRUE)
{
if (oneLine[0] == '\n' || oneLine[0] == '{')
{
fgets(oneLine, 80, stream);
continue;
}
if (0 == strcmp(oneLine, "}\n"))
{
break;
}
step = QuizStep_newFromString(oneLine, self->symbols);
if (!step)
{
fprintf(stderr, "Quiz_newFromFile: QuizStep_newString fails\n");
goto out;
}
rc = Collection_add(self->actions, (char *)step);
if (rc < PERFECT)
{
fprintf(stderr, "Quiz_newFromFile: Collection_add fails\n");
goto out;
}
fgets(oneLine, 80, stream);
}
out:
return self;
}
/* Quiz_perform - make up and ask a question */
int
Quiz_perform(self)
Quiz *self;
{
Collection *ts;
int rc;
rc = TRUE;
while (rc)
{
ts = Collection_new(NULLMATCH, (void (*)())free);
if (!ts)
{
fprintf(stderr, "Quiz_perform: Collection_new fails\n");
goto out;
}
rc = Collection_do(self->actions, QuizStep_perform, (char *)ts);
Collection_destroy(ts);
}
out:
return GotRight;
}
/* Quiz_destroy */
void
Quiz_destroy(self)
Quiz *self;
{
Collection_destroy(self->symbols);
Collection_destroy(self->actions);
free(self);
}
#ifndef PRODUCTION
/* Quiz_display - */
void
Quiz_display(self)
Quiz *self;
{
printf("%s (%d symbols):\n", self->name, Collection_size(self->symbols));
Collection_do(self->actions, QuizStep_display, 0);
}
#endif