summaryrefslogtreecommitdiff
path: root/apps/plugins/lib/highscore.c
blob: 9fbcdf25a313098a2ad6b0acf9aaf74f36c8de9c (plain)
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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 Linus Nielsen Feltzing
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "plugin.h"
#include "highscore.h"

static struct plugin_api *rb;

void highscore_init(struct plugin_api* newrb)
{
    rb = newrb;
}

int highscore_save(char *filename, struct highscore *scores, int num_scores)
{
    int i;
    int fd;
    int rc;
    char buf[80];

    fd = rb->open(filename, O_WRONLY|O_CREAT);
    if(fd < 0)
        return -1;
    
    for(i = 0;i < num_scores;i++)
    {
        rb->snprintf(buf, sizeof(buf)-1, "%s:%d:%d\n",
                     scores[i].name, scores[i].score, scores[i].level);
        rc = rb->write(fd, buf, rb->strlen(buf));
        if(rc < 0)
        {
            rb->close(fd);
            return -2;
        }
    }
    rb->close(fd);
    return 0;
}

int highscore_load(char *filename, struct highscore *scores, int num_scores)
{
    int i;
    int fd;
    char buf[80];
    char *name, *score, *level;
    char *ptr;

    fd = rb->open(filename, O_RDONLY);
    if(fd < 0)
        return -1;

    rb->memset(scores, 0, sizeof(struct highscore)*num_scores);

    i = -1;
    while(rb->read_line(fd, buf, sizeof(buf)-1) && i < num_scores)
    {
        i++;
        
        DEBUGF("%s\n", buf);
        name = buf;
        ptr = rb->strchr(buf, ':');
        if ( !ptr )
            continue;
        *ptr = 0;
        ptr++;
        
        rb->strncpy(scores[i].name, name, sizeof(scores[i].name));
        
        DEBUGF("%s\n", scores[i].name);
        score = ptr;
        
        ptr = rb->strchr(ptr, ':');
        if ( !ptr )
            continue;
        *ptr = 0;
        ptr++;
        
        scores[i].score = rb->atoi(score);
        
        level = ptr;
        scores[i].level = rb->atoi(level);
    }    
    return 0;
}