]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - monop/initdeck.c
1 /* $NetBSD: initdeck.c,v 1.16 2008/02/19 08:07:51 dholland Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
35 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
36 The Regents of the University of California. All rights reserved.\n");
41 static char sccsid
[] = "@(#)initdeck.c 8.1 (Berkeley) 5/31/93";
43 __RCSID("$NetBSD: initdeck.c,v 1.16 2008/02/19 08:07:51 dholland Exp $");
46 #endif /* __NetBSD__ */
51 #include <sys/types.h>
55 #define u_int32_t unsigned int
64 c
[0] = (h
>> 24) & 0xff;
65 c
[1] = (h
>> 16) & 0xff;
66 c
[2] = (h
>> 8) & 0xff;
67 c
[3] = (h
>> 0) & 0xff;
68 memcpy(&rv
, c
, sizeof rv
);
74 * This program initializes the card files for monopoly.
75 * It reads in a data file with Com. Chest cards, followed by
76 * the Chance card. The two are separated by a line of "%-".
77 * All other cards are separated by lines of "%%". In the front
78 * of the file is the data for the decks in the same order.
79 * This includes the seek pointer for the start of each card.
80 * All cards start with their execution code, followed by the
81 * string to print, terminated with a null byte.
89 const char *infile
= "cards.inp", /* input file */
90 *outfile
= "cards.pck"; /* "packed" file */
97 int main(int, char *[]);
98 static void getargs(int, char *[]);
99 static void fwrite_be_offt(off_t
, FILE *);
100 static void count(void);
101 static void putem(void);
111 if (sizeof(int) != 4) {
112 fprintf(stderr
, "sizeof(int) != 4\n");
117 if ((inf
= fopen(infile
, "r")) == NULL
) {
123 * allocate space for pointers.
125 CC_D
.offsets
= (off_t
*)calloc(CC_D
.num_cards
+ 1, /* sizeof (off_t) */ 8);
126 CH_D
.offsets
= (off_t
*)calloc(CH_D
.num_cards
+ 1, /* sizeof (off_t) */ 8);
127 if (CC_D
.offsets
== NULL
|| CH_D
.offsets
== NULL
) {
128 fprintf(stderr
, "out of memory\n");
131 fseek(inf
, 0L, SEEK_SET
);
132 if ((outf
= fopen(outfile
, "w")) == NULL
) {
138 * these fields will be overwritten after the offsets are calculated,
139 * so byte-order doesn't matter yet.
141 fwrite(&nc
, sizeof(nc
), 1, outf
);
142 fwrite(&nc
, sizeof(nc
), 1, outf
);
143 fwrite(CC_D
.offsets
, /* sizeof (off_t) */ 8, CC_D
.num_cards
, outf
);
144 fwrite(CH_D
.offsets
, /* sizeof (off_t) */ 8, CH_D
.num_cards
, outf
);
147 * write out the cards themselves (calculating the offsets).
152 fseek(outf
, 0, SEEK_SET
);
154 /* number of community chest cards first... */
155 nc
= h2nl(CC_D
.num_cards
);
156 fwrite(&nc
, sizeof(nc
), 1, outf
);
157 /* ... then number of chance cards. */
158 nc
= h2nl(CH_D
.num_cards
);
159 fwrite(&nc
, sizeof(nc
), 1, outf
);
161 /* dump offsets in big-endian byte order */
162 for (i
= 0; i
< CC_D
.num_cards
; i
++)
163 fwrite_be_offt(CC_D
.offsets
[i
], outf
);
164 for (i
= 0; i
< CH_D
.num_cards
; i
++)
165 fwrite_be_offt(CH_D
.offsets
[i
], outf
);
173 printf("There were %d com. chest and %d chance cards\n",
174 CC_D
.num_cards
, CH_D
.num_cards
);
201 while ((c
=getc(inf
)) != EOF
)
202 if (newline
&& c
== '%') {
204 in_deck
->num_cards
++;
205 if (getc(inf
) == '-')
209 newline
= (c
== '\n');
210 in_deck
->num_cards
++;
214 * put strings in the file
227 CC_D
.offsets
[0] = ftell(outf
);
228 putc(getc(inf
), outf
);
229 putc(getc(inf
), outf
);
230 for (num
= 0; (c
=getc(inf
)) != '\n'; )
231 num
= num
* 10 + (c
- '0');
232 putw(h2nl(num
), outf
);
234 while ((c
=getc(inf
)) != EOF
)
235 if (newline
&& c
== '%') {
238 if (getc(inf
) == '-')
240 while (getc(inf
) != '\n')
242 in_deck
->offsets
[in_deck
->num_cards
++] = ftell(outf
);
243 if ((c
=getc(inf
)) == EOF
)
246 putc(c
= getc(inf
), outf
);
247 for (num
= 0; (c
=getc(inf
)) != EOF
&& c
!= '\n'; )
248 num
= num
* 10 + (c
- '0');
249 putw(h2nl(num
), outf
);
253 newline
= (c
== '\n');
260 * Write out the off parameter as a 64 bit big endian number
264 fwrite_be_offt(off
, f
)
271 for (i
= 7; i
>= 0; i
--) {
275 fwrite(c
, sizeof(c
), 1, f
);