xref: /csrg-svn/games/monop/initdeck.c (revision 33207)
1*33207Sbostic /*
2*33207Sbostic  * Copyright (c) 1987 Regents of the University of California.
3*33207Sbostic  * All rights reserved.
4*33207Sbostic  *
5*33207Sbostic  * Redistribution and use in source and binary forms are permitted
6*33207Sbostic  * provided that this notice is preserved and that due credit is given
7*33207Sbostic  * to the University of California at Berkeley. The name of the University
8*33207Sbostic  * may not be used to endorse or promote products derived from this
9*33207Sbostic  * software without specific prior written permission. This software
10*33207Sbostic  * is provided ``as is'' without express or implied warranty.
11*33207Sbostic  */
12*33207Sbostic 
13*33207Sbostic #ifndef lint
14*33207Sbostic char copyright[] =
15*33207Sbostic "@(#) Copyright (c) 1987 Regents of the University of California.\n\
16*33207Sbostic  All rights reserved.\n";
17*33207Sbostic #endif /* not lint */
18*33207Sbostic 
19*33207Sbostic #ifndef lint
20*33207Sbostic static char sccsid[] = "@(#)initdeck.c	5.1 (Berkeley) 01/02/88";
21*33207Sbostic #endif /* not lint */
22*33207Sbostic 
23*33207Sbostic # include	<stdio.h>
24*33207Sbostic # include	"deck.h"
25*33207Sbostic 
26*33207Sbostic /*
27*33207Sbostic  *	This program initializes the card files for monopoly.
28*33207Sbostic  * It reads in a data file with Com. Chest cards, followed by
29*33207Sbostic  * the Chance card.  The two are seperated by a line of "%-".
30*33207Sbostic  * All other cards are seperated by lines of "%%".  In the front
31*33207Sbostic  * of the file is the data for the decks in the same order.
32*33207Sbostic  * This includes the seek pointer for the start of each card.
33*33207Sbostic  * All cards start with their execution code, followed by the
34*33207Sbostic  * string to print, terminated with a null byte.
35*33207Sbostic  */
36*33207Sbostic 
37*33207Sbostic # define	TRUE	1
38*33207Sbostic # define	FALSE	0
39*33207Sbostic 
40*33207Sbostic # define	bool	char
41*33207Sbostic # define	reg	register
42*33207Sbostic 
43*33207Sbostic char	*infile		= "cards.inp",		/* input file		*/
44*33207Sbostic 	*outfile	= "cards.pck";		/* "packed" file	*/
45*33207Sbostic 
46*33207Sbostic long	ftell();
47*33207Sbostic 
48*33207Sbostic DECK	deck[2];
49*33207Sbostic 
50*33207Sbostic FILE	*inf, *outf;
51*33207Sbostic 
52*33207Sbostic main(ac, av)
53*33207Sbostic int	ac;
54*33207Sbostic char	*av[]; {
55*33207Sbostic 
56*33207Sbostic 	getargs(ac, av);
57*33207Sbostic 	if ((inf = fopen(infile, "r")) == NULL) {
58*33207Sbostic 		perror(infile);
59*33207Sbostic 		exit(1);
60*33207Sbostic 	}
61*33207Sbostic 	count();
62*33207Sbostic 	/*
63*33207Sbostic 	 * allocate space for pointers.
64*33207Sbostic 	 */
65*33207Sbostic 	CC_D.offsets = calloc(CC_D.num_cards + 1, sizeof (long));
66*33207Sbostic 	CH_D.offsets = calloc(CH_D.num_cards + 1, sizeof (long));
67*33207Sbostic 	fseek(inf, 0L, 0);
68*33207Sbostic 	if ((outf = fopen(outfile, "w")) == NULL) {
69*33207Sbostic 		perror(outfile);
70*33207Sbostic 		exit(0);
71*33207Sbostic 	}
72*33207Sbostic 
73*33207Sbostic 	fwrite(deck, sizeof (DECK), 2, outf);
74*33207Sbostic 	fwrite(CC_D.offsets, sizeof (long), CC_D.num_cards, outf);
75*33207Sbostic 	fwrite(CH_D.offsets, sizeof (long), CH_D.num_cards, outf);
76*33207Sbostic 	putem();
77*33207Sbostic 
78*33207Sbostic 	fclose(inf);
79*33207Sbostic 	fseek(outf, 0, 0L);
80*33207Sbostic 	fwrite(deck, sizeof (DECK), 2, outf);
81*33207Sbostic 	fwrite(CC_D.offsets, sizeof (long), CC_D.num_cards, outf);
82*33207Sbostic 	fwrite(CH_D.offsets, sizeof (long), CH_D.num_cards, outf);
83*33207Sbostic 	fclose(outf);
84*33207Sbostic 	printf("There were %d com. chest and %d chance cards\n", CC_D.num_cards, CH_D.num_cards);
85*33207Sbostic 	exit(0);
86*33207Sbostic }
87*33207Sbostic 
88*33207Sbostic getargs(ac, av)
89*33207Sbostic int	ac;
90*33207Sbostic char	*av[]; {
91*33207Sbostic 
92*33207Sbostic 	if (ac > 2) {
93*33207Sbostic 		infile = av[2] ? av[2] : infile;
94*33207Sbostic 		if (ac > 3)
95*33207Sbostic 			outfile = av[3];
96*33207Sbostic 	}
97*33207Sbostic }
98*33207Sbostic /*
99*33207Sbostic  * count the cards
100*33207Sbostic  */
101*33207Sbostic count() {
102*33207Sbostic 
103*33207Sbostic 	reg bool	newline;
104*33207Sbostic 	reg DECK	*in_deck;
105*33207Sbostic 	reg char	c;
106*33207Sbostic 
107*33207Sbostic 	newline = TRUE;
108*33207Sbostic 	in_deck = &CC_D;
109*33207Sbostic 	while ((c=getc(inf)) != EOF)
110*33207Sbostic 		if (newline && c == '%') {
111*33207Sbostic 			newline = FALSE;
112*33207Sbostic 			in_deck->num_cards++;
113*33207Sbostic 			if (getc(inf) == '-')
114*33207Sbostic 				in_deck = &CH_D;
115*33207Sbostic 		}
116*33207Sbostic 		else
117*33207Sbostic 			newline = (c == '\n');
118*33207Sbostic 	in_deck->num_cards++;
119*33207Sbostic }
120*33207Sbostic /*
121*33207Sbostic  *	put strings in the file
122*33207Sbostic  */
123*33207Sbostic putem() {
124*33207Sbostic 
125*33207Sbostic 	reg bool	newline;
126*33207Sbostic 	reg DECK	*in_deck;
127*33207Sbostic 	reg char	c;
128*33207Sbostic 	reg int		num;
129*33207Sbostic 
130*33207Sbostic 	in_deck = &CC_D;
131*33207Sbostic 	CC_D.num_cards = 1;
132*33207Sbostic 	CH_D.num_cards = 0;
133*33207Sbostic 	CC_D.offsets[0] = ftell(outf);
134*33207Sbostic 	putc(getc(inf), outf);
135*33207Sbostic 	putc(getc(inf), outf);
136*33207Sbostic 	for (num = 0; (c=getc(inf)) != '\n'; )
137*33207Sbostic 		num = num * 10 + (c - '0');
138*33207Sbostic 	putw(num, outf);
139*33207Sbostic 	newline = FALSE;
140*33207Sbostic 	while ((c=getc(inf)) != EOF)
141*33207Sbostic 		if (newline && c == '%') {
142*33207Sbostic 			putc('\0', outf);
143*33207Sbostic 			newline = FALSE;
144*33207Sbostic 			if (getc(inf) == '-')
145*33207Sbostic 				in_deck = &CH_D;
146*33207Sbostic 			while (getc(inf) != '\n')
147*33207Sbostic 				continue;
148*33207Sbostic 			in_deck->offsets[in_deck->num_cards++] = ftell(outf);
149*33207Sbostic 			if ((c=getc(inf)) == EOF)
150*33207Sbostic 				break;
151*33207Sbostic 			putc(c, outf);
152*33207Sbostic 			putc(c = getc(inf), outf);
153*33207Sbostic 			for (num = 0; (c=getc(inf)) != EOF && c != '\n'; )
154*33207Sbostic 				num = num * 10 + (c - '0');
155*33207Sbostic 			putw(num, outf);
156*33207Sbostic 		}
157*33207Sbostic 		else {
158*33207Sbostic 			putc(c, outf);
159*33207Sbostic 			newline = (c == '\n');
160*33207Sbostic 		}
161*33207Sbostic 	putc('\0', outf);
162*33207Sbostic }
163