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