1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32: #ifdef __NetBSD__
33: #include <sys/cdefs.h>
34: #ifndef lint
35: __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
36: The Regents of the University of California. All rights reserved.\n");
37: #endif
38:
39: #ifndef lint
40: #if 0
41: static char sccsid[] = "@(#)initdeck.c 8.1 (Berkeley) 5/31/93";
42: #else
43: __RCSID("$NetBSD: initdeck.c,v 1.15 2003/08/07 09:37:28 agc Exp $");
44: #endif
45: #endif
46: #endif
47:
48: #include <stdio.h>
49: #include <stdlib.h>
50: #include <string.h>
51: #include <sys/types.h>
52: #include "deck.h"
53:
54: #ifndef u_int32_t
55: #define u_int32_t unsigned int
56: #endif
57:
58: static u_int32_t
59: h2nl(u_int32_t h)
60: {
61: unsigned char c[4];
62: u_int32_t rv;
63:
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);
69:
70: return (rv);
71: }
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84: #define TRUE 1
85: #define FALSE 0
86:
87: #define bool char
88:
89: const char *infile = "cards.inp",
90: *outfile = "cards.pck";
91:
92: DECK deck[2];
93:
94: FILE *inf, *outf;
95:
96:
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);
102:
103: int
104: main(ac, av)
105: int ac;
106: char *av[];
107: {
108: int i, nc;
109:
110:
111: if (sizeof(int) != 4) {
112: fprintf(stderr, "sizeof(int) != 4\n");
113: exit(1);
114: }
115:
116: getargs(ac, av);
117: if ((inf = fopen(infile, "r")) == NULL) {
118: perror(infile);
119: exit(1);
120: }
121: count();
122:
123:
124:
125: CC_D.offsets = calloc(CC_D.num_cards + 1, 8);
126: CH_D.offsets = calloc(CH_D.num_cards + 1, 8);
127: if (CC_D.offsets == NULL || CH_D.offsets == NULL) {
128: fprintf(stderr, "out of memory\n");
129: exit(1);
130: }
131: fseek(inf, 0L, SEEK_SET);
132: if ((outf = fopen(outfile, "w")) == NULL) {
133: perror(outfile);
134: exit(1);
135: }
136:
137:
138:
139:
140:
141: fwrite(&nc, sizeof(nc), 1, outf);
142: fwrite(&nc, sizeof(nc), 1, outf);
143: fwrite(CC_D.offsets, 8, CC_D.num_cards, outf);
144: fwrite(CH_D.offsets, 8, CH_D.num_cards, outf);
145:
146:
147:
148:
149: putem();
150:
151: fclose(inf);
152: fseek(outf, 0, SEEK_SET);
153:
154:
155: nc = h2nl(CC_D.num_cards);
156: fwrite(&nc, sizeof(nc), 1, outf);
157:
158: nc = h2nl(CH_D.num_cards);
159: fwrite(&nc, sizeof(nc), 1, outf);
160:
161:
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);
166:
167: fflush(outf);
168: if (ferror(outf)) {
169: perror(outfile);
170: exit(1);
171: }
172: fclose(outf);
173: printf("There were %d com. chest and %d chance cards\n",
174: CC_D.num_cards, CH_D.num_cards);
175: exit(0);
176: }
177:
178: static void
179: getargs(ac, av)
180: int ac;
181: char *av[];
182: {
183: if (ac > 1)
184: infile = av[1];
185: if (ac > 2)
186: outfile = av[2];
187: }
188:
189:
190:
191:
192: static void
193: count()
194: {
195: bool newline;
196: DECK *in_deck;
197: int c;
198:
199: newline = TRUE;
200: in_deck = &CC_D;
201: while ((c=getc(inf)) != EOF)
202: if (newline && c == '%') {
203: newline = FALSE;
204: in_deck->num_cards++;
205: if (getc(inf) == '-')
206: in_deck = &CH_D;
207: }
208: else
209: newline = (c == '\n');
210: in_deck->num_cards++;
211: }
212:
213:
214:
215:
216: static void
217: putem()
218: {
219: bool newline;
220: DECK *in_deck;
221: int c;
222: int num;
223:
224: in_deck = &CC_D;
225: CC_D.num_cards = 1;
226: CH_D.num_cards = 0;
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);
233: newline = FALSE;
234: while ((c=getc(inf)) != EOF)
235: if (newline && c == '%') {
236: putc('\0', outf);
237: newline = FALSE;
238: if (getc(inf) == '-')
239: in_deck = &CH_D;
240: while (getc(inf) != '\n')
241: continue;
242: in_deck->offsets[in_deck->num_cards++] = ftell(outf);
243: if ((c=getc(inf)) == EOF)
244: break;
245: putc(c, outf);
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);
250: }
251: else {
252: putc(c, outf);
253: newline = (c == '\n');
254: }
255: putc('\0', outf);
256: }
257:
258:
259:
260:
261:
262:
263: static void
264: fwrite_be_offt(off, f)
265: off_t off;
266: FILE *f;
267: {
268: int i;
269: unsigned char c[8];
270:
271: for (i = 7; i >= 0; i--) {
272: c[i] = off & 0xff;
273: off >>= 8;
274: }
275: fwrite(c, sizeof(c), 1, f);
276: }