File: showscore.c

package info (click to toggle)
xsok 1.02-14
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,492 kB
  • ctags: 532
  • sloc: ansic: 3,963; sh: 181; makefile: 110
file content (111 lines) | stat: -rw-r--r-- 2,548 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>

#define MAGICH	0x741d	/* magic of xsok version 1 highscore file */

static void portable_to_internal(long *args, unsigned char *p, int num) {
    do {
	int j;
	*args = 0;
	for (j = 0; j < 4; ++j)
	    *args += (long)p[3-j] << (j << 3);
	++args;
	p += 4;
    } while (--num);
}
int highscore[300];
static void ReadHighscores(const char *levelfile) {
    FILE *fp;
    char filename[256], base_name[32], *s;
    int i;

    memset(highscore, 0, sizeof(highscore));
    for (i = 100; i < 300; ++i)
	    highscore[i] = 0x7fffffff;

    strcpy(base_name, levelfile);
    if (!(s = strchr(base_name, '.')))
	return;
    strcpy(s+1, "score");
    sprintf(filename, "%s/%s", XSOKSAVE, base_name);
    if ((fp = fopen(filename, "rb"))) {
	long p[300];
	unsigned char ss[1200];
	fread(ss, 4, 300, fp);
	portable_to_internal(p, ss, 300);	
	for (i = 0; i < 300; ++i)
	    highscore[i] = p[i];
	fclose(fp);
    }
    highscore[0] = MAGICH;
}


static int brief = 1;
#define NARGS 16

static void checkfile(const char *filename) {
    FILE *fp;
    long p[NARGS];
    unsigned char s[256];
    char type[10], *z;
    int namelen, level;

    if (!(fp = fopen(filename, "rb")))
	return;
    fread(s, 4, NARGS, fp);
    portable_to_internal(p, s, NARGS);
    if (p[0] != 0x741b)
	return;
    fread(type, 1, 8, fp);
    level = p[6];
    type[8] = '\0';
    namelen = getc(fp);
    fread(s, 1, namelen, fp);
    s[namelen] = '\0';

    if (strrchr(filename, '/'))
	filename = strrchr(filename, '/') + 1;
    ReadHighscores(filename);

    z = ctime(&p[4]);
    if (strchr(z, '\n'))
	*strchr(z, '\n') = '\0';

    if (!brief) {
	printf("%-14s %-7s Level %2d:  Score: %5ld, Pushes: %4ld, Moves: %4ld\n",
	       filename, type, level, p[1], p[2], p[3]);
#if 0
	if (!p[5])	/* unsolved */
	    printf("\n");
	else
#endif
	    printf("saved %s by %s\n", z, s);
    } else {
	int c1, c2, c3;
	c1 = p[5] && highscore[level]     == p[1] ? '*' : ' ';
	c2 = p[5] && highscore[level+200] == p[2] ? '*' : ' ';
	c3 = p[5] && highscore[level+100] == p[3] ? '*' : ' ';
	printf("%2d %5ld%c %4ld%c %4ld%c  %s\n", level, p[1], c1, p[2], c2, p[3], c3, s);
    }
}

int main(int argc, char *argv[]) {
    int i;
    if (argc == 1) {
	fprintf(stderr, "usage: showscore [-b] (savefiles)\n");
	exit(1);
    }
    i = 1;
    if (!strcmp(argv[i], "-b")) {
	brief = 0;
	++i;
    } else
	printf("Lv Score Pushes Moves  Player\n");
    for (; i < argc; ++i)
	checkfile(argv[i]);
    return 0;
}