it's #else not #elif (fix red)
[Rockbox.git] / apps / plugins / lib / highscore.c
blobdf7a71bcdfb0e30a6a2302afa48e616a97068827
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "plugin.h"
20 #include "highscore.h"
22 static struct plugin_api *rb;
24 void highscore_init(struct plugin_api* newrb)
26 rb = newrb;
29 int highscore_save(char *filename, struct highscore *scores, int num_scores)
31 int i;
32 int fd;
33 int rc;
34 char buf[80];
36 fd = rb->open(filename, O_WRONLY|O_CREAT);
37 if(fd < 0)
38 return -1;
40 for(i = 0;i < num_scores;i++)
42 rb->snprintf(buf, sizeof(buf)-1, "%s:%d:%d\n",
43 scores[i].name, scores[i].score, scores[i].level);
44 rc = rb->write(fd, buf, rb->strlen(buf));
45 if(rc < 0)
47 rb->close(fd);
48 return -2;
51 rb->close(fd);
52 return 0;
55 int highscore_load(char *filename, struct highscore *scores, int num_scores)
57 int i;
58 int fd;
59 char buf[80];
60 char *name, *score, *level;
61 char *ptr;
63 fd = rb->open(filename, O_RDONLY);
65 rb->memset(scores, 0, sizeof(struct highscore)*(num_scores+1));
67 if(fd < 0)
68 return -1;
70 i = -1;
71 while(rb->read_line(fd, buf, sizeof(buf)-1) && i < num_scores)
73 i++;
75 DEBUGF("%s\n", buf);
76 name = buf;
77 ptr = rb->strchr(buf, ':');
78 if ( !ptr )
79 continue;
80 *ptr = 0;
81 ptr++;
83 rb->strncpy(scores[i].name, name, sizeof(scores[i].name));
85 DEBUGF("%s\n", scores[i].name);
86 score = ptr;
88 ptr = rb->strchr(ptr, ':');
89 if ( !ptr )
90 continue;
91 *ptr = 0;
92 ptr++;
94 scores[i].score = rb->atoi(score);
96 level = ptr;
97 scores[i].level = rb->atoi(level);
99 return 0;
102 int highscore_update(int score, int level, struct highscore *scores, int num_scores)
104 int i, j;
105 int new = 0;
107 /* look through the scores and see if this one is in the top ones */
108 for(i = num_scores-1;i >= 0; i--)
110 if ((score > scores[i].score))
112 /* Move the rest down one... */
113 if (i > 0)
115 for (j=1; j<=i; j++)
117 rb->memcpy((void *)&scores[j-1], (void *)&scores[j], sizeof(struct highscore));
120 scores[i].score = score;
121 scores[i].level = level;
122 /* Need to sort out entering a name... maybe old three letter arcade style */
123 new = 1;
124 break;
127 return new;