]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - monop/initdeck.c
Use the symbolic names `SEEK_SET' and `O_RDONLY' where appropriate in
[bsdgames-darwin.git] / monop / initdeck.c
1 /* $NetBSD: initdeck.c,v 1.9 1999/09/08 21:57:18 jsm 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 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)initdeck.c 8.1 (Berkeley) 5/31/93";
45 #else
46 __RCSID("$NetBSD: initdeck.c,v 1.9 1999/09/08 21:57:18 jsm Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <sys/types.h>
53 #include <sys/endian.h>
54 #include "deck.h"
55
56 /*
57 * This program initializes the card files for monopoly.
58 * It reads in a data file with Com. Chest cards, followed by
59 * the Chance card. The two are seperated by a line of "%-".
60 * All other cards are seperated by lines of "%%". In the front
61 * of the file is the data for the decks in the same order.
62 * This includes the seek pointer for the start of each card.
63 * All cards start with their execution code, followed by the
64 * string to print, terminated with a null byte.
65 */
66
67 #define TRUE 1
68 #define FALSE 0
69
70 #define bool char
71
72 const char *infile = "cards.inp", /* input file */
73 *outfile = "cards.pck"; /* "packed" file */
74
75 DECK deck[2];
76
77 FILE *inf, *outf;
78
79 /* initdeck.c */
80 int main __P((int, char *[]));
81 static void getargs __P((int, char *[]));
82 static void count __P((void));
83 static void putem __P((void));
84
85 int
86 main(ac, av)
87 int ac;
88 char *av[];
89 {
90 int i;
91 int32_t nc;
92
93 getargs(ac, av);
94 if ((inf = fopen(infile, "r")) == NULL) {
95 perror(infile);
96 exit(1);
97 }
98 count();
99 /*
100 * allocate space for pointers.
101 */
102 CC_D.offsets = (off_t *)calloc(CC_D.num_cards + 1, sizeof (off_t));
103 CH_D.offsets = (off_t *)calloc(CH_D.num_cards + 1, sizeof (off_t));
104 fseek(inf, 0L, SEEK_SET);
105 if ((outf = fopen(outfile, "w")) == NULL) {
106 perror(outfile);
107 exit(0);
108 }
109
110 /*
111 * these fields will be overwritten after the offsets are calculated,
112 * so byte-order doesn't matter yet.
113 */
114 fwrite(&nc, sizeof(nc), 1, outf);
115 fwrite(&nc, sizeof(nc), 1, outf);
116 fwrite(CC_D.offsets, sizeof (off_t), CC_D.num_cards, outf);
117 fwrite(CH_D.offsets, sizeof (off_t), CH_D.num_cards, outf);
118
119 /*
120 * write out the cards themselves (calculating the offsets).
121 */
122 putem();
123
124 fclose(inf);
125 fseek(outf, 0, SEEK_SET);
126
127 /* number of community chest cards first... */
128 nc = htobe32(CC_D.num_cards);
129 fwrite(&nc, sizeof(nc), 1, outf);
130 /* ... then number of chance cards. */
131 nc = htobe32(CH_D.num_cards);
132 fwrite(&nc, sizeof(nc), 1, outf);
133
134 /* convert offsets to big-endian byte order */
135 for (i = 0; i < CC_D.num_cards; i++)
136 HTOBE64(CC_D.offsets[i]);
137 for (i = 0; i < CH_D.num_cards; i++)
138 HTOBE64(CH_D.offsets[i]);
139 /* then dump the offsets out */
140 fwrite(CC_D.offsets, sizeof (off_t), CC_D.num_cards, outf);
141 fwrite(CH_D.offsets, sizeof (off_t), CH_D.num_cards, outf);
142
143 fclose(outf);
144 printf("There were %d com. chest and %d chance cards\n",
145 CC_D.num_cards, CH_D.num_cards);
146 exit(0);
147 }
148
149 static void
150 getargs(ac, av)
151 int ac;
152 char *av[];
153 {
154 if (ac > 1)
155 infile = av[1];
156 if (ac > 2)
157 outfile = av[2];
158 }
159
160 /*
161 * count the cards
162 */
163 static void
164 count()
165 {
166 bool newline;
167 DECK *in_deck;
168 int c;
169
170 newline = TRUE;
171 in_deck = &CC_D;
172 while ((c=getc(inf)) != EOF)
173 if (newline && c == '%') {
174 newline = FALSE;
175 in_deck->num_cards++;
176 if (getc(inf) == '-')
177 in_deck = &CH_D;
178 }
179 else
180 newline = (c == '\n');
181 in_deck->num_cards++;
182 }
183
184 /*
185 * put strings in the file
186 */
187 static void
188 putem()
189 {
190 bool newline;
191 DECK *in_deck;
192 int c;
193 int num;
194
195 in_deck = &CC_D;
196 CC_D.num_cards = 1;
197 CH_D.num_cards = 0;
198 CC_D.offsets[0] = ftell(outf);
199 putc(getc(inf), outf);
200 putc(getc(inf), outf);
201 for (num = 0; (c=getc(inf)) != '\n'; )
202 num = num * 10 + (c - '0');
203 putw(num, outf);
204 newline = FALSE;
205 while ((c=getc(inf)) != EOF)
206 if (newline && c == '%') {
207 putc('\0', outf);
208 newline = FALSE;
209 if (getc(inf) == '-')
210 in_deck = &CH_D;
211 while (getc(inf) != '\n')
212 continue;
213 in_deck->offsets[in_deck->num_cards++] = ftell(outf);
214 if ((c=getc(inf)) == EOF)
215 break;
216 putc(c, outf);
217 putc(c = getc(inf), outf);
218 for (num = 0; (c=getc(inf)) != EOF && c != '\n'; )
219 num = num * 10 + (c - '0');
220 putw(num, outf);
221 }
222 else {
223 putc(c, outf);
224 newline = (c == '\n');
225 }
226 putc('\0', outf);
227 }