Commit | Line | Data |
---|---|---|
ec5c7841 | 1 | /* |
d99e6414 | 2 | * Copyright (c) 1980 Regents of the University of California. |
ec5c7841 KB |
3 | * All rights reserved. |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
1c15e888 C |
6 | * provided that: (1) source distributions retain this entire copyright |
7 | * notice and comment, and (2) distributions including binaries display | |
8 | * the following acknowledgement: ``This product includes software | |
9 | * developed by the University of California, Berkeley and its contributors'' | |
10 | * in the documentation or other materials provided with the distribution | |
11 | * and in all advertising materials mentioning features or use of this | |
12 | * software. Neither the name of the University nor the names of its | |
13 | * contributors may be used to endorse or promote products derived | |
d99e6414 KB |
14 | * from this software without specific prior written permission. |
15 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
16 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
1c15e888 | 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
ec5c7841 KB |
18 | */ |
19 | ||
20 | #ifndef lint | |
21 | char copyright[] = | |
d99e6414 | 22 | "@(#) Copyright (c) 1980 Regents of the University of California.\n\ |
ec5c7841 KB |
23 | All rights reserved.\n"; |
24 | #endif /* not lint */ | |
25 | ||
26 | #ifndef lint | |
1c15e888 | 27 | static char sccsid[] = "@(#)initdeck.c 5.5 (Berkeley) 6/1/90"; |
ec5c7841 KB |
28 | #endif /* not lint */ |
29 | ||
30 | # include <stdio.h> | |
31 | # include "deck.h" | |
32 | ||
33 | /* | |
34 | * This program initializes the card files for monopoly. | |
35 | * It reads in a data file with Com. Chest cards, followed by | |
36 | * the Chance card. The two are seperated by a line of "%-". | |
37 | * All other cards are seperated by lines of "%%". In the front | |
38 | * of the file is the data for the decks in the same order. | |
39 | * This includes the seek pointer for the start of each card. | |
40 | * All cards start with their execution code, followed by the | |
41 | * string to print, terminated with a null byte. | |
42 | */ | |
43 | ||
44 | # define TRUE 1 | |
45 | # define FALSE 0 | |
46 | ||
47 | # define bool char | |
48 | # define reg register | |
49 | ||
50 | char *infile = "cards.inp", /* input file */ | |
51 | *outfile = "cards.pck"; /* "packed" file */ | |
52 | ||
2497392a KB |
53 | extern long ftell(); |
54 | extern char *calloc(); | |
ec5c7841 KB |
55 | |
56 | DECK deck[2]; | |
57 | ||
58 | FILE *inf, *outf; | |
59 | ||
60 | main(ac, av) | |
61 | int ac; | |
62 | char *av[]; { | |
63 | ||
64 | getargs(ac, av); | |
65 | if ((inf = fopen(infile, "r")) == NULL) { | |
66 | perror(infile); | |
67 | exit(1); | |
68 | } | |
69 | count(); | |
70 | /* | |
71 | * allocate space for pointers. | |
72 | */ | |
2497392a KB |
73 | CC_D.offsets = (long *)calloc(CC_D.num_cards + 1, sizeof (long)); |
74 | CH_D.offsets = (long *)calloc(CH_D.num_cards + 1, sizeof (long)); | |
ec5c7841 KB |
75 | fseek(inf, 0L, 0); |
76 | if ((outf = fopen(outfile, "w")) == NULL) { | |
77 | perror(outfile); | |
78 | exit(0); | |
79 | } | |
80 | ||
81 | fwrite(deck, sizeof (DECK), 2, outf); | |
82 | fwrite(CC_D.offsets, sizeof (long), CC_D.num_cards, outf); | |
83 | fwrite(CH_D.offsets, sizeof (long), CH_D.num_cards, outf); | |
84 | putem(); | |
85 | ||
86 | fclose(inf); | |
87 | fseek(outf, 0, 0L); | |
88 | fwrite(deck, sizeof (DECK), 2, outf); | |
89 | fwrite(CC_D.offsets, sizeof (long), CC_D.num_cards, outf); | |
90 | fwrite(CH_D.offsets, sizeof (long), CH_D.num_cards, outf); | |
91 | fclose(outf); | |
92 | printf("There were %d com. chest and %d chance cards\n", CC_D.num_cards, CH_D.num_cards); | |
93 | exit(0); | |
94 | } | |
95 | ||
96 | getargs(ac, av) | |
97 | int ac; | |
98 | char *av[]; { | |
99 | ||
7173c09c KB |
100 | if (ac > 1) |
101 | infile = av[1]; | |
102 | if (ac > 2) | |
103 | outfile = av[2]; | |
ec5c7841 | 104 | } |
7173c09c | 105 | |
ec5c7841 KB |
106 | /* |
107 | * count the cards | |
108 | */ | |
109 | count() { | |
110 | ||
111 | reg bool newline; | |
112 | reg DECK *in_deck; | |
113 | reg char c; | |
114 | ||
115 | newline = TRUE; | |
116 | in_deck = &CC_D; | |
117 | while ((c=getc(inf)) != EOF) | |
118 | if (newline && c == '%') { | |
119 | newline = FALSE; | |
120 | in_deck->num_cards++; | |
121 | if (getc(inf) == '-') | |
122 | in_deck = &CH_D; | |
123 | } | |
124 | else | |
125 | newline = (c == '\n'); | |
126 | in_deck->num_cards++; | |
127 | } | |
128 | /* | |
129 | * put strings in the file | |
130 | */ | |
131 | putem() { | |
132 | ||
133 | reg bool newline; | |
134 | reg DECK *in_deck; | |
135 | reg char c; | |
136 | reg int num; | |
137 | ||
138 | in_deck = &CC_D; | |
139 | CC_D.num_cards = 1; | |
140 | CH_D.num_cards = 0; | |
141 | CC_D.offsets[0] = ftell(outf); | |
142 | putc(getc(inf), outf); | |
143 | putc(getc(inf), outf); | |
144 | for (num = 0; (c=getc(inf)) != '\n'; ) | |
145 | num = num * 10 + (c - '0'); | |
146 | putw(num, outf); | |
147 | newline = FALSE; | |
148 | while ((c=getc(inf)) != EOF) | |
149 | if (newline && c == '%') { | |
150 | putc('\0', outf); | |
151 | newline = FALSE; | |
152 | if (getc(inf) == '-') | |
153 | in_deck = &CH_D; | |
154 | while (getc(inf) != '\n') | |
155 | continue; | |
156 | in_deck->offsets[in_deck->num_cards++] = ftell(outf); | |
157 | if ((c=getc(inf)) == EOF) | |
158 | break; | |
159 | putc(c, outf); | |
160 | putc(c = getc(inf), outf); | |
161 | for (num = 0; (c=getc(inf)) != EOF && c != '\n'; ) | |
162 | num = num * 10 + (c - '0'); | |
163 | putw(num, outf); | |
164 | } | |
165 | else { | |
166 | putc(c, outf); | |
167 | newline = (c == '\n'); | |
168 | } | |
169 | putc('\0', outf); | |
170 | } |