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