]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - monop/initdeck.c
remove the attributions from my slogans. they kept popping up in
[bsdgames-darwin.git] / monop / initdeck.c
1 /* $NetBSD: initdeck.c,v 1.14 2001/07/22 13:34:01 wiz Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifdef __NetBSD__
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
40 The Regents of the University of California. All rights reserved.\n");
41 #endif /* not lint */
42
43 #ifndef lint
44 #if 0
45 static char sccsid[] = "@(#)initdeck.c 8.1 (Berkeley) 5/31/93";
46 #else
47 __RCSID("$NetBSD: initdeck.c,v 1.14 2001/07/22 13:34:01 wiz Exp $");
48 #endif
49 #endif /* not lint */
50 #endif /* __NetBSD__ */
51
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <sys/types.h>
56 #include "deck.h"
57
58 #ifndef u_int32_t
59 #define u_int32_t unsigned int
60 #endif
61
62 u_int32_t
63 h2nl(u_int32_t h)
64 {
65 unsigned char c[4];
66 u_int32_t rv;
67
68 c[0] = (h >> 24) & 0xff;
69 c[1] = (h >> 16) & 0xff;
70 c[2] = (h >> 8) & 0xff;
71 c[3] = (h >> 0) & 0xff;
72 memcpy(&rv, c, sizeof rv);
73
74 return (rv);
75 }
76
77 /*
78 * This program initializes the card files for monopoly.
79 * It reads in a data file with Com. Chest cards, followed by
80 * the Chance card. The two are separated by a line of "%-".
81 * All other cards are separated by lines of "%%". In the front
82 * of the file is the data for the decks in the same order.
83 * This includes the seek pointer for the start of each card.
84 * All cards start with their execution code, followed by the
85 * string to print, terminated with a null byte.
86 */
87
88 #define TRUE 1
89 #define FALSE 0
90
91 #define bool char
92
93 const char *infile = "cards.inp", /* input file */
94 *outfile = "cards.pck"; /* "packed" file */
95
96 DECK deck[2];
97
98 FILE *inf, *outf;
99
100 /* initdeck.c */
101 int main(int, char *[]);
102 static void getargs(int, char *[]);
103 static void fwrite_be_offt(off_t, FILE *);
104 static void count(void);
105 static void putem(void);
106
107 int
108 main(ac, av)
109 int ac;
110 char *av[];
111 {
112 int i, nc;
113
114 /* sanity test */
115 if (sizeof(int) != 4) {
116 fprintf(stderr, "sizeof(int) != 4\n");
117 exit(1);
118 }
119
120 getargs(ac, av);
121 if ((inf = fopen(infile, "r")) == NULL) {
122 perror(infile);
123 exit(1);
124 }
125 count();
126 /*
127 * allocate space for pointers.
128 */
129 CC_D.offsets = (off_t *)calloc(CC_D.num_cards + 1, /* sizeof (off_t) */ 8);
130 CH_D.offsets = (off_t *)calloc(CH_D.num_cards + 1, /* sizeof (off_t) */ 8);
131 if (CC_D.offsets == NULL || CH_D.offsets == NULL) {
132 fprintf(stderr, "out of memory\n");
133 exit(1);
134 }
135 fseek(inf, 0L, SEEK_SET);
136 if ((outf = fopen(outfile, "w")) == NULL) {
137 perror(outfile);
138 exit(1);
139 }
140
141 /*
142 * these fields will be overwritten after the offsets are calculated,
143 * so byte-order doesn't matter yet.
144 */
145 fwrite(&nc, sizeof(nc), 1, outf);
146 fwrite(&nc, sizeof(nc), 1, outf);
147 fwrite(CC_D.offsets, /* sizeof (off_t) */ 8, CC_D.num_cards, outf);
148 fwrite(CH_D.offsets, /* sizeof (off_t) */ 8, CH_D.num_cards, outf);
149
150 /*
151 * write out the cards themselves (calculating the offsets).
152 */
153 putem();
154
155 fclose(inf);
156 fseek(outf, 0, SEEK_SET);
157
158 /* number of community chest cards first... */
159 nc = h2nl(CC_D.num_cards);
160 fwrite(&nc, sizeof(nc), 1, outf);
161 /* ... then number of chance cards. */
162 nc = h2nl(CH_D.num_cards);
163 fwrite(&nc, sizeof(nc), 1, outf);
164
165 /* dump offsets in big-endian byte order */
166 for (i = 0; i < CC_D.num_cards; i++)
167 fwrite_be_offt(CC_D.offsets[i], outf);
168 for (i = 0; i < CH_D.num_cards; i++)
169 fwrite_be_offt(CH_D.offsets[i], outf);
170
171 fflush(outf);
172 if (ferror(outf)) {
173 perror(outfile);
174 exit(1);
175 }
176 fclose(outf);
177 printf("There were %d com. chest and %d chance cards\n",
178 CC_D.num_cards, CH_D.num_cards);
179 exit(0);
180 }
181
182 static void
183 getargs(ac, av)
184 int ac;
185 char *av[];
186 {
187 if (ac > 1)
188 infile = av[1];
189 if (ac > 2)
190 outfile = av[2];
191 }
192
193 /*
194 * count the cards
195 */
196 static void
197 count()
198 {
199 bool newline;
200 DECK *in_deck;
201 int c;
202
203 newline = TRUE;
204 in_deck = &CC_D;
205 while ((c=getc(inf)) != EOF)
206 if (newline && c == '%') {
207 newline = FALSE;
208 in_deck->num_cards++;
209 if (getc(inf) == '-')
210 in_deck = &CH_D;
211 }
212 else
213 newline = (c == '\n');
214 in_deck->num_cards++;
215 }
216
217 /*
218 * put strings in the file
219 */
220 static void
221 putem()
222 {
223 bool newline;
224 DECK *in_deck;
225 int c;
226 int num;
227
228 in_deck = &CC_D;
229 CC_D.num_cards = 1;
230 CH_D.num_cards = 0;
231 CC_D.offsets[0] = ftell(outf);
232 putc(getc(inf), outf);
233 putc(getc(inf), outf);
234 for (num = 0; (c=getc(inf)) != '\n'; )
235 num = num * 10 + (c - '0');
236 putw(h2nl(num), outf);
237 newline = FALSE;
238 while ((c=getc(inf)) != EOF)
239 if (newline && c == '%') {
240 putc('\0', outf);
241 newline = FALSE;
242 if (getc(inf) == '-')
243 in_deck = &CH_D;
244 while (getc(inf) != '\n')
245 continue;
246 in_deck->offsets[in_deck->num_cards++] = ftell(outf);
247 if ((c=getc(inf)) == EOF)
248 break;
249 putc(c, outf);
250 putc(c = getc(inf), outf);
251 for (num = 0; (c=getc(inf)) != EOF && c != '\n'; )
252 num = num * 10 + (c - '0');
253 putw(h2nl(num), outf);
254 }
255 else {
256 putc(c, outf);
257 newline = (c == '\n');
258 }
259 putc('\0', outf);
260 }
261
262 /*
263 * fwrite_be_offt:
264 * Write out the off paramater as a 64 bit big endian number
265 */
266
267 static void
268 fwrite_be_offt(off, f)
269 off_t off;
270 FILE *f;
271 {
272 int i;
273 unsigned char c[8];
274
275 for (i = 7; i >= 0; i--) {
276 c[i] = off & 0xff;
277 off >>= 8;
278 }
279 fwrite(c, sizeof(c), 1, f);
280 }