summaryrefslogtreecommitdiffstats
path: root/mbbsd/topsong.c
blob: 3ea60dbf123f8e8b280588d87e89862884c3d047 (plain) (blame)
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
/* $Id$ */
#include "bbs.h"

#define MAX_SONGS 300
#define QCAST     int (*)(const void *, const void *)

typedef struct songcmp_t {
    char            name[100];
    char            cname[100];
    int             count;
}               songcmp_t;


static int
count_cmp(songcmp_t * b, songcmp_t * a)
{
    return (a->count - b->count);
}

int
topsong(void)
{
    more(FN_TOPSONG, YEA);
    return 0;
}


void
sortsong(void)
{
    FILE           *fo, *fp = fopen(BBSHOME "/" FN_USSONG, "r");
    songcmp_t       songs[MAX_SONGS + 1];
    int             n;
    char            buf[256], cbuf[256];
    int totalcount = 0;

    memset(songs, 0, sizeof(songs));
    if (!fp)
    return;
    if (!(fo = fopen(FN_TOPSONG, "w"))) {
    fclose(fp);
    return;
    }
    totalcount = 0;
    /* XXX: 除了前 MAX_SONGS 首, 剩下不會排序 */
    while (fgets(buf, 200, fp)) {
    chomp(buf);
    strip_blank(cbuf, buf);
    if (!cbuf[0] || !isprint2((int)cbuf[0]))
        continue;

    for (n = 0; n < MAX_SONGS && songs[n].name[0]; n++)
        if (!strcmp(songs[n].cname, cbuf))
        break;
    strlcpy(songs[n].name, buf, sizeof(songs[n].name));
    strlcpy(songs[n].cname, cbuf, sizeof(songs[n].cname));
    songs[n].count++;
    totalcount++;
    }
    qsort(songs, MAX_SONGS, sizeof(songcmp_t), (QCAST) count_cmp);
    fprintf(fo,
        "    " ANSI_COLOR(36) "──" ANSI_COLOR(37) "名次" ANSI_COLOR(36) "──────" ANSI_COLOR(37) "歌"
        "  名" ANSI_COLOR(36) "───────────" ANSI_COLOR(37) "次數" ANSI_COLOR(36) ""
        "──" ANSI_COLOR(32) "共%d次" ANSI_COLOR(36) "──" ANSI_RESET "\n", totalcount);
    for (n = 0; n < 100 && songs[n].name[0]; n++) {
    fprintf(fo, "      %5d. %-38.38s %4d " ANSI_COLOR(32) "[%.2f]" ANSI_RESET "\n", n + 1,
        songs[n].name, songs[n].count,
        (float)songs[n].count / totalcount);
    }
    fclose(fp);
    fclose(fo);
}