xref: /csrg-svn/games/monop/initdeck.c (revision 33216)
133207Sbostic /*
233207Sbostic  * Copyright (c) 1987 Regents of the University of California.
333207Sbostic  * All rights reserved.
433207Sbostic  *
533207Sbostic  * Redistribution and use in source and binary forms are permitted
633207Sbostic  * provided that this notice is preserved and that due credit is given
733207Sbostic  * to the University of California at Berkeley. The name of the University
833207Sbostic  * may not be used to endorse or promote products derived from this
933207Sbostic  * software without specific prior written permission. This software
1033207Sbostic  * is provided ``as is'' without express or implied warranty.
1133207Sbostic  */
1233207Sbostic 
1333207Sbostic #ifndef lint
1433207Sbostic char copyright[] =
1533207Sbostic "@(#) Copyright (c) 1987 Regents of the University of California.\n\
1633207Sbostic  All rights reserved.\n";
1733207Sbostic #endif /* not lint */
1833207Sbostic 
1933207Sbostic #ifndef lint
20*33216Sbostic static char sccsid[] = "@(#)initdeck.c	5.2 (Berkeley) 01/02/88";
2133207Sbostic #endif /* not lint */
2233207Sbostic 
2333207Sbostic # include	<stdio.h>
2433207Sbostic # include	"deck.h"
2533207Sbostic 
2633207Sbostic /*
2733207Sbostic  *	This program initializes the card files for monopoly.
2833207Sbostic  * It reads in a data file with Com. Chest cards, followed by
2933207Sbostic  * the Chance card.  The two are seperated by a line of "%-".
3033207Sbostic  * All other cards are seperated by lines of "%%".  In the front
3133207Sbostic  * of the file is the data for the decks in the same order.
3233207Sbostic  * This includes the seek pointer for the start of each card.
3333207Sbostic  * All cards start with their execution code, followed by the
3433207Sbostic  * string to print, terminated with a null byte.
3533207Sbostic  */
3633207Sbostic 
3733207Sbostic # define	TRUE	1
3833207Sbostic # define	FALSE	0
3933207Sbostic 
4033207Sbostic # define	bool	char
4133207Sbostic # define	reg	register
4233207Sbostic 
4333207Sbostic char	*infile		= "cards.inp",		/* input file		*/
4433207Sbostic 	*outfile	= "cards.pck";		/* "packed" file	*/
4533207Sbostic 
46*33216Sbostic extern long	ftell();
47*33216Sbostic extern char *calloc();
4833207Sbostic 
4933207Sbostic DECK	deck[2];
5033207Sbostic 
5133207Sbostic FILE	*inf, *outf;
5233207Sbostic 
5333207Sbostic main(ac, av)
5433207Sbostic int	ac;
5533207Sbostic char	*av[]; {
5633207Sbostic 
5733207Sbostic 	getargs(ac, av);
5833207Sbostic 	if ((inf = fopen(infile, "r")) == NULL) {
5933207Sbostic 		perror(infile);
6033207Sbostic 		exit(1);
6133207Sbostic 	}
6233207Sbostic 	count();
6333207Sbostic 	/*
6433207Sbostic 	 * allocate space for pointers.
6533207Sbostic 	 */
66*33216Sbostic 	CC_D.offsets = (long *)calloc(CC_D.num_cards + 1, sizeof (long));
67*33216Sbostic 	CH_D.offsets = (long *)calloc(CH_D.num_cards + 1, sizeof (long));
6833207Sbostic 	fseek(inf, 0L, 0);
6933207Sbostic 	if ((outf = fopen(outfile, "w")) == NULL) {
7033207Sbostic 		perror(outfile);
7133207Sbostic 		exit(0);
7233207Sbostic 	}
7333207Sbostic 
7433207Sbostic 	fwrite(deck, sizeof (DECK), 2, outf);
7533207Sbostic 	fwrite(CC_D.offsets, sizeof (long), CC_D.num_cards, outf);
7633207Sbostic 	fwrite(CH_D.offsets, sizeof (long), CH_D.num_cards, outf);
7733207Sbostic 	putem();
7833207Sbostic 
7933207Sbostic 	fclose(inf);
8033207Sbostic 	fseek(outf, 0, 0L);
8133207Sbostic 	fwrite(deck, sizeof (DECK), 2, outf);
8233207Sbostic 	fwrite(CC_D.offsets, sizeof (long), CC_D.num_cards, outf);
8333207Sbostic 	fwrite(CH_D.offsets, sizeof (long), CH_D.num_cards, outf);
8433207Sbostic 	fclose(outf);
8533207Sbostic 	printf("There were %d com. chest and %d chance cards\n", CC_D.num_cards, CH_D.num_cards);
8633207Sbostic 	exit(0);
8733207Sbostic }
8833207Sbostic 
8933207Sbostic getargs(ac, av)
9033207Sbostic int	ac;
9133207Sbostic char	*av[]; {
9233207Sbostic 
9333207Sbostic 	if (ac > 2) {
9433207Sbostic 		infile = av[2] ? av[2] : infile;
9533207Sbostic 		if (ac > 3)
9633207Sbostic 			outfile = av[3];
9733207Sbostic 	}
9833207Sbostic }
9933207Sbostic /*
10033207Sbostic  * count the cards
10133207Sbostic  */
10233207Sbostic count() {
10333207Sbostic 
10433207Sbostic 	reg bool	newline;
10533207Sbostic 	reg DECK	*in_deck;
10633207Sbostic 	reg char	c;
10733207Sbostic 
10833207Sbostic 	newline = TRUE;
10933207Sbostic 	in_deck = &CC_D;
11033207Sbostic 	while ((c=getc(inf)) != EOF)
11133207Sbostic 		if (newline && c == '%') {
11233207Sbostic 			newline = FALSE;
11333207Sbostic 			in_deck->num_cards++;
11433207Sbostic 			if (getc(inf) == '-')
11533207Sbostic 				in_deck = &CH_D;
11633207Sbostic 		}
11733207Sbostic 		else
11833207Sbostic 			newline = (c == '\n');
11933207Sbostic 	in_deck->num_cards++;
12033207Sbostic }
12133207Sbostic /*
12233207Sbostic  *	put strings in the file
12333207Sbostic  */
12433207Sbostic putem() {
12533207Sbostic 
12633207Sbostic 	reg bool	newline;
12733207Sbostic 	reg DECK	*in_deck;
12833207Sbostic 	reg char	c;
12933207Sbostic 	reg int		num;
13033207Sbostic 
13133207Sbostic 	in_deck = &CC_D;
13233207Sbostic 	CC_D.num_cards = 1;
13333207Sbostic 	CH_D.num_cards = 0;
13433207Sbostic 	CC_D.offsets[0] = ftell(outf);
13533207Sbostic 	putc(getc(inf), outf);
13633207Sbostic 	putc(getc(inf), outf);
13733207Sbostic 	for (num = 0; (c=getc(inf)) != '\n'; )
13833207Sbostic 		num = num * 10 + (c - '0');
13933207Sbostic 	putw(num, outf);
14033207Sbostic 	newline = FALSE;
14133207Sbostic 	while ((c=getc(inf)) != EOF)
14233207Sbostic 		if (newline && c == '%') {
14333207Sbostic 			putc('\0', outf);
14433207Sbostic 			newline = FALSE;
14533207Sbostic 			if (getc(inf) == '-')
14633207Sbostic 				in_deck = &CH_D;
14733207Sbostic 			while (getc(inf) != '\n')
14833207Sbostic 				continue;
14933207Sbostic 			in_deck->offsets[in_deck->num_cards++] = ftell(outf);
15033207Sbostic 			if ((c=getc(inf)) == EOF)
15133207Sbostic 				break;
15233207Sbostic 			putc(c, outf);
15333207Sbostic 			putc(c = getc(inf), outf);
15433207Sbostic 			for (num = 0; (c=getc(inf)) != EOF && c != '\n'; )
15533207Sbostic 				num = num * 10 + (c - '0');
15633207Sbostic 			putw(num, outf);
15733207Sbostic 		}
15833207Sbostic 		else {
15933207Sbostic 			putc(c, outf);
16033207Sbostic 			newline = (c == '\n');
16133207Sbostic 		}
16233207Sbostic 	putc('\0', outf);
16333207Sbostic }
164