BSD 4_2 release
[unix-history] / usr / src / games / monop / initdeck.c
CommitLineData
5244a77d
C
1# include <stdio.h>
2# include "deck.h"
3
4/*
5 * This program initializes the card files for monopoly.
6 * It reads in a data file with Com. Chest cards, followed by
7 * the Chance card. The two are seperated by a line of "%-".
8 * All other cards are seperated by lines of "%%". In the front
9 * of the file is the data for the decks in the same order.
10 * This includes the seek pointer for the start of each card.
11 * All cards start with their execution code, followed by the
12 * string to print, terminated with a null byte.
13 */
14
15# define TRUE 1
16# define FALSE 0
17
18# define bool char
19# define reg register
20
21char *infile = "cards.inp", /* input file */
22 *outfile = "cards.pck"; /* "packed" file */
23
24long ftell();
25
26DECK deck[2];
27
28FILE *inf, *outf;
29
30main(ac, av)
31int ac;
32char *av[]; {
33
34 getargs(ac, av);
35 if ((inf = fopen(infile, "r")) == NULL) {
36 perror(infile);
37 exit(1);
38 }
39 count();
40 /*
41 * allocate space for pointers.
42 */
43 CC_D.offsets = calloc(CC_D.num_cards + 1, sizeof (long));
44 CH_D.offsets = calloc(CH_D.num_cards + 1, sizeof (long));
45 fseek(inf, 0L, 0);
46 if ((outf = fopen(outfile, "w")) == NULL) {
47 perror(outfile);
48 exit(0);
49 }
50
51 fwrite(deck, sizeof (DECK), 2, outf);
52 fwrite(CC_D.offsets, sizeof (long), CC_D.num_cards, outf);
53 fwrite(CH_D.offsets, sizeof (long), CH_D.num_cards, outf);
54 putem();
55
56 fclose(inf);
57 fseek(outf, 0, 0L);
58 fwrite(deck, sizeof (DECK), 2, outf);
59 fwrite(CC_D.offsets, sizeof (long), CC_D.num_cards, outf);
60 fwrite(CH_D.offsets, sizeof (long), CH_D.num_cards, outf);
61 fclose(outf);
62 printf("There were %d com. chest and %d chance cards\n", CC_D.num_cards, CH_D.num_cards);
63 exit(0);
64}
65
66getargs(ac, av)
67int ac;
68char *av[]; {
69
70 if (ac > 2) {
71 infile = av[2] ? av[2] : infile;
72 if (ac > 3)
73 outfile = av[3];
74 }
75}
76/*
77 * count the cards
78 */
79count() {
80
81 reg bool newline;
82 reg DECK *in_deck;
83 reg char c;
84
85 newline = TRUE;
86 in_deck = &CC_D;
87 while ((c=getc(inf)) != EOF)
88 if (newline && c == '%') {
89 newline = FALSE;
90 in_deck->num_cards++;
91 if (getc(inf) == '-')
92 in_deck = &CH_D;
93 }
94 else
95 newline = (c == '\n');
96 in_deck->num_cards++;
97}
98/*
99 * put strings in the file
100 */
101putem() {
102
103 reg bool newline;
104 reg DECK *in_deck;
105 reg char c;
106 reg int num;
107
108 in_deck = &CC_D;
109 CC_D.num_cards = 1;
110 CH_D.num_cards = 0;
111 CC_D.offsets[0] = ftell(outf);
112 putc(getc(inf), outf);
113 putc(getc(inf), outf);
114 for (num = 0; (c=getc(inf)) != '\n'; )
115 num = num * 10 + (c - '0');
116 putw(num, outf);
117 newline = FALSE;
118 while ((c=getc(inf)) != EOF)
119 if (newline && c == '%') {
120 putc('\0', outf);
121 newline = FALSE;
122 if (getc(inf) == '-')
123 in_deck = &CH_D;
124 while (getc(inf) != '\n')
125 continue;
126 in_deck->offsets[in_deck->num_cards++] = ftell(outf);
127 if ((c=getc(inf)) == EOF)
128 break;
129 putc(c, outf);
130 putc(c = getc(inf), outf);
131 for (num = 0; (c=getc(inf)) != EOF && c != '\n'; )
132 num = num * 10 + (c - '0');
133 putw(num, outf);
134 }
135 else {
136 putc(c, outf);
137 newline = (c == '\n');
138 }
139 putc('\0', outf);
140}