Tuesday, June 26, 2007

resource: fortune cookie quote files




For Windows users, I recommend using these files with Catfood Fortune Cookies. You may need to do some delimiter tweaking first.

For UNIX users, compile and try this:

#include
#include

#define BUFSIZE 6000
#define SEP '|'
#define DEFAULT_QUOTE_FILE "qfn"

main (argc, argv)
int argc;
char *argv[];
{
FILE *fp;
char file[BUFSIZ];
void quote();

if (argc > 1)
strcpy (file, argv[1]);
else
#ifdef vms
sprintf (file, "%s", DEFAULT_QUOTE_FILE);
#else
sprintf (file, "%s", DEFAULT_QUOTE_FILE);
#endif

if ((fp = fopen(file, "r")) == NULL) {
perror(file);
exit(1);
}

/* initialize random seed */
srand((int) (getpid() + time((long *) 0)));

quote(fp);
fclose(fp);
exit(0);
}

void
quote (fp)
FILE *fp;
{
static long len = -1;
long offset;
int c, i = 0;
char buf[BUFSIZE];

/* Get length of file, go to a random place in it */
if (len == -1) {
if (fseek(fp, 0, 2) == -1) {
perror("fseek 1");
exit(1);
}
len = ftell(fp);
}
offset = rand() % len;
if (fseek(fp, offset, 0) == -1) {
perror("fseek 2");
exit(1);
}

/* Read until SEP, read next line, print it.
(Note that we will never print anything before the first seperator.)
If we hit EOF looking for the first SEP, just recurse. */
while ((c = getc(fp)) != SEP)
if (c == EOF) {
quote(fp);
return;
}

/* Skip leading whitespace, then read in a quotation.
If we hit EOF before we find a non-whitespace char, recurse. */
while (isspace(c = getc(fp)))
;
if (c == EOF) {
quote(fp);
return;
}
buf[i++] = c;
while ((c = getc(fp)) != SEP && c != EOF) {
buf[i++] = c;

if (i == BUFSIZ-1)
/* Whoa! Is this quotation too long yet? */
break;
}
buf[i++] = 0;
printf("%s\n", buf);
}

1 comment:

Melinda9 said...

I know this is an old post, but thank you for the MST3K fortune cookies. I had an MST3K cookie app years ago, and was searching for it when I found this one.