1
0
mirror of https://github.com/openbsd/src.git synced 2025-01-10 06:47:55 -08:00

Store the card deck using fixed sizes and network byte order

This commit is contained in:
tholo 1996-10-06 03:20:44 +00:00
parent a5e1d7896c
commit 6160caa11a
4 changed files with 75 additions and 24 deletions

View File

@ -69,8 +69,24 @@ file_err:
perror(cardfile);
exit(1);
}
if (fread(deck, sizeof (DECK), 2, deckf) != 2)
if (fread(&deck[0].num_cards, sizeof(deck[0].num_cards), 1, deckf) != 1)
goto file_err;
if (fread(&deck[0].last_card, sizeof(deck[0].last_card), 1, deckf) != 1)
goto file_err;
if (fread(&deck[0].gojf_used, sizeof(deck[0].gojf_used), 1, deckf) != 1)
goto file_err;
deck[0].num_cards = ntohs(deck[0].num_cards);
deck[0].last_card = ntohs(deck[0].last_card);
if (fread(&deck[1].num_cards, sizeof(deck[1].num_cards), 1, deckf) != 1)
goto file_err;
if (fread(&deck[1].last_card, sizeof(deck[1].last_card), 1, deckf) != 1)
goto file_err;
if (fread(&deck[1].gojf_used, sizeof(deck[1].gojf_used), 1, deckf) != 1)
goto file_err;
deck[1].num_cards = ntohs(deck[1].num_cards);
deck[1].last_card = ntohs(deck[1].last_card);
set_up(&CC_D);
set_up(&CH_D);
}
@ -83,10 +99,13 @@ DECK *dp; {
reg int r1, r2;
int i;
dp->offsets = (long *) calloc(sizeof (long), dp->num_cards);
if (fread(dp->offsets, sizeof(long), dp->num_cards, deckf) != dp->num_cards) {
perror(cardfile);
exit(1);
dp->offsets = (int32_t *) calloc(sizeof (int32_t), dp->num_cards);
for (i = 0 ; i < dp->num_cards ; i++) {
if (fread(&dp->offsets[i], sizeof(dp->offsets[i]), 1, deckf) != 1) {
perror(cardfile);
exit(1);
}
dp->offsets[i] = ntohl(dp->offsets[i]);
}
dp->last_card = 0;
dp->gojf_used = FALSE;
@ -107,7 +126,7 @@ get_card(dp)
DECK *dp; {
reg char type_maj, type_min;
reg int num;
int16_t num;
int i, per_h, per_H, num_h, num_H;
OWN *op;
@ -117,7 +136,8 @@ DECK *dp; {
type_maj = getc(deckf);
} while (dp->gojf_used && type_maj == GOJF);
type_min = getc(deckf);
num = getw(deckf);
fread(&num, sizeof(num), 1, deckf);
num = ntohs(num);
printmes();
switch (type_maj) {
case '+': /* get money */

View File

@ -35,16 +35,16 @@
* @(#)deck.h 8.1 (Berkeley) 5/31/93
*/
# define bool char
# define bool int8_t
# define CC_D deck[0]
# define CH_D deck[1]
struct dk_st { /* deck description structure */
int num_cards; /* number of cards in deck */
int last_card; /* number of last card picked */
int16_t num_cards; /* number of cards in deck */
int16_t last_card; /* number of last card picked */
bool gojf_used; /* set if gojf card out of deck */
long *offsets; /* offests for start of cards */
int32_t *offsets; /* offests for start of cards */
};
typedef struct dk_st DECK;

View File

@ -64,7 +64,7 @@ static char rcsid[] = "$NetBSD: initdeck.c,v 1.3 1995/03/23 08:34:43 cgd Exp $";
# define TRUE 1
# define FALSE 0
# define bool char
# define bool int8_t
# define reg register
char *infile = "cards.inp", /* input file */
@ -80,6 +80,7 @@ FILE *inf, *outf;
main(ac, av)
int ac;
char *av[]; {
int n;
getargs(ac, av);
if ((inf = fopen(infile, "r")) == NULL) {
@ -90,24 +91,52 @@ char *av[]; {
/*
* allocate space for pointers.
*/
CC_D.offsets = (long *)calloc(CC_D.num_cards + 1, sizeof (long));
CH_D.offsets = (long *)calloc(CH_D.num_cards + 1, sizeof (long));
CC_D.offsets = (int32_t *)calloc(CC_D.num_cards + 1, sizeof (int32_t));
CH_D.offsets = (int32_t *)calloc(CH_D.num_cards + 1, sizeof (int32_t));
fseek(inf, 0L, 0);
if ((outf = fopen(outfile, "w")) == NULL) {
perror(outfile);
exit(0);
}
fwrite(deck, sizeof (DECK), 2, outf);
fwrite(CC_D.offsets, sizeof (long), CC_D.num_cards, outf);
fwrite(CH_D.offsets, sizeof (long), CH_D.num_cards, outf);
fwrite(&deck[0].num_cards, sizeof(deck[0].num_cards), 1, outf);
fwrite(&deck[0].last_card, sizeof(deck[0].last_card), 1, outf);
fwrite(&deck[0].gojf_used, sizeof(deck[0].gojf_used), 1, outf);
fwrite(&deck[0].num_cards, sizeof(deck[0].num_cards), 1, outf);
fwrite(&deck[0].last_card, sizeof(deck[0].last_card), 1, outf);
fwrite(&deck[0].gojf_used, sizeof(deck[0].gojf_used), 1, outf);
fwrite(CC_D.offsets, sizeof(CC_D.offsets[0]), CC_D.num_cards, outf);
fwrite(CH_D.offsets, sizeof(CH_D.offsets[0]), CH_D.num_cards, outf);
putem();
fclose(inf);
fseek(outf, 0, 0L);
fwrite(deck, sizeof (DECK), 2, outf);
fwrite(CC_D.offsets, sizeof (long), CC_D.num_cards, outf);
fwrite(CH_D.offsets, sizeof (long), CH_D.num_cards, outf);
deck[0].num_cards = htons(deck[0].num_cards);
fwrite(&deck[0].num_cards, sizeof(deck[0].num_cards), 1, outf);
deck[0].last_card = htons(deck[0].last_card);
fwrite(&deck[0].last_card, sizeof(deck[0].last_card), 1, outf);
fwrite(&deck[0].gojf_used, sizeof(deck[0].gojf_used), 1, outf);
deck[0].num_cards = ntohs(deck[0].num_cards);
deck[1].num_cards = htons(deck[1].num_cards);
fwrite(&deck[1].num_cards, sizeof(deck[1].num_cards), 1, outf);
deck[1].last_card = htons(deck[1].last_card);
fwrite(&deck[1].last_card, sizeof(deck[1].last_card), 1, outf);
fwrite(&deck[1].gojf_used, sizeof(deck[1].gojf_used), 1, outf);
deck[1].num_cards = ntohs(deck[1].num_cards);
for (n = 0 ; n < CC_D.num_cards ; n++) {
CC_D.offsets[n] = htonl(CC_D.offsets[n]);
fwrite(&CC_D.offsets[n], sizeof(CC_D.offsets[n]), 1, outf);
}
for (n = 0 ; n < CH_D.num_cards ; n++) {
CH_D.offsets[n] = htonl(CH_D.offsets[n]);
fwrite(&CH_D.offsets[n], sizeof(CH_D.offsets[n]), 1, outf);
}
fclose(outf);
printf("There were %d com. chest and %d chance cards\n", CC_D.num_cards, CH_D.num_cards);
exit(0);
@ -153,7 +182,7 @@ putem() {
reg bool newline;
reg DECK *in_deck;
reg char c;
reg int num;
int16_t num;
in_deck = &CC_D;
CC_D.num_cards = 1;
@ -163,7 +192,8 @@ putem() {
putc(getc(inf), outf);
for (num = 0; (c=getc(inf)) != '\n'; )
num = num * 10 + (c - '0');
putw(num, outf);
num = htons(num);
fwrite(&num, sizeof(num), 1, outf);
newline = FALSE;
while ((c=getc(inf)) != EOF)
if (newline && c == '%') {
@ -180,7 +210,8 @@ putem() {
putc(c = getc(inf), outf);
for (num = 0; (c=getc(inf)) != EOF && c != '\n'; )
num = num * 10 + (c - '0');
putw(num, outf);
num = htons(num);
fwrite(&num, sizeof(num), 1, outf);
}
else {
putc(c, outf);

View File

@ -41,7 +41,7 @@
# define reg register
# define shrt char
# define bool char
# define bool int8_t
# define unsgn unsigned
# define TRUE (1)