xref: /csrg-svn/games/monop/initdeck.c (revision 60815)
133207Sbostic /*
2*60815Sbostic  * Copyright (c) 1980, 1993
3*60815Sbostic  *	The Regents of the University of California.  All rights reserved.
433207Sbostic  *
542583Sbostic  * %sccs.include.redist.c%
633207Sbostic  */
733207Sbostic 
833207Sbostic #ifndef lint
9*60815Sbostic static char copyright[] =
10*60815Sbostic "@(#) Copyright (c) 1980, 1993\n\
11*60815Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1233207Sbostic #endif /* not lint */
1333207Sbostic 
1433207Sbostic #ifndef lint
15*60815Sbostic static char sccsid[] = "@(#)initdeck.c	8.1 (Berkeley) 05/31/93";
1633207Sbostic #endif /* not lint */
1733207Sbostic 
1833207Sbostic # include	<stdio.h>
1933207Sbostic # include	"deck.h"
2033207Sbostic 
2133207Sbostic /*
2233207Sbostic  *	This program initializes the card files for monopoly.
2333207Sbostic  * It reads in a data file with Com. Chest cards, followed by
2433207Sbostic  * the Chance card.  The two are seperated by a line of "%-".
2533207Sbostic  * All other cards are seperated by lines of "%%".  In the front
2633207Sbostic  * of the file is the data for the decks in the same order.
2733207Sbostic  * This includes the seek pointer for the start of each card.
2833207Sbostic  * All cards start with their execution code, followed by the
2933207Sbostic  * string to print, terminated with a null byte.
3033207Sbostic  */
3133207Sbostic 
3233207Sbostic # define	TRUE	1
3333207Sbostic # define	FALSE	0
3433207Sbostic 
3533207Sbostic # define	bool	char
3633207Sbostic # define	reg	register
3733207Sbostic 
3833207Sbostic char	*infile		= "cards.inp",		/* input file		*/
3933207Sbostic 	*outfile	= "cards.pck";		/* "packed" file	*/
4033207Sbostic 
4133216Sbostic extern long	ftell();
4233216Sbostic extern char *calloc();
4333207Sbostic 
4433207Sbostic DECK	deck[2];
4533207Sbostic 
4633207Sbostic FILE	*inf, *outf;
4733207Sbostic 
main(ac,av)4833207Sbostic main(ac, av)
4933207Sbostic int	ac;
5033207Sbostic char	*av[]; {
5133207Sbostic 
5233207Sbostic 	getargs(ac, av);
5333207Sbostic 	if ((inf = fopen(infile, "r")) == NULL) {
5433207Sbostic 		perror(infile);
5533207Sbostic 		exit(1);
5633207Sbostic 	}
5733207Sbostic 	count();
5833207Sbostic 	/*
5933207Sbostic 	 * allocate space for pointers.
6033207Sbostic 	 */
6133216Sbostic 	CC_D.offsets = (long *)calloc(CC_D.num_cards + 1, sizeof (long));
6233216Sbostic 	CH_D.offsets = (long *)calloc(CH_D.num_cards + 1, sizeof (long));
6333207Sbostic 	fseek(inf, 0L, 0);
6433207Sbostic 	if ((outf = fopen(outfile, "w")) == NULL) {
6533207Sbostic 		perror(outfile);
6633207Sbostic 		exit(0);
6733207Sbostic 	}
6833207Sbostic 
6933207Sbostic 	fwrite(deck, sizeof (DECK), 2, outf);
7033207Sbostic 	fwrite(CC_D.offsets, sizeof (long), CC_D.num_cards, outf);
7133207Sbostic 	fwrite(CH_D.offsets, sizeof (long), CH_D.num_cards, outf);
7233207Sbostic 	putem();
7333207Sbostic 
7433207Sbostic 	fclose(inf);
7533207Sbostic 	fseek(outf, 0, 0L);
7633207Sbostic 	fwrite(deck, sizeof (DECK), 2, outf);
7733207Sbostic 	fwrite(CC_D.offsets, sizeof (long), CC_D.num_cards, outf);
7833207Sbostic 	fwrite(CH_D.offsets, sizeof (long), CH_D.num_cards, outf);
7933207Sbostic 	fclose(outf);
8033207Sbostic 	printf("There were %d com. chest and %d chance cards\n", CC_D.num_cards, CH_D.num_cards);
8133207Sbostic 	exit(0);
8233207Sbostic }
8333207Sbostic 
getargs(ac,av)8433207Sbostic getargs(ac, av)
8533207Sbostic int	ac;
8633207Sbostic char	*av[]; {
8733207Sbostic 
8841291Sbostic 	if (ac > 1)
8941291Sbostic 		infile = av[1];
9041291Sbostic 	if (ac > 2)
9141291Sbostic 		outfile = av[2];
9233207Sbostic }
9341291Sbostic 
9433207Sbostic /*
9533207Sbostic  * count the cards
9633207Sbostic  */
count()9733207Sbostic count() {
9833207Sbostic 
9933207Sbostic 	reg bool	newline;
10033207Sbostic 	reg DECK	*in_deck;
10133207Sbostic 	reg char	c;
10233207Sbostic 
10333207Sbostic 	newline = TRUE;
10433207Sbostic 	in_deck = &CC_D;
10533207Sbostic 	while ((c=getc(inf)) != EOF)
10633207Sbostic 		if (newline && c == '%') {
10733207Sbostic 			newline = FALSE;
10833207Sbostic 			in_deck->num_cards++;
10933207Sbostic 			if (getc(inf) == '-')
11033207Sbostic 				in_deck = &CH_D;
11133207Sbostic 		}
11233207Sbostic 		else
11333207Sbostic 			newline = (c == '\n');
11433207Sbostic 	in_deck->num_cards++;
11533207Sbostic }
11633207Sbostic /*
11733207Sbostic  *	put strings in the file
11833207Sbostic  */
putem()11933207Sbostic putem() {
12033207Sbostic 
12133207Sbostic 	reg bool	newline;
12233207Sbostic 	reg DECK	*in_deck;
12333207Sbostic 	reg char	c;
12433207Sbostic 	reg int		num;
12533207Sbostic 
12633207Sbostic 	in_deck = &CC_D;
12733207Sbostic 	CC_D.num_cards = 1;
12833207Sbostic 	CH_D.num_cards = 0;
12933207Sbostic 	CC_D.offsets[0] = ftell(outf);
13033207Sbostic 	putc(getc(inf), outf);
13133207Sbostic 	putc(getc(inf), outf);
13233207Sbostic 	for (num = 0; (c=getc(inf)) != '\n'; )
13333207Sbostic 		num = num * 10 + (c - '0');
13433207Sbostic 	putw(num, outf);
13533207Sbostic 	newline = FALSE;
13633207Sbostic 	while ((c=getc(inf)) != EOF)
13733207Sbostic 		if (newline && c == '%') {
13833207Sbostic 			putc('\0', outf);
13933207Sbostic 			newline = FALSE;
14033207Sbostic 			if (getc(inf) == '-')
14133207Sbostic 				in_deck = &CH_D;
14233207Sbostic 			while (getc(inf) != '\n')
14333207Sbostic 				continue;
14433207Sbostic 			in_deck->offsets[in_deck->num_cards++] = ftell(outf);
14533207Sbostic 			if ((c=getc(inf)) == EOF)
14633207Sbostic 				break;
14733207Sbostic 			putc(c, outf);
14833207Sbostic 			putc(c = getc(inf), outf);
14933207Sbostic 			for (num = 0; (c=getc(inf)) != EOF && c != '\n'; )
15033207Sbostic 				num = num * 10 + (c - '0');
15133207Sbostic 			putw(num, outf);
15233207Sbostic 		}
15333207Sbostic 		else {
15433207Sbostic 			putc(c, outf);
15533207Sbostic 			newline = (c == '\n');
15633207Sbostic 		}
15733207Sbostic 	putc('\0', outf);
15833207Sbostic }
159