Fix voicing of incorrect run time (top time instead of run time). Simplify runtime...
[kugel-rb.git] / apps / plugins / lib / highscore.c
blobf475651eabf330d068fda69b59e117ae67665183
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Linus Nielsen Feltzing
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "plugin.h"
22 #include "highscore.h"
24 static const struct plugin_api *rb;
26 void highscore_init(const struct plugin_api* newrb)
28 rb = newrb;
31 int highscore_save(char *filename, struct highscore *scores, int num_scores)
33 int i;
34 int fd;
35 int rc;
36 char buf[80];
38 fd = rb->open(filename, O_WRONLY|O_CREAT);
39 if(fd < 0)
40 return -1;
42 for(i = 0;i < num_scores;i++)
44 rb->snprintf(buf, sizeof(buf)-1, "%s:%d:%d\n",
45 scores[i].name, scores[i].score, scores[i].level);
46 rc = rb->write(fd, buf, rb->strlen(buf));
47 if(rc < 0)
49 rb->close(fd);
50 return -2;
53 rb->close(fd);
54 return 0;
57 int highscore_load(char *filename, struct highscore *scores, int num_scores)
59 int i;
60 int fd;
61 char buf[80];
62 char *name, *score, *level;
63 char *ptr;
65 fd = rb->open(filename, O_RDONLY);
67 rb->memset(scores, 0, sizeof(struct highscore)*(num_scores+1));
69 if(fd < 0)
70 return -1;
72 i = -1;
73 while(rb->read_line(fd, buf, sizeof(buf)-1) && i < num_scores)
75 i++;
77 DEBUGF("%s\n", buf);
78 name = buf;
79 ptr = rb->strchr(buf, ':');
80 if ( !ptr )
81 continue;
82 *ptr = 0;
83 ptr++;
85 rb->strncpy(scores[i].name, name, sizeof(scores[i].name));
87 DEBUGF("%s\n", scores[i].name);
88 score = ptr;
90 ptr = rb->strchr(ptr, ':');
91 if ( !ptr )
92 continue;
93 *ptr = 0;
94 ptr++;
96 scores[i].score = rb->atoi(score);
98 level = ptr;
99 scores[i].level = rb->atoi(level);
101 return 0;
104 int highscore_update(int score, int level, struct highscore *scores, int num_scores)
106 int i, j;
107 int new = 0;
109 /* look through the scores and see if this one is in the top ones */
110 for(i = num_scores-1;i >= 0; i--)
112 if ((score > scores[i].score))
114 /* Move the rest down one... */
115 if (i > 0)
117 for (j=1; j<=i; j++)
119 rb->memcpy((void *)&scores[j-1], (void *)&scores[j], sizeof(struct highscore));
122 scores[i].score = score;
123 scores[i].level = level;
124 /* Need to sort out entering a name... maybe old three letter arcade style */
125 new = 1;
126 break;
129 return new;