xref: /386bsd/usr/src/games/monop/initdeck.c (revision a2142627)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
37  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 static char sccsid[] = "@(#)initdeck.c	5.5 (Berkeley) 6/1/90";
42 #endif /* not lint */
43 
44 # include	<stdio.h>
45 # include	"deck.h"
46 
47 /*
48  *	This program initializes the card files for monopoly.
49  * It reads in a data file with Com. Chest cards, followed by
50  * the Chance card.  The two are seperated by a line of "%-".
51  * All other cards are seperated by lines of "%%".  In the front
52  * of the file is the data for the decks in the same order.
53  * This includes the seek pointer for the start of each card.
54  * All cards start with their execution code, followed by the
55  * string to print, terminated with a null byte.
56  */
57 
58 # define	TRUE	1
59 # define	FALSE	0
60 
61 # define	bool	char
62 # define	reg	register
63 
64 char	*infile		= "cards.inp",		/* input file		*/
65 	*outfile	= "cards.pck";		/* "packed" file	*/
66 
67 extern long	ftell();
68 extern char *calloc();
69 
70 DECK	deck[2];
71 
72 FILE	*inf, *outf;
73 
main(ac,av)74 main(ac, av)
75 int	ac;
76 char	*av[]; {
77 
78 	getargs(ac, av);
79 	if ((inf = fopen(infile, "r")) == NULL) {
80 		perror(infile);
81 		exit(1);
82 	}
83 	count();
84 	/*
85 	 * allocate space for pointers.
86 	 */
87 	CC_D.offsets = (long *)calloc(CC_D.num_cards + 1, sizeof (long));
88 	CH_D.offsets = (long *)calloc(CH_D.num_cards + 1, sizeof (long));
89 	fseek(inf, 0L, 0);
90 	if ((outf = fopen(outfile, "w")) == NULL) {
91 		perror(outfile);
92 		exit(0);
93 	}
94 
95 	fwrite(deck, sizeof (DECK), 2, outf);
96 	fwrite(CC_D.offsets, sizeof (long), CC_D.num_cards, outf);
97 	fwrite(CH_D.offsets, sizeof (long), CH_D.num_cards, outf);
98 	putem();
99 
100 	fclose(inf);
101 	fseek(outf, 0, 0L);
102 	fwrite(deck, sizeof (DECK), 2, outf);
103 	fwrite(CC_D.offsets, sizeof (long), CC_D.num_cards, outf);
104 	fwrite(CH_D.offsets, sizeof (long), CH_D.num_cards, outf);
105 	fclose(outf);
106 	printf("There were %d com. chest and %d chance cards\n", CC_D.num_cards, CH_D.num_cards);
107 	exit(0);
108 }
109 
getargs(ac,av)110 getargs(ac, av)
111 int	ac;
112 char	*av[]; {
113 
114 	if (ac > 1)
115 		infile = av[1];
116 	if (ac > 2)
117 		outfile = av[2];
118 }
119 
120 /*
121  * count the cards
122  */
count()123 count() {
124 
125 	reg bool	newline;
126 	reg DECK	*in_deck;
127 	reg char	c;
128 
129 	newline = TRUE;
130 	in_deck = &CC_D;
131 	while ((c=getc(inf)) != EOF)
132 		if (newline && c == '%') {
133 			newline = FALSE;
134 			in_deck->num_cards++;
135 			if (getc(inf) == '-')
136 				in_deck = &CH_D;
137 		}
138 		else
139 			newline = (c == '\n');
140 	in_deck->num_cards++;
141 }
142 /*
143  *	put strings in the file
144  */
putem()145 putem() {
146 
147 	reg bool	newline;
148 	reg DECK	*in_deck;
149 	reg char	c;
150 	reg int		num;
151 
152 	in_deck = &CC_D;
153 	CC_D.num_cards = 1;
154 	CH_D.num_cards = 0;
155 	CC_D.offsets[0] = ftell(outf);
156 	putc(getc(inf), outf);
157 	putc(getc(inf), outf);
158 	for (num = 0; (c=getc(inf)) != '\n'; )
159 		num = num * 10 + (c - '0');
160 	putw(num, outf);
161 	newline = FALSE;
162 	while ((c=getc(inf)) != EOF)
163 		if (newline && c == '%') {
164 			putc('\0', outf);
165 			newline = FALSE;
166 			if (getc(inf) == '-')
167 				in_deck = &CH_D;
168 			while (getc(inf) != '\n')
169 				continue;
170 			in_deck->offsets[in_deck->num_cards++] = ftell(outf);
171 			if ((c=getc(inf)) == EOF)
172 				break;
173 			putc(c, outf);
174 			putc(c = getc(inf), outf);
175 			for (num = 0; (c=getc(inf)) != EOF && c != '\n'; )
176 				num = num * 10 + (c - '0');
177 			putw(num, outf);
178 		}
179 		else {
180 			putc(c, outf);
181 			newline = (c == '\n');
182 		}
183 	putc('\0', outf);
184 }
185