xref: /csrg-svn/games/monop/initdeck.c (revision 41291)
133207Sbostic /*
234788Sbostic  * Copyright (c) 1980 Regents of the University of California.
333207Sbostic  * All rights reserved.
433207Sbostic  *
533207Sbostic  * Redistribution and use in source and binary forms are permitted
634788Sbostic  * provided that the above copyright notice and this paragraph are
734788Sbostic  * duplicated in all such forms and that any documentation,
834788Sbostic  * advertising materials, and other materials related to such
934788Sbostic  * distribution and use acknowledge that the software was developed
1034788Sbostic  * by the University of California, Berkeley.  The name of the
1134788Sbostic  * University may not be used to endorse or promote products derived
1234788Sbostic  * from this software without specific prior written permission.
1334788Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434788Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534788Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1633207Sbostic  */
1733207Sbostic 
1833207Sbostic #ifndef lint
1933207Sbostic char copyright[] =
2034788Sbostic "@(#) Copyright (c) 1980 Regents of the University of California.\n\
2133207Sbostic  All rights reserved.\n";
2233207Sbostic #endif /* not lint */
2333207Sbostic 
2433207Sbostic #ifndef lint
25*41291Sbostic static char sccsid[] = "@(#)initdeck.c	5.4 (Berkeley) 05/02/90";
2633207Sbostic #endif /* not lint */
2733207Sbostic 
2833207Sbostic # include	<stdio.h>
2933207Sbostic # include	"deck.h"
3033207Sbostic 
3133207Sbostic /*
3233207Sbostic  *	This program initializes the card files for monopoly.
3333207Sbostic  * It reads in a data file with Com. Chest cards, followed by
3433207Sbostic  * the Chance card.  The two are seperated by a line of "%-".
3533207Sbostic  * All other cards are seperated by lines of "%%".  In the front
3633207Sbostic  * of the file is the data for the decks in the same order.
3733207Sbostic  * This includes the seek pointer for the start of each card.
3833207Sbostic  * All cards start with their execution code, followed by the
3933207Sbostic  * string to print, terminated with a null byte.
4033207Sbostic  */
4133207Sbostic 
4233207Sbostic # define	TRUE	1
4333207Sbostic # define	FALSE	0
4433207Sbostic 
4533207Sbostic # define	bool	char
4633207Sbostic # define	reg	register
4733207Sbostic 
4833207Sbostic char	*infile		= "cards.inp",		/* input file		*/
4933207Sbostic 	*outfile	= "cards.pck";		/* "packed" file	*/
5033207Sbostic 
5133216Sbostic extern long	ftell();
5233216Sbostic extern char *calloc();
5333207Sbostic 
5433207Sbostic DECK	deck[2];
5533207Sbostic 
5633207Sbostic FILE	*inf, *outf;
5733207Sbostic 
5833207Sbostic main(ac, av)
5933207Sbostic int	ac;
6033207Sbostic char	*av[]; {
6133207Sbostic 
6233207Sbostic 	getargs(ac, av);
6333207Sbostic 	if ((inf = fopen(infile, "r")) == NULL) {
6433207Sbostic 		perror(infile);
6533207Sbostic 		exit(1);
6633207Sbostic 	}
6733207Sbostic 	count();
6833207Sbostic 	/*
6933207Sbostic 	 * allocate space for pointers.
7033207Sbostic 	 */
7133216Sbostic 	CC_D.offsets = (long *)calloc(CC_D.num_cards + 1, sizeof (long));
7233216Sbostic 	CH_D.offsets = (long *)calloc(CH_D.num_cards + 1, sizeof (long));
7333207Sbostic 	fseek(inf, 0L, 0);
7433207Sbostic 	if ((outf = fopen(outfile, "w")) == NULL) {
7533207Sbostic 		perror(outfile);
7633207Sbostic 		exit(0);
7733207Sbostic 	}
7833207Sbostic 
7933207Sbostic 	fwrite(deck, sizeof (DECK), 2, outf);
8033207Sbostic 	fwrite(CC_D.offsets, sizeof (long), CC_D.num_cards, outf);
8133207Sbostic 	fwrite(CH_D.offsets, sizeof (long), CH_D.num_cards, outf);
8233207Sbostic 	putem();
8333207Sbostic 
8433207Sbostic 	fclose(inf);
8533207Sbostic 	fseek(outf, 0, 0L);
8633207Sbostic 	fwrite(deck, sizeof (DECK), 2, outf);
8733207Sbostic 	fwrite(CC_D.offsets, sizeof (long), CC_D.num_cards, outf);
8833207Sbostic 	fwrite(CH_D.offsets, sizeof (long), CH_D.num_cards, outf);
8933207Sbostic 	fclose(outf);
9033207Sbostic 	printf("There were %d com. chest and %d chance cards\n", CC_D.num_cards, CH_D.num_cards);
9133207Sbostic 	exit(0);
9233207Sbostic }
9333207Sbostic 
9433207Sbostic getargs(ac, av)
9533207Sbostic int	ac;
9633207Sbostic char	*av[]; {
9733207Sbostic 
98*41291Sbostic 	if (ac > 1)
99*41291Sbostic 		infile = av[1];
100*41291Sbostic 	if (ac > 2)
101*41291Sbostic 		outfile = av[2];
10233207Sbostic }
103*41291Sbostic 
10433207Sbostic /*
10533207Sbostic  * count the cards
10633207Sbostic  */
10733207Sbostic count() {
10833207Sbostic 
10933207Sbostic 	reg bool	newline;
11033207Sbostic 	reg DECK	*in_deck;
11133207Sbostic 	reg char	c;
11233207Sbostic 
11333207Sbostic 	newline = TRUE;
11433207Sbostic 	in_deck = &CC_D;
11533207Sbostic 	while ((c=getc(inf)) != EOF)
11633207Sbostic 		if (newline && c == '%') {
11733207Sbostic 			newline = FALSE;
11833207Sbostic 			in_deck->num_cards++;
11933207Sbostic 			if (getc(inf) == '-')
12033207Sbostic 				in_deck = &CH_D;
12133207Sbostic 		}
12233207Sbostic 		else
12333207Sbostic 			newline = (c == '\n');
12433207Sbostic 	in_deck->num_cards++;
12533207Sbostic }
12633207Sbostic /*
12733207Sbostic  *	put strings in the file
12833207Sbostic  */
12933207Sbostic putem() {
13033207Sbostic 
13133207Sbostic 	reg bool	newline;
13233207Sbostic 	reg DECK	*in_deck;
13333207Sbostic 	reg char	c;
13433207Sbostic 	reg int		num;
13533207Sbostic 
13633207Sbostic 	in_deck = &CC_D;
13733207Sbostic 	CC_D.num_cards = 1;
13833207Sbostic 	CH_D.num_cards = 0;
13933207Sbostic 	CC_D.offsets[0] = ftell(outf);
14033207Sbostic 	putc(getc(inf), outf);
14133207Sbostic 	putc(getc(inf), outf);
14233207Sbostic 	for (num = 0; (c=getc(inf)) != '\n'; )
14333207Sbostic 		num = num * 10 + (c - '0');
14433207Sbostic 	putw(num, outf);
14533207Sbostic 	newline = FALSE;
14633207Sbostic 	while ((c=getc(inf)) != EOF)
14733207Sbostic 		if (newline && c == '%') {
14833207Sbostic 			putc('\0', outf);
14933207Sbostic 			newline = FALSE;
15033207Sbostic 			if (getc(inf) == '-')
15133207Sbostic 				in_deck = &CH_D;
15233207Sbostic 			while (getc(inf) != '\n')
15333207Sbostic 				continue;
15433207Sbostic 			in_deck->offsets[in_deck->num_cards++] = ftell(outf);
15533207Sbostic 			if ((c=getc(inf)) == EOF)
15633207Sbostic 				break;
15733207Sbostic 			putc(c, outf);
15833207Sbostic 			putc(c = getc(inf), outf);
15933207Sbostic 			for (num = 0; (c=getc(inf)) != EOF && c != '\n'; )
16033207Sbostic 				num = num * 10 + (c - '0');
16133207Sbostic 			putw(num, outf);
16233207Sbostic 		}
16333207Sbostic 		else {
16433207Sbostic 			putc(c, outf);
16533207Sbostic 			newline = (c == '\n');
16633207Sbostic 		}
16733207Sbostic 	putc('\0', outf);
16833207Sbostic }
169