xref: /openbsd-src/games/monop/initdeck.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: initdeck.c,v 1.13 2009/10/27 23:59:26 deraadt 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 #include	<err.h>
34 #include	<stdio.h>
35 #include	<stdlib.h>
36 #include	<unistd.h>
37 #include	"deck.h"
38 
39 /*
40  *	This program initializes the card files for monopoly.
41  * It reads in a data file with Com. Chest cards, followed by
42  * the Chance card.  The two are separated by a line of "%-".
43  * All other cards are separated by lines of "%%".  In the front
44  * of the file is the data for the decks in the same order.
45  * This includes the seek pointer for the start of each card.
46  * All cards start with their execution code, followed by the
47  * string to print, terminated with a null byte.
48  */
49 
50 #define	TRUE	1
51 #define	FALSE	0
52 
53 #define	bool	int8_t
54 
55 char	*infile		= "cards.inp",		/* input file		*/
56 	*outfile	= "cards.pck";		/* "packed" file	*/
57 
58 DECK	deck[2];
59 
60 FILE	*inf, *outf;
61 
62 static void	getargs(int, char *[]);
63 static void	count(void);
64 static void	putem(void);
65 
66 int
67 main(ac, av)
68 	int	ac;
69 	char	*av[];
70 {
71 	int n;
72 
73 	getargs(ac, av);
74 	if ((inf = fopen(infile, "r")) == NULL)
75 		err(1, "%s", infile);
76 	count();
77 	/*
78 	 * allocate space for pointers.
79 	 */
80 	if ((CC_D.offsets = (int32_t *)calloc(CC_D.num_cards + 1,
81 			sizeof (int32_t))) == NULL ||
82 	    (CH_D.offsets = (int32_t *)calloc(CH_D.num_cards + 1,
83 			sizeof (int32_t))) == NULL)
84 		err(1, NULL);
85 	fseek(inf, 0L, SEEK_SET);
86 	if ((outf = fopen(outfile, "w")) == NULL)
87 		err(1, "%s", outfile);
88 
89 	fwrite(&deck[0].num_cards, sizeof(deck[0].num_cards), 1, outf);
90 	fwrite(&deck[0].top_card, sizeof(deck[0].top_card), 1, outf);
91 	fwrite(&deck[0].gojf_used, sizeof(deck[0].gojf_used), 1, outf);
92 
93 	fwrite(&deck[0].num_cards, sizeof(deck[0].num_cards), 1, outf);
94 	fwrite(&deck[0].top_card, sizeof(deck[0].top_card), 1, outf);
95 	fwrite(&deck[0].gojf_used, sizeof(deck[0].gojf_used), 1, outf);
96 
97 	fwrite(CC_D.offsets, sizeof(CC_D.offsets[0]), CC_D.num_cards, outf);
98 	fwrite(CH_D.offsets, sizeof(CH_D.offsets[0]), CH_D.num_cards, outf);
99 	putem();
100 
101 	fclose(inf);
102 	fseek(outf, 0L, SEEK_SET);
103 
104 	deck[0].num_cards = htons(deck[0].num_cards);
105 	fwrite(&deck[0].num_cards, sizeof(deck[0].num_cards), 1, outf);
106 	deck[0].top_card = htons(deck[0].top_card);
107 	fwrite(&deck[0].top_card, sizeof(deck[0].top_card), 1, outf);
108 	fwrite(&deck[0].gojf_used, sizeof(deck[0].gojf_used), 1, outf);
109 	deck[0].num_cards = ntohs(deck[0].num_cards);
110 
111 	deck[1].num_cards = htons(deck[1].num_cards);
112 	fwrite(&deck[1].num_cards, sizeof(deck[1].num_cards), 1, outf);
113 	deck[1].top_card = htons(deck[1].top_card);
114 	fwrite(&deck[1].top_card, sizeof(deck[1].top_card), 1, outf);
115 	fwrite(&deck[1].gojf_used, sizeof(deck[1].gojf_used), 1, outf);
116 	deck[1].num_cards = ntohs(deck[1].num_cards);
117 
118 	for (n = 0 ; n < CC_D.num_cards ; n++) {
119 		CC_D.offsets[n] = htonl(CC_D.offsets[n]);
120 		fwrite(&CC_D.offsets[n], sizeof(CC_D.offsets[n]), 1, outf);
121 	}
122 	for (n = 0 ; n < CH_D.num_cards ; n++) {
123 		CH_D.offsets[n] = htonl(CH_D.offsets[n]);
124 		fwrite(&CH_D.offsets[n], sizeof(CH_D.offsets[n]), 1, outf);
125 	}
126 
127 	fclose(outf);
128 	printf("There were %d com. chest and %d chance cards\n", CC_D.num_cards, CH_D.num_cards);
129 	exit(0);
130 }
131 
132 static void
133 getargs(ac, av)
134 	int	ac;
135 	char	*av[];
136 {
137 	if (ac > 1)
138 		infile = av[1];
139 	if (ac > 2)
140 		outfile = av[2];
141 }
142 
143 /*
144  * count the cards
145  */
146 static void
147 count()
148 {
149 	bool	newline;
150 	DECK	*in_deck;
151 	int	c;
152 
153 	newline = TRUE;
154 	in_deck = &CC_D;
155 	while ((c=getc(inf)) != EOF)
156 		if (newline && c == '%') {
157 			newline = FALSE;
158 			in_deck->num_cards++;
159 			if (getc(inf) == '-')
160 				in_deck = &CH_D;
161 		}
162 		else
163 			newline = (c == '\n');
164 	in_deck->num_cards++;
165 }
166 /*
167  *	put strings in the file
168  */
169 static void
170 putem()
171 {
172 	bool	newline;
173 	DECK	*in_deck;
174 	int	c;
175 	int16_t	num;
176 
177 	in_deck = &CC_D;
178 	CC_D.num_cards = 1;
179 	CH_D.num_cards = 0;
180 	CC_D.offsets[0] = ftell(outf);
181 	putc(getc(inf), outf);
182 	putc(getc(inf), outf);
183 	for (num = 0; (c=getc(inf)) != '\n'; )
184 		num = num * 10 + (c - '0');
185 	num = htons(num);
186 	fwrite(&num, sizeof(num), 1, outf);
187 	newline = FALSE;
188 	while ((c=getc(inf)) != EOF)
189 		if (newline && c == '%') {
190 			putc('\0', outf);
191 			newline = FALSE;
192 			if (getc(inf) == '-')
193 				in_deck = &CH_D;
194 			while (getc(inf) != '\n')
195 				continue;
196 			in_deck->offsets[in_deck->num_cards++] = ftell(outf);
197 			if ((c=getc(inf)) == EOF)
198 				break;
199 			putc(c, outf);
200 			putc(c = getc(inf), outf);
201 			for (num = 0; (c=getc(inf)) != EOF && c != '\n'; )
202 				num = num * 10 + (c - '0');
203 			num = htons(num);
204 			fwrite(&num, sizeof(num), 1, outf);
205 		}
206 		else {
207 			putc(c, outf);
208 			newline = (c == '\n');
209 		}
210 	putc('\0', outf);
211 }
212