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